C Notes
C Notes
Algorithm
The word Algorithm means “a process or set of rules to be followed in calculations or other problem-solving operations”.
Therefore Algorithm refers to a set of rules/instructions that step-by-step define how a work is to be executed upon
in order to get the expected results.
Advantages of Algorithms:
It is easy to understand.
In Algorithm the problem is broken down into smaller pieces or steps hence, it is easier for the programmer to convert it
into an actual program.
Disadvantages of Algorithms:
Flowcharts
What is a Flowchart?
Flowchart is a graphical representation of an algorithm. Programmers often use it as a program-planning tool to solve a
problem. It makes use of symbols which are connected among them to indicate the flow of information and
processing.
The process of drawing a flowchart for an algorithm is known as “flowcharting”.
Terminal: The oval symbol indicates Start, Stop and Halt in a program’s logic flow. A pause/halt is generally used in a
program logic under some error conditions. Terminal is the first and last symbols in the flowchart.
Input/Output: A parallelogram denotes any function of input/output type. Program instructions that take input from input
devices and display output on output devices are indicated with parallelogram in a flowchart.
Processing: A box represents arithmetic instructions. All arithmetic processes such as adding, subtracting, multiplication
and division are indicated by action or process symbol.
Diamond : It represents a decision point. Decision based operations such as yes/no question or true/false are indicated
by diamond in flowchart.
Connectors: Whenever flowchart becomes complex or it spreads over more than one page, it is useful to use connectors
to avoid any confusions. It is represented by a circle.
Flow lines: Flow lines indicate the exact sequence in which instructions are executed. Arrows represent the direction of
flow of control and relationship among different symbols of flowchart.
Advantages of Flowchart:
Disadvantages of Flowchart:
We use system in Computer architecture, Operating system, Networking communication, Database, graphics……..etc.,
Most of the web browsers and word process are writer in c language.
History of language.
According to c….
In 1967 BCPL(Basic combined programming Language) it is developed by Martin Richard at Cambridge university.
C++:
JAVA:
C#:
JAVA Script:
Applications of C:
OS(operating Systems):
These are like Unix, Linux, window XP……etc…. In this operating systems are used in c coding.
ES(Embedded System):
These are nothing but the combination of software and hardware is known as c in embedded systems.
DD(Device Drivers):
In every software we can use device drivers. In this device driver it stores audios/videos configuration etc….,
Compiler:
These are nothing but the program to check the errors in program
Types of language:
Use English word to develop programs. These are easy to learn and use like COBOL, PHP or java. C also uses English word
in its programs and hence it is called high level programming language.
Use machine code instructions to developed programs. These instructions directly interact with the CPU. Machine language
and assembly language are called low level languages.
C tokens
Character sets :
These are nothing but the set of alphabets, digits are some specials characters. That is valid in c language.
Alphabets:
Upper Case: A to Z
Lower Case: a to z
Digits
0 to 9
Specials Characters:
Separators
Separators are symbols that indicate the division and arrangement for groups of codes.
parentheses ( )
Used to define precedence in expressions, to enclose parameters in method definitions, and enclosing cast types.
braces { }
brackets [ ]
semicolon ;
comma ,
period (.)
Used to separate package names from classes and subclasses, for separate a variable or a method from reference
variable.
Keywords:
These are the reserved words used in programming each key word has a fixed meaning that can’t be changed by
a key words.
auto, break, case, char, const, continue, default, do, double, else, enum, extern, float, for, go to, if, int, long, register,
return, short, signed, sizeof, static, struct, switch, typedef, union, unsigned, void, volatile, while
Input function:
scanf(“-----“); it is an input function. This is the function which can be used to read an input from the command
prompt.
Output function:
printf(“-----“); it is a output function. In this function which can be used to write the function an output from the
command prompt.
Sample Program Syntax:
#include<stdio.h>
main()
printf(“Welcome to ISAN”);
stdio.h – Standard input output.header. It can store the printf & scanf functions.
Data types:
int (0 to 9)
Int 2 bytes
Long 8 bytes
Char 1 byte
Float 4 bytes
Double 8 bytes
Formats:
int %d
float %f , %g
double %lf
char %c
string %s
short %h
unsigned %u
#include<stdio.h>
main()
char ch='A';
Escape sequences:
\nnew line
\ttab space
//double slash
#include<stdio.h>
main()
#include<stdio.h>
main()
int a=1;
float b=2.0;
char g=’k’;
#include<stdio.h>
main()
int a;
float b;
char g;
scanf("%c",&g);
scanf("%d",&a);
scanf("%f",&b);
printf("the values of a,b,g are %d %f %c",a,b,g);
#include<stdio.h>
main()
int a;
float b;
char ch[3]={'a','k','g'};
scanf("%d",&a);
scanf("%f",&b);
Operators
An operator is a symbol that operates on a value or a variable. For example: + is an operator to perform addition.
Arithmetic operators
Relational Operators
Assignment Operators
Logical Operators
Comma Operator
Sizeof Operator
Bitwise Operators
C Arithmetic Operators
An arithmetic operator performs mathematical operations such as addition, subtraction, multiplication, division etc on
numerical values (constants and variables).
* multiplication
/ division
#include <stdio.h>
main()
{
int a = 9,b = 4, c;
c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c = a/b;
printf("a/b = %d \n",c);
c = a%b;
C Relational Operators
A relational operator checks the relationship between two operands. If the relation is true, it returns 1; if the relation is
false, it returns value 0.
== Equal to
!= Not equal to
#include <stdio.h>
main()
int a = 5, b = 5, c = 10;
}
C Assignment Operators
An assignment operator is used for assigning a value to a variable. The most common assignment operator is =
= a=b a=b
+= a += b a = a+b
-= a -= b a = a-b
*= a *= b a = a*b
/= a /= b a = a/b
%= a %= b a = a%b
#include <stdio.h>
main()
int a = 5, c;
c = a;
c += a;
c -= a;
c *= a;
c /= a;
c %= a;
C programming has two operators increment ++ and decrement -- to change the value of an operand (constant or
variable) by 1.
Increment ++ increases the value by 1 whereas decrement -- decreases the value by 1. These two operators are unary
operators, meaning they only operate on a single operand.
#include <stdio.h>
main()
printf("%f\n",c);
printf("%f",d);
C Logical Operators:
Operator Meaning
#include <stdio.h>
main()
result = (a == b) || (c <b);
Comma Operator
Comma operators are used to link related expressions together. For example:
int a, c = 5, d;
sizeof Operator
The sizeof is a unary operator that returns the size of data (constants, variables, array, structure, etc).
#include <stdio.h>
main()
{
int a;
float b;
double c;
char d;
In arithmetic-logic unit (which is within the CPU), mathematical operations like: addition, subtraction, multiplication and
division are done in bit-level. To perform bit-level operations in C programming, bitwise operators are used.
| Bitwise OR
^ Bitwise XOR
~ Bitwise complement
The output of bitwise AND is 1 if the corresponding bits of two operands is 1. If either bit of an operand is 0, the result of
corresponding bit is evaluated to 0.
Let us suppose the bitwise AND operation of two integers 12 and 25.
00001100
&00011001
________
#include<stdio.h>
int main()
return0;
Bitwise OR operator |
The output of bitwise OR is 1 if at least one corresponding bit of two operands is 1. In C Programming, bitwise OR operator
is denoted by |.
00001100
|00011001
________
#include<stdio.h>
int main()
return0;
The result of bitwise XOR operator is 1 if the corresponding bits of two operands are opposite. It is denoted by ^.
00001100
^00011001
________
#include<stdio.h>
int main()
return0;
Output
Output = 21
Bitwise compliment operator is an unary operator (works on only one operand). It changes 1 to 0 and 0 to 1. It is denoted
by ~.
~ 00100011
________
If we give numbers and ask the character data type of that given number , it gives the ASCII value of that given number.
ASCII value is something which is having a fixed value for each and every character, digit, symbol and every input from
the keyboard.
#include<stdio.h>
main()
int a;
scanf("%d",&a);
printf("%c",(char)a);
Control Statements
In this statements we have if, if else, nested if else., statements are presents.
In this statements groups of statements are executed where condition becomes true. If condition false then else
statements will be printed.
if statements:
In this statements check whether the condition and text expression inside parenthesis “()” is true are not.
If the text expression is true statements inside the condition is executed. If it is false it ignores.
Syntax:
if(expression)
Statements;
#include<stdio.h>
main()
int a=10,b=20;
if(a==b)
printf("apple");
}
If_else statements:
In this statements when the if condition becomes false when the else part is printed.
When the if condition is true then the else part is not printed.
Syntax:
if(condition)
statements;
else
statement;
#include<stdio.h>
main()
int a=10,b=20;
if(a==b)
printf("banana");
else
printf("orange");
In this statements if condition one is false then the condition true is checked and statements are executed if it is
true. When condition is true is also false when else part will be executed.
Syntax:
if(condition)
Stmts;
else if(condition)
Stmts;
else
Stmts;
}
#include<stdio.h>
main()
int a=10,b=10;
if(a>b)
printf("apple");
else if(a<b)
printf("asdf");
else
printf("jkl");
In this statements in c are used to perform looping operation until the given condition is true.
Control comes out of the loop statements one’s condition becomes is false.
for loop
while loop
For loop:
In programming, a loop is used to repeat a block of code until the specified condition is met.
Syntax:
for(Initialization;condition checking;Increment/decrement)
Statements;
#include<stdio.h>
main()
int a;
for(a=20;a>10;a--)
{
printf("%d \n",a);
While loop:
Syntax:
while(condition)
Stmts;
Increments or decrements;
#include<stdio.h>
main()
int i=1;
while(i<10)
printf("%d \n",i);
i++;
Do while loop:
In this loop is executed for the first time it ignores the condition after execution the while loop for the first time the
condition will be checked.
Syntax:
do
Stmts;
Increment or decrement;
while(condition);
#include<stdio.h>
main()
int i=0;
do
{
printf("%d \n",i);
i++;
while(i<=10);
#include<stdio.h>
main()
int a;
scanf("%d",&a);
do
printf("%d \n",a);
a++;
while(a<=10);
In this statements which are used to executed only specific block of statements is a series of block are called case
control statements.
Switch case
break
continue
goto
Switch case:
In this statements which are used to executed only specific block of statements is a series of block are called case
control statements.
Syntax:
Switch(condition)
Case 1:statements;
Case 2:statements;
Case 3:statemtents;
--------
--------
default: stmts;
Break case:
The first one is used to terminate the loop, the second one is used to true condition in the switch.
Syntax:
break;
#include<stdio.h>
main()
int num=2;
switch(num)
case 1:
printf("hello");
break;
case 2:
printf("rams");
break;
case 3:
printf("raja");
break;
default:
Continue:
In continue statements is used to continue the next iteration for loop, while loop and so to the remaining
statements are striked with in the loop for the particle interaction.
Syntax: Continue;
#include<stdio.h>
main()
int a=15;
do
if(a==20)
a=a+1;
continue;
a++;
while(a<25);
Goto statements:
In this statements is used to transform a normal flow of a program to specific table program.
Syntax:
goto label
stmts;
stmts;
------
------
label
#include<stdio.h>
main()
int number;
printf("WWW.");
goto x;
y:
printf("expert");
goto z;
x:
printf("c programming");
goto y;
z:
printf(".com");
#include<stdio.h>
int main()
scanf(“%d %d”,&num1,&num2);
if(num1 > num2)
printf(“%d is greater”,num1);
else
printf(“%d is greater”,num2);
return 0;
#include<stdio.h>
int main()
scanf(“%d %d %d”,&num1,&num2,&num3);
printf(“%d is greater”,num1);
printf(“%d is greater”,num2);
else
printf(“%d is greater”,num3);
return 0;
//Prime or Not
#include<stdio.h>
int main()
int n,i,m=0,flag=0;
scanf("%d",&n);
m=n/2;
for(i=2;i<=m;i++)
{
if(n%i==0)
flag=1;
break;
if(flag==0)
printf("Number is prime");
return 0;
//Even or Odd
#include <stdio.h>
int main()
int num;
scanf("%d", &num);
if(num % 2 == 0)
else
return 0;
#include <stdio.h>
void main()
int i;
for (i=1;i<=10;i++)
printf("%d ",i);
printf("\n");
#include<stdio.h>
int main(void)
{
int n=0,i=0;
for(i=2;i<=20;i=i+2)
printf("%d ",i);
return 0;
#include<stdio.h>
int main(void)
int n=0,i=0;
for(i=1;i<=20;i=i+2)
printf("%d ",i);
return 0;
#include<stdio.h>
int main()
int m,n,i,sum=0;
scanf("%d %d",&m,&n);
for(i=m;i<=n;i=i+2)
sum=sum+i;
return 0;
#include<stdio.h>
int main()
int m,n,i,sum=0;
scanf("%d %d",&m,&n);
for(i=m;i<=n;i=i+2)
sum=sum+i;
return 0;
#include<stdio.h>
int main()
int m,n,i,sum=0;
scanf("%d %d",&m,&n);
for(i=m;i<=n;i=i++)
sum=sum+i;
return 0;
#include<stdio.h>
int main()
int n,r,sum=0,temp;
scanf("%d",&n);
temp=n;
while(n>0)
r=n%10;
sum=(sum*10)+r;
n=n/10;
if(temp==sum)
else
printf("not palindrome");
return 0;
//Armstrong or not
#include<stdio.h>
int main()
int n,r,sum=0,temp;
scanf("%d",&n);
temp=n;
while(n>0)
r=n%10;
sum=sum+(r*r*r);
n=n/10;
if(temp==sum)
else
return 0;
//Reverse of a number
#include<stdio.h>
int main()
scanf("%d", &n);
while(n!=0)
rem=n%10;
reverse=reverse*10+rem;
n/=10;
return 0;
#include<stdio.h>
main()
int a,b,c,j;
scanf("%d %d",&a,&b);
printf("case 1:Add\n case 2:sub\n case 3:mul\n case 4:div\n case 5:mod\n");
scanf("%d",&j);
switch(j)
case 1:c=a+b;
break;
case 2:c=a-b;
break;
case 3:c=a*b;
break;
case 4:c=a/b;
break;
case 5:c=a%b;
break;
default:
//Vowel Program
#include<stdio.h>
main()
char i;
scanf("%c",&i);
switch(i)
case 'a':
printf("It is vowel");
break;
case 'e':
printf("It is vowel");
break;
case 'i':
printf("It is vowel");
break;
case 'o':
printf("It is vowel");
break;
case 'u':
printf("It is vowel");
break;
default:
#include<stdio.h>
int main()
char c;
scanf("%c" , &c);
return 0;
//Eligibility to Vote
#include<stdio.h>
int main()
int a ;
scanf("%d",&a);
if (a>=18)
else
{
return 0;
#include<stdio.h>
void main()
int x, y, temp;
scanf("%d %d",&x,&y);
printf("Before Swap\n");
temp = x;
x = y;
y = temp;
printf("\nAfter Swap");
#include <stdio.h>
main()
int year;
printf("Enter a year:\n");
scanf("%d",&year);
if (year % 4 == 0)
else
// Factorial of a number
#include<stdio.h>
int main()
int i,fact=1,number;
scanf("%d",&number);
for(i=1;i<=number;i++)
{
fact=fact*i;
return 0;
#include <stdio.h>
int main()
GCD_Num = i;
printf (" GCD of two numbers %d and %d is %d.", n1, n2, GCD_Num);
return 0;
#include <stdio.h>
void main()
printf( " Enter any two positive numbers to get the LCM \n ");
while (flag)
printf( " The LCM of %d and %d is %d. ", num1, num2, max_div);
break;
++max_div;
}
#include <stdio.h>
int main()
{
char s[1000];
int c=0,j;
printf("Enter string: ");
gets(s);
for(j = 0; s[j] != '\0'; j++)
{
if(s[j]=='0' || s[j]=='1'|| s[j]=='2'||s[j]=='3'|| s[j]=='4'|| s[j]=='5'||s[j]=='6'|| s[j]=='7'|| s[j]=='8'|| s[j]=='9')
c++;
}
printf("\nNumber of digits in string = %d", c);
return 0;
}
//Power of a Number
#include <stdio.h>
int main()
scanf("%d", &base);
scanf("%d", &exp);
while (exp != 0)
result *= base;
--exp;
return 0;
// Fibonacci Series
#include<stdio.h>
int main()
int n1=0,n2=1,n3,i,n;
scanf("%d",&n);
printf("\n%d %d",n1,n2);
for(i=2;i<n;++i)
n3=n1+n2;
printf(" %d",n3);
n1=n2;
n2=n3;
return 0;
#include <stdio.h>
int main()
int num, i;
scanf("%d", &num);
if (num % i == 0)
return 0;
#include<stdio.h>
void main()
int i,j,n;
scanf("%d",&n);
for(i=2;i<=n;i++)
int c=0;
for(j=1;j<=i;j++)
if(i%j==0)
c++;
}
if(c==2)
printf("%d ",i);
#include<stdio.h>
main()
int r;
float C,A;
scanf("%d",&r);
C= 2*3.14*r;
A=3.14*r*r;
//Area Of Triangle
#include<stdio.h>
main()
int b,h;
float area;
printf("Enter breadth:\n");
scanf("%d",&b);
printf("Enter Height:\n");
scanf("%d",&h);
area=0.5*b*h;
#include<stdio.h>
#include<math.h>
main()
int a,b,c;
float x,y;
x= (-b+sqrt(b*b-4*a*c))/4*a;
y= (-b-sqrt(b*b-4*a*c))/4*a;
#include<stdio.h>
int main()
int i, n;
scanf("%d", &n);
if(n == i*i)
return 0;
return 0;
// 111
222
333
#include<stdio.h>
main()
int i,j;
for(i=1;i<=3;i++)
for(j=1;j<=3;j++)
printf("%d\t",i);
printf("\n");
}
// 123
123
123
#include<stdio.h>
main()
int i,j;
for(i=1;i<=3;i++)
for(j=1;j<=3;j++)
printf("%d\t",j);
printf("\n");
//1
22
333
#include<stdio.h>
main()
int i,j;
for(i=1;i<=3;i++)
for(j=1;j<=i;j++)
printf("%d\t",i);
printf("\n");
}
//1
12
123
1234
#include<stdio.h>
main()
int i,j;
for(i=1;i<=4;i++)
for(j=1;j<=i;j++)
printf("%d\t",j);
printf("\n");
}
// $
$$
$$$
#include<stdio.h>
main()
int i,j;
for(i=1;i<=3;i++)
for(j=1;j<=i;j++)
printf("$");
printf("\n");
}
// $
$ $
$$ $
#include<stdio.h>
main()
int i,j,k;
for(i=1;i<=3;i++)
for(j=1;j<=3;j++)
if(i==j)
printf("$");
else
printf(" ");
printf("\n");
// $
#include<stdio.h>
main()
int i,j;
for(i=1;i<=3;i++)
for(j=1;j<=3;j++)
if(i==j)
printf(" $ ");
printf(" ");
}
printf("\n");
// $
$ $
#include<stdio.h>
main()
int i,j,k;
for(i=1;i<=3;i++)
for(j=1;j<=3;j++)
if((i-j)==1 || (j-i)==1)
printf("$");
else
printf(" ");
printf("\n");
}
Arrays:
An array is a data structure containing loop data values which have the same type.
These values are known as elements. It can be individuals sections by their position within the array.
An array is a variable which is capable of holed by many values where as that are older variables can hold a single value at
a time.
An array is a data structure in c that can store a fixed size and sequential collection of elements of same data type.
For example if u want to store 10 members then instead of defining 10 variables its easy to define an array of 10
length.
Declaration of an array:
This is a simple kind of array as just One dimensional array. The elements are One dimensional array of continuously
arranged one after another in a single row and columns.
#include<stdio.h>
main()
int sai[5]={10,20,30,40};
printf("sai[0]=%d \n",sai[0]);
printf("sai[1]=%d \n",sai[1]);
printf("sai[2]=%d \n",sai[2]);
printf("sai[3]=%d \n",sai[3]);
#include<stdio.h>
main()
int i,a[5];
for(i=0;i<5;i++)
scanf("%d",&a[i]);
for(i=0;i<5;i++)
printf("%d \n",a[i]);
}
#include<stdio.h>
main()
int i,a[5]={1,2,3,4,5};
for(i=0;i<5;i++)
printf("a[%d]=%d \n",i,a[i]);
#include<stdio.h>
main()
int i,a[10],b[10],j=0;
for(i=0;i<10;i++)
scanf("%d",&a[i]);
if(a[i]%2==0)
b[j] = a[i];
j++;
for(i=0;i<j;i++)
printf("%3d",b[i]);
Multi-dimensional array:
When the data must be stored in the form of a matrix. We use the two dimensional array.
a[0][0]=10;
a[0][1]=20;
a[1][0]=30;
a[1][1]=40;
An above declaration in two dimensional array consisting of two rows and two columns. So that total no of
elements are four.
#include<stdio.h>
main()
int i,j,e[2][2]={11,12,13,14};
for(i=0;i<2;i++)
for(j=0;j<2;j++)
printf("e[%d][%d]=%d \t",i,j,e[i][j]);
printf("\n");
#include<stdio.h>
#include<conio.h>
main()
int a[2][2],b[2][2],c[2][2],i,j;
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",&a[i][j]);
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",&b[i][j]);
}
}
for(i=0;i<2;i++)
for(j=0;j<2;j++)
c[i][j]=a[i][j]+b[i][j];
for(i=0;i<2;i++)
for(j=0;j<2;j++)
printf("%3d",c[i][j]);
printf("\n");
#include <stdio.h>
void main ()
int num[20];
int i, j, a, n;
scanf("%d", &n);
scanf("%d", &num[i]);
// Array Sorting
#include <stdio.h>
void main ()
{
int num[20];
int i, j, a, n;
scanf("%d", &n);
scanf("%d", &num[i]);
a = num[i];
num[i] = num[j];
num[j] = a;
printf("%d\n", num[i]);
#include <stdio.h>
void main ()
int num[20];
int i, j, a, n;
scanf("%d", &n);
scanf("%d", &num[i]);
a = num[i];
num[i] = num[j];
num[j] = a;
#include<stdio.h>
main()
int i,j,m,n,a[10][10];
scanf("%d",&m);
scanf("%d",&n);
printf("Enter elements:");
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
printf("%d\t",a[i][j]);
printf("\n");
//Matrix multiplication
#include<stdio.h>
#include<stdlib.h>
int main(){
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
scanf("%d",&r);
scanf("%d",&c);
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&a[i][j]);
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&b[i][j]);
for(i=0;i<r;i++)
for(j=0;j<c;j++)
mul[i][j]=0;
for(k=0;k<c;k++)
mul[i][j]+=a[i][k]*b[k][j];
for(i=0;i<r;i++)
for(j=0;j<c;j++)
printf("%d\t",mul[i][j]);
}
printf("\n");
return 0;
//Matrix Addition
#include <stdio.h>
main()
scanf("%d", &r);
scanf("%d", &c);
scanf("%d", &a[i][j]);
scanf("%d", &b[i][j]);
if (j == c - 1)
{
printf("\n\n");
#include<stdio.h>
int main()
int n,arr[n],i;
scanf("%d",&n);
scanf("%d",&arr[i]);
printf("%d\n",arr[i]);
return 0;
#include<stdio.h>
int main()
int sum=0,n,arr[n],i;
scanf("%d",&n);
scanf("%d",&arr[i]);
sum += arr[i];
return 0;
#include<stdlib.h>
int main()
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
printf("%d ",a[i]);
for(i=0;i<n;i++)
if(a[i] == a[j])
a[k] = a[k+1];
j--;
n--;
for(i=0;i<n;i++)
printf("%d ",a[i]);
}
Strings:
Strings are nothing but array of character and entered with null characters[‘\0’].
String are always enclosed by (“---”) double equations were as character is enclosed by string single quotation (‘----‘).
Declaration:
Initialization of string:
char s[10]={‘H’,’E’,’L’,’L’,’O’};
char s[6]=”hello”;
char s[]=”hai”;
Difference between above Declaration when we declare char is s[10] 10 bytes memory space is allocated for holding the
string.
When we declare as s[] memory space will be allocated as per the required during for the program.
#include<stdio.h>
main()
char s1[10]=”hello”;
printf(“s1=%s \n”,s1);
String functions:
strcat
strncat
strcpy
strncpy
strlen
strupr
strlwr
strrev
strcmp
strset
String concatenation:
In this function concatenation the position of one string at the end of the other string.
Syntax:
strcat(s1,s2);
#include<stdio.h>
#include<string.h>
main()
strcat(s1,s2);
printf("s1=%s \n",s1);
String unconcatenation:
It is also same as String concatenation but it will declare the length of the s1 or s2. So it will prints the length position.
Syntax:
strncat(s1,s2,5);
#include<stdio.h>
#include<string.h>
main()
char s1[10]="hello";
strncat(s1,s2,5);
printf("s1=%s \n",s1);
It copies the s1 content to the s2 content. In this s1 content has 30 bytes of memory and s2 has declared 20 bytes of
memory only means. It will only print 20 characters.
Syntax:
strcpy(s1,s2);
#include<stdio.h>
#include<string.h>
main()
char s1[10]="hello";
char s2[10]="lucky";
strcpy(s1,s2);
printf("s1=%s \n",s1);
In this it will be copy the lengthened position which will be mentioned in the function.
Syntax:
strncpy(s2,s3,3);
#include<stdio.h>
#include<string.h>
main()
{
char s1[10]="hello";
char s2[10];
strncpy(s2,s1,3);
printf("s2=%s \n",s2);
String length:
Syntax:
len=strlen(s);
#include<stdio.h>
#include<string.h>
main()
char s1[10]="hello";
char s2[10]="hello";
int len;
len=strlen(s1);
printf("len=%d \n",len);
String reverse:
Syntax:
strrev(s);
#include<stdio.h>
#include<string.h>
main()
char s1[10]="apple";
printf("s1=%s \n",strrev(s1));
String lower:
In this string lower it will print the higher level language to the lower level language.
Syntax:
strlwr(s);
#include<stdio.h>
#include<string.h>
main()
char s1[10]="HELLO";
printf("s1=%s \n",strlwr(s1));
String upper:
Syntax:
strupr(s)
#include<stdio.h>
#include<string.h>
main()
char s1[10]="hello";
printf("s1=%s \n",strupr(s1));
String compare:
In this function s1 and s2 are same It will print 0. If s1 is less than s2 it will print -1. If s1 is greater than s2 it will print 1.
Syntax:
strcmp(s)
#include<stdio.h>
#include<string.h>
main()
char s1[10]="apple";
char s2[10]="app";
int x,y,z;
x=strcmp(s1,s2);
y=strcmp(s2,"app");
z=strcmp(s1,"apples");
printf("x=%d \n",x);
printf("y=%d \n",y);
printf("z=%d \n",z);
String set:
In this program of string “ hello” is set to ‘&’ using string set function and output will be display like ‘&&&&&’ .
Syntax:
strset(s)
#include<stdio.h>
#include<string.h>
main()
char s1[7]="orange";
strset(s1,'&');
printf("s1=%s \n",s1);
#include<stdio.h>
#include<string.h>
main()
int c=0;
char s[10]="Meghana",Ch,i;
//scanf("%s",&s);
scanf("%c",&Ch);
for (i=0;i<strlen(s);i++)
if(s[i]==Ch)
c++;
#include <stdio.h>
int main()
char line[150];
line[i] == 'U')
++vowels;
else if ((line[i] >= 'a' && line[i] <= 'z') || (line[i] >= 'A' && line[i] <= 'Z'))
++consonant;
++digit;
++space;
return 0;
#include <stdio.h>
int main()
printf("Input a string\n");
gets(x);
reverse(x);
return 0;
}
int reverse(char *s)
char r[1000];
count++;
end = count - 1;
r[begin] = s[end];
end--;
r[begin] = '\0';
printf("%s\n", r);
return 0;
#include <stdio.h>
#include<string.h>
int main()
char str[50];
scanf("%s", str);
str_length = strlen(str);
printf("%c",str[index]);
return 0;
#include<string.h>
int main()
char str[50];
scanf("%s", str);
str_length = strlen(str);
count++;
printf("%d",count);
return 0;
#include <stdio.h>
#include <string.h>
int main()
int i,j;
strcpy(temp, str[i]);
strcpy(str[i], str[j]);
strcpy(str[j], temp);
fputs(str[i], stdout);
return 0;
Functions:
A function is a self contained block are a program of one or more statements. That perform a special task when that
is called as function.
Built-in function:
In this function is also called as library function.These function can not be changed by a user.
In this function user can give the unique name to a function and to create a function by using functional elements.
Example:variables , constants………
Uses of function:
If we want to perform a task continuously then it is not necessary to rewrite the program again and again. If we
shift the particular statements in a user define function. So it can be used many no of times.
Declaration function.
Called function.
Define function.
Declaration function:
Function type
Function name
Parameter list
Semicolon(;)
Function type:
Return type
Return type:
int, float, char, double in this it only return one value at a time is known as return type.
Function name:
Parameter list:
It means how many input user giving are receiving is known as parameter list.
Actual parameters
Formal parameters
Example : actual(10,20);
The arguments are called function is known as formal parameter list but it copies the actual arguments values is
also known as formal arguments.
Semicolon:
Function declaration:
int hello();
void hello();
Called function:
call function
calling function
Call function:
This function which is calls the calling function is known as called function.
Example:hello()
Calling function:
Example: hello();
There are the function in which no parameters are passed from calling function to the called function. In values are
return from called function to the calling function.
#include<stdio.h>
#include<string.h>
main()
clzname();
function();
rollno();
avg();
void clzname()
void function()
char name[10]="abdul";
printf("name=%s \n",name);
void rollno()
int rollno=12345;
void avg()
float avg=000;
printf("avg=%f \n",avg);
#include<stdio.h>
int c;
c=a+b;
printf("c=%d \n",c);
main()
sai(10,20);
sai(10,30);
In this function category no arguments are passing from calling function to called function but values are a return
from called function to the calling function.
#include<stdio.h>
int r()
int a,b;
scanf("%d %d",&a,&b);
return(a+b);
main()
int c;
c=r();
printf("c=%d \n",c);
These are the function in which parameter are passed from calling function to the called function and values are
return from function to be calling function.
#include<stdio.h>
int square(int i)
return i*i;
}
main()
int a;
scanf("%d",&a);
printf("%d",square(a));
#include<stdio.h>
int getValue()
int a;
scanf("%d",&a);
return a;
int square(int n)
return n * n;
int cube(int m)
return m *square(m);
main()
Function Calling:
Call By Value
Call By Reference
Call By Value:
This method copies the actual value of an argument into the formal parameter of the function. In this case, changes made
to the parameter inside the function have no effect on the argument. In Call By Value method, the value of the actual
parameters is copied into the formal parameters.
Here , the parameter which is used in the function call is called actual parameter and the parameter which is used in the
function definition is called formal parameter.
#include<stdio.h>
num=num+100;
int main()
int x=100;
change(x);
return 0;
#include <stdio.h>
int main()
int a = 10;
int b = 20;
swap(a,b);
int temp;
temp = a;
a=b;
b=temp;
Call By Reference:
This method copies the address of an argument into the formal parameter.
Inside the function, the address is used to access the actual argument used in the call. This means that changes made to
the parameter affect the argument.
In Call By Reference, The address of the variable is passed into the function call as actual parameter.
The value of the actual parameters can be modified by changing the formal parameters since the address of the actual
parameters is passed.
In Call By Reference, the memory location is similar for both actual parameters and formal parameters. All the operations
in the function are performed on the value stored at the address of the actual parameters, and the modified value
gets stored at same address.
#include<stdio.h>
(*num) += 100;
int main()
int x=100;
change(&x);
return 0;
#include <stdio.h>
int main()
int a = 10;
int b = 20;
swap(&a,&b);
int temp;
temp = *a;
*a=*b;
*b=temp;
#include<stdio.h>
if (a > b)
return a;
else
return b;
int main()
int a, b, large;
return 0;
#include<stdio.h>
main()
int n, result;
scanf("%d",&n);
result = check_prime(n);
if ( result == 1 )
else
return 0;
}
int check_prime(int a)
int c;
if ( a%c == 0 )
return 0;
return 1;
#include<stdio.h>
int Palindrome(int n)
temp = n;
while( n!=0 )
r = n % 10;
rev = rev*10 + r;
n /= 10;
else return 1;
int main()
int n;
scanf("%d", &n);
if(Palindrome(n) == 0)
else
return 0;
//ArmStrong or Not
#include<stdio.h>
int Armstrong(int n)
{
int l = 0;
int p = 0;
int sum = 0;
int x = n;
while(x!=0)
l = x % 10;
p = l*l*l;
sum += p;
x /= 10;
if(sum == n)
return 0;
else
return 1;
int main()
int n;
scanf("%d",&n);
if(Armstrong(n) == 0)
else
return 0;
#include<stdio.h>
main()
int ch,a,b,c;
scanf("%d %d",&a,&b);
scanf("%d",&ch);
switch(ch)
case 1: c=a+b;
printf("%d + %d = %d",a,b,c);
break;
case 2: if (a>b)
c=a-b;
printf("%d + %d = %d",a,b,c);
else
c=b-a;
printf("%d - %d = %d",b,a,c);
break;
case 3: c=a*b;
printf("%d * %d = %d",a,b,c);
break;
case 4: c=a/b;
printf("%d / %d = %d",a,b,c);
break;
break;
#include<stdio.h>
int main()
int n1,n2;
printf("------------------------------------------------\n");
scanf("%d",&n1);
scanf("%d",&n2);
swap(&n1,&n2);
return 0;
}
void swap(int *p,int *q)
int tmp;
tmp = *p;
*p=*q;
*q=tmp;
#include<stdio.h>
int main()
int i,fact=1,number;
scanf("%d",&number);
for(i=1;i<=number;i++)
fact=fact*i;
return 0;
#include<stdio.h>
int factorial(int n)
if (n == 0)
return 1;
else
return(n * factorial(n-1));
void main()
int number;
long fact;
scanf("%d", &number);
fact = factorial(number);
return 0;
}
//LCM and GCD of two numbers using Functions
#include <stdio.h>
int main()
printf("GCD: %d",GCD);
return 0;
if (y == 0)
return x;
else
#include <stdio.h>
int i,j,temp;
if(a[j]>a[j+1])
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
int main()
int a[100],i,n,key;
scanf("%d", &n);
scanf("%d",&a[i]);
sort(a,n);
printf("%d ",a[i]);
// Calculate n
pr and n
cr
#include <stdio.h>
long factorial(int);
int main()
int n, r;
scanf("%d%d",&n,&r);
return 0;
long result;
result = factorial(n)/(factorial(r)*factorial(n-r));
return result;
long result;
result = factorial(n)/factorial(n-r);
return result;
long factorial(int n)
int c;
long result = 1;
result = result*c;
return result;
Storage classes:
A function can access a variable when it declared in a specific block other function(outside) cannot be accessed.
The space of a variable depends upon it’s storage classes.They are 4 types:
Auto (Automatic)
External variable
Static variable
Register variable
The space of this variable is within the function only it is quaintly to local variable all local variables are auto
variable by default.
#include<stdio.h>
void sai()
printf("%f \n",a);
a++;
main()
sai();
sai();
}
Static variable:
When a variable is define static its garbage value is removed and it is initialized to null values.
The content store in this variable remains content throughout the program.
#include<stdio.h>
void fun1()
printf("%d \n",a);
a--;
main()
fun1();
fun1();
fun1();
External variable:
The scope of the variable thought out the program it is the equitant to globalvariable definition for external variable
may be any were the program.
#include<stdio.h>
main()
int pravi=20;
Register variable:
It is also local variable but stored in the register memory were as auto variable are stored in the main memory.
Register variable will be accessed very faster the normal variable. Since they are stored in register memory rather
then main memory. But only limited variable can be used as a register memory since it can used register size is
very low.
#include<stdio.h>
int main()
int num1,num2;
scanf("%d",&num2);
return(0);
Pointers:
A pointer is a variable that stores the address of another variable. C pointer is used to allocate memory dynamically
that is run time.
The pointer variable might be belong to the data type. Such as int, float, char, double.
Normal variable store the value were as pointer variable store the address of the variable.
The content of the ‘C’ pointer always be a whole number that is address.
Values of a operator:
Pointer variable are the special type of the variable that holds the memory address rather then the data that is a
variable that holds address value is called a pointer variable.
Addresses of values:
%u
%x
#include<stdio.h>
main()
int a=10,*p;
p=&a;
printf("a=%d \n",a);
printf("&a=%u \n",&a);
printf("p=%u \n",p);
printf("*p=%u \n",*p);
printf("&p=%p \n",&p);
Point of pointers:
We already know that a pointer points to a location in memory and thus used to store address of variables. So,
when we define a pointer to pointer. The first pointer is used to store the address of second pointer. That is why
they are also known as double pointers.
int *a,**b;
#include<stdio.h>
main()
int a=15,*p,**p1;
p=&a;
p1=&p;
printf("a=%d \n",a);
printf("&a=%u \n",&a);
printf("p=%u \n",p);
printf("*p=%u \n",*p);
printf("&p=%p \n",&p);
printf("p1=%x \n",p1);
printf("*p1=%p \n",*p1);
printf("**p1=%u \n",**p1);
printf("&p1=%p \n",&p1);
Array of pointer:
Arrays are closed related to the pointer in the c programming but the important difference between that is the a
pointer variable can take a address of then values were as array as a fixed value.
#include<stdio.h>
main()
int a[4]={10,20,30,40};
printf("a[0]=%d \n",*a);
printf("a[1]=%d \n",*(a+1));
printf("a[2]=%d \n",*(a+2));
printf("a[3]=%d \n",*(a+3));
#include<stdio.h>
int main()
int x,y,*a,*b,temp;
scanf("%d%d",&x,&y);
a= &x;
b= &y;
temp= *b;
*b = *a;
*a = temp;
return 0;
#include<stdio.h>
int main()
int n1,n2;
scanf("%d",&n1);
scanf("%d",&n2);
swap(&n1,&n2);
int tmp;
tmp=*p;
*p=*q;
*q=tmp;
return 1;
#include <stdio.h>
int main()
gets(aa);
gets(bb);
char *a = aa;
char *b = bb;
while(*a)
a++;
while(*b)
*a = *b;
b++;
a++;
*a = '\0';
return 0;
#include <stdio.h>
#include <string.h>
int l, I;
l = strlen(str);
begin_ptr = str;
end_ptr = str;
end_ptr++;
// Swap the char from start and end index using begin_ptr and end_ptr
ch = *end_ptr;
*end_ptr = *begin_ptr;
*begin_ptr = ch;
begin_ptr++;
end_ptr--;
int main()
{
char str[50];
printf("Enter a string:\n");
scanf("%s",str);
reverseString(str);
return 0;
#include <stdio.h>
int t;
t = *x;
*x = *y;
*y = t;
int main()
int num1,num2;
scanf("%d",&num1);
scanf("%d",&num2);
swap(&num1,&num2);
return 0;
//Pointer to a Function
#include <stdio.h>
int add(int,int);
int main()
int a,b;
int (*ip)(int,int);
int result;
scanf("%d %d",&a,&b);
ip=add;
result=(*ip)(a,b);
return 0;
int c=a+b;
return c;
Structure:
Structure is a collection of different data type which grouped together and each in a structure is called member.
Many structure variable can be declaredfor some structure and memory with the allocated for each separately.
A structure is a collection of one or more variable of different data type grouped together under a single name.
Syntax:
struct structer_name
Datatype variable;
Datatype variable;
Datatype variable;
};
Declaration of structure:
struct student
char name[20];
int roll_no;
float avg;
};
All the members of a structure are the related to the structure variable members are s1.name.
#include<stdio.h>
#include<string.h>
struct student
char name[20];
int rollno;
float avg;
};
main()
In c language pointer can also be applied to structure that is we can have pointers pointing to the structure in such
the same way we have pointer pointing to int, float, char, double……….., in such that pointer are known as
structure pointer. Structure elements can be accessed by structure variable and a dot operator are by using
structure in arrow operator is used.
#include<stdio.h>
#include<string.h>
struct student
char name[20];
int rollno;
float avg;
};
main()
p=&s;
Nested structure:
#include<stdio.h>
#include<string.h>
struct address
char sn[10];
int hn;
};
struct student
char name[20];
int rollno;
};
main()
printf("%s \n %d \n %s \n %d \n",rama.name,rama.rollno,rama.addr.sn,rama.addr.hn);
Array of structure:
Array is a collection of similar data type grouping structure variable in to one array is a referred to as on array of
structure c permits us to declare of structure.
We know that structure is a collection of dissimilar data types. So, by using both array and structure we can add
more students details.
#include<stdio.h>
#include<string.h>
struct student
char name[10];
int rollno;
float avg;
};
main()
int i;
for(i=0;i<3;i++)
printf("\n %s %d %f \n",st[i].name,st[i].rollno,st[i].avg);
#include <stdio.h>
int main()
struct student
int rollno;
char name[25];
int totalmark;
}stud[100];
int n,i;
scanf("%d",&n);
for(i=0;i<n;i++)
printf("Name:\n");
scanf("%s",&stud[i].name);
printf("Roll number:\n");
scanf("%d",&stud[i].rollno);
printf("Total mark:\n");
scanf("%d",&stud[i].totalmark);
printf("STUDENTS DETAILS:\n");
for(i=0;i<n;i++)
printf("\nRoll number:%d\n",stud[i].rollno);
printf("Name:%s\n",stud[i].name);
printf("Totel mark:%d\n",stud[i].totalmark);
return 0;
Structure padding:
In order to align the data in memory, one or more empty bytes (addresses) are inserted (or left) empty between memory
addresses which are allocated for other structure members while memory allocation. This concept is called
structure padding.
Unions:
A union is a special data type available in C that enables you to store different data types in the same memory location.
You can define a union with many members, but only one member can contain a value at any given time. Unions
provide an efficient way of using the same memory location for multi-purpose.
Syntax:
member definition;
member definition;
...
member definition;
union test {
int x, y;
};
int main()
union test t;
t.x = 2;
t.x, t.y);
t.y = 10;
t.x, t.y);
return 0;
#include<stdio.h>
union Test1
int x;
int y;
};
union Test2
int x;
char y;
};
main()
Pointers to unions
Like structures, we can have pointers to unions and can access members using the arrow operator (->). The
following example demonstrates the same.
#include <stdio.h>
union test {
int x;
char y;
};
int main()
p1.x = 65;
// p2 is a pointer to union p1
return 0;
Files:
When a program is terminated, the entire data is lost. Storing in a file will preserve your data even if the program
terminates.
If you have to enter large number of data, it will take a lot of time to enter them all. However, if you have a file containing
all the data, you can easily access the contents of the file using a few commands in C
You can easily move your data from one computer to another without any changes.
Types Of Files:
Text Files
Binary Files
Files Operations
Closing a File
When working with files, you need to declare a pointer of type file. This declaration is needed for communication between
the file and the program.
FILE *fptr;
Opening of Files:
Opening a file is performed using the fopen() function defined in the stdio.h header file
Syntax:
ptr = fopen(“Filename”,”mode”);
File Opening Mode Meaning of Opening Mode Operation
Closing of files:
The file (both text and binary) should be closed after reading/writing.
Syntax: fclose(fptr)
For reading and writing to a text file,we use the functions fprintf() and fscaf()
#include <stdio.h>
#include <stdlib.h>
int main()
int num;
FILE *fptr;
fptr = fopen("E:\\Program.txt","w");
if(fptr == NULL)
printf("Error!");
exit(1);
scanf("%d",&num);
fprintf(fptr,"%d",num);
fclose(fptr);
return 0;
#include <stdio.h>
#include <stdlib.h>
int main()
int num;
FILE *fptr;
exit(1);
fscanf(fptr,"%d", &num);
fclose(fptr);
return 0;
int main()
FILE *fp;
char ch;
fp = fopen("E:\\Program.txt", "r");
fclose(fp);
fp = fopen("E:\\Program.txt", "a");
fclose(fp);
fp = fopen("E:\\Program.txt", "r");
fclose(fp);
return 0;