All C program code Examples

Structure & Data Structures

// Simple structure example

#include

struct student
{
int rollno;
char name[20];
};
void main()
{

student s1={45,”Bhavin”},s2={65,”Dhwani”};
printf(“%d\t%s\n”,s1.rollno,s1.name);
printf(“%d\t%s\n”,s2.rollno,s2.name);
getch();
}

Output:
45 Bhavin
65 Dhwani

// Global Structure Example

#include
#include

/* Global Structure */
struct Distance
{
int feet;
float inches;
};

struct Distance d1; /* Global variable */

int main ()
{
struct Distance d2; /* Local variable */
clrscr();

d1.feet = 23;
d1.inches = 7.5;

d2.feet = 14;
d2.inches = 2.5;

printf( “\n %d\’-%f\””, d1.feet, d1.inches );
printf( “\n %d\’-%f\””, d2.feet, d2.inches );

getch();
return 0;
}

Output:

12′-9.500000″
14′-2.500000″

C Programs. Get More Example from
https://play.google.com/store/apps/details?id=com.kapidhvaj.cprograms

// Local Structure Example

#include
#include

int main ()
{
// Local Structure
struct Distance
{
int feet;
float inches;
}d1;

struct Distance d2; // Local variable
clrscr();

d1.feet = 23;
d1.inches = 7.5;

d2.feet = 14;
d2.inches = 2.5;

printf( “\n %d\’-%f\””, d1.feet, d1.inches );
printf( “\n %d\’-%f\””, d2.feet, d2.inches );

getch();
return 0;
}

Output:

12′-9.500000″
14′-2.500000″

C Programs. Get More Example from
https://play.google.com/store/apps/details?id=com.kapidhvaj.cprograms

// Print Student data using Structer

#include

struct student
{
int rollno;
char name[20];
};
void main()
{
student s1,s2;

printf(“Enter rollno and name for first student:”);
scanf(“%d%s”,&s1.rollno,s1.name);
printf(“Enter rollno and name for second student:”);
scanf(“%d%s”,&s2.rollno,s2.name);
printf(“Student#1:%d\t%s\n”,s1.rollno,s1.name);
printf(“Student#2:%d\t%s\n”,s2.rollno,s2.name);

}

Output:
Enter rollno and name for first student:
11 Akash
Enter rollno and name for second student:
22 Bhavin
Student#1:11 Akash
Student#2:22 Bhavin

// Print n number of Student data using Structer

#include

struct student
{
int rollno;
char name[20];
};
void main()
{
student s[10];
int i,n;

printf(“How many students?”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“Student#%d:Rollno and Name?”,i);
scanf(“%d%s”,&s[i].rollno,s[i].name);
}
printf(“You have entered\n”);
for(i=0;i<n;i++)
{
printf(“student#%d:Rollno:%d Name:%s\n”,i,s [i].rollno,s[i].name);
}

}

Output:
How many students?3
Student#0:Rollno and Name?11 Akash
Student#1:Rollno and Name?22 Bhavin
Student#2:Rollno and Name?33 Dipak

// Show the data of item by the code

#include

struct item
{
int code;
char name[20];
int qty;
};
void main()
{
item it[10];
int n,i,icode,flag=0;

printf(“How many item?”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“Item#%d\n”,i);
printf(“Code,Name,Quantity:”);
scanf(“%d%s%d”,&it[i].code,it[i].name,&it[i].qty);
}
printf(“Enter item code for the item you want to view:”);
scanf(“%d”,&icode);
for(i=0;i<n;i++)
{
if(it[i].code==icode)
{
flag=1;
printf(“name:%s\t Quantity:%d\n”,it [i].name,it[i].qty);
break;
}
}
if(flag==0)
{
printf(“Item not found…”);
}

}

Output:
How many Items?2
Item#0
Code,Name,Quantity:101 Salt 45
Item#1
Code,Name,Quantity:201 Tea 20
Enter item code for the item you want to view:201
Name:Tea Quantity:20

// Anonymous Structure Example

#include

// struct Dist
struct
{
int feet;
float inches;
}d1, d2 = {23, 7.5};

int main()
{

d1.feet = 14;
d1.inches = 8.5;

printf( “\n d1 : %d\’-%.1f\””, d1.feet, d1.inches);
printf( “\n d2 : %d\’-%.1f\””, d2.feet, d2.inches);

return 0;
}

Output:

d1 : 14′-8.5″
d2 : 23′-7.5″

// Sort the item data by code in structure

#include

struct item
{
int code;
char name[20];
int qty;
};
void main()
{
item it[10],t;
int n,i,j;

printf(“How many items?”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“Item#%d\n”,i);
printf(“Code,Name,Quantity:”);
scanf(“%d%s%d”,&it[i].code,it[i].name,&it [i].qty);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;jit[j].code)
{
t=it[i];it[i]=it[j];it[j]=t;
}
}
}
printf(“After sorting on code\n”);
for(i=0;i<n;i++)
{
printf(“%d\t%s\t%d\n”,it[i].code,it[i].name,it [i].qty);
}

}

Output:
How many items?4
Item#0
Code,Name,Quantity:201 Soap 187
Item#1
Code,Name,Quantity:401 Toothpast 35
Item#2
Code,Name,Quantity:101 Salt 20
Item#3
Code,Name,Quantity:301 Coffee 50
After sorting on code
101 Salt 20
201 Soap 187
301 Coffee 50
401 Toothpast 35

/*Array of Structure*/

#include
struct book
{
int id;
char name[40];
};

void main()
{
struct book b[10];
int i,n;
printf(“Enter total number of book : “);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“\nEnter book name and id of Book %d:\n”,i+1);
scanf(“%s%d”,&b[i].name,&b[i].id);
}
for(i=0;i<n;i++)
{
printf(“\nBook %d:\n”,i+1);
printf(“Book id = %d “,b[i].id);
printf(“Book name= %s”,b[i].name);
}

}

