like

Tuesday, December 20, 2011

*********************** THE C PREPROCESSOR *************************

***********************************************************************


1] If the file to be included doesn't exist, the preprocessor flashes an error message. < True / False>


2] The preprocessor can trap simple errors like missing declarations, nested comments or mismatck of braces.


3] What would be the output of the following program?

# define SQR(x) (x * x)
main()
{
int a, b = 3;
a = SQR(b + 2);
printf("\n %d",a);
}

OPTIONS:
(a) 25
(b) 11
(c) Error
(d) Some garbage value


4] How would you define the SQR macro in 6.3 above such that it gives the result of a as 25.


5] What would be the output of the following program?

# define CUBE(x) (x * x * x)
main()
{
int a, b = 3;
a = CUBE(b++);
printf("\n %d %d",a,b);
}



6] Indicate what would the SWAP macro be expanded to on preprocessing. Would the code compile?

# define SWAP(a,b,c) (ct; t = a; a = b; b = t;)
main()
{
int x = 10,y = 20;
SWAP(x,y,int);
printf("%d %d",x,y);
}


7] How would you modify the SWAP macro in 6.6 above such that it is able to exchange two integers.


8] What is the limitation of the SWAP macro of 6.7 above?


9] In which line of the following program an error would be reported?

(1) #define CIRCUM(R) (3.14 * R * R);
(2) main()
(3) {
(4) float r = 1.0, c;
(5) c = CIRCUM(r);
(6) printf("\n %f",c);
(7) if(CIRCUM(r) == 6.28)
(8) printf("\n Gobbledygook");
(9) }


10] What is the type of the variable 'b' in the following declaration?

# define FLOATPTR float*
FLOATPTR a,b;


11] Is it necessary that the header files should have a '.h' extension?


12] What do the header files usually contain?


13] Would it result into an error if a header file is included twice? < Yes / No>


14] How can a header file ensure that it doesn't get included more than once?


15] On inclusion, where are the header files searched for?


16] Would the following typedef work?

typedef #include I;


17] Would the following code compile correctly?

main()
{
#ifdef NOTE
/* unterminated comment */
int a;
a = 10;
#else
int a;
a = 20;
#endif

printf("%d",a);
}


18] What would be the output of the following program?

#define MESS Junk
main()
{
printf("MESS");
}


19] Would the following program print the message infinite number of times?

#define INFINITELOOP while(1)
main()
{
INFINITELOOP
printf("\n Grey haired");
}


20] What would be the output of the following program?

#define MAX(a,b) ( a>b? a:b)
main()
{
int x;
x = MAX(3 + 2,2 + 7);
printf("%d",x);
}


21] What would be the output of the following program?

#define PRINT(int) printf("%d",int)
main()
{
int x = 2, y = 3, z = 4;
PRINT(x);
PRINT(y);
PRINT(z);
}


22] What would be the output of the following program?

#define PRINT(int) printf(*int = %d*,int)
main()
{
int x = 2, y = 3, z = 4;
PRINT(x);
PRINT(y);
PRINT(z);
}


23] How would you modify the macro of 6.22 such that it outputs:

x = 2 y = 3 z = 4


24] Would the following program compile successfully? < Yes / No) main() { printf("Tips" "Traps); } 25] Define the macro DEBUG such that the following program outputs: DEBUG: x = 4 DEBUG: y = 3.140000 DEBUG: ch = A main() { int x = 4; float a = 3.14; char ch = 'A'; DEBUG(x,%d); DEBUG(a,%f); DEBUG(ch,%c); } *********************************************************************** ********************** ANSWERS *************************** *********************************************************************** 1] True 2] False 3] B. Because, on preprocessing the expression becomes 'a = a(3 + 2 * 3 + 2). 4] #define SQR(x) ((x) * (x)) 5] 27 6. Though some compilers may give this as the answer, according to the ANSI C standard the expression b++ * b++ * b++ is undefined. Refer Chapter 3 for more details on this. 6] (int t; t = a, a = b, b= t); This code won't compile since declaration of t cannot occur within parentheses. 7] #define SWAP(a,b,c) ct; t = a, a = b, b = t; 8] It cannot swap pointers. For example, the following code would not compile. #define SWAP(a,b,c) ct; t = a, a = b, b = t; main() { float x = 10,y = 20; float *p,*q; p = &x; q = &y; SWAP(p,q,float*); printf("%f %f",x,y); } 9] Line number 7, whereas the culprit is really the semicolon in line number 1. On expansion line 7 becomes ' if ((3.14 * 1.0 * 1.0); == 6.28). Hence the error. 10] 'float' and not a pointer to a float, since on expansion the declaration becomes: float *a,b; 11] No. However, traditionally they have been given a .h extension to identify them as someting different than the .c program files. 12] Preprocessor directives like #define, structure, union and enum declarations, typedef declarations, global variable declarations and external function declarations. YOu should not write the actual code ( i.e. function bodies) or global variable definition (that is difining or initialising instances) in header files. The # include directives should be used to pull in header files, not other .c files. 13] Yes, unless the header file has taken care to ensure that if already included it doesn't get included again. 14] All declarations must be written in the manner shown below. Assume that the name of the header file in FUNCS.H. /* funcs.h */ #ifdef_FUNCS #define_FUNCS /* all declarations would go here */ #endif Now if we include this file twice as shown below, it would get included only once. #include "goto.c" #include "goto.c" main() { /* some code */ } 15] If included using <> the files get searched in the predefined (can be changed) include path. If included withe the " " syntax in addition to the predefined include path the files also get searched in the current directory ( usually the directory form which you inboked the compiler).


16] No. Because 'typedef' goes to work after preprocessing.


17] No. Even though the #ifdef fails in this case ( NOTE being undefined) and the if block doesn't go for compilation errors in it are not permitted.


18] MESS


19] Yes


20] 9


21] 2 3 4


22] int = 2 int = 3 int = 4


23] #defien PRINT(int) printf(#int "= %d", int)
main()
{
int x = 2, y = 3, z = 4;
PRINT(x);
PRINT(y);
PRINT(z);
}

The rule is if the parameter name is preceded by a # in the macro expansion, the combination(of # and parameter) will be expanded into quoted string with the parameter replaced by the actual argument. This can be combined with string concatenation to print the output desired in our program. On expansion the macro becomes:

printf("x" "= %d",x);

The two strings get concatenated, so the effect is

printf("x = %d",x);


24] Yes. The output would be TipsTraps. In fact this result has been used in 6.23 above.


25] #define DEBUG(var,fmt) printf("DEBUG:" #var "="#fmt"\n",var)


26] multiply
Here two operations are being carried out expansion and stringizinf. Xstr() macro expands its argument, and then str() stringizes it.


27] #define PRINT(var1,var2,var3) printf("\n" #var1 "= %d" #var2 "= %d" #var3 "= %d",var1,var2,var3)

No comments:

Post a Comment