Академический Документы
Профессиональный Документы
Культура Документы
Team Emertxe
Introduction
Advanced C
SDLC A Quick Introduction
Code
Test
Advanced C
SDLC A Quick Introduction
Code
Test
Advanced C
SDLC A Quick Introduction
Commenting
Test
Advanced C
SDLC A Quick Introduction
Code
Test
Advanced C
Problem Solving What?
Polya's rule
Understand the problem
Devise a plan
Carryout the Plan
Look back
Advanced C
How should I proceed with my Application?
Natural Language
Pseudo Codes
Flowcharts etc.,
Advanced C
Algorithm Daily Life Example
Pros
You might say walking is a good exercise :)
Might have good time prediction
Save some penny
Cons
Depends on where you stay (you would choose if
you stay closer)
Should start early
Would get tired
Freshness would have gone
Advanced C
Algorithm Reaching this Room Take a Bus
Pros
You might save some time
Less tiredness comparatively
Cons
Have to wait walk to the bus stop
Have to wait for the right bus (No prediction of time)
Might not be comfortable on rush hours
Advanced C
Algorithm Reaching this Room Take a Car
Pros
Proper control of time and most comfortable
Less tiresome
Cons
Could have issues on traffic congestions
Will be costly
Advanced C
Algorithm Reaching this Room Let's Pool
Pros
You might save some time
Less costly comparatively
Cons
Have to wait for partner to reach
Could have issues on traffic congestions
Advanced C
Algorithm Daily Life Example - Conclusion
Start
Read A and B
Add A and B
Print SUM
Stop
Advanced C
Algorithm DIY
1234554321
1234__4321
123____321
12______21
1________1
Advanced C
Algorithm DIY Pattern (challenge)
Print rhombus
*
* * *
* * * * *
* * * * * * *
* * * * *
* * *
*
Advanced C
Algorithm DIY Pattern (challenge)
* * * * * * * * * * *
* *
* * * * * * * * *
* * * *
* * * * * * *
* * * * * *
* * * * * * *
* * * *
* * * * * * * * *
* *
* * * * * * * * * * *
Advanced C
Have you ever pondered how
- powerful it is?
- efficient it is?
- flexible it is?
- deep you can explore your system?
if [ NO ]
Wait!! get some concepts right before you dive into it
else
You shouldn't be here!!
Advanced C
Where is it used?
OS Kernel Development
Firmware, Middle-ware and Driver Development
Data Types
Qualifiers
Loop
Storage Class
Decision
Jump
Derived
User Defined
Others
Advanced C
Keywords Data Types
ANSI C says that the size of short and long implementation defined,
but ensures that the non-decreasing order of char, short, int, and long
is preserved i.e.,
typedef Do not create new types - they just add new type names
Helpful in managing complex declarations
Increase portability of the code
Obey scoping rules and are not textual replacements (as
opposed to #defines)
enum A set of named constants that are internally represented
as integers
Makes the code more readable and self-documenting
Advanced C
Keywords Others
Documentation
A typical code might contain the
blocks shown on left side
Preprocessor Statements It is generally recommended to
practice writing codes with all
Global Declaration
the blocks
The Main Code:
--------------------
Local Declarations
Program Statements
Function Calls
{
/* To display Hello world */ Comment
}
Advanced C
Compilation
Decimal 1
2
1
2
1
2
0 0 0 0
0 0 1
1
0 0
Octal 3 3 3 0 0 1
0 1
0
4 4 4 0 1
0 0 0
Hexadecimal 5 5 5 0 1
0 0 1
0
6 6 6 0 1
0 1
0 0
Binary 7 7 7 0 1
0 1
0 1
0
8 10 8 0 0 0 0
1
9 11 9 0 0 0 1
1 0
10 12 A 0 0 1
1 0 0
Type Range (8 Bits)
11 13 B 0 0 1
1 0 1
0
Decimal 0 - 255
12 14 C 0 1
1 0 0 0
Octal 000 - 0377
13 15 D 0 1
1 0 0 1
0
Hexadecimal 0x00 - 0xFF
14 16 E 0 1
1 0 1
0 0
Binary 0b00000000 - 0b11111111
15 17 F 0 1
1 0 1
0 1
0
Advanced C
Number Systems Decimal to Binary
12510 to Binary
2 125
2 62 1 LSB
2 31 0
2 15 1
2 7 1
2 3 1
2 1 1
0 1 MSB
So 12510 is 11111012
Advanced C
Number Systems Decimal to Octal
21210 to Octal
8 212
8 26 4 LSB
8 3 2
0 3 MSB
So 21210 is 3248
Advanced C
Number Systems Decimal to Hexadecimal
47210 to Hexadecimal
16 472
16 29 8 LSB
16 1 13
0 1 MSB
Representation Substitutes
Dec 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Hex 0 1 2 3 4 5 6 7 8 9 A B C D E F
So 47210 is 1D816
Advanced C
Number Systems Hexadecimal to Binary
1D816 to Binary
1 D 8
1 D 8
0 0 0 0
1 0
1 0
1 0 0
1 0
1 0 0 0
3248 to Binary
3 2 4
3 2 4
0 0
1 0
1 0 0
1 0 0
1 0 0
3248 to Hexadecimal
3 2 4
3 2 4
0 1 1 0 1 0 1 0 0
0 1 1 0 1 0 1 0 0
0 0 0 0 1 1 0 1 0 1 0 0
0 D 4
So 3248 is 0D416 which is nothing but D416
Advanced C
Number Systems Hexadecimal to Octal
1D816 to Octal
1 D 8
1 D 8
0 0 0 1 1 1 0 1 1 0 0 0
0 0 0 1 1 1 0 1 1 0 0 0
0 0 0 1 1 1 0 1 1 0 0 0
0 7 3 0
So 1D816 is 07308 which is nothing but 7308
Advanced C
Number Systems Binary to Decimal
1110110002 to Decimal
1 1 1 0 1 1 0 0 0
Bit Position 8 7 6 5 4 3 2 1 0
28 27 26 25 24 23 22 21 20
256 + 128 + 64 + 0 + 16 + 8 + 0 + 0 + 0 =
472
So 1110110002 is 47210
Advanced C
Data Representation - Bit
Value No of Bits
0 0
1 1
Advanced C
Data Representation - Byte
Value No of Bits
0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 1
Advanced C
Data Representation - Character
One byte represents one unique character like 'A', 'b', '1',
'$' ...
Its possible to have 256 different combinations of 0s and
1s to form a individual character
There are different types of character code
representation like
ASCII American Standard Code for Information
Interchange 7 Bits (Extended - 8 Bits)
EBCDIC Extended BCD Interchange Code 8 Bits
Unicode Universal Code - 16 Bits and more
Advanced C
Data Representation - Character
Value No of Bits
0 0 1 1 0 0 0 0 0
A 0 1 0 0 0 0 0 1
Advanced C
Data Representation - word
Value No of Bits
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
Advanced C
Integer Number - Positive
Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1
Advanced C
Integer Number - Negative
Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1
1's Compli 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 0
Add 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
2's Compli 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1
Mathematically : -k 2 - k
Advanced C
Float Point Number
Value 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Advanced C
Float Point Number Conversion - Example 2
Convert 0.625 to IEEE 32-bit floating point format
Step 1: Step 2:
Value 0 0 1 1 1 1 1 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Advanced C
Float Point Number Conversion - Example 3
Convert 39887.5625 to IEEE 32-bit floating point format
Step 1: Step 2:
Value 0 1 0 0 0 1 1 1 0 0 0 1 1 0 1 1 1 1 0 0 1 1 1 1 1 0 0 1 0 0 0 0
Advanced C
Float Point Number Conversion - Example 5
Convert -13.3125 to IEEE 32-bit floating point format
Step 1: Step 2:
Value 1 1 0 0 0 0 0 1 0 1 0 1 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Advanced C
Float Point Number Conversion - Example 6
Convert 1.7 to IEEE 32-bit floating point format
Step 1: Step 2:
0.4 2= 0.8 0
0.8 2= 1.6 1
1.710 = 1.101100110011001100110012
0.6 2= 1.2 1
Value 0 0 1 1 1 1 1 1 1 1 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1
Advanced C
Basic Data Types
char
Integral
int
Data
Types
float
Floating Point
double
Advanced C
Data Type Modifiers and Qualifiers
short T
Size
long T
Modifiers
signed T
Signedness
unsigned T
const V
Qualifiers
volatile V V Variables
T Data Types
auto V
static V F
Storage
Modifiers
extern V F
register V
inline F
V Variables
T Data Types
F Functions
Advanced C
Code Statements - Simple
int main()
{
number = 5; Assignment statement
3; +5;
Valid statement, But smart compilers
sum = number + 5;
might remove it
4 + 5;
; Assignment statement. Result of the
} number + 5 will be assigned to sum
do while
Advanced C
Conditional Constructs - if
Syntax Example
if (condition) #include <stdio.h>
{
statement(s); int main()
} {
int num = 2;
int main()
{
int num = 10;
if (num < 5)
{
printf(num is smaller than 5\n);
}
else
{
printf(num is greater than 5\n);
}
return 0;
}
Advanced C
Conditional Constructs if else if
Syntax Flow
if (condition1)
{
statement(s);
} true
else if (condition2) cond1? code
{
statement(s); false
} true
else cond2? code
{
statement(s); false
}
code
Advanced C
Conditional Constructs if else if
Example
#include <stdio.h>
int main()
{
int num = 10;
if (num < 5)
{
printf(num is smaller than 5\n);
}
else if (num > 5)
{
printf(num is greater than 5\n);
}
else
{
printf(num is equal to 5\n);
}
return 0;
}
Advanced C
Conditional Constructs Exercise
int main()
{
int option;
printf(Enter the value\n);
scanf(%d, &option);
switch (option)
{
case 10:
printf(You entered 10\n);
break;
case 20:
printf(You entered 20\n);
break;
default:
printf(Try again\n);
}
return 0;
}
Advanced C
Conditional Constructs switch - DIY
Example
false
#include <stdio.h>
cond?
int main()
{ true
int iter; code
iter = 0;
while (iter < 5)
{
printf(Looped %d times\n, iter);
iter++;
}
return 0;
}
Advanced C
Conditional Constructs do while
Syntax Flow
do
{
Controls the loop.
statement(s);
Evaluated after each
} while (condition); execution of loop body
Example
code
#include <stdio.h>
iter = 0;
do false
{
printf(Looped %d times\n, iter);
iter++;
} while (iter < 10);
return 0;
}
Advanced C
Conditional Constructs for
Syntax Flow
for (init; condition; post evaluation expr)
{
statement(s); Controls the loop.
}
Evaluated before each init
execution of loop body
Example
#include <stdio.h> false
cond?
int main()
{ true
int iter; code
return 0;
}
Advanced C
Conditional Constructs - Classwork
*
***
*****
*******
Advanced C
Conditional Constructs for DIY
*
***
*****
*******
*****
***
*
Advanced C
Conditional Constructs break
Flow
A break statement shall appear
only in switch body or loop
body
break is used to exit the loop, code block
true false
Syntax
do
loop
{ cond?
conditional statement
break; false
} while (condition);
Advanced C
Conditional Constructs break
Example
#include <stdio.h>
int main()
{
int iter;
return 0;
}
Advanced C
Conditional Constructs continue
Flow
code block
Syntax
do
loop
{ cond?
true
conditional statement
continue; false
} while (condition);
Advanced C
Conditional Constructs continue
Example
#include <stdio.h>
int main()
{
int iter;
return 0;
}
Advanced C
Operators
Operand binary
ternary
Arithmetic + - * / % ...
Category
Logical ! || && ...
Example
#include <stdio.h>
Num = 7 - 4 * 3 / 2 + 5;
return 0;
}
Advanced C
Operators Language - sizeof()
Example
#include <stdio.h>
int main()
{
int num = 5;
return 0;
}
Example
#include <stdio.h>
int main()
{
int num1 = 5;
int num2 = sizeof(++num1);
return 0;
}
Advanced C
Operators Language - sizeof()
Type
Conversion
long double
double
float
unsigned long long
signed long long
unsigned long
signed long
unsigned int
signed int
unsigned short
signed short
unsigned char
signed char
Advanced C
Type Conversion - Implicit
Example
Fractional part will be truncated during conversion of float to int.
Advanced C
Type Conversion Explicit (Type Casting)
Syntax
(data type) expression
Example
#include <stdio.h>
int main()
{
int num1 = 5, num2 = 3;
return 0;
}
Advanced C
Operators - Logical
Operator Description Associativity
! Logical NOT R to L
&& Logical AND L to R
Example
|| Logical OR L to R
#include <stdio.h>
int main()
{ What will be
int num1 = 1, num2 = 0;
the output?
if (++num1 || num2++)
{
printf(num1 is %d num2 is %d\n, num1, num2);
}
num1 = 1, num2 = 0;
if (num1++ && ++num2)
{
printf(num1 is %d num2 is %d\n, num1, num2);
}
else
{
printf(num1 is %d num2 is %d\n, num1, num2);
}
return 0;
}
Advanced C
Operators Circuit Logical
int main()
{
float num1 = 0.7;
return 0;
}
Advanced C
Operators - Assignment
Example Example
#include <stdio.h> #include <stdio.h>
return 0;
}
Advanced C
Operators - Bitwise
Operand
Value Value
Bitwise ANDing of 0x60
A 0x61 0 1 1 0 0 0 0 1
& Bitwise AND all the bits in two 0x13
B 0x13 0 0 0 1 0 0 1 1
operands
A0x13
&B 0x01 0 0 0 0 0 0 0 1
Operand
Value Value
Bitwise ORing of 0x60
A 0x61 0 1 1 0 0 0 0 1
| Bitwise OR all the bits in two 0x13
B 0x13 0 0 0 1 0 0 1 1
operands
A0x13
|B 0x73 0 1 1 1 0 0 1 1
Advanced C
Operators - Bitwise
Operand
Value Value
Bitwise XORing of 0x60
A 0x61 0 1 1 0 0 0 0 1
^ Bitwise XOR all the bits in two 0x13
B 0x13 0 0 0 1 0 0 1 1
operands
A0x13
^B 0x72 0 1 1 1 0 0 1 0
Complimenting Operand
Value Value
Syntax
Sign
Zero
bitfilling
fillingright
rightshift
shift
Advanced C
Operators - Bitwise
Example
#include <stdio.h>
int main()
{
int count;
unsigned char iter = 0xFF;
return 0;
}
Advanced C
Operators Bitwise Shift
Example
#include <stdio.h>
int main()
{
int x = 7, y = 7;
x = 7 << 32;
printf(x is %x\n, x);
x = y << 32;
printf(x is %x\n, x);
return 0;
}
Advanced C
Operators Bitwise - Shift
Set bit
Get bit
Clear bit
Advanced C
Operators Ternary
Syntax
Condition ? Expression 1 : Expression 2;
Example
#include <stdio.h>
int main()
{ #include <stdio.h>
int num1 = 10;
int num2 = 20; int main()
int num3; {
int num1 = 10;
if (num1 > num2) int num2 = 20;
{ int num3;
num3 = num1;
} num3 = num1 > num2 ? num1 : num2;
else printf(Greater num is %d\n, num3);
{
num3 = num2; return 0;
} }
printf(%d\n, num3);
return 0;
}
Advanced C
Operators Comma
Overflow
(127 + 1)
Underflow
(-128 - 1)
Advanced C
Overflow Signed Numbers
Say A = +127
Add 1 0 0 0 0 0 0 0 1
sign bit
Advanced C
Underflow Signed Numbers
Say A = -128
Add -1 1 1 1 1 1 1 1 1
sign bit
Spill over bit is discarded
Advanced C
Arrays Know the Concept
A conveyor belt
Starts here
Equally spaced
Defined length
Carry similar items
Index as 10th item
Ends here
Advanced C
Arrays Know the Concept
Conveyor Belt An Array
Top view First Element
Start (Base) address
Total Elements
Fixed size
Contiguous Address
Elements are
accessed by
indexing
Legal access region
Last Element
End address
What is the type of conveyor belt?
Advanced C
Arrays
Syntax
data_type name[SIZE];
Example
int age[5] = {10, 20, 30, 40, 50};
1 2 3 4 5
Index Index Index Index Index
10 20 30 40 50
base addr + 16
base addr + 12
base addr + 4
base addr + 8
base addr
Advanced C
Arrays Points to be noted
First element with lowest address and the last element with
highest address
int main()
{
int array[5] = {1, 2, 3, 4, 5};
int index;
index = 0;
do
{
printf(Index %d has Element %d\n, index, array[index]);
index++;
} while (index < 5);
return 0;
}
Advanced C
Arrays - Storing
Example
#include <stdio.h>
int main()
{
int array[5];
int index;
return 0;
}
Advanced C
Arrays - Initializing
Example
#include <stdio.h>
int main()
{
int array1[5] = {1, 2, 3, 4, 5};
int array2[5] = {1, 2};
int array3[] = {1, 2};
int array4[]; /* Invalid */
printf(%u\n, sizeof(array1));
printf(%u\n, sizeof(array2));
printf(%u\n, sizeof(array3));
return 0;
}
Advanced C
Arrays Copying
int main()
{
int array_org[5] = {1, 2, 3, 4, 5}; Waow!! so simple?
int array_bak[5];
But can I do this
int index;
array_bak = array_org;
if (array_bak == array_org)
{
printf(Copied\n);
}
return 0;
}
Advanced C
Arrays Copying
Input
Output
f(x) = x + 1
x
x+1
Output
x=2
2
2+1
3
Advanced C
Functions How to write
Syntax
return_data_type function_name(arg_1, arg_2, ..., arg_n)
{
/* Function Body */
}
Example
y=x+1
Example
int foo(int x)
{
int ret = 0;
ret = x + 1;
Formal from
Return arguments
function
return ret;
}
Advanced C
Functions How to call
Example
#include <stdio.h>
int main()
{
int x, y;
The function call
x = 2;
y = foo(x);
printf(y is %d\n, y);
return 0;
}
int foo(int x)
{
int ret = 0;
ret = x + 1;
return ret;
}
Advanced C
Functions Why?
Re usability
Functions can be stored in library & re-used
When some specific code is to be used more than once, at different
places, functions avoids repetition of the code.
One can suppress, how the task is done inside the function,
which is called Abstraction
Advanced C
Functions A complete look
Example
#include <stdio.h>
int main()
{
int num1 = 10, num2 = 20;
Ignored the return from function
int sum = 0;
In C, it is up to the programmer to
add_numbers(num1, num2); capture or ignore the return value
printf(Sum is %d\n, sum);
return 0;
}
int add_numbers(int num1, int num2)
{
int sum = 0;
return sum;
}
Advanced C
Functions DIY
Parameter List
Hole
P3
Heap
. .BSS
Segment
(Uninitialized)
. Data
. Initialized
Pn-1
Code Segment
Pn
Advanced C
Functions Parameter Passing Types
This method copies the actual This method copies the address of
value of an argument into the an argument into the formal
formal parameter of the function. parameter.
In this case, changes made to the Inside the function, the address is
parameter inside the function have used to access the actual
no effect on the actual argument. argument used in the call. This
means that changes made to the
parameter affect the argument.
Advanced C
Functions Pass by Value
Example
#include <stdio.h>
int main()
{
int num1 = 10, num2 = 20, sum;
return 0;
}
int add_numbers(int num1, int num2)
{
int sum = 0;
return sum;
}
Advanced C
Functions Pass by Value
Example
#include <stdio.h>
int main()
{
int num1 = 10;
printf(Before Modification\n);
printf(num1 is %d\n, num1);
modify(num1);
printf(After Modification\n);
printf(num1 is %d\n, num1);
return 0;
}
Advanced C
Functions Pass by Value
int main()
{
int num = 10;
printf(Before Modification\n);
printf(num1 is %d\n, num);
modify(&num);
printf(After Modification\n);
printf(num1 is %d\n, num);
return 0;
}
Advanced C
Functions Pass by Reference
2000
Arguments 1000
num_ptr
Advanced C
Functions Pass by Reference
Modification
in modify function
will literally modify
1000 num the value of num
11 Local Variables
*
2000
Arguments 1000
num_ptr
Advanced C
Functions Pass by Reference - Advantages
int main()
{
int array[5] = {10, 20, 30, 40, 50};
print_array(array);
return 0;
}
int main()
{
int array[5] = {10, 20, 30, 40, 50};
print_array(array);
return 0;
}
int main()
{
int array[5] = {10, 20, 30, 40, 50};
print_array(array, 5);
return 0;
}
return 0;
}
array_val = return_array();
print_array(array_val, 5);
return 0;
}
Local return
Void return
Advanced C
Functions
Advanced C
Functions Recursive
Two steps
Step 1: Identification of base case
Step 2: Writing a recursive case
Advanced C
Functions Recursive Example
Example
#include <stdio.h>
/* Factorial of 3 numbers */ n !n
int factorial(int number) 0 1
{
if (number <= 1) 1 1
{
return 1; 2 2
}
else 3 6
{
}
return number * factorial(number 1); 4 24
}
int main()
{
int ret;
ret = factorial(3);
printf(Factorial of 3 is %d\n, ret);
return 0;
}
Advanced C
Functions Function Pointers
Syntax
return data_type function_name(parameter list, ...);
Advanced C
Functions Variadic Definition & Usage
/* Cleanup */
va_end(ap);
return sum;
}
Chapter 6
Pointers and Strings
Advanced C
Pointers Jargon
What's a Jargon?
Jargon may refer to terminology used in a certain
profession, such as computer jargon, or it may refer
to any nonsensical language that is not understood by
most people.
Speech or writing having unusual or pretentious
vocabulary, convoluted phrasing, and vague meaning.
6 i
CPU
6 p
RAM
Integer i;
Pointer p;
Say:
i = 6;
p = 6;
Advanced C
Pointers The 7 Rules Rule 1
Exceptions:
May not be address and data bus of same size
Rule 2 (Will see why? while discussing it)
Advanced C
Pointers Rule 1 in detail
Example
#include <stdio.h> x
int main() 1000 ? Say 4 bytes
{
int x;
int *ptr; ptr
x = 5; 2000 ? Say 4 bytes
ptr = 5;
return 0;
}
Advanced C
Pointers Rule 1 in detail
Example
#include <stdio.h> x
int main() 1000 5 Say 4 bytes
{
int x;
int *ptr; ptr
x = 5; 2000 5 Say 4 bytes
ptr = 5;
return 0;
}
So pointer is an integer
&
Variable Address
*
Advanced C
Pointers Rule 2 in detail
Example
#include <stdio.h> x
int main() 1000 5 Say 4 bytes
{
int x;
int *ptr; ptr
x = 5; 2000 ? Say 4 bytes
return 0;
}
return 0;
}
return 0;
}
return 0;
}
return 0;
}
int main()
{
int number = 10;
int *ptr;
ptr = &number;
return 0;
}
Advanced C
Pointers Rule 2 in detail
Example
#include <stdio.h>
int main()
{
int number = 10;
int *ptr;
ptr = &number;
return 0;
}
Advanced C
Pointers Rule 2 in detail
Example
#include <stdio.h>
int main()
{
int number = 10;
int *ptr;
ptr = &number;
*ptr = 100;
return 0;
}
1008
1012
1016
ptr 1000
1020
1024
Advanced C
Pointers The 7 Rules Rule 4
LSB
The byte of a multi byte number with the least
importance
The change in it would have least effect on complete
number
MSB
The byte of a multi byte number with the most
importance
The change in it would have more effect on complete
change number
Advanced C
Pointers Rule 4 in detail - Endianness
Example
Let us consider the following
#include <stdio.h>
example and how it would be
int main()
{ stored in both machine types
int num = 0x12345678;
return 0;
}
1000 num
78 56 34 12
2000 iptr
1000
2004 cptr
1000
int main()
{
if (sizeof(char *) == sizeof(long long *))
{
printf(Yes its Equal\n);
}
return 0;
}
Advanced C
Pointers The 7 Rules Rule 5
Pointer Arithmetic
Rule: Value(p + i) = Value(p) + i * sizeof(*p)
Advanced C
Pointers The Rule 5 in detail
So, 1024
ptr 1000
Address of array = 1000
Base address = 1000
&array[0] = 1 1000
&array[1] = 2 1004
Advanced C
Pointers The Rule 5 in detail
Example
array 1000
#include <stdio.h> 1
1004
int main() 2
{ 1008
int array[5] = {1, 2, 3, 4, 5}; 3
int *ptr = array; 1012
4
printf(%d\n, *ptr); 1016
5
return 0; 1020
}
1024
ptr 1000
This code should print 1 as output
since its points to the base address
Now, what should happen if we do
ptr = ptr + 1;
Advanced C
Pointers The Rule 5 in detail
ptr = ptr + 4
Here ptr = 1000 so
ptr = 1000 + 4
ptr = 1004
Advanced C
Pointers The Rule 5 in detail
Relation with array can be explained
array
1
1000 as
2
1004 ptr + 2
1008
3 ptr + 2 * sizeof(int)
1012
4
1000 + 2 * 4
1016
5
1020 1008 &array[2]
ptr 1008
1024
So,
ptr = ptr + 2;
ptr + 2 1008 &array[2]
*(ptr + 2) *(1008) array[2]
Advanced C
Pointers The Rule 5 in detail
Which results to
ptr = array
return 0;
}
?
Where am I
?
pointing to?
?
What does it ?
Contain? ?
Can I read or
write wherever
I am pointing?
Advanced C
Pointers Rule 6 in detail NULL Pointer
Solution
Need to reserve one valid value
Which valid value could be most useless?
In wake of OSes sitting from the start of memory, 0 is a
good choice
As discussed in previous slides it is implementation
dependent
Advanced C
Pointers Rule 6 in detail NULL Pointer
Example
#include <stdio.h>
int main()
{
int *num;
num = NULL;
return 0;
}
Example
#include <stdio.h>
int main()
{
int *num = NULL;
return 0;
}
Advanced C
Pointers Void Pointer
Exercise -
W.A.P to swap any given data type
Advanced C
void Pointers Size of void
GCC Extension :
6.22 Arithmetic on void- and Function-Pointers
In GNU C, addition and subtraction operations are supported on
pointers to void and on pointers to functions. This is done by
treating the size of a void or of a function as 1.
A consequence of this is that sizeof is also allowed on void and on
function types, and returns 1.
The option -Wpointer-arith requests a warning if these extensions
are used
Advanced C
Pointers The 7 Rules Rule 7
1 2 3 4 5 6 7 8
Compiler
The compiler will allocate the required memory
Required memory is computed at compile time
Memory is allocated in data segment or stack
User
The user has to allocate the memory whenever
required and deallocate whenever required
This is done by using malloc and free functions
Memory is allocated in heap
Advanced C
Pointers Rule 7 in detail
ptr = malloc(4);
return 0;
}
Advanced C
Pointers Rule 7 in detail
ptr = malloc(4);
return 0;
}
Advanced C
Pointers Rule 7 in detail
ptr = malloc(4);
?
return 0;
} ?
?
500 ?
?
Advanced C
Pointers Rule 7 in detail - Dynamic Allocation
The need
You can decide size of the memory at run time
You can resize it whenever required
You can decide when to create and destroy it
Advanced C
Pointers Rule 7 Dynamic Allocation - malloc
Prototype
void *malloc(size_t size);
ptr
Example
#include <stdio.h> 1000 500
int main()
{ ?
char *str; ?
str = malloc(5); ?
return 0; ?
}
?
? Allocate 5 Bytes
?
500 ?
?
?
Advanced C
Pointers Rule 7 Dynamic Allocation - malloc
ptr
Example
#include <stdio.h> 1000 NULL
int main()
{ ?
char *str; ?
str = malloc(10); ?
NULL
Advanced C
Pointers Rule 7 Dynamic Allocation - calloc
Prototype
void *calloc(size_t nmemb, size_t size);
ptr
Example
#include <stdio.h> 1000 500
int main()
{ ?
char *str; ?
str = calloc(5, 1); ?
return 0; 0
}
0
Allocate 5 Bytes
0 and all are set
0
to zeros
500 0
?
?
Advanced C
Pointers Rule 7 Dynamic Allocation - realloc
Prototype
void *realloc(void *ptr, size_t size);
ptr
Example
#include <stdio.h> 1000 500
int main()
{ ?
char *str; ?
str = malloc(5); ?
ptr
Example
#include <stdio.h> 1000 500
int main()
{ ?
char *str; ?
str = malloc(5); ?
ptr
Example
#include <stdio.h> 1000 500
int main()
{ ?
char *str; ?
str = malloc(5); ?
Points to be noted
Reallocating existing memory will be like deallocating
the allocated memory
If the requested chunk of memory cannot be
extended in the existing block, it would allocate in a
new free block starting from different memory!
If new memory block is allocated then old memory
block is automatically freed by realloc function
Advanced C
Pointers Rule 7 Dynamic Deallocation - free
Prototype
void free(void *ptr);
ptr
Example
#include <stdio.h> 1000 ?
int main()
{ ?
char *ptr; ?
ptr = malloc(5); ?
return 0; ?
} ?
?
Advanced C
Pointers Rule 7 Dynamic Deallocation - free
ptr
Example
#include <stdio.h> 1000 500
int main()
{ ?
char *ptr; ?
ptr = malloc(5); ?
return 0; 500 ?
} ?
?
Advanced C
Pointers Rule 7 Dynamic Deallocation - free
ptr
Example
#include <stdio.h> 1000 500
int main()
{ ?
char *ptr; ?
ptr = malloc(5); ?
return 0; 500 A
} ?
?
Advanced C
Pointers Rule 7 Dynamic Deallocation - free
ptr
Example
#include <stdio.h> 1000 500
int main()
{ ?
char *ptr; ?
ptr = malloc(5); ?
return 0; 500 A
} ?
?
Advanced C
Pointers Rule 7 Dynamic Deallocation - free
Points to be noted
Free releases the allocated block, but the pointer
would still be pointing to the same block!!, So
accessing the freed block will have undefined behavior
This type of pointer which are pointing to freed
locations are called as Dangling Pointers
Doesn't clear the memory after freeing
Advanced C
Pointers Multilevel
Example
#include <stdio.h> num
int main() 1000 10
{
int num = 10;
int *ptr1 = #
int **ptr2 = &ptr1;
int ***ptr3 = &ptr2;
printf(%d, ptr3);
printf(%d, *ptr3);
printf(%d, **ptr3);
printf(%d, ***ptr3);
return 0;
}
Advanced C
Pointers Multilevel
Example
#include <stdio.h> num
int main() 1000 10
{
int num = 10;
int *ptr1 = # ptr1
int **ptr2 = &ptr1; 2000 1000
int ***ptr3 = &ptr2;
printf(%d, ptr3);
printf(%d, *ptr3);
printf(%d, **ptr3);
printf(%d, ***ptr3);
return 0;
}
Advanced C
Pointers Multilevel
Example
#include <stdio.h> num
int main() 1000 10
{
int num = 10;
int *ptr1 = # ptr1
int **ptr2 = &ptr1; 2000 1000
int ***ptr3 = &ptr2;
return 0;
}
Advanced C
Pointers Multilevel
Example
#include <stdio.h> num
int main() 1000 10
{
int num = 10;
int *ptr1 = # ptr1
int **ptr2 = &ptr1; 2000 1000
int ***ptr3 = &ptr2;
Example
#include <stdio.h> num
int main() 1000 10
{
int num = 10;
int *ptr1 = # ptr1
int **ptr2 = &ptr1; 2000 1000
int ***ptr3 = &ptr2;
Output 3000
Advanced C
Pointers Multilevel
Example
#include <stdio.h> num
int main() 1000 10
{
int num = 10;
int *ptr1 = # ptr1
int **ptr2 = &ptr1; 2000 1000
int ***ptr3 = &ptr2;
Output 2000
Advanced C
Pointers Multilevel
Example
#include <stdio.h> num
int main() 1000 10
{
int num = 10;
int *ptr1 = # ptr1
int **ptr2 = &ptr1; 2000 1000
int ***ptr3 = &ptr2;
Output 1000
Advanced C
Pointers Multilevel
Example
#include <stdio.h> num
int main() 1000 10
{
int num = 10;
int *ptr1 = # ptr1
int **ptr2 = &ptr1; 2000 1000
int ***ptr3 = &ptr2;
Output 10
Advanced C
Pointers Constant Pointer
int arr[5];
S
W
RO
COL
UM S
Advanced C
Pointers 2D Array
COLS C0 C1 C2
R0 00 01 02
ROWS
1 1 1
2 2 2 R1 10 11 12
Advanced C
Pointers 2D Array
Example
#include <stdio.h>
int main()
{
int a[2][3] = {1, 2, 3, 4, 5, 6};
return 0;
}
2 * 1D array linearly
placed in memory
Example a b c
int a = 10; 1000 10 2000 20 3000 30
int b = 20;
int c = 30;
ptr 1000 2000 3000
int *ptr[3] = {&a, &b, &c};
4000 4004 4008
Example
int a[2] = {10, 20}; 1004 20 2004 40 3004 60
int b[2] = {30, 40}; 1000 10 a 2000 30 b 3000 50 c
int c[2] = {50, 60};
print_array(ptr);
return 0;
}
Chapter 4
Standard Input / Output
Advanced C
Standard I/O
STDERR
Text Terminal
Program
STDOUT
STDIN
Advanced C
Standard I/O The File Descriptors
int main()
{
int ch;
puts(EOF Received);
return 0;
}
Advanced C
Standard I/O Formatted
What is this!?
Advanced C
Standard I/O printf()
Prototype
int printf(char *format, arg1, arg2, ...);
What is this!?
Is called as ellipses
Example
int printf(%c %d %f, arg1, arg2, arg3);
format 0x1000
0x2000 E m e r t x e \0
0x1000
So the base address of the array gets passed to the pointer, Hence the output
Note: You will get a warning while compiling the above code.
So this method of passing is not recommended
Advanced C
Standard I/O printf() - Type Specifiers
int main()
{
int num1 = 123;
char ch = 'A';
float num2 = 12.345;
char string[] = Hello World;
return 0;
}
Advanced C
Standard I/O printf() - Return
Example
#include <stdio.h>
int main()
{
int ret;
char string[] = Hello World;
return 0;
}
Advanced C
Standard I/O sprintf() - Printing to string
Prototype
int sprintf(char *string, char *format, arg1, arg2, ...);
int main()
{
int num1 = 123;
char ch = 'A';
float num2 = 12.345;
char string1[] = sprintf() Test;
char string2[100];
return 0;
}
Advanced C
Standard I/O Formatted Input - scanf()
Prototype
int scanf(char *format, ...);
or
int scanf(string, [variables]);
int main()
{
int num1;
char ch;
float num2;
char string[10];
return 0;
}
Advanced C
Standard I/O scanf() - Format Specifier
int main()
{
int num, int ret;
if (ret != 1)
{
printf(Invalid input. The number is still %d\n, num);
return 1;
}
else
{
printf(Number is modified with %d\n, num);
}
return 0;
}
Advanced C
Standard I/O sscanf() - Reading from string
Prototype
int sscanf(char *string, char *format, ...);
int main()
{
int age;
char array_1[10];
char array_2[10];
return 0;
}
Advanced C
Standard I/O Buffering
RAM
Output
Buffer
Input
Buffer
CPU
Advanced C
Standard I/O Buffering
fread() fwrite()
write()
read()
Disk Keyboard
Advanced C
Standard I/O Buffering
Kernel
Space
Advanced C
Memory Segments
Linux OS User Space
The User space contains
P1 P1
P1 many processes
Every process will be
P2 scheduled by the kernel
Pn-1
Kernel
Space Pn
Advanced C
Memory Segments
User Space Memory Segments
The memory segment of a
P1
P1 Command Line
Arguments program contains four
major areas.
Stack
P2
Hole
Text Segment
Stack
P3
Data Segment
Heap
Heap
.
.BSS
Segment
.
(Uninitialized)
.BSS Block Started by Symbol
Data
. Initialized
Pn-1
Text Segment
Pn
Advanced C
Memory Segments Text Segment
Memory Segments
Command Line Also referred as Code Segment
Arguments
Holds one of the section of program in
Stack
object file or memory
Hole In memory, this is place below the heap or
stack to prevent getting over written
Heap
Is a read only section and size is fixed
.BSS
Segment
(Uninitialized)
Data
Initialized
Text Segment
Advanced C
Memory Segments Data Segment
Memory Segments
Command Line Contains 2 sections as initialized and
Arguments uninitialized data segments
Stack
Initialized section is generally called as Data
Segment
Hole
Uninitialized section is referred as BSS
Heap (Block Started by Symbol) usually filled
with 0s
.BSS
Segment
(Uninitialized)
Data
Initialized
Text Segment
Advanced C
Memory Segments Data Segment
Memory Segments
Command Line Dynamic memory allocation takes place here
Arguments
Begins at the end of BSS and grows
Stack
upward from there
Hole
Heap
.BSS
Segment
(Uninitialized)
Data
Initialized
Text Segment
Advanced C
Memory Segments Stack Segment
Memory Segments
Command Line Adjoins the heap area and grow in opposite
Arguments area of heap when stack and heap pointer
Stack meet (Memory Exhausted)
Typically
Typicallyloaded
loadedat
atthe
thehigher
higherpart
partofofmemory
Hole memory
(Uninitialized)
Data
Text Segment
Advanced C
Memory Segments Stack Segment
Memory Segments Stack Frame
Command Line
Arguments Local Variables A stack frame contain at least
Stack
of a return address
Return Address
Parameter List
Hole
Heap
.BSS
Segment
(Uninitialized)
Data
Initialized
Text Segment
Advanced C
Memory Segments Stack Frame
Stack Frame
#include <stdio.h>
num1 = 10
int main() num2 = 20
{ sum = 0 main()
int num1 = 10, num2 = 20; Return Address to the caller
int sum = 0;
s=0
sum = add_numbers(num1, num2);
printf(Sum is %d\n, sum); Return Address to the main()
add_numbers()
return 0; n1 = 10
} n2 = 20
s = n1 + n2;
return s;
}
Advanced C
Memory Segments - Runtime
Heap
Dynamic memory
Allocated by malloc()
De-allocated by free()
Advanced C
Memory Segments - Runtime
return 0;
}
Advanced C
Declaration
extern int num1; Declaration specifies type to the
extern int num1; variables
Its like an announcement and hence
int main(); can be made 1 or more times
...
}
Advanced C
Storage Classes - extern
file2.c
#include <stdio.h>
file1.c extern int num;
#include <stdio.h>
int func_1()
int num; {
printf(num is %d from file2\n, num);
int main()
{ return 0;
while (1) }
{ file3.c
num++;
func_1(); #include <stdio.h>
sleep(1);
func_2(); extern int num;
}
int func_2()
return 0; {
} printf(num is %d from file3\n, num);
return 0;
}
Chapter 6
Strings
Advanced C
S_____s Fill in the blanks please ;)
`
Advanced C
Strings
String of Beads
String
Start of String
Bead
Contains n numbers of Beads String ends here
Advanced C
Strings
Example
You know, now this is what a string is!
Advanced C
Strings - Initializations
Examples
int main()
{
char char_array_1[5] = {'H', 'E', 'L', 'L', O'}; The size of the array
char char_array_2[] = Hello; Is calculated So,
sizeof(char_array_1);
sizeof(char_array_2);
5, 6
return 0;
}
int main()
{
char *str = Hello; The size of pointer is
always constant so,
sizeof(str);
4 (32 Bit Sys)
return 0;
}
Advanced C
Strings - Size
Example
#include <stdio.h>
int main()
{
if (sizeof(Hello World) == sizeof(Hello) + sizeof(World)
{
printf(WoW\n);
}
else
{
printf(HuH\n);
}
return 0;
}
Advanced C
Strings - Manipulations
Examples
char str1[6] = Hello;
char str2[6]; Not possible to assign a string to a
array since its a constant pointer
str2 = World;
int main()
{
char *str1 = Hello;
char *str2 = Hello;
if (str1 == str2)
{
printf(Hoo. They share same space\n);
}
else
{
printf(No. They are in different space\n);
}
return 0;
}
Advanced C
Strings Library Functions
Expanded Source
Compiler .i Code
gcc -E file.c
Assembly Source
Assembler .s Code
gcc -S file.c
#include #elif
#define #error
#undef #warning
#ifdef #line
#ifndef #pragma
#else #
#endif ##
#if
#else
Advanced C
Preprocessor Header Files
.h VS .c
Declarations Function and variable
Sharable/reusable definitions
#defines
Non sharable/reusable
Datatypes
#defines
Used by more than 1
Datatypes
file
Advanced C
Preprocessor Header Files - Syntax
Syntax
#include <file.h> System header files
It searches for a file named file
in a standard list of system
directories
Syntax
#include file.h
Local (your) header files
It searches for a file named file
first in the directory containing the
current file, then in the quote
directories and then the same
directories used for <file>
Advanced C
Preprocessor Header Files - Operation
file2.c file2.h
char *test(void) char *test(void);
{
static char *str = Hello;
return str;
}
file1.c
int num; int num;
return 0; return 0;
} }
Advanced C
Preprocessor Header Files Search Path
#endif
Advanced C
Preprocessor Macro Object-Like
Example
#define BUFFER_SIZE 1024
Advanced C
Preprocessor Macro Arguments
int main()
{
SET_BIT(0, 2);
return 0;
}
Advanced C
Preprocessor Macro Multiple Lines
expanded, however, it
int main()
will all come out on one {
line int num1 = 10, num1= 20;
SWAP(num1, num2);
return 0;
}
Advanced C
Preprocessor Macro Stringification
Example
#include <stdio.h>
#define WARM_IF(EXP) \
do \
{ \
x--; \
if (EXP) \
{ \
fprintf(stderr, "Warning: " #EXP "\n"); \
} \
} while (x);
int main()
{
int x = 5;
You can convert a macro
WARN_IF(x == 0);
argument into a string
}
return 0; constant by adding #
Advanced C
Preprocessor Macro Standard Predefined
int main()
{
return 0;
}
Advanced C
Preprocessor Conditional Compilation
Syntax
#ifdef MACRO
/* Controlled Text */
#endif
Advanced C
Preprocessor Conditional Compilation - defined
Syntax
#if defined (ERROR) && (WARNING)
/* Controlled Text */
#endif
Advanced C
Preprocessor Conditional Compilation - if
Syntax
#if expression
/* Controlled Text */
#endif
Advanced C
Preprocessor Conditional Compilation - else
Syntax
#if expression
#else
#endif
Advanced C
Preprocessor Conditional Compilation - elif
Syntax
#if DEBUG_LEVEL == 1
/* Controlled Text*/
#elif DEBUG_LEVEL == 2
/* Controlled Text */
#else
/* Controlled Text */
#endif
Advanced C
Preprocessor Conditional Com... Deleted Code
Syntax
#if 0
#endif
Advanced C
Preprocessor Diagnostic
Hoo!!, let's not forget our old friend _r_a_ which a user
defined data type too!!.
Advanced C
UDDs Structures (Composite data types)
Syntax
If we consider the Student as
struct StructureName
{
an example, The admin
/* Group of data types */ should have at least some
};
important data like name, ID
and address.
Example
struct Student
Name of the datatype. Note it's
{ struct Student and not Student
int id;
char name[30];
Are called as fields or members of
char address[150]; of the structure
}; Declaration ends here
The memory is not yet allocated!!
int main()
{
s1 is a variable of type
struct Student s1; struct Student
The memory is allocated now
return 0;
}
Advanced C
UDDs Structures Memory Layout
return 0;
}
? address
Advanced C
UDDs Structures - Access
Example
How to write into id now?
struct Student
{
It's by using . (Dot) operator
int id; (member access operator)
char name[30];
char address[150];
}; s1
10 id
int main()
{
? name
struct Student s1;
s1.id = 10;
return 0; ? address
}
Example
s1
struct Student
{ 10 id
int id;
char name[30]; Tingu name
char address[150];
};
int main()
{ Bangalore address
struct Student s1 = {10, Tingu, Bangalore};
return 0;
}
Advanced C
UDDs Structures - Copy
Example
s2
struct Student
{ 10 id
int id;
char name[30]; Tingu name
char address[150];
};
int main()
{ Bangalore address
struct Student s1 = {10, Tingu, Bangalore};
struct Student s2;
s2 = s1;
return 0;
}
Structure name does not represent its address. (No correlation with arrays)
Advanced C
UDDs Structures - Address
Example
struct Student
{
int id;
char name[30];
char address[150];
};
int main()
{
struct Student s1 = {10, Tingu, Bangalore};
return 0;
}
Advanced C
UDDs Structures - Pointers
Pointers!!!. Not again ;). Fine don't worry, not a big deal
Example
struct Student sptr
{ 2000 1000
int id;
char name[30];
char address[150];
};
s1
int main()
1000 ? id
{
struct Student s1;
struct Student *sptr = &s1; ? name
return 0;
}
? address
Advanced C
UDDs Structures Pointer - Access
Example
struct Student sptr
{ 2000 1000
int id;
char name[30];
char address[150];
};
s1
int main()
1000 10 id
{
struct Student s1;
struct Student *sptr = &s1; ? name
(*sptr).id = 10;
return 0;
? address
}
Advanced C
UDDs Structures Pointer Access Arrow
Example
struct Student sptr
{ 2000 1000
int id;
char name[30];
char address[150];
};
s1
int main()
1000 10 id
{
struct Student s1;
struct Student *sptr = &s1; ? name
sptr->id = 10;
return 0;
? address
}
Example
struct Student
{
int id;
char name[30];
char address[150];
};
int main()
{
struct Student s1;
data(s1);
Example
struct Student
{
int id;
char name[30];
char address[150]
};
int main()
{
struct Student s1;
data(&s1);
return 0; Recommended on
} larger structures
Advanced C
UDDs Structures Functions Return
Example
struct Student
{
int id;
char name[30];
char address[150]
};
return s;
}
int main()
{
struct Student s1;
s1 = data(s1);
return 0;
}
Advanced C
UDTs Structures Padding
Adding of few extra useless bytes (in fact skip address) in
between the address of the members are called
structure padding.
Example
Lets consider the code as
int main() given
{
char ch = 'A';
int num = 0x12345678;
The memory allocation we
} expect would be like shown
in figure
So lets see how the CPU tries
0 ch
to access these data in next
1 78
56
slides
2
3 34
4 12
5 ?
6 ?
7 ?
Advanced C
Data Alignment
Example
Fetching the character by
int main()
{
the CPU will be like shown
char ch = 'A'; below
int num = 0x12345678;
}
0 41 41 41
1 78 78 0
0 4 Bytes
2 56 56 0
3 34 34 0
4 12
5 ?
6 ?
7 ?
Advanced C
Data Alignment
Example
Fetching the integer by
int main()
{
the CPU will be like shown
char ch = 'A'; below
int num = 0x12345678;
}
41
0 41 78
1 78 56
0 4 Bytes
34
2 56
3 34
4 12
5 ?
6 ?
7 ?
Advanced C
Data Alignment
Example
Fetching the integer by
int main()
{
the CPU will be like shown
char ch = 'A'; below
int num = 0x12345678;
}
41
0 41 78
1 78 56
2 56 34
3 34
4 12
5 ? 12
4 4 Bytes
6 ? ?
7 ? ?
?
Advanced C
Data Alignment
Example
Fetching the integer by
int main()
{
the CPU will be like shown
char ch = 'A'; below
int num = 0x12345678;
}
Shift 1 Byte Up
41 78
0 41 78 56
1 78 56 34
2 56 34 0 78
3 34 56
4 12 34
5 ? 12 0 12
6 ? ? 0 Combine
7 ? ? 0
? 12
Example
#pragma pack(1) 0 ch1
1 num
struct main()
{ 2 num
char ch1; 3 num
int num; 4 num
char ch2;
} 5 ch2
Advanced C
UDTs Structures Bit Fields
The compiler generally gives the memory allocation in
multiples of bytes, like 1, 2, 4 etc.,
What if we want to have freedom of having getting
allocations in bits?!.
Example
struct Nibble
{
unsigned char upper : 4;
unsigned char lower : 4;
};
Example
union DummyVar
{
char option;
int id;
double height;
};
The above union will get the size allocated for the type
double
Sequence of bytes
Why?
Persistent storage
Theoretically unlimited size
Flexibility of storing any type data
Advanced C
File Input / Output Via Redirection
The above line feed the input from input_file and output
to output_file
The above might look useful, but its the part of the OS
and the C doesn't work this way
FILE pointer, which will let the program keep track of the
file being accessed
Example
#include <stdio.h>
int main()
{
FILE *fp;
fp = fopen(test.txt, r);
fclose(fp);
return 0;
}
Advanced C
File Input / Output Functions
Example
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *input_fp;
if (input_fp == NULL)
{
exit(1);
}
fclose(input_fp);
return 0;
}
Advanced C
File Input / Output Functions
Prototype Example
int *fgetc(FILE *fp); #include <stdio.h>
int fputc(int c, FILE *fp); #include <stdlib.h>
int main()
{
FILE *input_fp;
char ch;
if (input_fp == NULL)
{
exit(1);
}
fclose(input_fp);
return 0;
}
Advanced C
File Input / Output Functions
Prototype
int fprintf(char *string, char *format, ...);
Example
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *input_fp;
if (input_fp == NULL)
{
fprintf(stderr, "Can't open input file text.txt!\n");
exit(1);
}
fclose(input_fp);
return 0;
}
Stay Connected
About us: Emertxe is Indias one of the top IT finishing schools & self learning kits provider. Our primary
focus is on Embedded with diversification focus on Java, Oracle and Android areas