What would be the output of the following program ? main() { unsigned int a = oxffff; ~a; printf ("%x", a); }
Options
A. ffff
B. 0000
C. 00ff
D. None of the above
Correct Answer
ffff
Programming problems
Search Results
1. If the following program (myprog) is run from the command line as myprog 1 2 3 what would be the output? main(int argc, char *argv[]) { int i, j = 0; for (i = 0; i < argc ; i++) j = j + atoi ( argv[i]); printf ("%d", j); }
When atoi() tries to convert argv[0] to a number it cannot do so (argv[0] being a file name) and hence returns a zero.
2. What would be the output of the following program? /* sample.c */ main ( int argc, char **argv ) { argc = argc - (argc -1); printf ("%s", argv[argc - 1]); }
3. Point out the error, if any, in the following program. # include "stdio.h" main() { FILE *fp; char str[80]; fp = fopen ("trail", "r"); while (!feof (fp)) { fgets (str, 80, fp); puts (str); } fclose (fp); }
Correct Answer: struct emp { char n[20]; int age; }; main() { struct emp e1 = {"Dravid", 23}; struct emp e2; scanf ("%s %d",e2n, & e2age); if( structcmp (e1,e2) ==0) printf ("The structures are equal"); else printf ("The structures are unequal"); } structcmp ( struct emp x, struct emp y) { if (strcmp (xn,yn) ==0) if (xage == yage) return (0); return (1); } In short, if you nee to compare two structures, you'll have to write your own function to do so which carries out the comparison field by field
5. What would be the output of the following program? main() { struct emp { char *n; int age; }; struct emp e1 = { "Dravid", 23}; struct emp e2 = e1; strupr (e2.n); printf ("\n%s",e1.n); }
Correct Answer: DRAVID When a structure is assigned, passed, or returned, the copying is done monolithically This means that the copies of any pointer fields will point to the same place as the original In other words, anything pointed to is not copied Hence, on changing the name through e2n it automatically changed e1n
6. Answer the following Program #define CHARSIZE 8 #define MASK(y) (1 << y % CHARSIZE) #define BITSLOT (y) (y / CHARSIZE) #define SET(x,y) ( x[BITSLOT(y)] = MASK(y) ) #define TEST(x,y) ( x[BITSLOT(y)] & MASK(y) ) #define NUMSLOTS(n) ((n + CHARSIZE - 1) / CHARSIZE) Give the above macros how would you 1. declare an array arr of 50 bits 2. put the 20th bit on 3. test whether the 40th bit is on or off
Correct Answer: 1 char arr[NUMSLOTS(50)]; 2 SET (arr, 20); 3 if (TEST (arr, 40))
7. In the following code can we declare a new typedef name emp even though struct employee has not been completely defined while using typedef? < Yes / No> typedef struct employee *ptr; struct employee { char name[20]; int age; ptr next; };
Correct Answer: #include "alloch" #define MAXROW 3 #define MAXcol 4 main() { int *p, i, J; p = (int *) malloc (MAXROW * MAXCOL * sizeof (int)); for ( i=0; i < MaxROW ; i++) { for (j=0; j < MAXCOL ; j++) { p [ i * MAXCOL + j] = i; printf ( "%d", p [i * MAXCOL + j] ); } printf ("\n"); } }
10. How would you free the memory allocated by the following program? #include "alloc.h" #define MAXROW 3 #define MAXCOL 4 main() { int **p, i; p = (int **) malloc (MAXROW * sizeof (int *)); for ( i = 0; i < MAXROW ; i++) p[i] = (int *) malloc (MAXCOL * sizeof (int )); }