Pages

Ads 468x60px

Monday, November 17, 2008

C Questions

Questions L2.Q1 : Write the output of this program #include

main(){ int *a, *s, i;

s = a = (int *) malloc( 4 * sizeof(int));

for (i=0; i<4; i++) *(a+i) = i * 10;

printf(”%d\n”, *s++); printf(”%d\n”, (*s)++); printf(”%d\n”, *s); printf(”%d\n”, *++s); printf(”%d\n”, ++*s);}


L2.Q2 : Checkout this program result #include

void fn(int);

static int val = 5;

main(){ while (val –) fn(val); printf(”%d\n”, val);}

void fn(int val){ static int val = 0;

for (; val < 5; val ++) printf(”%d\n”, val);}


L2.Q3 : Can you predict the output of this program ? #include

main(){ typedef union { int a; char b[10]; float c; }
Union;

Union x, y = { 100 };

x.a = 50; strcpy (x.b, “hello”); x.c = 21.50;

printf (”Union 2 : %d %s %f\n”, x.a, x.b, x.c); printf (”Union Y : %d %s %f\n”, y.a, y.b, y.c);}


L2.Q4 : Print the output of the program #include

main(){ struct Data { int a; int b; } y[4] = { 1, 10, 3, 30, 2, 20, 4, 40};

struct Data *x = y; int i;

for(i=0; i<4; i++) { x->a = x->b, ++x++->b; printf(”%d %d\t”, y[i].a, y[i].b); }}


L2.Q5 : Write the output of this program #include

main(){ typedef struct { int a; int b; int c; char ch; int d; }xyz;

typedef union { xyz X; char y[100]; }abc;

printf(”sizeof xyz = %d sizeof abc = %d\n”, sizeof(xyz), sizeof(abc));}


L2.Q6 : Find out the error in this code #include #include

#define Error(str) printf(”Error : %s\n”, str); exit(1);

main(){ int fd; char str[20] = “Hello! Test me”;

if ((fd = open(”xx”, O_CREAT | O_RDWR)) < 0) Error(”open failed”);

if (write(fd, str, strlen(str)) < 0) Error(”Write failed”); if (read(fd, str, strlen(str)) < 0) Error(”read failed”);

printf(”File read : %s\n”, str); close(fd);}


L2.Q7 : What will be the output of this program ? #include

main(){ int *a, i;

a = (int *) malloc(10*sizeof(int));

for (i=0; i<10; i++) *(a + i) = i * i; for (i=0; i<10; i++) printf(”%d\t”, *a++);

free(a);}


L2.Q8 : Write a program to calculate number of 1’s (bit) in a giveninteger number i.e) Number of 1’s in the given integer’sequivalent binary representation.


Answers L2.A1
Solution for L2.Q1 The output will be : 0 10 11 20 21

*s++ => *(s++)*++s => *(++s)++*s => ++(*s)


L2.A2
Solution for L2.Q2 Some compiler (ansi) may give warning message, but it will compile without errors.The output will be : 0 1 2 3 4 and -1


L2.A3
Solution for L2.Q3 This is the problem about Unions. Unions are similar tostructures but it differs in some ways. Unions can beassigned only with one field at any time. In this case,unions x and y can be assigned with any of the one field aor b or c at one time. During initialisation of unions ittakes the value (whatever assigned ) only for the firstfield. So, The statement y = {100} intialises the union ywith field a = 100.

In this example, all fields of union x are assigned withsome values. But at any time only one of the union fieldcan be assigned. So, for the union x the field c isassigned as 21.50.

Thus, The output will be Union 2 : 22 22 21.50 Union Y : 100 22 22 ( 22 refers unpredictable results )


L2.A4
Solution for L2.Q4 The pointer x points to the same location where y is stored.So, The changes in y reflects in x.

The output will be : 10 11 30 31 20 21 40 41


L2.A5
Solution for L2.Q5 The output of this program is purely depends on theprocessor architecuture. If the sizeof integer is 4 bytesand the size of character is 1 byte (In some computers), theoutput will be

sizeof xyz = 20 sizeof abc = 100

The output can be generalized to some extent as follows,

sizeof xyz = 4 * sizeof(int) + 1 * sizeof(char) + padding bytes sizeof abc = 100 * sizeof(char) + padding bytes

To keep the structures/unions byte aligned, some paddingbytes are added in between the sturcture fields. In thisexample 3 bytes are padded between ‘ char ch’ and ‘int d’fields. The unused bytes are called holes. To understandmore about padding bytes (holes) try varing the field typesof the structures and see the output.


L2.A6
Solution for L2.Q6 Just try to execute this file as such. You can find outthat it will exit immediately. Do you know why?

With this hint, we can trace out the error. If you lookinto the macro ‘Error’, you can easily identify that thereare two separete statements without brases ‘{ ..}’. That isthe problem. So, it exits after the calling open(). Themacro should be put inside the brases like this.

#define Error(str) { printf(”Error : %s\n”, str); exit(1); }


L2.A7
Solution for L2.Q7 This program will fault (Memory fault/segmentation fault).Can you predict Why?

Remove the statment ‘free(a);’ from the program, thenexecute the program. It will run. It gives the resultscorrectly.

What causes ‘free(a)’ to generate fault?

Just trace the address location of pointer variable ‘a’.The variable ‘a’ is incremented inside the ‘for loop’. Outside the ‘for loop’ the variable ‘a’ will point to ‘null’.When the free() call is made, it will free the data areafrom the base_address (which is passed as the argument ofthe free call) upto the length of the data allocatedpreviously. In this case, free() tries to free the lengthof 10 *sizeof(int) from the base pointer location passed asthe argument to the free call, which is ‘null’ in this case.Thus, it generates memory fault.


L2.A8
Solution for L2.Q8 #include

main(argc, argv)int argc;char *argv[];{ int count = 0, i; int v = atoi(argv[1]);

for(i=0; i<8*sizeof(int); i++) if(v &(1<< } count); v, %d=’%d\n”,’ in 1?s of printf(?No count++;>

0 comments: