- During editing
- During preprocessing
- During execution
Explanation:
During the initial processing phase, the line # include<stdio.h> that references the system header file is substituted with the content of the file stdio.h.
Thus, the complete content of the 'stdio.h' file is substituted with the #include directive.
2) C preprocessor directive #undef can be used with a macro that has been #define earlier.
- True
- False
Explanation:
Yes, the #undef directive is specifically employed with a macro that has been previously defined using the #define directive within a program.
For Example: # define PI 3.14
We can undefine PI macro by # undef PI
3) C preprocessor directive #ifdef...#elif?#endif is used for conditional compilation.
- True
- False
Explanation:
Indeed, the C macros such as #ifdef...#elif?#endif are employed to execute conditional operations within a C program.
The syntax of C preprocessor directive is:
#if <constant-expression>
#elif <constant-expression>
#endif
4) What will be the output of the below program?
#include<stdio.h>
#define SWAP(x, y) int t; t=x, x=y, y=t;
int main()
{
int x=10, y=20;
SWAP(x, y);
printf("x = %d, y = %d\n", x, y);
return 0;
}
- x=10 , y=20
- x=20, y=10
- Error: Undefined symbol 't'
- Error: Declaration not allowed in macro
Explanation:
The macro function SWAP(x, y) int temp; temp = x, x = y, y = temp; exchanges the values of the provided pair of variables.
Step 1: Declaring two variables, x and y, as integers and assigning the values 10 and 20 to them respectively.
Step 2: Executing the SWAP(x, y); macro will result in the interchange of values between variables x and y.
Hence the output of the program is x=20, y=10.
5) Which of the following are correctly formed #define statements in C language?
- #define CUBE(x) (XXX)
- #define CUBE(X) {XXX}
- #define CUBE (X) XXX
- #define CUBE(X) (X)(X)(X)
Explanation:
The syntax for macro definition with argument is:
"#define MACRO_NAME(ARG) (ARG*ARG*ARG) "
- There should be no space between macro's name and it's '(args)'.
- The variables used as macro's argument are case sensitive and its expansion should be same. i.e. 'x' and 'X' are different variables.
- A macro expansion should be enclosed within parenthesis '' (do not use { } or ).
- square of j=9
- Compile error
- No output
6) What will be the output of the below program?
#include <stdio.h>
#define DEF
int main(){
int j=3;
#ifdef DEF
printf("square of j=%d\n",j*j);
#else
printf("j=%d\n",j);
#endif
return 0;
}
Explanation:
In program #ifdef, #else, and #endif are directives used by the preprocessor.
If the macro DEF is established using a # define directive, subsequent code following # ifdef is processed; otherwise, the code enclosed between # else and #endif statements is executed. In this case, as DEF is defined, # ifdef DEF resolves to true, resulting in the calculation and printing of the square of 'j', which is 9.
When defining the macro DEF, it is optional to specify an expansion for the macro statement since the value of the macro expansion is not utilized in the program.
Therefore, the result of the program is the square of j, which is equal to 81.
7) What will be the output of the below program?
#include<stdio.h>
#define IT 0.1
#define HRA 0.2
#define DA 0.3
int main()
{
float bas_sal, net_sal;
bas_sal=1000;
net_sal=bas_sal*(1+HRA+DA-IT);
printf("Gross salary=%f\n", net_sal);
return 0;
}
- Gross salary=1000
- Gross salary=1400
- Compile error
- No output
Explanation:
When the preprocessor forwards the program for compilation, the placeholders for IT, HRA, and DA are substituted with 0.1, 0.2, and 0.3 correspondingly. Assuming the base salary is 1000, the net salary is computed as:
net_sal=bas_sal*(1+0.2+0.3-0.1);
The code printf("Gross salary=%f\n", net_sal); displays the gross salary value, resulting in the output.
Gross salary=1400
8) It is necessary that header files must have the .h extension?
Explanation:
No, header files are not required to have the .h extension; they can have any extension.
9) Which header file is used for supporting the functions- malloc and calloc.
- stdio.h
- math.h
- stdlib.h
- memory.h
Explanation:
Function calloc necessitates two arguments of type size_h.
The function malloc necessitates a single argument of type size_h.
For enabling the functionality of malloc and calloc, the stdlib.h header file must be included.
10) A macro statement can execute faster than function.
- True
- False
Explanation:
The execution of a macro statement can occur more quickly without the additional burden of a context switch since the macro's code expands directly at the point of invocation.
Therefore the above statement is true.
11) What will be the output of the below program?
#define MYFILE <stdio.h>
int main()
{
printf("Hello\n");
printf("Welcome to logic practice\n");
return 0;
}
#include MYFILE
- Hello\n Welcome to logic practice\n
- Hello Welcome to logic practice
- Hello Welcome to logic practice
- Compile error
Explanation:
- A file's name can serve as an expansion name as well.
- The #include preprocessor directive is permissible at any point within the program.
During preprocessing, the macro MYFILE is substituted with <stdio.h>, after which the program is compiled and run as illustrated below:
int main()
{
printf("Hello\n");
printf("Welcome to logic practice\n");
return 0;
}
#include <stdio.h>
Therefore the output of program is:
Hello
Welcome to logic practice
12) Will it be result in to an error if a header file is included twice in a program?
- It is compiler dependent
Explanation:
In GCC compilers and Turbo C, these issues are automatically handled by the compiler, resulting in error-free code generation. However, some other compilers may encounter errors in such cases.
Unless the header file has implemented a mechanism to prevent multiple inclusions, it should be ensured that it is not included more than once.
Hence, a program error caused by the inclusion of a header file twice is dependent on the compiler being used.
13) The preprocessor can trap simple error like nested comments, mismatch of braces or missing declarations.
- True
- False
Explanation:
The assertion is incorrect because the preprocessor is incapable of detecting errors; it simply substitutes the macro with the specified expression.
The compiler is employed to identify errors within a program.
14) In every C program there is at least one preprocessor directive.
- True
- False
Explanation:
The claim is inaccurate as the preprocessor directive is not mandatory in every C program. It is entirely possible to write a C program without employing any preprocessor directives.
15) What will be the output of the below program?
#include<stdio.h>
#define MAX(x, y) (x > y ? x : y)
int main()
{
int a;
a = MAX(3+1, 2+4);
printf("%d\n", a);
return 0;
}
Explanation:
The MAX(x, y) macro computes the larger value between two specified numbers, x and y, by employing a ternary operator.
#if <constant-expression>
#elif <constant-expression>
#endif
Step 2: a = MAX(3+1, 2+4); becomes,
=> a = (3+1 > 2+4 ? 3+1 : 2+4)
=> a = (4 > 6 ? 4 : 6)
=> a = 6
Display the value stored in variable 'a' by using printf("%d\n", a); in Step 3.
Hence the output of the program is 6.