Pages

Friday, February 29, 2008

C Questions 6




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();

Wednesday, February 27, 2008

C Questions 5




Let's start today with something else. The English calendar of September 1752 contained only 19 days, and is the shortest month. Surprised. To know more please read this:
In September 1752 the Julian calendar was replaced with the Gregorian calendar in Great Britain and its American colonies. The Julian calendar was 11 days behind the Gregorian calendar, so 14 September got to follow 2 September on the day of the change. The result was that between 3 and 13 September, absolutely nothing happened!
To know more:
http://www.didyouknow.cd/calendar.htm

Q0. Do you know what happens when you type:
$cal 9 1752
September 1752
S M Tu W Th F S
1 2 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
And in this calendar days from 3rd till 13th are missing.

Q1. void *p++;
void *q; q++;
What p,q will point to?
Q2. DMA uses Logical address, or physical address?
Q3. What printf returns?
Q4. While in bottom half, what is the context, is it still the process context?
Q5. (MS) You are trying to use notepad, then while clicking on save, the application hangs. How will you go about debugging this? The complete source code of NOTEPAD is available with you. List down the scenarios and steps.

Q6. Tell me the o/p.
main(){
const int i=5;
i++;
printf("%d\n", i);
}
Ans:
Compilation:
warning: initialization discards qualifiers from pointer target type
warning: increment of read-only variable `i'
Result: i= 6

Q7. Tell me the o/p.
main() {
const int i =5;
int *p=&i;
p++;
printf("*p=%d", *p);
}
Ans:
Compilation:
warning message: warning: initialization discards qualifiers from pointer target type
Result:
*p=garbage value

Q8. Tell me the o/p.
main() {
const int i =5;
int *p=&i;
*p++;
printf("i=%d\n", i);
printf("*p=%d\n", *p);
}
Ans:
Compilation:
warning message: warning: initialization discards qualifiers from pointer target type
Result:
i=5
*p=garbage value

Q9. Tell me the o/p.
main() {
const int i =5;
int *p=&i;
(*p)++;
printf("i=%d\n", i);
printf("*p=%d\n", *p);
}
Ans:
Compilation:
warning message: warning: initialization discards qualifiers from pointer target type
Result:
i=6
*p=6

Q10.
main()
{
const i=9;
const int *p;
p=&i;
printf("*p=%d\n",*p);
p++;
printf("*p=%d\n",*p);
}

Ans:
*p=9
*p=garbage value

Q11. What are the differences in the following declarations:
a)
const i;
const int i;
b)
const *p;
const int *p;
int * const p;
const *int p;
int const *p;

Ans:
const int * p;
declares that p is variable pointer to a constant integer and
int const * p;
is an alternative syntax which does the same, whereas
int * const q;
declares that q is constant pointer to a variable integer and
int const * const p1;
declares that p1 is constant pointer to a constant integer. Basically ‘const’ applies to whatever is on its immediate left (other than if there is nothing there in which case it applies to whatever is its immediate right).
Q12.
int func()
{
printf("Hi");
func();
}
int main(){ func();}
What will be the o/p?

Ans:
Stack overflow, why?
The stack frame for func needs to store the entry and exit point each time func() is recursively called. This will fill up the stack, and hence overflow.
The func() is calling printf(), and in the printf's stack frame you will put the string argument "Hi", and not in the stack frame of func().

Q13.
main()
{
const int i=10;
i=20;
printf(i=%d\n",i);
}
Ans:
warning: assignment of read-only variable `i'
Result:
i=20
Now, tell me where the const variable is stored?