C Puzzles : Questions and Answers - Level 2
Questions
#includemain() { 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); }
#includevoid 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); }
#includemain() { 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); }
#includemain() { 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); } }
#includemain() { 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)); }
#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); }
#includemain() { 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); }
Write a program to calculate number of 1's (bit) in a given integer number i.e) Number of 1's in the given integer's equivalent binary representation.
Answers
The output will be : 0 10 11 20 21 *s++ => *(s++) *++s => *(++s) ++*s => ++(*s)
Some compiler (ansi) may give warning message, but it will compile without errors. The output will be : 0 1 2 3 4 and -1
This is the problem about Unions. Unions are similar to structures but it differs in some ways. Unions can be assigned only with one field at any time. In this case, unions x and y can be assigned with any of the one field a or b or c at one time. During initialisation of unions it takes the value (whatever assigned ) only for the first field. So, The statement y = {100} intialises the union y with field a = 100. In this example, all fields of union x are assigned with some values. But at any time only one of the union field can be assigned. So, for the union x the field c is assigned as 21.50. Thus, The output will be Union 2 : 22 22 21.50 Union Y : 100 22 22 ( 22 refers unpredictable results )
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
The output of this program is purely depends on the processor architecuture. If the sizeof integer is 4 bytes and the size of character is 1 byte (In some computers), the output 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 padding bytes are added in between the sturcture fields. In this example 3 bytes are padded between ' char ch' and 'int d' fields. The unused bytes are called holes. To understand more about padding bytes (holes) try varing the field types of the structures and see the output.
Just try to execute this file as such. You can find out that it will exit immediately. Do you know why? With this hint, we can trace out the error. If you look into the macro 'Error', you can easily identify that there are two separete statements without brases '{ ..}'. That is the problem. So, it exits after the calling open(). The macro should be put inside the brases like this. #define Error(str) { printf("Error : %s\n", str); exit(1); }
This program will fault (Memory fault/segmentation fault). Can you predict Why? Remove the statment 'free(a);' from the program, then execute the program. It will run. It gives the results correctly. 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'. Out side the 'for loop' the variable 'a' will point to 'null'. When the free() call is made, it will free the data area from the base_address (which is passed as the argument of the free call) upto the length of the data allocated previously. In this case, free() tries to free the length of 10 *sizeof(int) from the base pointer location passed as the argument to the free call, which is 'null' in this case. Thus, it generates memory fault.
#includemain(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<
Goto C Puzzle
level 1 level 3
For any further clarifications/ informations and to contribute some more puzzles for this page please feel free to contact Achutha Raman
About me Publications C puzzles Home Form Music library Picture library