


































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



































(a) Integer Types:
✓ short int,
✓ int,
✓ long int,
✓ long long int,
unsigned short int
unsigned int
unsigned long int
unsigned long long int
(b) Character Types:
✓ char,unsigned char
(c) Floating Types:
✓ float, double, long double
(d) Other:
✓ void, bool
(a) Derived data type:
✓ Array
✓ Pointer
✓ String
(b) User defined data type:
✓ Structure
✓ union
✓ Enum
✓ typedef
different ranges.
which is no longer that long type.
C Programming
x = 13
Finally, 13 is assigned to x.
(i) Logical AND (&&)
(ii) Logical OR (||)
(iii) Logical NOT (!)
If the first operand is evaluated as false then the second operand is not evaluated.
Example1: void main () {
printf (“Hello”) || printf (“Pankaj”);
O/P: Hello
printf function display everything written within double quotes on the monitor and the result / value returned
by printf () is the number of characters successfully displayed on the screen.
So, printf (“Hello”) prints Hello and return 5.
So, the expression
printf (“Hello”) || printf (“Pankaj”);
becomes
5 || printf (“Pankaj”)
As the first operand for logical OR is evaluated as true, second printf () will never execute.
(i) pre increment ++x
(ii) post increment x++
(iii) pre decrement - - x
(iv) post decrement x - -
st
step: Increment the value of variable.
nd
step: After increment, use the value of variable.
st
step: Use the value.
nd
step: Increase the value of variable by 1.
(i) Bitwise AND (&)
(ii) Bitwise OR (|)
(iii) Bitwise XOR (^)
(iv) Bitwise Left Shift (<<)
(v) Bitwise Right Shift (>>)
(vi) Bitwise Not ( )
// output 2
// output 7
// output 5
#include
int main () {
int x = 3, y = 6;
printf (“%d\n”,x & y);
printf (“%d\n”,x|y);
printf (“%d\n”, x^y)
return 0;
binary representation of 3: 00000000...
binary representation of 6: 00000000...
XOR of two bits is 1 if both bits are different.
XOR of two bits is 0 if both bits are same.
(i) To find the number of elements present in an array.
(ii) To dynamically allocate block of memory.
Left? Middle : Right
Right expression
int a;
a = 7 > 2? 3 2 + 5 : 6! = 7
Left expression: 7 > 2 is true
So, the value returned would be the middle argument.
i.e., 3 2 + 5 = 11
so, a will get 11.
An iterative statement, or loop, repeatedly executes a group of statements, known as body of loop, until the controlling
expression is false.
executes to false.
for (expression – 1(optional); expression – 2(optional); expression – 3(optional))
statement – 1
statement – 2
statement – n
(a) expression – 1 is evaluated once before the first iteration of the loop.
(b) expression – 2 is a expression that determines whether to terminate the loop. expression – 2 is evaluated before every
iteration.
If the expression is true (non - zero), the loop body executed.
If the expression is false (zero), execution of the for statement is terminated.
(c) expression – 3 is evaluated after each iteration.
(d) The for statement executes until expression-2 is false (0), or until a jump statement terminates execution of the loop.
(a) If we omit expression-2, the condition is considered as true for an example:
for (i = 0; ; i++) represent an infinite loop
statement.
then the while statement terminates.
while (expression) while (expression)
statement- 1
statement- 2
statement
statement-n
condition after executing the loop body.
do
do {
statement- 1
OR statement- 2
statement
while (expression);
statement-n
} while (expression);
terminate the repetition process, then use break statement.
Example:
void main ()
int i = 1;
while (i < = 15)
zero (default value).
which may be use brk() and sbrk() system calls.
Storage
Class
Scope Life Time Default Storage area
auto Local
Within
function
Garbage
Value
static Local
Till end of
main
program
extern Global
Till end of
main
program
Register Local
Within
function
Garbage Register
some other source file from the file where it is being used.
Definition: When the body of a function call the function itself directly or indirectly.
argument.
Example 1:
Consider the factorial code:
int factorial (int n)
if (n = = 0)
return 1;
else
return n * factorial (n - 1);
If we call factorial (3), it will call factorial of 2 and so on …the argument will get closer to 0 i.e., the base argument.
Sort.
Static scoping is also called as lexical scoping. In this scoping, the binding of a variable can be determined by the program text.
In this type of scoping compiler first looks in the current block (local), then in global variables i.e., local and then ancestors’
strategy is followed.
C Programming
and dynamic scoping, let’s try to find the output.
The order of function calls is:
g() is printing i, which is not present inside current block. So, because of dynamic scoping compiler will go to the function that
calls g() i.e., compiler will search f() for variable i. Hence 20 is printed.
ARRAYS AND POINTERS
double etc.
memory location.
base address and size of the data element.
Code Optimization: Less code to access the data.
Ease of traversing: By using the for loop, we can retrieve the elements of an array easily.
Ease of sorting: To sort the elements of the array, we need a few lines of code only.
Random Access: We can access any element randomly using the array.
Declaration of C Array
Syntax:
Example:
Initialization of C array:
We can initialize each element of the array by using the index. Suppose array int marks [5]. Then
Example:
Example: Program of Two-Dimensional Array
int main()
int i = 0, j = 0;
int arrayHello[4][3] = {{1, 2, 3}, {2, 3, 4}, {4, 5, 6}};
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
printf(“arrayHello[%d][%d]=%d\n”,i,j,arrayHello[i][j])
return 0;
Initialization of 2D Array
int array Hello [4] [3] = {{1,2,3}, {3,4,5}, {4, 5, 6}};
Output: arrayHello[0][0] = 1
arrayHello[0][1] = 2
arrayHello[0][2] = 3
arrayHello[1][0] = 2
arrayHello[1][1] = 3
arrayHello[1][2] = 4
arrayHello[2][0] = 3
arrayHello[2][1] = 4
arrayHello[2][2] = 5
arrayHello[3][0] = 4
data_type array_name [rows] [columns]
int hello [4] [3]
No of rows
Array name
(user define)
Data
Type
No of columns
Array name
(user define)
Data
Type
1
st
Row 2
nd
Row 3
rd
Row
arrayHello[3][1] = 5
arrayHello[3][2] = 6
Example:
void Helloarray (int marks [ ])
for(int i = 0; i < 5; i++)
printf(“%d”, marks[i]);
int main()
int marks[5] = {45, 67, 34, 78, 90};
Helloarray(marks);
return 0;
Note: void Helloarray(int marks[ ])
Without return type function
Now, we will see how to pass an array to a function as a pointer.
void helloarray(char * marks)
printf(“Elements of array are:”);
for(int i = 0; i < 5; i++)
printf(“%c”, marks[i])
int main()
char marks[5] = {‘A’, ‘B’, ‘C’, ‘D’, ‘E’};
helloarray(marks);
return 0;
Note:
Whenever you pass an array, it is always call by reference. In previous 2 programs, we passed an address & formal
argument in both the cases is a pointer internally.
data type
User
defined
function
User define
Array name
valid
invalid
invalid
Note:
sizes of dimensions.
(8),(14) are following this rule while (15), (16) are not following this rule.
also be done with pointes.
int a[5] = {10, 20, 30, 40, 50};
int* Pa;
Pa = &a[0];
Syntax:
*int Pa:
data type * Identifier
Pa is a pointer to integer variable
i.e., Pa can hold address of integer variable
Pa contains address of a[0]
i. &: address of operator
ii. *: value at operator
i,e *Pa is same as 10
elements before Pa.
i.e, In our example Pa points to a[0]
So, Pa+1 will point [1]
Pa+2 will point to a[2]
i,e
Pa+i is address of a[i]
Pa + i & a[i]
*(Pa+i) *& a[i] a [i]
i.e., Pa = &a[0];
and
Pa = a ;
both are same.
All are same and can be used interchangeably in program except in declaration.
int 3[a]; instead of int a[3]
int a[10];
i. a++, ++a, – – a , a – – are all invalid as we cannot apply increment/decrement operator on constants:
ii. Array name cannot be Lvalue of on assignment statement.
i.e.
array_name = any expression/address/value is invalid
int a[5]
int = *Pa;
Pa = a : valid
all arevalid
Pa+ + ;
Pa – – :
int x
int *P
int **q
x = 10
p = &x
q = &p
: x can store value
: p can store address of integer variable
: q can store address of pointer variable
: valid
: valid
: valid
p 1020 i, e address of x
*P value at (memory location 1020)
*p 10
q 1096 i,e address of variable p
*q value at (memory location 1096)
1020 which is again an address
q value at (memory address 1020)
**q 10
This situation raises an error known as dangling pointer problem. Dangling pointer occurs when a pointer pointing to a
variable goes out of scope or when a variables memory gets deallocated.
Consider the code.
int *f( ) {