Exercise: C Preprocessor

Questions for: C Preprocessor

Will it result in to an error if a header file is included twice?
A:
Yes
B:
No
C:
It is compiler dependent.
D:
Answer: C
Unless the header file has taken care to ensure that if already included it doesn't get included again.

Turbo C, GCC compilers would take care of these problems, generate no error.
Will the following program print the message infinite number of times?
#include<stdio.h>
#define INFINITELOOP while(1)

int main()
{
    INFINITELOOP
    printf("ExamAdept");
    return 0;
}
A:
Yes
B:
No
C:
D:
Answer: A

Yes, the program prints "ExamAdept" and runs infinitely.

The macro INFINITELOOP while(1) replaces the text 'INFINITELOOP' by 'while(1)'

In the main function, while(1) satisfies the while condition and it prints "ExamAdept". Then it comes to while(1) and the loop runs infinitely.

Will the program compile successfully?
#include<stdio.h>

int main()
{
    #ifdef NOTE
        int a;
        a=10;
    #else
        int a;
        a=20;
    #endif
    printf("%d\n", a);
    return 0;
}
A:
Yes
B:
No
C:
D:
Answer: A

Yes, this program will compile and run successfully and prints 20.

The macro #ifdef NOTE evaluates the given expression to 1. If satisfied it executes the #ifdef block statements. Here #ifdef condition fails because the Macro NOTE is nowhere declared.

Hence the #else block gets executed, the variable a is declared and assigned a value of 20.

printf("%d\n", a); It prints the value of variable a 20.

It is necessary that a header files should have a .h extension?
A:
Yes
B:
No
C:
D:
Answer: B

No, the header files have any kind of extension.

Will the program compile successfully?
#include<stdio.h>

int main()
{
    printf("India" "BIX\n");
    return 0;
}
A:
Yes
B:
No
C:
D:
Answer: A
Yes, It prints "ExamAdept"
Ad Slot (Above Pagination)
Quiz