II-284 Programming Concepts
Additional Questions
What are the outputs of the following programs?
Programs on declaration and initialization of structures
1. Program to display information of a student
#include <stdio.h>
#include<conio.h>
void main()
{
struct emp
{
char name[30];
int age;
int sal ;
};
struct emp e={"Satish",28,20000};
clrscr();
printf(" Name : %s",e.name);
printf(" Age : %d",e.age);
printf(" Salary : %d",e.sal);
getche();
}
OUTPUT:
Name : Satish
Age : 28
Salary : 20000
2. Program to display time
#include <stdio.h>
#include <conio.h>
void main()
{ struct time
{
int second;
int minute;
int hour;
} t;
M13_ITL-ESL4791_02_SE_C13.indd 284 12/22/2012 5:06:16 PM
Structure and Union II-285
t.second=45;
t.minute=15;
t.hour=11;
clrscr();
printf("%d:%d:%d",t.hour,t.minute,t.second);
}
OUTPUT:
11 : 15 : 45
3. Program to display information of a book
#include <stdio.h>
#include <conio.h>
void main()
{
struct book
{
char *book;
int pages;
float price;
};
struct book bk;
bk.book="Programming in C";
bk.pages=300;
bk.price=250.00;
clrscr();
printf(" Book : %s",bk.book);
printf(" Pages : %d",bk.pages);
printf(" Price : %g",bk.price);
}
OUTPUT:
Book : Programming in C
Pages : 300
Price : 250
M13_ITL-ESL4791_02_SE_C13.indd 285 12/22/2012 5:06:16 PM
II-286 Programming Concepts
4. Program to display total size of variables
#include <stdio.h>
#include<conio.h>
void main()
{
struct emp
{
char name[30];
int age;
int sal ;
};
struct emp e={"Santosh",28,7000};
clrscr();
printf(" SIZE OF e : %d",sizeof(e));
}
OUTPUT:
SIZE OF e : 34
5. Program to display numbers
#include <stdio.h>
#include<conio.h>
void main()
{
struct emp
{
int k;
int a;
int s;
};
struct emp e={1,2,4};
e.k++;
clrscr();
printf("%d %d %d",e.k,e.a,e.s);
}
OUTPUT:
2 2 4
M13_ITL-ESL4791_02_SE_C13.indd 286 12/22/2012 5:06:16 PM
Structure and Union II-287
6. Program to display authors information
#include <stdio.h>
#include<conio.h>
void main()
{
struct emp
{
char book_name[40];
char author[20];
int pages;
int cost;
};
struct emp e={"Programming with ANSI and Turbo
C","Ashok",594,195};
clrscr();
printf(" Book Name : %s",e.book_name);
printf(" Author's Name : %s",e.author);
printf(" Pages : %d",e.pages);
printf(" Cost : %d",e.cost );
getche();
}
OUTPUT:
Book Name : Programming with ANSI and Turbo C
Author's Name : Ashok
Pages : 594
Cost : 195
7. Some tricky problems.
#include <stdio.h>
#include<conio.h>
void main()
{
struct emp
{
int a;
int b;
int c;
};
M13_ITL-ESL4791_02_SE_C13.indd 287 12/22/2012 5:06:16 PM
II-288 Programming Concepts
struct emp e={10,20,40};
e.a++;
e.b++;
e.c=e.a+e.b;
clrscr();
printf("%d %d %d",e.a,e.b,e.c);
getche();
}
OUTPUT:
11 21 32
8. Program to display book information
#include <stdio.h>
#include <conio.h>
void main()
{
struct book
{
char *book;
int pages;
float price;
};
struct book bk,ck;
bk.book="Data Structure through C";
bk.pages=400;
bk.price=215.00;
ck=bk;
clrscr();
printf(" Book : %s",ck.book);
printf(" Pages : %d",ck.pages);
printf(" Price : %g",ck.price);
}
OUTPUT:
Book : Data Structure through C
Pages : 400
Price : 215
M13_ITL-ESL4791_02_SE_C13.indd 288 12/22/2012 5:06:16 PM
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset