3i Infotech Papers: Sample Questions 334 - 335 of 1245

Examrace Placement Series prepares you for the toughest placement exams to top companies.

Question number: 334

» Languages » C & C Plus Plus

Short Answer Question▾

Write in Short

  1. #define int char
  2. main ()
  3. {
  4.     int i = 65;
  5.     printf ( “sizeof (i) = %d” sizeof (i) );
  6. }

Explanation

In a program

Table shows the program

Table shows the program

#define int char

#define replaces the string int by the macro char

main ()

{

int i = 65;

printf (“sizeof (i) =%d” sizeof (i) );

}

Here, int value replace in char value

Then, print sizeof (i) =1

Because, character variable minimum size is 1 byte or 1 character

Question number: 335

» Languages » C & C Plus Plus

Essay Question▾

Describe in Detail

What is the output of the following program?

  1. #define assert (condif ( (cond)! )  (fprintf (stderr, “assertion failed: %sfile %sline %d  ” #cond,  __FILE__, __LINE__), abort () )
  2. void main ()
  3. {
  4.     int i = 10;
  5.     if (i = = 0)
  6.     assert (i < 100);
  7.     else
  8.     printf ( “This statement becomes else for if in assert macro” );
  9. }

Explanation

  • In the program

Table shows the program

Table shows the program

#define assert (cond) if ( (cond)! ) (fprintf (stderr, “assertion failed: %s, file % s, line % d ” #cond, ________FILE________, ________LINE________), abort () )

  • Define macro condition

if (i == 0)

assert (i < 100);

  • The else with printf becomes the else for if in the assert macro.

  • Hence nothing is printed.

  • The solution is to use conditional operator instead of if statement

  • #define assert (cond) ( (cond)? (0): (fprintf (stderr, “assertion failed: %s, file % s, line % d ”, #cond, ________FILE________, ________LINE________), abort () ) )

  • Note: This problem of “matching with nearest else” cannot be solved by the usual method of placing the if statement inside a block like this, #define assert (cond) {if (! (cond) ) (fprintf (stderr, “assertion failed: %s, file % s, line % d ”, #cond, ________FILE________, ________LINE________), abort () ) }