Monday, December 1, 2008

C language placement problem with solution

1)
#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 <>
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()) \
}




2)
Is the following code legal?
struct a
    { 
int x;
 struct a b;
    }


Answer:
No

Explanation:
Is it not legal for a structure to contain a member that is of the same
type as in this case. Because this will cause the structure declaration to be recursive without end.


3)
Is the following code legal?
struct a
    { 
int x;
            struct a *b;
    }


Answer:
Yes.

Explanation:
*b is a pointer to type struct a and so is legal. The compiler knows, the size of the pointer to a structure even before the size of the structure
is determined(as you know the pointer to any type is of same size). This type of structures is known as ‘self-referencing’ structure.



4)
Is the following code legal?
 typedef struct a
    { 
  int x;
 aType *b;
    }
aType


Answer:
No
Explanation:
The typename aType is not known at the point of declaring the structure (forward references are not made for typedefs).

5)
Is the following code legal?
typedef struct a aType;
struct a
int x;
aType *b;
};


Answer:-
Yes

Explanation:
The typename aType is known at the point of declaring the structure, because it is already typedefined.

ALL PLACEMENT TECHNICAL INTERVIEW QUESTION  BANCK   

1Technical /logical interview questions and solution in C language |2Technical / logical interview questions in C language |3C programming Language Technical Questions And Answers |4C Language Technical Questions And Answers |5C Language Technical Interview Questions And solution |6C Language Technical Interview Questions And Answers |7C Language Interview Questions And Solution |8online Placement Question of c programming languge |9online placement c programming question|10online test of c programming languge|11placement c programming quiz with solution |12c programming quiz with solution |13C programming languge quiz with solution |14C quiz with solution |15C languge quiz with solution  |16C programming quiz with solution |17questions with answer in C programming language|18Technical questions with solution in C programming language  |19Technical questions with answer in C programming language |20placement Question and answer in Simple C Language |21Simple C Language placement Question and answer22Simple C programming Question and answer |23Simple C programming Question and answer |24Simple C Language Question and answer |25c progrmming Interview question with solution  |26Technical placement question with solution in c progrmming |27placement c progrmming Question with Answer  |28c progrmming placement question with solution  |29 placement c progrmming languge question with Answer |30placement c progrmming languge question with solution  |31c progrmming languge placement question with solution  |32c languge placement question with solution |33placement c programming languge problem with solution  |34c programming languge placement problem with solution  |35c language placement problem with solution  |36Placement c languge problem with solution  |37c language problem with answer  |38c language problem with solution  |


No comments:

Post a Comment

Thanks to given comments.......

My Blog List