GEETHANJALI COLLEGE OF ENGINEERING AND
TECHNOLOGY
FDP-“C PROGAMMING”
Programs Used in Arrays and Strings Session:
SPEAKER: J Uma Mahesh, CSE Dept.
//SYNTAX ERROR
#include<stdio.h>
int main()
print("welcome to 5 WEEK FDP ON C PROGRAMMING ");
return 0;
//runtime error
#include<stdio.h>
int main()
int a,b,c;
printf("enter a and b values\n");
scanf("%d%d",&a,&b);
c=a/b;
printf("division result=%d",c);
return 0;
}
//Logical Errors
#include<stdio.h>
main() {
int i;
for(i = 0; i<5; i++); {
printf("Hello World\n");
//semantic error
#include<stdio.h>
main() {
int x, y, z;
x = 10;
y = 20;
x+y=z;
printf("%d",z);
//Linker Error
#include<stdio.h>
int Main()
int a,b,c;
printf("enter a and b values\n");
scanf("%d%d",&a,&b);
c=a/b;
printf("division result=%d",c);
return 0;
/*undeclared variables,unintialized variables,using single sign to chk equality,undeclared functions,
termination of statements,incomplete loops,ignoring the precedence operators,overstepping array
boundaries*/
#include<stdio.h>
void undeclared();
void singleequalsign();
void semicolon_forloopend();
int sum(int count);
float main()
undeclared();
singleequalsign();
semicolon_forloopend();
int count;
while(count<100)
printf("count=%d\n",count);
count++;
sum(count);
return 0;
void undeclared()
{
int a=2,b=10,c;
c=a+b;
printf("undeclared=%d\n",c);
void singleequalsign()
int x=10,y=20;
if(x==y)
printf("equal=%d\n",x);
int sum(int count)
printf("sum=%d\n",count);
return 0;
void semicolon_forloopend()
int i;
for(i=1;i<=10;i++);
printf("semicolon=%d\n",i);
}
//break and continue
#include<stdio.h>
int main()
int i;
for(i=1;i<=10;i++)
if(i==3)
continue;
if(i==7)
break;
printf("%d\t",i);
return 0;
//goto
#include<stdio.h>
int main()
int i;
for(i=1;i<=10;i++)
if(i==3)
continue;
if(i==7)
break;
printf("%d\t",i);
return 0;
//increment and decrement opeerators
#include<stdio.h>
int main()
int a=10,b;//a=2,b=4
printf("%d\n",a);
printf("%d\n",a);
int c,d;//c=3,d=5
printf("enter a and b values\n");
scanf("%d%d",&a,&b);
printf("enter c and d values\n");
scanf("%d%d",&c,&d);
c=a++;
printf("post increment=%d\n",c);//c is 2
c=++a;
printf("pre increment=%d\n",c);//c is 4
d=b++;
printf("post increment=%d\n",d);//d is 4
d=++b;
printf("pre increment=%d\n",d);//d is 6
int e=a++ + b++;
printf("%d\n",e);//e is 10
int f=++a + ++b;
printf("%d\n",f);//f is 14
int g=a-- - b--;
printf("%d\n",g);//-2
int h=--a - --b;
printf("%d\n",h);//-2
return 0;
//storage classes are auto,static,register and extern
/*properties:scope,visibility,life time,default value,storage*/
//example for auto storage class
//category :function with arguments with return values
#include<stdio.h>
#include <stdio.h>
int sum(int n1,int n2);
int sum(int n1, int n2)
{
return n1+n2; //declaration of auto(local) variable
int main(){
int i = 2, j = 3, k;
k = sum(i, j);
printf("sum is : %d\n", k);
return 0;
//auto
#include <stdio.h>
int main(void)
int i;
for (i = 0; i < 5; ++i)
printf("C programming");
// Error: i is not declared at this point
printf("%d", i);
return 0;
//auto
#include<stdio.h>
int main()
int a1=10,b;
int a2;
a2=11;
printf("%d\n",a2);
printf("%d\n",a1);
printf("%d\n",b);
return 0;
/*static:A variable declared as static once initialized,
exists till the end of the program*/
/*If a static variable is declared outside all the functions
in a program, it can be used only in the program
in which it is declared and
is not visible to other program files(as in extern).*/
#include<stdio.h>
static int g = 5;
void fn(){
static int i = 0;
printf("g = %d\t", g--);
printf("i = %d\n",i++);
}
int main(){
while(g >= 2)
fn();
return 0;
/*register:It tells the compiler that
the variable will get stored
in a register instead of memory (RAM) */
#include<stdio.h>
int main()
register int n = 20;
int *ptr;
ptr = &n;
printf("address of n : %u", ptr);
return 0;
/*extern keyword before any variable
tells us that this variable is a global variable
declared in some other program file.*/
#include<stdio.h>
int g;
void print(){
g = 10;
printf("g = %d\n", g);
}
int main(){
g = 7;
printf("g = %d\n", g);
print();
return 0;
//IMPLICIT TYPE CASTING:LOWER TO HIGH
#include<stdio.h>
int main(){
int a; //initializing variable of int data type
double b=10.00000; //declaring double variable
a=b; //?
printf("%lf\n",(double)a);
printf("%lf\n",b);
return 0;
//explicit type casting
/*There are some scenarios in which we may have
to force type conversion.
Suppose we have a variable div
that stores the division of two operands
which are declared as an int data type.*/
/*eg:int result, var1=10, var2=3;
result=var1/var2;
In this case, after the division performed
on variables var1 and var2 the result
stored in the variable "result"
will be in an integer format.
Whenever this happens,
the value stored in the variable
"result" loses its meaning because
it does not consider the fraction part
which is normally obtained in the division of two numbers.
*/
#include<stdio.h>
int main()
double a = 1.2;
int b = a;
//int b = a + 1;
printf("Value of a is %lf\n", a);
printf("Value of b is %d\n",b);
return 0;
//explicit type casting
#include<stdio.h>
int main()
int sum = 22, count = 5;
double mean = (double)sum/count;
printf("%lf\n",mean);
return 0;
}
//sample exercise problems
//p1
#include<stdio.h>
main()
static int c=5;
printf("c=%d\n",c--);
if(c)
main();
//p2
#include<stdio.h>
main()
register int i;
for(i=1;i<=100;i++)
printf("%d\n",i);
//p3
#include<stdio.h>
main()
int x=10,y;
y=--x--;
printf("x=%d\ny=%d",x,y);
}
//p4
#include<stdio.h>
main()
int x=10,y=x,z=x,t;
y-=x;
z=-x;
t=-x;
printf("y=%d\nz=%d\nt=%d",y,z,t);
//p5
#include<stdio.h>
main()
int a=10,b=10;
printf("ans=%d",a>b?a*a:b/b);
/we have a list of 1000 student's marks of an integer type.
/*if using the basic data type (int) we will declare something like the following
in/t main()
int studmark1,studmark2.....studmark1000;
---
---
return 0;
*/
/*by using array we just do like this int studmark[1000]; this will reserve 1000
contigious memory locations for storing the students marks*/
//A program that can print its input in reverse order.
#include<stdio.h>
int main()
int a,b,c;
//printf("enter a ,b and c values");
scanf("%d%d%d",&a,&b,&c);
printf("reverse order=%d\t%d\t%d",c,b,a);
return 0;
//one dimensional array
#include<stdio.h>
int main()
int i=0;
int empID[5];
//empID[]=10280;//what is empID[0]->index
empID[1]=10260;
empID[2]=10270;
// empID[3]=10285;
empID[4]=10;
empID[5]=20;
for(i=0;i<5;i++)
printf("%d \n",empID[i]);
return 0;
//two dimensional array
#include<stdio.h>
int main()
int disp[2][3];
int i, j;
for(i=0; i<2; i++)
for(j=0;j<3;j++)
printf("Enter value for disp[%d][%d]:", i, j);
scanf("%d", &disp[i][j]);
printf("Two Dimensional array elements:\n");
for(i=0; i<2; i++)
for(j=0;j<3;j++)
{
printf("%d ", disp[i][j]);
if(j==2)
printf("\n");
return 0;
/*Output:
Enter value for disp[0][0]:1
Enter value for disp[0][1]:2
Enter value for disp[0][2]:3
Enter value for disp[1][0]:4
Enter value for disp[1][1]:5
Enter value for disp[1][2]:6
Two Dimensional array elements:
123
4 5 6*/
//three dimensional array
#include<stdio.h>
int main()
int arr[3][3][3] =
/*first value in [] corresponds to the grid no. i.e. 3 and
the second value in [] means third row in the corresponding grid and last [] means third column*/
{ 1, 2, 3,
4, 5, 6,
7, 8, 9,
10, 11, 12,dio.
13, 14, 15,
16, 17, 18,
19, 20, 21,
22, 23, 24,
25, 26, 27
};
printf("%d",arr[0][2][2]);
//reading elements into the array
#include<stdio.h>
int main()
int arr[50],n,i ;
printf("Enter the size of array:\n");
scanf("%d",&n);
printf("Enter the elements of array:n");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
printf("elements in the array are:");
for(i=0;i<n;i++)
printf("%d\n",arr[i]);
return 0;
//recursion
#include<stdio.h>
int fun(int n);
int fun(int n);
int main()
int n=3;
printf("%d\n",fun(4));
return 0;
int fun(int n)
if(n==0)
return 1;
else
return 7+fun(n-2);
/*int fun(int n)
if(n==0)
return 1;
else
return 5+fun(n-1);
}*/
//sizeof operator
#include<stdio.h>
int main()
char a;
printf("%d",sizeof(a));
//addition of two matrix
#include<stdio.h>
void main()
int a[10][10],b[10][10],c[10][10];
int i,j,m,n,p,q;
printf("\n Enter the size of Matrix A:");
scanf("%d%d", &m,&n);
printf("\n Enter the size of Matrix B:");
scanf("%d%d", &p,&q);
if(m!=p || n!=q)
printf("Matrix addition not possible.");
printf(" Enter the Matrix A values:\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf(" Enter the Matrix B values:\n");
for(i=0;i<p;i++)
for(j=0;j<q;j++)
scanf("%d",&b[i][j]);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
c[i][j]=a[i][j]+b[i][j];
printf("\n The Matrix A is\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
printf(" %d",a[i][j]);
printf("\n");
printf("\n The Matrix B is\n");
for(i=0;i<p;i++)
for(j=0;j<q;j++)
printf(" %d",b[i][j]);
printf("\n");
printf("\n The Output Matrix C is\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
printf(" %d",c[i][j]);
printf("\n");
}
/*OUTPUT:
Enter the size of Matrix A:2
Enter the size of Matrix B:2
Enter the Matrix A values:
Enter the Matrix B values:
The Matrix A is
123
456
The Matrix B is
654
321
The Output Matrix C is
777
777
*/
/*In order for matrix multiplication to be defined,
the number of columns in the first matrix must be equal
to the number of rows in the second matrix.*/
#include<stdio.h>
void main()
int a[10][10],b[10][10],c[10][10];
int i,j,k,m,n,p,q;
printf("\n Enter the size of Matrix A:");
scanf("%d%d", &m,&n);
printf("\ Enter the size of Matrix B:");
scanf("%d%d", &p,&q);
if(n!=p)
printf("Matrix Multiplication not possible.");
printf(" Enter the Matrix A values:\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf(" Enter the Matrix B values:\n");
for(i=0;i<p;i++)
for(j=0;j<q;j++)
scanf("%d",&b[i][j]);
for(i=0;i<m;i++)
for(j=0;j<q;j++)
c[i][j]=0;
for(k=0;k<n;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
printf("\n The Matrix A is\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
printf(" %d",a[i][j]);
printf("\n");
printf("\n The Matrix B is\n");
for(i=0;i<p;i++)
for(j=0;j<q;j++)
printf(" %d",b[i][j]);
printf("\n");
printf("\n The Output Matrix C is\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
printf(" %d",c[i][j]);
printf("\n");
/*output
Enter the size of Matrix A:2
Enter the size of Matrix B:3
Enter the Matrix A values:
Enter the Matrix B values:
The Matrix A is
222
222
The Matrix B is
33
33
33
The Output Matrix C is
15 15
15 5
*/
//strlen
#include<stdio.h>
#include<string.h>
main()
char string1[50];
int length;
printf("\n Enter any string:");
gets(string1);
length=strlen(string1);
printf("\n The length of string=%d",length);
//strcpy
#include<stdio.h>
#include<string.h> main()
{
char string1[30],string2[30];
printf("\n Enter first string:");
gets(string1);
printf("\n Enter second string:");
gets(string2);
strcpy(string1,string2);
printf("\n First string=%s",string1);
printf("\n Second string=%s",string2);
//strcmp
#include<stdio.h>
#include<string.h> main()
char string1[30],string2[15];
int x;
printf("\n Enter first string:");
gets(string1);
printf("\n Enter second string:");
gets(string2);
x=strcmp(string1,string2);
if(x==0)
printf("\n Both strings are equal");
else if(x>0)
printf("\n First string is bigger");
else
printf("\n Second string is bigger");
//strcat
#include<stdio.h>
#include<string.h>
main()
char string1[30],string2[15];
printf(“\n Enter first string:”);
gets(string1);
printf(“\n Enter second
string:”); gets(string2);
strcat(string1,string2);
printf(“\n Concatenated string=%s”,string1);