© 2014 Firstsoft Technologies (P) Limited. login
Hi 'Guest'
Home SiteMap Contact Us Disclaimer
enggedu

C and C++ Interview Questions

8. Explain the #pragma inline ?

Ans:

#pragma inline only tells the compiler that source code of program contain inline assembly language code .In in C we can write assembly language program with help of asm keyword.

Ex - 1: Waht is the ouyput ?
#define assert(cond) if(!(cond)) \
  (fprintf(stderr, "assertion failed: %s, file %s, line %d \n",#cond,\
 __FILE__,__LINE__), abort())
 void main()
{
int i = 10;
if(i==0)   
    assert(i < 100);
else
    printf("This statement becomes else for if in assert macro");
}
Answer:
No output
Explanation:

The else part in which the printf is there 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 \n",#cond, __FILE__,__LINE__), abort()))
Note:
However 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 \n",#cond,\
 __FILE__,__LINE__), abort()) \ 
}

Ex - 2:  What is the output?
  #define max 5
   #define int arr1[max]
      main()
      {
     typedef char arr2[max];
     arr1 list={0,1,2,3,4};
      arr2 name="name";
      printf("%d %s",list[0],name);
      }
Answer:
Compiler error (in the line arr1 list = {0,1,2,3,4})
Explanation:

arr2 is declared of type array of size 5 of characters. So it can be used to declare the variable name of the type arr2. But it is not the case of arr1. Hence an error.
Rule of Thumb:
#defines are used for textual replacement whereas typedefs are used for declaring new types.


SLogix Student Projects
bottom