What will be output of following c code? void main() { struct bitfield { unsigned a:5; unsigned c:5; unsigned b:6; }bit; char *p; struct bitfield *ptr,bit1={1,3,3}; p=&bit1; p++; clrscr(); printf("%d",*p); getch(); }
Correct Answer
Output: 12 Explanation: Binary value of a=1 is 00001 (in 5 bit) Binary value of b=3 is 00011 (in 5 bit) Binary value of c=3 is 000011 (in 6 bit) In memory it is represented as: Let address of bit1 is 500 which initialize to char pointer p Since can is one byte data type so p++ will be 501 *p means content of memory location 501 which is (00001100) and its binary equivalent is 12 Hence output is 12
Programming problems
Search Results
1. What will be output of following c code? #include int main() { int i; for(i=10;i<=15;i++){ while(i){ do{ printf("%d ",1); if(i>1) continue; }while(0); break; } } return 0; }
Correct Answer: Output: 1 1 1 1 1 1 For loop will execute six times Note: continue keyword in do-while loop bring the program its while condition (while(0)) which is always false
2. How would you use qsort() function to sort an array of structures?
Correct Answer: When a function accepts a variable number of arguments , its prototype cannot provide any information about the number of arguments and type of those variable arguments Hence the compiler cannot warn about the mismatches The programmer must make sure that arguments match or must manually insert explicit typecast
4. What is the difference between malloc() and calloc() functions?
Correct Answer: As against malloc(), calloc() needs two arguments, the number of elements to be allocated and the size of each element For example, p = (int *) calloc (10, sizeof (int)); would allocate space for a 10- integer array Additionally, calloc() would also set each of this element with a value 0 Thus the above call to calloc() is equivalent to: p = (int *) malloc (10 * sizeof (int)); memset (p, 0, 10 * sizeof( int ));
5. Can I increase the size of a dynamically allocated array? < Yes / No> if yes, how?
Correct Answer: Yes, using the realloc() function as shown below: main() { int *p; p = ( int *) malloc (20) ; t = p; t = (int *) realloc ( p, 40); if ( t == NULL ) Printf (" Cannot reallocate, leaves previous allocated region unchanged "); else { if ( p ==t ) ; / * the array expanded at the same region */ else { free ( p ); / * deallocate the original array */ p = t; /* set p to newly allocated region */ } } }
6. What will be output when you will execute following c code? #include enum actor { SeanPenn=5, AlPacino=-2, GaryOldman, EdNorton }; void main() { enum actor a=0; switch(a) { case SeanPenn: printf("Kevin Spacey"); break; case AlPacino: printf("Paul Giamatti"); break; case GaryOldman:printf("Donald Shuterland"); break; case EdNorton: printf("Johnny Depp"); } }
Default value of enum constant GaryOldman = -2 +1 = -1 And default value of enum constant EdNorton = -1 + 1 = 0 Note: Case expression can be enum constant.
Correct Answer: #include int main( int count,char * argv[] ) { int i; FILE *ptr; char *str; char ch; if( count == 1) { printf( "The syntax of the command is incorrect\n" ); } for( i=1;i2) printf("\nError occurred while procesing : %s\n",argv[i]); } else { if(count>2) { printf("%s\n\n",argv[i]); } while((ch=getc(ptr))!=-1) printf("%c",ch); } fclose(ptr); } return 0; } Save the above file as openc, compile and execute the go to command mode (current working directory) and write: open xyc (xyc any file present in that directory) To run the open command in all directories and drive you will have to give the path of current working directory in command mode Write: C:tc\bin>PATH c:\tc\bin Now press enter key Now your open command will work in all directory and drive
Correct Answer: It is also called as post tested loop It is used when it is necessary to execute the loop at least one time Syntax: do { Loop body } while (Expression); Example: int main(){ int num,i=0; do{ printf("To enter press 1\n"); printf("To exit press 2"); scanf("%d",&num); ++i; switch(num){ case 1:printf("You are welcome\n");break; default : exit(0); } } while(i<=10); return 0; } Output: 3 3 4 4
9. Swap two variables without using third variable.
Correct Answer: #include int main(){ int k,r; long int i=0l,j=1,f; //Taking maximum numbers form user printf("Enter the number range:"); scanf("%d",&r); printf("FIBONACCI SERIES: "); printf("%ld %ld",i,j); //printing firts two values for(k=2;k