Peter van der Linden, in his book 'Expert C programming - Deep C secrets'
Q1.
main()
{
unsigned i=-10;
printf("i=%d\n",i);
}
Ans:
i=-10
Q2.
main()
{
unsigned i=-10;
printf("i=%u\n",i);
}
Ans:
i=4294967286
Q3. How do you include assembly instructions inside a c program?
Ans:
_asm_ volatile {"nop" "\n\t"}
Why the asm word was used here?
~To tell the assembler that it is an assembly instruction.
Why the volatile word is being used here?
~To tell the compiler, not to optimize the code here.
Q4. Though the sizeof array is Zero?
struct test {
int len;
char a[0];
}
int main()
{
struct test m1;
m1.a[0] = 'a';
printf("%c\n",m1.a[0]);
printf("sizeof=%d",sizeof(m1));
}
Q5. Write a self reproducible C program.
Ans:
char*f="char*f=%c%s%c;main()
{printf(f,34,f,34,10);}%c";
main(){printf(f,34,f,34,10);}
Note: Here is a list of other such quines written using C language.
http://www.nyx.net/~gthompso/self_c.txt
Q6. Tell me the o/p:
main()
{
char i=255;
printf("i=%d\t%c\n",i,i);
}
Ans:
i= -1
Q7.
main()
{
unsigned char i=-1;
printf("i=%d\t%c\n",i,i);
}
Ans:
i=255
Q8.
1 #include
2
3 main()
4 {
5 char *pary="string";
6 char ary[]="string";
7 printf("sizeof=%d %d\n", sizeof(ary), sizeof(pary));
8 printf("strlen=%d %d\n", strlen(ary), strlen(pary));
9 }
Ans:
sizeof=7 4
strlen=6 6
Note: machine details:
SunOS devtest6 5.8 Generic_117350-39 sun4u sparc SUNW,Ultra-80
Q9. Compile this code:
1 #include
2
3 main()
4 {
5 char *pary="string";
6 char ary[]="string";
7 const char *a="mystring";
8 char const *b="mystring";
9 // const *char c="mystring";
10 char *const d="mystring";
11 const char *const dd="mystring";
12
13 const char e[]="mystring";
14 char const f[]="mystring";
15
16 printf("sizeof=%d %d\n", sizeof(ary), sizeof(pary));
17 printf("strlen=%d %d\n", strlen(ary), strlen(pary));
18
19 a="new";
20 //a[0]='n';
21
22 b="new";
23 //b[0]='n';
24
25 d="new";
26 //d[0]='n';
27
28 dd="new";
29 //dd[0]='n';
30
31 strcpy(e,"new");
32 e[0]='0';
33
34 strcpy(f,"new");
35 f[0]='0';
36
37 }
38
Ans::
In Solaris:
devtest6:/home/jkongkon/sea>!gc
gcc -v
Reading specs from /export/tools/SunOS5.8/gcc-2.95.3/lib/gcc-lib/sparc-sun-solaris2.8/2.95.3/specs
gcc version 2.95.3 20010315 (release)
devtest6:/home/jkongkon/sea>gcc -g 2.c
2.c: In function `main':
2.c:25: warning: assignment of read-only variable `d'
2.c:28: warning: assignment of read-only variable `dd'
2.c:31: warning: passing arg 1 of `strcpy' discards qualifiers from pointer target type
2.c:32: warning: assignment of read-only location
2.c:34: warning: passing arg 1 of `strcpy' discards qualifiers from pointer target type
2.c:35: warning: assignment of read-only location
devtest6:/home/jkongkon/sea>
In Linux:
devtest21.hursley.ibm.com:/home/jkongkon/sea>gcc -v
Reading specs from /usr/lib/gcc/x86_64-redhat-linux/3.4.4/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --disable-checking --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-java-awt=gtk --host=x86_64-redhat-linux
Thread model: posix
gcc version 3.4.4 20050721 (Red Hat 3.4.4-2)
devtest21.hursley.ibm.com:/home/jkongkon/sea>cc
cc: no input files
devtest21.hursley.ibm.com:/home/jkongkon/sea>gcc -g 2.c
2.c: In function `main':
2.c:25: error: assignment of read-only variable `d'
2.c:28: error: assignment of read-only variable `dd'
2.c:31: warning: passing arg 1 of `strcpy' discards qualifiers from pointer target type
2.c:32: error: assignment of read-only location
2.c:34: warning: passing arg 1 of `strcpy' discards qualifiers from pointer target type
2.c:35: error: assignment of read-only location
devtest21.hursley.ibm.com:/home/jkongkon/sea>
Q10. What the following code will do:
void (*fp)[3] () = { do_something, do_another_thing, do_something_else };
int ch = getchar();
if (ch >= 'a' && ch <= 'c') (*(fp + ch - 'a'))(); else do_default(); Ans: The above code looks complicated, but the meaning is very simple. if((ch = getchar()) == 'a') do_something(); else if(ch = = 'b') do_another_thing(); else if(ch = = 'c') do_something_else(); else do_default(); Or ch= getchar(); switch(ch) { case 'a': { do_something(); break; } case 'b': { do_another_thing(); break; } case 'c': { do_something_else(); break; } default: do_default(); Q12:What is the side effect? malloc(); free(); free(); Q13:What is the side effect? fopen(); fclose(); fclose();
No comments:
Post a Comment