C Programming Notes - 21759696
C Programming Notes - 21759696
C Programming Notes - 21759696
C Programming Notes
1) Arithmetic Operators :
Arithmetical Operators are used to perform mathematical operations, such as
addition , subtraction, multiplication , division and modulus.
Operator Use
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
Example :
#include <stdio.h>
void main()
{
int a=2, b=3;
printf("a+b=%d\n", a+b);
printf("a-b=%d\n", a-b);
printf("a*b=%d\n", a*b);
printf("a/b=%d\namespace", a/b);
printf("a%b=%d\n", a%b);
}
2) Increment /Decrement Operators :
• These operators are used to increment / decrement value of variable by one.
• In post increment/decrement , Statement get executed before
increment/decrement
• In pre increment/decrement , Statement get executed After increment/decrement.
Operator Use
Post Pre
Example :
/increment decrement
#include <stdio.h>
int main()
{
int var;
printf("enter value for var :");
scanf("%d", &var); // 6
printf("var++ = %d\n", var++); //6 //post incr
printf("++var= %d\n", var--); // 7 pre inc
printf("var-- = %d\n", ++var); // 7 post dec
printf("--var= %d\n", --var); // 6 pre dnc
return 0;
Note :
//post incre/decr -> statement first and then incr/decr
//pre incr/decr -> incr/decr first and statement
3) Relational Operators :
• Relational operators are simply used to check the relationship between the two
entities .
• It compares the operands beside operator and give result by comparing it .
• Result given by these operators is always 0 (false) or 1 (true).
Operator Use
Example:
//relational operator
#include <stdio.h>
void main()
{
int a=5, b=2;
printf("%d \n", (a<b));
printf("%d \n", (a>b));
printf("%d \n", (a==b));
printf("%d \n", (a!=b));
printf("%d \n", (a<=b));
printf("%d", (a>=b));
4) Assignment Operators :
• Assignment operators are used to assign the value of one operand to another
operand.
• Just like relational operator , assignment operators are also used in almost every
program.
Operator Use
Example :
#include <stdio.h>
void main()
{
int a=2, b=3;
printf("a=b=%d\n", a=b);
printf("a+=b=%d\n", a+=b);
printf("a-=b=%d\n", a-=b);
printf("a*=b=%d\n", a*=b);
printf("a/=b=%d\n", a/=b);
printf("a%=b=%d\n", a%=b);
}
5) Logical Operators :
• Logical operators are used to compare between two test expressions (conditions).
• It compares two Boolean values and give result in true or false , but mostly it is used
to compare expressions.
&& ( Logical AND) : returns one only if both conditions are true.
|| (Logical OR) : returns one when any one of both condition is true.
! (Logical NOT) : It returns the opposite state of result , for ex. If output is true then
return false
Operator Use
Example :
#include <stdio.h>
void main()
{
int a=2, b=3;
printf("(a>b)&&(b>a)=%d\n",(a>b)&&(b<a));
printf("(a>b)||(b>a)=%d\n",(a>b)||(b<a));
printf("!(a>b)=%d", !(a>b));
}
6) Bitwise Operators :
• These operations are performed at bit level.
• For output, they convert our input values(can be decimal values) to binary values
first and then process it with given respective operator used with it .
• At last again gives result by again converting binary values into decimal.
Operator Description
| Bitwise OR
^ Bitwise XOR
~ Bitwise Complement
Example :
#include <stdio.h>
void main()
{
int a=2, b=3;
printf("a & b=%d\n",(a & b));
printf("a | b=%d\n",(a | b));
printf("a ^ b=%d\n",(a ^ b));
printf("~a=%d\n",(~a));
printf("a<<2 =%d\n",(a << 2));
printf("a>>2 =%d\n",(a >> 2));
}
Decision control and loop control
1.1 : Decision Making statements .
o If
o If - else
o If - else ladder
o Nested If - else
1.2 : Selection Statements.
o Switch statement
1.3 : Looping Statements .
o While
o Do-while
o For
1.4 : Break, continue, goto, Return.
For More inquiry about M1, M2, Engg. Graphics, C, C++, Java, python , DS, Database → Contact : 7020715325
Decision Making Statements
• Decision making statements are used to decide flow of program on the basis of
condition.
• If the test expression (Condition) is true, statements inside the body of if are
executed.
• If the test expression (Condition) is false, statements inside the body of else are
executed.
1) if :
Syntax :
if(Condition)
{
// Execute the Statements if condition is TRUE;
}
Flowchart :
Example:
#include<stdio.h>
void main()
{
int num ;
printf ( "Enter a number less than 10 " );
scanf ( "%d", &num );
if ( num <= 10 )
{
printf ( "Number is less than 10");
For More inquiry about M1, M2, Engg. Graphics, C, C++, Java, python , DS, Database → Contact : 7020715325
}
printf("Wrong Number !!! please enter number less than 10 ");
2) if -else:
Syntax :
if(Condition)
{
// Execute the Statements if condition is TRUE;
}
else
{
// Execute the Statements if condition is FALSE;
}
Flowchart :
Example:
#include<stdio.h>
void main()
{
int num ;
printf ( "Enter a number =\t " );
scanf ( "%d", &num );
if ( num >0 )
{
printf ( "\t\t\t Its positive number");
For More inquiry about M1, M2, Engg. Graphics, C, C++, Java, python , DS, Database → Contact : 7020715325
}
else
{
printf("\t\t\t Its negative number ");
}
}
3) if -else ladder:
Syntax :
if(Condition 1)
{
// Execute the Statement 1 if condition 1 is TRUE;
}
else if(Condition 2)
{
// Execute the Statement 2 if condition 2 is TRUE;
}
else if(Condition 3)
{
// Execute the Statement 3 if condition 3 is TRUE;
}
else
{
// Execute the Statement if every condition is FALSE;
}
Flowchart :
For More inquiry about M1, M2, Engg. Graphics, C, C++, Java, python , DS, Database → Contact : 7020715325
Example:
void main()
{
int age;
printf("Enter your age\n");
scanf("%d", &age);
if (age>=18)
{
printf("You can vote!");
}
else if(age>=10)
{
printf("You are between 10 to 18 and you can vote for
classroom election");
}
else if(age>=3)
{
printf("You are between 3 to 10 and you can vote for babies
election");
}
else{
printf("You cannot vote because your not born!");
}
4) Nested if -else :
Syntax :
if(Condition 1)
{
// Execute the Statements if condition 1 is TRUE;
if(Condition 2)
{
// Execute the Statements if condition 2 is TRUE;
if(Condition 3)
{
// Execute the Statements if condition 3 is
TRUE;
}
else
For More inquiry about M1, M2, Engg. Graphics, C, C++, Java, python , DS, Database → Contact : 7020715325
{
// Execute the Statement if condition 3 is
FALSE;
}
}
else
{
// Execute the Statement if condition 2 is FALSE;
}
}
else
{
// Execute the Statement if condition 1 is FALSE;
}
Flowchart :
Example:
#include<stdio.h>
void main()
{
int num;
printf("enter the number greater than 0 and less than 100 = \t");
scanf("%d \n",&num);
if(num>0)
{
For More inquiry about M1, M2, Engg. Graphics, C, C++, Java, python , DS, Database → Contact : 7020715325
if(num<100)
{
printf("\t\t\tnumber is in between 1 to 100 \n");
}
else
{
printf("\t\t\tnumber must be greater than 100 \n");
}
}
else
{
For More inquiry about M1, M2, Engg. Graphics, C, C++, Java, python , DS, Database → Contact : 7020715325
Selection Statement
o Every time we execute the switch the value inside choice variable is
matched with case name/ case value.
o Switch executes that block of code, which matches the case value/ case-
name. If the value does not match with any of the cases, then the
default block is executed.
Switch Case:
Syntax :
switch(Choice variable)
{
case casename1: statement_1;
break;
case casename 2: statement_2;
..... break;
......
......
case casename n: statement_n;
break;
default: default statement;
}
NOTE: Even if you don’t write break it will not give you an error , because it is not
official part of syntax .
Example:
#include<stdio.h>
void main()
{
int order;
printf("\t\t\t\t\t\t\t\tWELCOME TO ROYAL HOTEL\n\n");
printf("\t\t\t\t\t\t************************ MENU
************************\t\t\t\t\t\t\n");
printf("\t\t\t\t\t\t\t\t\t 1. TEA\n");
printf("\t\t\t\t\t\t\t\t\t 2. COFFEE\n");
printf("\t\t\t\t\t\t\t\t\t 3. WATER\n");
printf("\n\n\t\t\t\t enter your order = ");
scanf("%d",&order);
switch(order)
{
case 1: printf("\t\t\t\t\t\t\t serve Tea\n");
break;
case 2: printf("\t\t\t\t\t\t\t serve Coffee\n");
break;
For More inquiry about M1, M2, Engg. Graphics, C, C++, Java, python , DS, Database → Contact : 7020715325
case 3: printf("\t\t\t\t\t\t\t serve Water\n");
break;
For More inquiry about M1, M2, Engg. Graphics, C, C++, Java, python , DS, Database → Contact : 7020715325
Looping Statement
o In a programming sometimes we need to perform some particular task
repetitively .Thus to perform some part of code again and again we use
loops in C.
o There are two types of loops in C:
▪ Entry controlled loops : Test condition is checked before
entering into loop body. Example : while and for.
▪ Exit controlled loops : Test condition is checked after evaluating
loop body. Example : do-while.
1) While loop:
Syntax :
while(Condition)
{
// Statement to execute if condition is TRUE;
}
Example :
// program to print 1 to 50 number
#include<stdio.h>
void main()
{
int i = 1;
while (i<51)
{
printf("%d\n", i);
i++;
}
}
2) Do-while loop:
Syntax :
do
{
// Statement to execute if condition is TRUE;
}
while(Condition);
Example :
// program to print 1 to 50 number
#include<stdio.h>
void main()
{
int i = 1;
do
For More inquiry about M1, M2, Engg. Graphics, C, C++, Java, python , DS, Database → Contact : 7020715325
{
printf("%d\n", i);
i++;
}while (i<51);
}
3) For loop :
Syntax :
for(initialization ; Condition ; increment/ decrement)
{
// Statement to execute if condition is TRUE;
}
Example :
// program to print 1 to 50 number
#include<stdio.h>
void main()
{
for(int i=1;i<51;i++)
{
printf("%d\n", i);
}
}
For More inquiry about M1, M2, Engg. Graphics, C, C++, Java, python , DS, Database → Contact : 7020715325
4) Return :
The return statement is used to return the value of the expression back to the
main function.
Example :
#include<stdio.h>
void main()
{
printf(“%d”, add());
}
int add()
{
int a=4;
return a;
}
For More inquiry about M1, M2, Engg. Graphics, C, C++, Java, python , DS, Database → Contact : 7020715325
Array
- In C, An array is collection of similar type of data.
- In array elements are stored continuously.
- In C, there is no dimensions limit to an array.
- Array index always start from 0.
- Last index=size-1
- Declaration of array syntax :
Datatype arrayname[Column/Sizeofarray];
Example : int list[10];
float marks[5];
- Properties of array –
1) Data is stored continuously in the array.
2) Each element in array has same size as per its datatype.
3) Any elements or item of array can be accessed easily using its index and base
address.
- Advantages of array –
1) Array is used to represent multiple data items/elements (of same type) using
single name.
2) Accessing elements of array is fast.
- Most commonly used types of array –
1) One dimensional array :-
Syntax : datatype arrayname[column/size];
Example :
Output:
Output:
3) Three dimensional array :-
Syntax : datatype arrayname[no_of_array][no_of_rows][no_of_columns];
Example :
Output:
FUNCTION
• Functions are block of statements that perform some given task to it.
• Functions are used to divide large amount of code into smaller pieces.
• Functions can be called multiple times to perform its task repetitively .
Advantages : 1) Provides Modularity
2) Provides Reusability.
3) Make code easy to debug & understand.
Types of function :
1) System Defined Function/Library Function :
- Are also called as Pre-defined function.
- These functions are defined inside system libraries in C.
- Example of system defines functions are : main(), printf(), getch(),
scanf() , abs(), clrscr() etc :
- Note : Every program must contain one library function that is main() ,
because execution of program start from main. Its also called as entry
point of program .
Function has four types based on its return type and argumets :
1) Without arguments and without return value –
In this type of function we don’t need to pass any value to function and
also don’t need to take any return value from it.
Example :
Output:
2) Without arguments and with return value -
In this type of function we don’t need to pass any arguments to function
but must get something in return from it.
Example :
Output:
Output:
KEYS:
Actual parameters are the parameters that are passed to function while calling it.
Formal parameters are the parameters that are used in the function definition as
function arguments.
Call by value :
- In this method , Values of actual parameters are copied into Formal Parameters.
- In this type of function call, change in values of formal parameters in called
function does not affect the values of actual parameters.
Example :
Output:
Call by reference :
- In this method , address of actual parameters are copied into Formal Parameters.
- In this type of function call, change in values of formal parameters in called
function directly affect the values of actual parameters.
- Example :
Output:
Output:
Recursive Function :
- The process of function calling itself to work on smaller problems is called
as recursion . And the function that calls itself directly or indirectly get called
as recursive function.
Example :
Output :
Structure
__________________________________________________________
• It is a collection of different type of data in same location .
• Structures are used when we want to store dissimilar data together.
• Syntax :
struct structure_name
{
//element 1;
//element 2;
.
.
.
//element n;
};
• Structure variable syntax:
struct structure_name var_name= value;
• Example :
Output
Union
Example :
Output:
Pointer
• In c , Pointer is a special variable that stores the address of another variable .
fffh
fffg
fffg
50
P
(pointer variable) A
(normal variable)
Output :
Note :
Call by reference and Call by value notes are included in chapter function .