Wednesday, February 22, 2012

C question set 5


1===========

Is the following statement a declaration/definition. Find what does it mean?
int (*x)[10];
Answer:

Definition.
x is a pointer to array of(size 10) integers.
Apply clock-wise rule to find the meaning of this definition.


2===========

What is the output for the program given below
typedef enum errorType{warning, error, exception,}error;
     main()
    {
        error g1;
        g1=1;
        printf("%d",g1);
     }
Answer:

Compiler error: Multiple declaration for error
Explanation:

The name error is used in the two meanings. One means that it is a enumerator constant with value 1.
 The another use is that it is a type name (due to typedef) for enum errorType. Given a situation the
 compiler cannot distinguish the meaning of error to know in what sense the error is used:
error g1;
g1=error;
// which error it refers in each case? When the compiler can distinguish between usages then it will not
 issue error (in pure technical terms, names can only be overloaded in different namespaces).
Note:

the extra comma in the declaration,
enum errorType{warning, error, exception,}
is not an error. An extra comma is valid and is provided just for programmer's convenience.



3============


#ifdef something
int some=0;
#endif

main()
{
int thing = 0;
printf("%d %d\n", some ,thing);
}
Answer:

Compiler error : undefined symbol some
Explanation:

This is a very simple example for conditional compilation. The name something is not already known to the
compiler making the declaration int some = 0; effectively removed from the source code.


4============


#if something == 0
int some=0;
#endif

main()
{
int thing = 0;
printf("%d %d\n", some ,thing);
}
Answer:

0 0
Explanation:

This code is to show that preprocessor expressions are not the same as the ordinary expressions. If a name
 is not known the preprocessor treats it to be equal to zero.

5============

main()
{
char *p = "ayqm";
printf("%c",++*(p++));
}

Answer: z

6============

main()
{
char *p = "ayqm";
char c;
c = ++*p++;
printf("%c",c);
}
Answer:

b
Explanation:

There is no difference between the expression ++*(p++) and ++*p++. Parenthesis just works as a visual
 clue for the reader to see which expression is first evaluated.

7============

#include<stdio.h>




main()
{
char *p = "ayqm";
printf("%c...%c....%c",*p,++*(p++),*p);
}

Answer:y...z....a

8============

int aaa() {printf("Hi");}
int bbb(){printf("hello");}
iny ccc(){printf("bye");}

main()
{
int ( * ptr[3]) ();
ptr[0] = aaa;
ptr[1] = bbb;
ptr[2] =ccc;
ptr[2]();
}
Answer:

bye
Explanation:

int (* ptr[3])() says that ptr is an array of pointers to functions that takes no arguments and returns the
type int. By the assignment ptr[0] = aaa; it means that the first function pointer in the array is initialized
 with the address of the function aaa. Similarly, the other two array elements also get initialized with the
addresses of the functions bbb and ccc. Since ptr[2] contains the address of the function ccc, the call to
 the function ptr[2]() is same as calling ccc(). So it results in printing "bye".

9============
main()
{
while (strcmp("some","some\0"))
printf("Strings are not equal\n");
}
Answer:

No output
Explanation:

Ending the string constant with \0 explicitly makes no difference. So "some" and "some\0" are equivalent.
So, strcmp returns 0 (false) hence breaking out of the while loop.

10===========

main()
{
char str1[] = {'s','o','m','e'};
char str2[] = {'s','o','m','e','\0'};
while (strcmp(str1,str2))
printf("Strings are not equal\n");
}
Answer:

"Strings are not equal"
"Strings are not equal" ...
Explanation:

If a string constant is initialized explicitly with characters, '\0' is not appended automatically to the string.
 Since str1 doesn't have null termination, it treats whatever the values that are in the following positions as
 part of the string until it randomly reaches a '\0'. So str1 and str2 are not the same, hence the result.

No comments:

Post a Comment