## INDEX

### Structure questions with explanation in c

Structure in c example

(q) What will be output of following c code?

void main()
{
struct employee
{
unsigned id: 8;
unsigned sex:1;
unsigned age:7;
};
struct employee emp1={203,1,23};
clrscr();
printf("%d\t%d\t%d",emp1.id,emp1.sex,emp1.age);
getch();
}

Output: 203 1 23
We can access the data member in same way.
How bit data is stored in the memory:

Minimum size of structure which data member in bit is two byte i.e. 16 bit. This is called word size of microprocessor. Word size depends on microprocessor. Turbo c is based on 8086 microprocessor which word size is two byte.

Bits are filed in from right to left direction 8 bit for id,1 bit for sex and 7 bit for age.

(q) What will be output of following c code?

void main()
{
struct bitfield
{
unsigned a:5;
unsigned c:5;
unsigned b:6;

}bit;
char *p;
struct bitfield *ptr,bit1={1,3,3};
p=&bit1;
p++;
clrscr();
printf("%d",*p);
getch();
}

Output: 12
Explanation:
Binary value of a=1 is 00001 (in 5 bit)
Binary value of b=3 is 00011 (in 5 bit)
Binary value of c=3 is 000011 (in 6 bit)
In memory it is represented as:

Let address of bit1 is 500 which initialize to char pointer p. Since can is one byte data type so p++ will be 501. *p means content of memory location 501 which is (00001100) and its binary equivalent is 12. Hence output is 12.

(q) What will be output of following c code?

void main()
{
struct bitfield
{
signed int a:3;
unsigned int b:13;
unsigned int c:1;
};
struct bitfield bit1={2,14,1};
clrscr();
printf("%d",sizeof(bit1));
getch();
}

Output: 4
(q) What will be output of following c code?

void main()
{
struct bitfield
{
unsigned a:3;
char b;
unsigned c:5;
int d;
}bit;
clrscr();
printf("%d",sizeof(bit));
getch();
}

Output: 5

Note: (Actual output will 6 due to slack byte ,So Before executing this program first go to option menu then compiler then code generation then select word alignment then press OK)

(q) What will be output of following c code?
void main()
{
struct field
{
int a;
char b;
}bit;
struct field bit1={5,'A'};
char *p=&bit1;
*p=45;
clrscr();
printf("\n%d",bit1.a);
getch();
}

Output: 45
Nesting of structure:

Nesting of structure is possible i.e. we can declare a structure within another structure but it is necessary inner structure must declares structure variable otherwise we can not access the data member of inner structure.
Example:

void main()
{
struct world
{
int a;
char b;
struct india
{
char c;
float d;
}p;
};
struct world st ={1,'A','i',1.8};
clrscr();
printf("%d\t%c\t%c\t%f",st.a,st.b,st.p.c,st.p.d);
getch();
}

Output: 1 A I 1.800000

(q) What will be output of following c code?

void main()
{
struct india
{
char c;
float d;
};
struct world
{
int a[3];
char b;
struct india orissa;
};
struct world st ={{1,2,3},'P','q',1.4};
clrscr();
printf("%d\t%c\t%c\t%f",st.a[1],st.b,st.orissa.c,st.orissa.d);
getch();
}

Output: 2 p q 1.400000

Vishnu said...

the answer to the third question is coming out to be 3 in borland C 4.5
and individually the size of signed and unsigned int is coming to be 2 each
wat is the correct ans....m getting confused

Anonymous said...

please include web designing tutorial

Ambili said...

C is compiler dependent as we all know.So if u try the 5th question in borland c you will get the answer 5 where as in visual studio you will find an answer of 16.So dnt get confuse with that concepts.

Unknown said...

file pointer is a pointer to a structure of ________ type.

suresh said...

hi please explay any one question number two brifely

Anonymous said...

please give the answer for the 3 question

Anonymous said...

how to write a program to find grade of student

Harekrushna said...

Plz tell me if there hav any secret method or easier technique for coding in C using RECURSION...

