Programming and Data Structure
C-PROGRAMMING
C-PROGRAMMING
Variable
A variable is nothing but a name given to a storage area in our programs where we
store values, etc.
Variable types Size (In Bytes) Range of Values
Int 2 -32768 to +32767
Signed Int 2 -32768 to +32767
Unsigned Int 2 0 to 65535
Float 4 3.4E-38 to 3.4E+38
Double 8 1.7E-308 to 1.7E+308
Char 1 -128 to +127
Unsigned Char 1 0 to 255
Global variable:
visible globally and can be modified by anyone who reference it. It is created in
static area of memory allocation.
Local variable:
scope is restricted within the block where it is defined. It is created in stack area of
corresponding block.
General memory structure of “C” program:
Stack
↓
Free area where stack & heap
can grow dynamically
↑
Heap
Uninitialized data segment
Initialized data segment
Text segment ‘or’ code
segment
Static & dynamic scoping:
int a = 5, b = 6;
Main ()
{
int a = 6, b = 7;
c ();
}
c ()
{
b = 9;
print (a, b);
}
What is output of above program?
The output of above program depends on the type of scoping the compiler is
following (by default our C compiler follows static scoping)
Function call stack diagram of the above program is: -
no variable is created inside B() but only assigned (as no local variable to it hence
the variable access depends on scoping). Local variable of main is created inside
stack. global variable is created in static area.
Dynamic scoping: -
The value taken from dynamic area (nearest in stack). Here main() is nearest
hence output is a = 6, b = 9.
NOTE: -If nearest function has no such variable the it goes to second nearest and
so on. If all such has no variable the it goes to static area.
Static scoping: -
The value is taken from static area. Hence global a & b taken. O/P a = 5, b = 9
Storage classes:
variable's storage class that determines in which of the above two types of
locations, the value should be stored.
Storage Storage Default scope life
class value
Automatic Memory Garbage Local to Till control inside
block the block
Register CPU Garbage Local to Till control inside
register block block
Static Memory Zero Local to Value of variable
block persist b/w
different function
call
Extern Memory Zero Global As long as program
execution does not
come to end
Note:
1. Register can’t be used with static, pointer variable
2. Static variable created only once during compile time.
Control flow statements:
if else
if (condition)
{
block
}
Else
{
block
}
switch statement
Switch(expression)
{
Case value 1: stmt1:
Break;
Case value 1: stmt2:
Break;
.
.
Default: stmt;
}
Notes:
1. after every case break required else remaining cases also execute.
2. after default no break
3. default can be placed anywhere
4. default will execute only when other cases don’t match
5. if default not in end then use break after default.
conditional operator (? :)
Condition? value to be reformed if true: Value to be returned if false
while loop
If condition expression true then execute the block else exit.
While(expression)
{
Block
}
do-while loop
Execute once block & then check condition expression, if true then again execute
the block else exit.
do
{
Block
} while (expression)
For loop
𝐹𝑜𝑟( < 𝑖 > < 𝑖𝑖 > < 𝑖𝑖𝑖 > 𝑖𝑛𝑖𝑡𝑎𝑙𝑖𝑧𝑒; 𝑐𝑜𝑛𝑑𝑖𝑡𝑖𝑜𝑛; 𝑖𝑛𝑐𝑟𝑒𝑚𝑒𝑛𝑡 )
{
Block …………………………………………… <iv>
}
Important tip: - Execution sequence of For loop is first initialization then condition
check, after that block execution and increment, again goes to condition check and
repeats till the condition fails.
i.e. <i> <ii> <iv> <iii> <ii> <iv> <iii>…………………….
Notes:
1. break: - used to jump out of loop immediately
2. continue: - used to take control to beginning of loop immediately.
3. printf () function returns number of characters that is printed.
4. a = printf (“%S”, “Blue”)
5. hence value of a = 4(as blue has 4 character)
6. scanf () function return number of inputs that are scanned.
7. scanf (“%d”, &X) ,here &X → address of variable where value needs to be
stored.
8. Short circuit in && and || || operator
a. (condition 1 && condition 2) → if first condition is false then do not evaluate
second & other conditions.
b. (condition 1 || || condition 2) → if first condition is true then do not evaluate
second & other conditions.
Functions:
function in C can perform a particular task, and supports the concept of modularity.
Syntax of Function:
return_data_type function_name (data_type variable1, data_type variable2, ...)
{
function body
}
Parameter passing :-
Call by value: sum (a, b); → value of a, b is passed.
Call by reference: sum (&a, &b); → address of a, b is passed.
Operator precedence:
Associativity
() Parenthesis Left-to-right
[] Brackets (array subscript)
. Member selection via object
→ Member selection via pointer
++-- Post increment/decrement
++-- Pre increment/decrement Right-to-left
+- Unary plus/minus
!~ Logical negation/bitwise complement
(type) Type casting
* Dereference
& Address of
Size of Determines size
*/% Multiplication/division/module Left-to-right
+- Addition/subtraction
≪≫ Bitwise shift left, bitwise shift right
< <= Relational less than or equal to
> >= Relational greater than or equal to
==!= Relational is equal/is not equal Left-to-right
& Bitwise AND
^ Bitwise exclusive OR
| Bitwise inclusive OR
&& Logical AND
|| Logical OR
?: Ternary conditional
= Assignment Right-to-left
+=-= Addition/subtraction assignment
*=/= Multiplication/division assignment
Pointers:
A variable that stores memory address.
Printf (“%d”, a*b) – error (no operator in between a and *b)
Printf (“%U”, *c) – 1000
Printf (“%d”, a**b) – 400
Note:
Note:
1. Scanf (“%d”, b) here ‘b’ gives address of variable ‘a’.
2. NULL Pointers: Uninitialized pointers start out with random unknown values.
3. ‘&’ =Address of operator.
4. ‘*’ = indirection operator (returns value at given address).
5. &i returns the address of the variable i.
6. *(&i) return the value stored at a particular address.
Array:
collection of same data type elements
int a [5] = {23, 25, 27, 29, 31};
0 1 2 3 4 ← index value
23 25 27 29 31
1000 1002 1004 1006 1008 ← Memory address
int b [3][4];
Given below is two-dimensional array representation (here memory address is put inside the
cells).
2000 2002 2004 2006
2008 2010 2012 2014
2016 2018 2020 2022
Note:
1) a is a constant pointer which contain base address of array.
2) Pointer arithmetic
a) a + 1 → pointer move to next block(1000 + 2 = 1002)
b) b + 1 → pointer move to second 1 –D array(2000 + 4 × 2 = 2008)
8
c) 1008 − 𝑃2 1000 𝑃1 = 2
= 4 (No. of element from P1 to P2)
3) array index expansion
a) a[i] = *(a + i) = i[a]
b) a[i][j] = *(*(a + i) + j)
4) a = a + 3 → error (a is constant pointer)
5) few points
a) a = 1000
b) &a = 1000
c) a + 1 = 1002
d) &a + 1 = 1010 → Skip whole array.
e) b + 1 → skip ‘1’ 1 – D array
f) &b + 1 → skip whole 2 – D array.
String:
String is stored as array of character along with “\O” at end.
char a [] = “Testbook”
1000 1001 1002 1003 - - - - 1008
T E s t b o o k \O
0 1 2 3 4 5 6 7 8
𝑃𝑟𝑖𝑛𝑡𝑓 (%𝑠, 𝑎) 𝑃𝑟𝑖𝑛𝑡𝑓 (%𝑠, 𝑇𝑒𝑠𝑡𝑏𝑜𝑜𝑘) 𝑃𝑟𝑖𝑛𝑡𝑓 (%𝑠, 1000) }𝑇𝑒𝑠𝑡𝑏𝑜𝑜𝑘
Printf (“%s”, a + 3) – tbook
Printf (“%c”, a [1]) – e
Printf (“%c”, *(a + 3)) – t
Printf (“%c”, a + 3) – error
Printf (“%s”, *(a + 3)) – error.
Structure and Union:
used to encapsulate different data in one object.
𝑠𝑡𝑟𝑐𝑡 𝑛𝑜𝑑𝑒 { 𝑖𝑛𝑡 𝑎; 𝑓𝑙𝑜𝑎𝑡 𝑏; 𝑐ℎ𝑎𝑟 𝑐; } ]𝑑𝑎𝑡𝑎 𝑡𝑦𝑝𝑒 𝑐𝑟𝑒𝑎𝑡𝑒𝑑
Struct node d = {10, 20.5, ‘X’}; → memory created
Struct node *e = &d
d.c = ‘x’
(*e).c = ‘x’
e → c = ‘x’
Note:
1. Union is same to structure except in case of union memory of only one data type
is created which is maximum.
2. only one of all data types is used.
3. malloc → used to create memory at runtime with garbage value (malloc (size)).
4. Calloc → used to create memory at runtime with clear memory (calloc (size)).
Extra points:
1. Post increment/decrement: Increment or decrement is done after the value
evaluated in expression
E.g., {𝑖 = 5 𝑥 = 𝑖 ++ ⇒ 𝑖 = 6 𝑥 = 5
2. Pre increment/decrement: Increment or decrement is done before the value is
evaluated in expression.
E.g., {𝑖 = 5 𝑥 =++ 𝑖 ⇒ 𝑖 = 6 𝑥 = 6
3. Format specifiers of C language.
a) %c – character
b) %d – signed integer
c) %f – float values
d) %s – string
e) %u – unsigned integer
f) %L – long
g) %if – double.
4.ptr = (cast-type*) malloc (byte-size)
ptr = (int *) malloc (size of (int))
a) malloc create memory of given size & return its address
b) address is stored in pointer & cast type used to give certain data type
whatever required.
c) If space is not available it returns a NULL pointer.
d) default pointer return type is void.