Saturday, 16 March 2013

not=!2;


main()
    {
    char not;
    not=!2;
    printf("%d",not);
    }
Answer:
0
Explanation:
! is a logical operator. In C the value 0 is considered to be the boolean value FALSE, and any non-zero value is considered to be the boolean value TRUE. Here 2 is a non-zero value so TRUE. !TRUE is FALSE (0) so it prints 0.

Blog Author: Vijay Kumar

Go to: Java Aptitude


printf("%d %d %d",sizeof(str1),sizeof(str2),sizeof("abcd"));


main()
    {
    char *str1="abcd";
    char str2[]="abcd";
    printf("%d %d %d",sizeof(str1),sizeof(str2),sizeof("abcd"));
    }
Answer:
2 5 5
Explanation:
In first sizeof, str1 is a character pointer so it gives you the size of the pointer variable. In second sizeof the name str2 indicates the name of the array whose size is 5 (including the '\0' termination character). The third sizeof is similar to the second one.

Blog Author: Vijay Kumar

Go to: Java Aptitude


calling of main() only


main()
    {
    main();
    }
Answer:
 Runtime error : Stack overflow.
Explanation:
main function calls itself again and again. Each time the function is called its return address is stored in the call stack. Since there is no condition to terminate the function call, the call stack overflows at runtime. So it terminates the program and results in an error.


Blog Author: Vijay Kumar

Go to: Java Aptitude


files which are automatically opened


What are the files which are automatically opened when a C file is executed?
Answer: stdin, stdout, stderr (standard input,standard output,standard error).

Blog Author: Vijay Kumar

Go to: Java Aptitude


Unary + operator


main()
    {
    int i=-1;
    +i;
    printf("i = %d, +i = %d \n",i,+i);
    }
Answer:
 i = -1, +i = -1
Explanation:
Unary + is the only dummy operator in C. Where-ever it comes you can just ignore it just because it has no effect in the expressions (hence the name dummy operator).

Blog Author: Vijay Kumar

Go to: Java Aptitude