Priyanka kumari said...

Hi Harekrushna,

Good questions. Go through the following link:
http://cquestionbank.blogspot.com/2009/06/function-recursion-in-c-programming.html

Anonymous said...

excelent questions and it will be more help to me but i think we need more explanation

Md Johiruddin Sk said...

#include
#include

struct std{
int roll;
void f(){
struct std x;
roll =7;
printf("%d",roll);
};

void main(){
struct std x;
x.f();
}
"THIS PROGRAM CAN NOT RUN COMPILATION ERROR AND THE IS decleration terminate
incorrectly" plz help me...

Anonymous said...

Hi All,
can anyone tell me why the o/p of below code is 20 under gcc..

struct abc {
char name[10];
int i;
char val;
};
int main(){
printf("%d\n",sizeof(struct abc));
}

Unknown said...

PLZ ANS THIS--enter the basic salary of employe calculate the following-
1)per day salary
2)weekly salary
3)annual salary
4)calculate houserent 15% of basic salary
5)caculate medical 10%of basic salary
6)caculate da 30% of baic salary
7)calculate ta 10% of basic salary
8)calculate pf 12% of basic salary
9) calculate gross salary
10)calculate net salary

Anonymous said...

16

Avinandan said...

in c language you can not define a function inside the structure.to overcome such limitation class concept came in C++.
Hope you find it useful :)

Aarav said...

what is slack memory?

Usha Thyagaraj said...

Hi Please explain in brief about the allignment of data types in structure? How can we do structure padding with an example. it would be very confusing for me ...

Anonymous said...

Nice collection..

Anonymous said...

please include ethical hacking tutorial hacking tutorials

Unknown said...

13

Unknown said...

13

Unknown said...

hi bro can you help write a Write a program that, using a recursive function, prints the sum of consecutive numbers from 1
to n, where n > 1.
The function should take the n number.
• The function should calculate and return the sum of the numbers.
• Determine the exit condition of the recursion.
• If the (n <1), then let the function returns -1. Print a message.
• Let the function returns a long int

Unknown said...

good

Unknown said...

Because Unix memory management system uses multiple of 4 location for memory allocation, so 4+4+4(for char[10])+4(int)+(1 byte will be used for char but it will use only 1 byte out of 4)=20byte only 15byte will be used for storage and 5 will be holes. to avoid this use pragama pack(1);

Unknown said...

13 is the answer because of char array has 10 byetes(i.e 10 char each 1 byte so 10 bytes)and int size is 2 and 4 bytes in 32 and 64 bit operating system.at last again char occupies 1 byte,so finally 10+2+1=13(32 bit of) or
10+4+1=15(64 bit os)

Ravi said...

You write this post very carefully I think, which is easily understandable to me. Not only this, but another post is also good. As a newbie, this info is really helpful for me. Thanks to you.
Tally ERP 9 Training
tally classes
Tally Training institute in Chennai
Tally course in Chennai

Ravi said...

This content of information has
helped me a lot. It is very well explained and easy to understand.
seo training classes
seo training course
seo training institute in chennai
seo training institutes
seo courses in chennai
seo institutes in chennai
seo classes in chennai
seo training center in chennai

w3webschool said...

Great Article. Thank you for sharing! Really an awesome post for every one.
Digital Marketing Course In Kolkata
Web Design Course In Kolkata

Vijayakash said...

C C++ Training in Chennai

I am glad that I have visited this blog. Really helpful, eagerly waiting for more updates.

Vijayakash said...

Top Courses to learn.
I am glad that I have visited this blog. Really helpful, eagerly waiting for more updates.

Vijayakash said...
Vijayakash said...
Vijayakash said...

This is good site and nice point of view.I learnt lots of useful information.
php training in tambaram
php Training in Anna Nagar
php training in velachery
PHP Training in T Nagar
PHP Training in Porur
PHP training in OMR
PHP Training in Chennai

Nino Nurmadi , S.Kom said...
Vijayakash said...