Machine-translated by Google. Questions are authored in English.
Top

Structures, Unions, Enums Questions and Answers

1.

Consider the following C program.

#include\(\lt\)stdio.h\(\gt\)
struct Ournode{
    char x,y,z;
};

int main(){
    struct Ournode p = {'1', '0', 'a'+2};
    struct Ournode *q = &p;
    printf ("%c, %c", *((char*)q+1), *((char*)q+2));
    return 0;
}

The output of this program is:

Exam Year(s)

GATE CS - 2018,

Answer: A

The program prints 0, c.

The structure holds three chars in order, x = '1', y = '0' and z = 'a'+2. Since 'a'+2 is evaluated as a character code, z holds 'c'. Casting q to char* and adding 1 and 2 points at y and z, so the two values printed are '0' and 'c'.

The format %c prints just the character, without quotes, so the options with quotes are wrong. 'a+2' would need the expression to be stored as text, but it is computed when the structure is initialised.

Please log in or register to report this question.

2.

How much memory will be allocated for the instances of structure S (with respect to C language) ?

typedef struct {
    char a;
    int b;
} S;

Exam Year(s)

MH SET - 2025, SET Exam - 2025,

Answer: C

An instance of S is allocated the size required to store a character and an integer.

A structure stores all of its members at once, each with its own memory, so S holds both a and b. On a typical system with 4-byte integers the compiler also inserts padding after the char to align the int, so sizeof(S) is usually 8 rather than 5.

Storing only a character or only an integer would describe a single member; the size of the largest member alone is what a union uses. The size of a structure is fixed at compile time and does not depend on available memory.

Please log in or register to report this question.

3.

Which operator is used to access a structure member through a pointer ?

Exam Year(s)

Tripura Public Service Commission - 2026,

Answer: A

A structure member is reached through a pointer with the -> (arrow) operator.

If p points to a structure, p->member is shorthand for (*p).member: it dereferences the pointer and selects the member in one step.

The dot operator is used with a structure variable itself, not a pointer to one. :: is the C++ scope resolution operator, and * on its own only dereferences the pointer; combined with the dot it needs brackets, (*p).member, because . binds more tightly than *.

Please log in or register to report this question.

Answer: C

Enumeration is the process of assigning the legal values possible for a variable: an enumerated type lists the named constants that a variable of that type may take, for example the days of a week.

It is not about declaring a set of numbers in general, sorting a list of strings, or sequencing a list of operators. The values in an enumeration are named integer constants, usually numbered from 0 by default.

Please log in or register to report this question.

5.

What is the size of the following Union ? Assume that the size of int = 2, size of float = 4, size of char = 1

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

Exam Year(s)

UGC NET - 2013,

Answer: B

All members of a union share the same memory, so its size is that of its largest member. Here the members are an int of 2 bytes, a float of 4 bytes and a char of 1 byte, so the largest is 4 bytes.

A size of 2 or 1 is smaller than the float, and 7 is the sum of the sizes, which is the size of a structure and not of a union, if padding is ignored. Option B is correct. The compiler allocates the space of the largest member to the union, and the members take turns to use it, so only one of them is valid at a time.

Please log in or register to report this question.

6.

What will be the size of the following structure?

struct demo{
  int a;
  char b;
  float c;
}

 

Answer: A

Correct answer: 12

Members are laid out with padding so that each is properly aligned:

int a4 bytes
char b1 byte + 3 padding
float c4 bytes
total12 bytes

The three padding bytes exist so that the float begins on a 4-byte boundary. Without alignment the total would be 9.

Please log in or register to report this question.

7.

Which of the following operators can be applied on structure variables?

Answer: B

Correct answer: Assignment ( = )

A structure may be assigned as a whole, which copies it member by member. It may not be compared with ==: the compiler could not simply compare the bytes, because padding between members holds unspecified values, and comparing member by member is left to the programmer.

Please log in or register to report this question.

8.

How many bytes does the structure below occupy?

#include <stdio.h>
struct test
{
  int k;
  char c;
};

 

Answer: A

Correct answer: Multiple of integer size

The members are an int and a char, but the compiler adds padding so that the whole structure aligns to its strictest member. The size therefore comes out a multiple of the int size - 8 bytes on a typical machine, not the 5 the members occupy. Padding is what makes sizeof a structure unpredictable from its members alone.

Please log in or register to report this question.

9.

What will be the output of the following C code?

#include <stdio.h>
#define MAX 4
enum sanfoundry
{
    a,b=3,c
};
main()
{
    if(MAX!=c)
        printf("hello");
    else
        printf("welcome");
}

 

Answer: C

Correct answer: welcome

The enumerators are a = 0, b = 3 and therefore c = 4 - values continue from the last one given. MAX is also 4, so the condition MAX != c is false and the else branch prints welcome.

Please log in or register to report this question.

10.

What will be the output of the following C code if input given is 2?

#include <stdio.h>
enum day
{
    a,b,c=5,d,e
};
main()
{
    printf("Enter the value for a");
    scanf("%d",a);
    printf("%d",a);
}

 

Answer: C

The code shown above results in an error. This is because memory is not allocated for constants in enum. Thereore ampersand(&) cannot be used with them. Also, a value has already been assigned to ‘a’, which cannot be changed.

Please log in or register to report this question.

Loading…

Other Subjects

Tags: Structures, Unions, Enums Questions and Answers || Structures, Unions, Enums MCQ Questions and Answers || Structures, Unions, Enums GK Questions and Answers || Structures, Unions, Enums GK MCQ Questions || Structures, Unions, Enums Multiple Choice Questions and Answers || Structures, Unions, Enums GK || GK on Structures, Unions, Enums || C Programming Questions and Answers || C Programming MCQ Questions and Answers || C Programming GK Questions and Answers || GK on C Programming