Output:

Enter total number of book : 3

Enter book name and id of Book 1
BeTheDeveloper.com 23
Enter book name and id of Book 2
skyNils.com 14
Enter book name and id of Book 3
MyQuotes 10

Book 1:
Book id = 23 Book name= BeTheDeveloper.com

Book 2:
Book id = 14 Book name= skyNils.com

Book 3:
Book id = 10 Book name= MyQuotes

// enumeration type ( enum )

#include

enum days_of_week { sun, mon, tue, wed, thur, fri, sat };

int main ()
{
enum days_of_week day1, day2;
int diff;

day1 = mon;
day2 = thur;

diff = day2 – day1;

printf( ” Days between day1 & day2 = %d \n”, diff );

if( day1 < day2 )
printf( ” Day1 comes before Day2.” );

return 0;
}

Output:

Days between day1 & day2 = 3
Day1 comes before Day2.

/*
* C program to find the size of a union */

#include
void main()
{
union sample
{
int m;
float n;
char ch;
};
union sample u;

printf(“The size of union = %d”, sizeof(u));

u.m = 25;
printf(“%d %f %c”, u.m, u.n, u.ch);

u.n = 0.2;
printf(“%d %f %c”, u.m, u.n, u.ch);

u.ch = ‘p’;
printf(“%d %f %c”, u.m, u.n, u.ch);

}

Output:

The size of union = 4
25 0.000000
1045220557 0.200000
1045220464 0.199999

// differentiate Structure and Union

#include

union ABC
{
char a;
int b;
float c;
};

struct XYZ
{
char x;
int y;
float z;
};

