1 2 3 4 5 |
main() { int x = -10; printf("%dn", ~x+1); } |
1 2 3 4 |
main() { printf("%cn", '1' + 1); } |
1 2 3 4 5 6 |
main() { char x = 0x80; int y = x; printf("%dn", y); } |
a)0
1 2 3 4 5 6 |
main() { int x = 0xdeadbeef; char y = x; printf("%xn", y); } |
a)de
1 2 3 4 5 6 7 8 |
main() { char y[10] = "abcdefghi"; char *p = y; p = p + 9; printf("%cn",*p); } |
a)i
1 2 3 4 5 6 7 8 |
main() { int y[2][2] = { {1,2}, {3,4} }; int *p = &y[1]; p = p + 1; printf("%dn",*p); } |
a)4
1 2 3 4 5 6 |
typedef struct date { int day; int month; int year; } MYDATE; |
1 2 3 4 |
main() { printf("%dn",f1(10,10)); } |
Assuming f1 is defined in another file, which of the following statements are acceptable by compiler as function prototype of f1.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
int y = 10; main() { int x = 10; int y = 20; x = x + y; if (x >= 30) { int y = 30; x = x + y; } else { int y = 40; x = x + y; } printf("%dn", x); } |
a)40
1 2 3 4 5 6 7 8 9 |
main() { int x = 10; int y = 20; if (x & y) if(x | y) x = 20; else x = 30; printf("%dn", x); } |
a)10
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
main() { int x = 1; int y = 2; switch(x | y) { case 1: x = 2; case 2: x = 3; case 3: x = 4; case 4: x = 5; default:x = 6; } printf("%dn", x); } |
a)6
1 2 3 |
#define MAKEDOUBLE(x) x+x a = MAKEDOUBLE(4) * 3; VALUE OF a |
1 2 3 4 5 |
main() { int i = 8; i = i++; } |
a)i takes value 8
unpredictable
1 2 3 4 5 6 7 8 9 10 |
void function(int *ip) { static int myValue = 100; ip = &myValue; } main() { int *myPointer; function(myPointer); } |
What happens when the following call is made:
1 2 3 4 5 |
int x[100]; main() { printf("%dn",x[99]); } |
a)Unpredicatable
1 2 3 4 |
main() { printf("%dn", 100 / 10 / 10); } |
a)1
1 2 3 4 5 6 7 8 |
main() { int a=5; if (a=1) { printf("%d", a); } } |
In the above code
1 2 3 4 5 6 7 8 |
int main() { int a[10]; int i; for (i=0; i<10; i++) a[i] = i; printf("%d", a[-1]); } |
The above program
1 2 3 4 5 6 7 |
main() { int a=3, b=5, c=1; int x=8; x = a < (b < c); printf("%d", x); } |
1 2 3 4 5 6 7 8 |
main() { unsigned int i = 5; while (--i >= 0) { printf("Hello Worldn"); } } |
1 2 3 4 5 6 7 8 9 10 11 12 |
main() { extern int change(float); int x; x = change(5.42); printf("%dn", x); } int change(float x) { return ((int)x); } <span style="line-height: 1.5;"> </span> |
1 2 3 4 5 |
main() { char str[7] = "Strings"; printf("%s", str); } |
a)Strings
1 2 3 4 5 6 |
main() { int y = 100; const int x = y; printf("%dn", x); } |
a)100
1 2 3 4 5 6 7 8 |
main() { int i, j, k; for(i = 1; i <= 2; ++i) for(j = 1; j <= i; ++j) for(k = i; k <= j; ++k) printf("Hello Worldn"); } |
a)2
code or the libraries used
1 2 3 4 5 6 7 8 |
int main() { int i = 0; int sizeint; sizeint = sizeof i++; printf("%dn", i); return 0; } |
Considering that integer requires 4 bytes of memory, the value of i printed by the above program is:
1 2 3 4 5 6 7 8 |
int main() { int i = 0; int sizeint; sizeint = sizeof(i++); printf("%dn", sizeint); retrun 0; } |
a)1
1 2 3 4 5 6 7 8 |
int main() { int j = 6; int i; i = 5, j++; printf("%dn", i); return 0; } |
Which of the following statements are true about the code snippet given above
1 2 3 4 5 6 7 8 |
int main() { int j=6; int i; i = (5, j); printf("%d %dn", i, j); return 0; } |
The output from the above program is:
1 2 3 4 5 6 7 8 |
int main() { int j=6; int i; i = (5, j++); printf("%d %dn", i, j); return 0; } |
The output from the above program is:
1 2 3 4 5 6 |
main() { int x=10; x= x++; printf("%dn", x); } |
a)10
1 2 3 4 5 6 7 |
main() { int x = 0; int y; y = (x++, x++); printf("%d", y); } |
1 2 3 4 5 6 7 8 9 10 |
main() { int x = 0; int y = 2; if (1 < ++x | 2 < y++) { ++y; } printf("%d %dn", x, y); } |
a)1 2
1 2 3 4 5 6 7 8 9 10 |
main() { int x = 0; int y = 0; if (x++ && ++y) { ++x; } printf("%d %dn",x, y); } |
a)0 0
1 2 3 4 5 6 7 8 9 10 |
main() { int x = 0; int y = 0; if (++x || ++y) { x++; } printf("%d %dn",x, y); } |
a)1 1
1 2 3 4 5 6 7 8 9 10 |
main() { int x = 0; int y = 0; if (!(++x && ++y)) { x++; } printf("%d %dn",x, y); } |
a)1 1
1 2 3 4 5 6 7 8 9 10 |
main() { int x = 0; int y = 0; if (!(++x || ++y)) { x++; } printf("%d %dn",x, y); } |
a)1 0
1 2 3 4 5 6 |
main() { int x = 0x01C0FFEE; int y = x >> 15 + 1 & 0xFF; printf("%xn", y); } |
Priority is +, >>, &
1 2 3 4 5 6 |
main() { int x = 0xdeadbeef; int y = ~0xdeadbabe ^ x & 0xFFFF; printf("%xn", y); } |
a)2152FBAE
1 2 3 4 5 6 7 8 9 10 |
main() { int x = 10; int y = 20; if (x <= y == 1) { ++x; } printf("%xn", x); } |
a)11
1 2 3 4 5 6 7 8 9 10 |
main() { int x = 1; int y = 0; if (x | y++) { ++y; } printf("%d %dn", x); } |
a)Compiler Dependent
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
int x; int f() { x += 10; return x; } int g() { x /= 10 ; return x; } int h() { x *=2; return x; } int main() { x = 10; printf("%dn", f() + g() * h()); } |
a)14
1 2 3 4 5 6 7 |
int main() { int x=10; int *y=&x; x = x+ *y*x/ *y ; printf("%dn",x); } |
a)Syntax Error
1 2 3 4 5 6 7 |
int main() { int a = 0; int b; b = (a++)? a++: a++; printf("%d %dn", a,b); } |
a)2 1
1 2 3 4 5 6 7 8 |
int main() { int a = 0; int b = 1; int c; c = a?1:b?2:3; printf("%dn", c); } |
a)1
54)What is the output of following program?
1 2 3 4 5 |
#define DOUBLE(x) x + x main() { printf("%dn", DOUBLE(10)*2); } |
1 2 3 4 5 6 7 |
#define SQUARE(x) (x * x) main() { int x = 10; int y = 5; printf("%dn", SQUARE(x +y)); } |
1 2 3 4 5 6 7 8 |
#define ABC(x) DEF(x) #define DEF(x) GHI(x) #define GHI(x) printf x main() { int x = 100; ABC(("value of x is %dn", x)); } |
a)value of x is 100
1 2 3 4 5 6 7 8 9 |
#define ABC(x) DEF(x) #define DEF(x) GHI(x) #define GHI(x) printf(x) main() { int x = 100; int y = 200; ABC(("Sum of x + y is %d", x + y)); } |
1 2 3 4 5 6 7 |
#define ABC(x,y) printf(x":printed from ABCn", y) main() { int x = 100; int y = 200; ABC("Sum of x + y is %d", x + y); } |
1 2 3 4 5 6 7 |
#define KING_KONG 0xDEADCA1F #define KING_ARTHUR 0xDEADBEAD #define ABC(x) (KING_##x) main() { printf("0x%Xn", ABC(ARTHUR)); } |
1 2 3 4 5 |
#define MYPRINT(x) printf(#x) main() { MYPRINT(This appears like syntax errorn); } |
a)This appears like syntax error
unpredicatable.
1 2 3 4 5 6 7 8 9 10 11 |
#define MYPRINTF(x) printf("%s: printed at time:%dn", #x, __TIME__) main() { int i = 5; while(i--) { MYPRINTF(Hello World); sleep(1); } } |
Assume, sizeof(int) is 4 and sizeof(char) is 1.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#pragma pack(1) typedef struct { char day; char month; int year; }DATE; #pragma pack() typedef struct { int empno; DATE doj; /* date of joining */ int salary; } EmpRec; main() { printf("%d %dn", sizeof(DATE), sizeof(EmpRec)); } |
1 2 3 4 5 6 7 8 9 10 |
#include <stdio.h> #define a(x,y) x##y #define b(x) #x #define c(x) b(x) int main() { printf("%st",c(a(34,56))); printf("%sn",b(a(34,56))); return 0; } |
65)Assuming the code below is stored in a file “a.c” and the command “gcc a.c” is used to compile the file, what is the output of this program:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#ifdef DEBUG #define DEBUG_PRINT(x) printf("debug :%sn", (x)); #else #define DEBUG_PRINT(x) #endif main() { int x; DEBUG_PRINT("In main"); x=10; printf("%dn", x); DEBUG_PRINT("Exiting main"); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#ifdef DEBUG #define DEBUG_PRINT(x) printf("debug :%sn", (x)); #else #define DEBUG_PRINT(x) #endif main() { #define DEBUG DEBUG_PRINT("In main"); int x; x=10; printf("%dn", x); DEBUG_PRINT("Exiting main"); } |
1 2 3 4 5 6 7 8 9 |
main() { int x; #if 0 x=10; #endif printf("%dn", x); DEBUG_PRINT("Exiting main"); } |
68)What is the output of the following program?
1 2 3 4 5 |
main() { int xyz = 0xA0A0A0A0; printf("0x%X", xyz >> 1); } |
1 2 3 4 5 |
main() { unsigned int xyz = 0; printf("0x%Xn",--xyz); } |
a)-0
1 2 3 4 5 6 |
main() { int number = 0xdeadbeef; int mask = 0x7ff0; printf("0x%Xn", (number & mask) >> 16); } |
a)0
1 2 3 4 5 6 7 8 9 10 11 12 13 |
main() { int x; scanf("%d", &x); if (x & 3) { printf("Non"); } else { printf("Yesn"); } } |
1 2 3 4 5 6 |
main() { int number = 0xdeadbeef; int mask = 0x7ff0; printf("0x%Xn", (number & mask) << 16); } |
a)0
1 2 3 4 5 6 7 8 |
typedef struct { int ctrl1; char flag; int ctrl2; char flag2; int ctrl3; } MYREG; |
a)14
1 2 3 4 5 6 7 8 |
typedef struct { int ctrl1; char flag; char flag2; int ctrl2; int ctrl3; } MYREG; |
a)14
1 2 3 4 |
int foo(int a) { return !(a & (a - 1)) && a; } |
a)Returns non zero when a is odd
1 2 3 4 5 6 7 8 9 |
int foo(int a) { int c; for (c = 0; a; c++) { a &= a - 1; } return c; } |
a)Number of bits set in variable a
1 2 3 4 5 6 7 8 9 |
void main() { int x, y,z; z = (x=0xaaaa) && (y=0x5555); printf("logical AND result: %x;", z); x =0, y =0; z = (x=0xaaaa) & (y=0x5555); printf(" AND operator result:%x n", z); } |
a)logical AND result: 1; AND operator result: ffff
1 2 3 4 5 6 7 8 9 |
void main() { int x, y,z; z = (x=0xaaaa) || (y=0x5555); printf("logical OR result:%x;", z); x =0, y =0; z = (x=0xaaaa) | (y=0x5555); printf(" bitwise OR result:%x n", z); } |
incorrect results
1 2 3 4 5 6 |
#define MASK 0xFFFFFFFF void bit_op(unsigned int p) { unsigned int i; i = MASK ^ ((p ^ MASK) + p); } |
Which of the following statements are true with respect to the above code?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
struct x { unsigned int a:2; unsigned int b:2; unsigned int c:1; unsigned int d:1; int e; }; int main() { struct x foo; memset(&foo, 0, sizeof(foo)); foo.a = 2; foo.b = 7; foo.c = 3; printf("a=%d, b=%d, c=%d, d=%dn", foo.a, foo.b, foo.c,foo.d); return 0; } |
bits are used for ‘b’ and one bit for ‘c’. Make sure that you are careful about using such code while writing portable applications.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
struct x { int a:2; int b:2; int c; int d:1; int e:1; }; int main() { printf("%dn", sizeof(struct x)); return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 |
struct record { char *name; int refcount:4; unsigned int active:1; }; int main() { printf("%dn", sizeof(struct record)); return 0; } |
a)5
will be packed into adjacent bits of the same unit. Here, since refcount uses only 4 bits of the int, active will be packed into the same int. So name uses 4 bytes (pointer to a char), and refcount and active together use up another 4
bytes.
1 2 3 4 |
int fn(int x) { return (x && ((x ^ (x - 1)) == ((2 * x) - 1))); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
struct st { char a[3]; short int b; long int c; char d[1]; }; int main() { printf("%dn", sizeof(struct st)); return 0; } |
3, then getbits() should return 3bits in positions 4, 3 and 2 (101″011″10), i. e., getbits(174, 4, 3) = 3.
1 2 3 4 5 |
#define BIT(n) (1 << (n)) void bit_op(unsigned int a, unsigned int n) { a |= BIT(n); } |
Which of the following statements is most accurate about the above code?
88)Consider the following piece of code (on a32-bit Intel/Linux system):
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#define BITS_IN_CHAR 8 int fn1(int a) { int i; i = -(a < 0); return i; } int fn2(int a) { int i; i = a >> (sizeof(int) * BITS_IN_CHAR - 1); return i; } |
fn1() will avoid branching on CPUs with flag registers.
fn2() will avoid branching on CPUs with flag registers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
struct mystruct { char a; int b; short c; char d; }; int main() { struct mystruct foo[3]; char ch; int i; printf("%p %p %p %p %pn", &foo[0], &foo[1], &foo[2], &ch, &i); return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
/* Assume a > 0 */ unsigned long bit_op(unsigned long a) { int n = 0; if ((a & 0xffff) == 0) { n += 16; a >>= 16; } if ((a & 0xff) == 0) { n += 8; a >>= 8; } if ((a & 0xf) == 0) { n += 4; a >>= 4; } if ((a & 0x3) == 0) { n += 2; a >>= 2; } if ((a & 0x1) == 0) n += 1; return n; } |
Which of the following statements is mostaccurate about the above code?
1 2 3 4 5 6 7 8 9 10 11 12 13 |
# pragma pack (1) struct x { char a; int b; short c; }; # pragma pack () int main() { printf("%dn", sizeof(struct x)); return 0; } |
What is the output of the above code?
#pragma pack() is used to get back to default alignment. Since struct x is defined within #pragma pack(1) directive, it will align to 1 byte boundary. No padding will be done between the members a, b or c of the structure. Hence a
will take up 1 byte, b will take up 4 bytes and c will take up 2 bytes.
1 2 3 4 5 |
main() { char **p = 0; printf("%dn", ++p); } |
a)4
1 2 3 4 5 6 7 |
main() { int x, y; int *p1 = &x; int *p2 = &y; printf("%dn", p1-p2); } |
a)4
1 2 3 4 5 6 7 |
main() { int x, y ; int *p1 = &x; int *p2 = &y; printf("%dn", (int)p1-(int)p2); } |
a)4
p1 is pushed earlier than p2, it will have higher memory address than p2. Since the pointers are cast to integer, by subtracting them, they just behave like normal numbers and hence output would be 4.
1 2 3 4 5 6 7 |
main() { int x, y ; char *p1 = &x; char *p2 = &y; printf("%dn", p1-p2); } |
a)4
p1 is pushed earlier than p2, it will have higher memory address than p2. However, subtracting two pointers of the same type, will give the “number of elements” between them. Since sizeof char is 1 byte, output would be 4.
1 2 3 4 5 6 7 8 9 10 11 12 |
typedef struct { int day; int month; int year; } MYDATE; main() { MYDATE *dp = 0xB8000000; printf("0x%xn", &dp->year); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
typedef struct { int day; int month; int year; } MYDATE; int x = 0x10; int y = 0x20; int z = 0x30; int a = 0x40; int b = 0x50; int c = 0x60; int d = 0x70; MYDATE abc = { 1, 1, 2006 }; main() { MYDATE *dp = &x; ++dp; ++dp ; printf("%xn",dp->day); } |
dp->day will print 0x70.
1 2 3 4 5 6 7 |
main() { int xyz = 0xC0FFEE; unsigned char *p = &xyz; ++p; printf("0x%Xn",*p); } |
a)0xC0
1 2 3 4 5 6 |
main() { int x = 10; int *p = &x; printf("%d %d %dn", x, *(&x), **(&p)); } |
a)Syntax Error
1 2 3 4 5 6 7 8 |
main() { int x = 10; int y = 20 ; int *p = &y ; &x = p ; printf("%d %d %dn", x, y, *p); } |
a)Syntax Error
1 2 3 4 5 6 7 8 9 |
main() { int x = 10 ; int y = 20 ; int *p = &x ; int **pp=&p ; *pp = &y ; printf("%d %dn", *p, **pp); } |
a)Syntax Error or Runtime Error
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
typedef struct { int day; int month; int year; } MYDATE; MYDATE abc = {11, 9, 2006}; main() { MYDATE *dp = &abc; dp = (MYDATE *)((char *)dp + 8); printf("%dn", dp->day); } |
a)Syntax Error or Runtime Error
1 2 3 4 5 6 7 8 |
char *def[5] = {"pqrs","rstu", "tuvw", "vwxyz", "xyzab" }; char abc[5][5] = {"abc","def", "ghi", "jkl", "mno"}; main() { char *p =(char *)def; p = p + 40; printf("%sn", p); } |
abc. i.e it just crosses a[3] and points to a[4]. Hence the above program would print “mno” which is the last element of abc.
1 2 3 4 5 6 7 8 9 10 11 |
f1(char *p) { char abc[6]="Hello"; p = abc; } main() { char *p="World"; f1(p); printf("%sn", p); } |
106)What is the output of the following program?
1 2 3 4 5 6 7 8 9 10 11 12 |
char *f1() { char abc[6]="Hello"; char *p = abc; return abc; } main() { char *p; p = f1(); printf("%sn", p); } |
1 2 3 4 5 6 7 8 9 10 11 |
char *f1() { char *abc ="Hello"; return abc; } main() { char *p; p = f1(); printf("%sn", p); } |
kept in the constant data segment and the address of it would get assigned to abc. The same is being returned by the function.
1 2 3 4 5 6 7 |
main() { char *p; char buff[10] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}; p = (buff + 2)[5]; printf("%c" , p); } |
1 2 3 4 5 6 7 |
main() { int x[][3] = {1, 2, 3, 4, 5, 6}; int (*ptr)[3] = x; printf("%d ", sizeof(ptr)); printf("%dn", sizeof(x)); } |
a 2-d array containing 6 integers. Hence its size is 6*4 = 24.
1 2 3 4 5 6 7 8 |
main() { int x[][4] = {1, 2, 3, 4, 5, 6, 7, 8}; int (*ptr)[4] = x; printf("%d %d ", (*ptr)[1], (*ptr)[2]); ++ptr; printf("%d %dn", (*ptr)[1], (*ptr)[2]); } |
gets expanded to (*((*ptr)+1)). So ((*ptr)+1) would point to address of 2. Since ptr is a pointer to an array of 4 integers, ptr++ would make it move by 4 integers. This is nothing but address of 6.
1 2 3 4 5 6 7 8 |
main() { int i; char str[80] = "I am feeling good today"; for(i=0; i <strlen(str); i++) printf("%c", i[str]); printf("n"); } |
1 2 3 4 5 6 7 8 |
main() { int StatArr[2]; int *DynArr; DynArr = (int*)malloc(2*sizeof(int)); printf("sizeof(StatArr) : %d, sizeof(DynArr): %dn", sizeof(StatArr), sizeof(DynArr)); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
int i = 10; int f1(int(*p)()) { --i; if (i > 0) { int j; j = p(p); return(j); } return 1; } main() { printf("%dn", f1(f1)); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
int f1(int); int f2(int); int (*fp[2])(int) = { f1, f2 }; int f1(int x) { return(x+10); } int f2(int x) { return(x+20); } main() { int sum = 0; int (*fp1)(int); fp1 = fp[0]; ++fp1; sum = fp1(sum); printf("%dn",sum); } |
different. Since fp1 is moved only by one byte, the control would go to a wrong entry point and the program willl
have run time error.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
int f1(int); int f2(int); int (*fp[2])(int) = { f1, f2 }; int f1(int x) { return(x+10); } int f2(int x) { return(x+20); } main() { int sum = 0; int (**fp1)(int); fp1 = fp; ++fp1; sum = (**fp1)(sum); printf("%dn",sum); } |
1 2 3 4 5 6 7 |
main() { int i=15; char *str; strcpy(str, "Hello, World!"); printf("i=%dn", i--); } |
into it will corrupt the stack
1 2 3 4 5 6 7 |
#include <stdio.h> int i=0; main() { int i=5; printf("%dn", i++); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <stdio.h> int i=0; main() { printf("%d ", i); { int i=2; printf("%d ", i); { i++; printf("%d ", i); } printf("%d ", i); } printf("%dn", i); } |
1 2 3 4 5 6 7 8 9 10 11 |
int i=5; int f1(int i) { i=i/2; return i; } main() { int i=10; printf("%d, %dn", f1(i), i); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
int i=10; int f1() { static int i = 15; printf("f1:%d ", i); return i--; } main() { int i, j; i = 5; printf("%d %d %d", f1(), f1(), i); } |
is the call made from the 3rd parameter of printf, During this call, 15 is printed, and the static variable i gets decremented to 14. Again, f1 is called and this time, 14 gets printed. 14 is also returned. In the sequence in which
values are returned, the printf statement prints them. Hence the output.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
File a.c contains: static int i=12; forward(void) { return (i+=1); } backward(void) { return (i-=1); } f1(int i) { static int j=2; return(i=j+=i); } File b.c contains: extern int i; int restore() { return i; } Another file c.c contains: #include <stdio.h> int i=5; main() { int i, j; i=restore(); printf("%d ", forward()); printf("%d ", backward()); printf("%d ", f1(i)); } |
value of i in main(). The scope of i in a.c is that of the static variable with visibility in the file a.c. In file b.c the global variable i defined in a.c holds. Within the function main() the scope of i is that of the on-stack local variable. With this in mind, when forward() is called, the static value of i(initialized to 12) in a.c gets incremented and printed. Then, when backward() is called the same gets decremented. When f1() is called, 2 gets added to the value of i which was assigned 5 during call to restore.
1 2 3 4 5 6 7 8 9 10 11 12 |
File a.c contains int f1(int x, int y) { printf("%d %d", x, y); return x+y; } File b.c contains extern int f1(); main() { printf(" %dn", f1(10, 20, 3)); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
void Recurse(int x) { int y = 10; printf("%d %pn", y, &y); if (x == 0) { ++y; Recurse(1); } } main() { Recurse(0); } |
Its value is being printed each time it is assigned the value 10. Therefore, the value of y printed will always be 10, but its address will vary for each call to the function.
1 2 3 4 5 6 7 8 9 10 |
int func(int x) { char a[10]; strcpy(a, "Hello, This is indeed a very big world"); return (x+1); } main() { printf("%dn", func(1)); } |
1 2 3 4 5 6 7 8 9 10 11 12 |
int * func(int *xp) { int y = 10 + *xp; return (&y); } main() { int x = 10; int *xp = func(&x); printf("%dn", x); printf("%dn", *xp); } |
1 2 3 4 5 6 7 8 9 10 |
void func(int a[10]) { a[0] = 1; } main() { int a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; func(a); printf("%dn", a[0]); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <stdarg.h> int func(int x, ...) { char y; int z; va_list ap; va_start(ap, x); y = (char)va_arg(ap, int); z = va_arg(ap, int); printf("%c 0x%xn", y, z); va_end(ap); } main() { func(0x10, 0x32, 0x31); } |
character and print 2. The next argument is interpreted as an integer and hence its hex value is printed. The cast to char is necessary since va_arg is capable of interpreting only fully promoted types.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <stdarg.h> int foo(char *fmt, ...) { va_list ap; int d; int count=0; char c, *p, *s; va_start(ap, fmt); while (*fmt) switch(*fmt++) { case '%': if((*fmt) && (*(fmt) != '%')) count++; else if (*fmt) fmt++; break; } va_end(ap); return count; } |
1 2 3 4 5 |
main() { char *s = "Hello Worldn"; printf("%s", s); } |
1 2 3 4 5 6 |
main() { char *s = (char *) malloc(sizeof("Hello Worldn")); strcpy(s, "Hello Worldn"); printf("%s", s); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
main() { char *p = (char *)malloc(sizeof ("Hello Worldn")); if (p) { strcpy(p,"Hello World"); printf("%s", p); } p= (char *)malloc(sizeof("Good Morningn")); if(p) { strcpy("p, "Good Morning"); printf("%s", p); } if (p) free(p); } |
1 2 3 4 5 6 7 8 9 |
main() { char *p = (char *)malloc(strlen("Hello Worldn")); if (p) { strcpy(p, "Hello Worldn"); free(p); } } |
necessary to be very discplined during coding.
prepare it for running. Hence it needs to understand the OS specifics to enable running of the program.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
main() { char *p = (char *)malloc(1024); if (p) { strcpy(p,"Hellon"); p += 10; strcpy(p,"Worldn"); p += 10; strcpy(p,"That is alln"); free (p); } } |
1 2 3 4 5 6 7 |
main(int argc, char *argv[]) { int flag; char filename[10]; strcpy(filename, argv[1]); printf("%s", filename); } |
and the subsequent behaviour of the program would be unpredictable.
1 2 3 4 5 6 7 |
main(int argc, char *argv[]) { int flag; char filename[10]; strcpy(filename, argv[1]); printf("%s", filename); } |
1 2 3 4 5 6 7 8 9 10 11 |
main() { struct { int arr[4]; int key; } name_key; int i; name_key.key = 0; for (i=0; i<=4; i++) name_key.arr[i] = i+5; } |
141)Which of the following statements is true ?
overwritten when another variable is being written to.
Permalink
Thanks for posting all the concepts at one place …
Good Job !!!
Permalink
Thank you Rajesh!!
Please suggest if anything needs to be added
Permalink
Please give the explanation of the ans