int main()
{
union ABC p;
struct XYZ q;

printf( “\n Size of Structure : %d”, sizeof(struct XYZ) );
q.x = ‘N’;
q.y = 10;
q.z = 14.345;
printf( “\n q.x = %c\n q.y = %d\n q.z = %f\n”, q.x, q.y, q.z );

printf( “\n Size of Union : %d”, sizeof(union ABC) );
p.a = ‘N’;
printf( “\n p.a : %c”, p.a );
p.b = 10;
printf( “\n p.b : %d”, p.b );
p.c = 14.345;
printf( “\n p.c : %f”, p.c );

return 0;
}

Output:

Size of Structure : 9
q.x = N
q.y = 10
q.z = 14.345000

Size of Union : 4
p.x = N
p.y = 10
p.z = 14.345000

/*
* C program to illustrate the concept of unions
*/

#include
void main()
{
union number
{
int n1;
float n2;
};
union number x;

printf(“Enter the value of n1: “);
scanf(“%d”, &x.n1);

printf(“Value of n1 = %d”, x.n1);
printf(“Enter the value of n2: “);
scanf(“%f”, &x.n2);
printf(“Value of n2 = %f”, x.n2);
}

Output:

Enter the value of n1: 10
Value of n1 = 10
Enter the value of n2: 50
Value of n2 = 50.000000

// Product information using Structer

#include

struct product
{
int id;
char name[20];
char piece;
int price;
int total[5];
}p[5];
void main()
{
int total[5],i,n;
int n1;

printf(“\t\tEnter the product information”);
printf(“\n\n\nEnter the products Record:>>”);
scanf(“%d”,&n);
for(i=0;i>”);
scanf(“%d”,&p[i].id);
printf(“Enter the products Name:>>”);
scanf(“%s”,&p[i].name);
printf(“\n\nEnter the product pieces:>>”);
scanf(“%d”,&p[i].piece);
printf(“\n\nEnter the product price:>>”);
scanf(“%d”,&p[i].price);
total[i]=p[i].piece*p[i].price;
printf(“\n\n\n\t\t\t Total::>>%d”,total[i]);
}
printf(“\n\n\t\t enter pid:”);
scanf(“%d”,&n1);
for(i=0;i%d Total:>>%d\n”,p[i].id,p [i].name);
}
}

}

Output:
Enter the product information

Enter the products Record:>>1

Enter the products Name:>>Coffee

Enter the product pieces:>>10

Enter the product price:>>35

Total::>>350

// Add client information using Structer

#include

struct client
{
int no;
char name[30];
char address[80];
char city[20];
char state[20];
int pin[20];
}p[5];
void main()
{
int i,n,n1;

printf(“Enter the client details:”);
scanf(“%d”,&n);
printf(“\n***********************”);
for(i=0;i>”);
scanf(“%d”,&p[i].no);
printf(“\n Enter the client name:>>”);
scanf(“%s”,&p[i].name);
printf(“\n Enter the client address:>>”);
scanf(“%s”,&p[i].address);
printf(“\n Enter the client city:>>”);
scanf(“%s”,&p[i].city);
printf(“\n Enter the client state:>>”);
scanf(“%s”,&p[i].state);
printf(“\n Enter the pin code:>>”);
scanf(“%d”,p[i].pin);
}
printf(“\n\n\n\n Enter the no:>>”);
scanf(“%d”,&n1);
for(i=1;i>%d name:>>%s Address:>>%s \n”,p[i].no,p[i].name,p[i].address);
}
}

}

Output:
Enter the client details:2
**************************
Enter the client number:>>101
Enter the client name:>>Ashish
Enter the client address:>>15,Akash app.
Enter the client city:>>Ahamedabad
Enter the client state:>>Gujarat
Enter the pin code:>>380059
Enter the client number:>>201
Enter the client name:>>Shatish
Enter the client address:>>15,Akashat app.
Enter the client city:>>Ahamedabad
Enter the client state:>>Gujarat
Enter the pin code:>>380050

Enter the no:101
No:>>101 Name:>>Ashish Address:>>15,Akash app.