Questions of Decode C
Questions of Decode C
Questions of Decode C
1> What will print it out? main() { char *p1=name; char *p2; p2=(char*)malloc(20); memset (p2, 0, 20); while(*p2++ = *p1++); printf(%s\n,p2); } Answer: empty string. Explanation: The pointer p2 value is also increasing with p1 . *p2++ = *p1++ means copy value of *p1 to *p2 , then increment both addresses (p1,p2) by one , so that they can point to next address . So when the loop exits (i.e. when address p1 reaches next character to name i.e. null) p2 address also points to next location to name . When we try to print string with p2 as starting address , it will try to print string from location after name . Hence it is null string. eg : initially p1 = 2000 (address) , p2 = 3000 *p1 has value n ..after 4 increments , loop exits at that time p1 value will be 2004 , p2 =3004 the actual result is stored in 3000 - n , 3001 - a , 3002 - m , 3003 -e we r trying to print from 3004 . where no data is present thats why its printing null . 2> Differentiate between a linker and linkage? Answer: A linker converts an object code into an executable code by linking together the necessary build in functions. The form and place of declaration where the variable is declared in a program determine the linkage of variable. 3> Write the equivalent expression for x%8? Answer: x&7 4> Why n++ executes faster than n+1? Answer: The expression n++ requires a single machine instruction such as INR to carry out the increment operation whereas; n+1 requires more instructions to carry out this operation. 5> What is #line used for? Answer: The #line preprocessor directive is used to reset the values of the _ _LINE_ _ and _ _FILE_ _ symbols, respectively. This directive is commonly used in fourth-generation languages that generate C language source files.
6> Write the Output. main() { if (!(1&&0)) { printf("OK I am done."); } else { printf("OK I am gone."); }} Answer : OK I am done 7> What will be the output of the given program? main() { extern int i; i=20; printf("%d",i); } Answer: Linker Error : Undefined symbol '_i' Explanation: extern storage class in the following declaration, extern int i; specifies to the compiler that the memory for i is allocated in some other program and that address will be given to the current program at the time of linking. But linker finds that no other variable of name i is available in any other program with memory space allocated for it. Hence a linker error has occurred . 8> Write down the equivalent pointer expression for referring the same element a[i][j][k][l]? Answer: a[i] == *(a+i) a[i][j] == *(*(a+i)+j) a[i][j][k] == *(*(*(a+i)+j)+k) a[i][j][k][l] == *(*(*(*(a+i)+j)+k)+l) 9> Write down the Output. main() { char a[4]="HELL"; printf("%s",a); }
Answer: HELL%@!~@!@???@~~! Explanation: The character array has the memory just enough to hold the string HELL and doesnt have enough space to store the terminating null character. So it prints the HELL correctly and continues to printgarbage values till it accidentally comes across a NULL character. 10>Is there any difference between the two declarations, 1. int foo(int *arr[]) and 2. int foo(int *arr[2]) Answer: No Explanation: Functions can only pass pointers and not arrays. The numbers that are allowed inside the [] is just for more readability. So there is no difference between the two declarations. 11> Write down the Output. main() { int c[ ]={2.8,3.4,4,6.7,5}; int j,*p=c,*q=c; for(j=0;j<5;j++) { printf(" %d ",*c); ++q; } for(j=0;j<5;j++){ printf(" %d ",*p); ++p; } } Answer: 2 2 2 2 2 2 3 4 6 5 Explanation: Initially pointer c is assigned to both p and q. In the first loop, since only q is incremented and not c , the value 2 will be printed 5 times. In second loop p itself is incremented. So the values 2 3 4 6 5 will be printed 12> Write down the Output. main() { unsigned int i=10; while(i-->=0) printf("%u ",i); } Answer: 10 9 8 7 6 5 4 3 2 1 0 65535 65534..
Explanation: Since i is an unsigned integer it can never become negative. So the expression i-- >=0 will always be true, leading to an infinite loop 13> Write down the Output. main() { int i=10; void pascal f(int,int,int); f(i++,i++,i++); printf(" %d",i); } void pascal f(integer :i,integer:j,integer :k) { write(i,j,k); } Answer: Compiler error: unknown type integer Compiler error: Undeclared functions write Explanation: Pascal keyword doesnt mean that Pascal code can be used. It means that the function follows Pascal argument passing mechanism in calling the functions. 14> Find the output for the following C program #define swap1(a,b) a=a+b;b=a-b;a=a-b; main() { int x=5,y=10; swap1(x,y); printf("%d %d\n",x,y); swap2(x,y); printf("%d %d\n",x,y); } int swap2(int a,int b) { int temp; temp=a; b=a; a=temp; return; } Answer: 10 5 15> What are the different storage classes in C?
Answer: C has three types of storage: automatic, static and allocated. Variable having block scope and without static specifier have automatic storage duration. Variables with block scope, and with static specifier have static scope. Global variables (i.e, file scope) with or without the the static specifier also have static scope. Memory obtained from calls to malloc(), alloc() or realloc() belongs to allocated storage class. 16> Can a variable be both const and volatile? Answer: Yes. The const modifier means that this code cannot change the value of the variable, but that does not mean that the value cannot be changed by means outside this code. For instance, in the example in FAQ 8, the timer structure was accessed through a volatile const pointer. The function itself did not change the value of the timer, so it was declared const. However, the value was changed by hardware on the computer, so it was declared volatile. If a variable is both const and volatile, the two modifiers can appear in either order. 17> What is the difference between a string copy (strcpy) and a memory copy (memcpy)? When should each be used? Answer: The strcpy() function is designed to work exclusively with strings. It copies each byte of the source string to the destination string and stops when the terminating null character () has been moved. On the other hand, the memcpy() function is designed to work with any type of data. Because not all data ends with a null character, you must provide the memcpy() function with the number of bytes you want to copy from the source to the destination. 18> How can you restore a redirected standard stream? Answer: The preceding example showed how you can redirect a standard stream from within your program. But what if later in your program you wanted to restore the standard stream to its original state? By using the standard C library functions named dup() and fdopen(), you can restore a standard stream such as stdout to its original state. The dup() function duplicates a file handle. You can use the dup() function to save the file handle corresponding to the stdout standard stream. The fdopen() function opens a stream that has been duplicated with the dup() function. 19> Can the sizeof operator be used to tell the size of an array passed to a function? Explain. Answer: No. Theres no way to tell, at runtime, how many elements are in an array parameter just by looking at the array parameter itself. Remember, passing an array to a function is exactly the same as passing a pointer to the first element. 20> Can math operations be performed on a void pointer? Explain. Answer: No. Pointer addition and subtraction are based on advancing the pointer by a number of elements. By definition, if you have a void pointer, you dont know what its
pointing to, so you dont know the size of what its pointing to. If you want pointer arithmetic to work on raw addresses, use character pointers.
Choose the Correct Answer:21> Choose the Output. const int perplexed = 2; #define perplexed 3 main() { #ifdef perplexed #undef perplexed #define perplexed 4 #endif printf("%d",perplexed); }
a> 2
b> 4
c> 3
Answer : 2 Explanation: as that value perplexed is const variable and a const cannot be changed 22> The output for the following C program ismain() { printf("%x",-1<<4); } a> fff0 b> 4 Answer: fff0 Explanation : -1 is internally represented as all 1's. When left shifted four times the least significant 4 bits are filled with 0's.The %x format specifier specifies that the integer value be printed as a hexadecimal value. 23> The output for the following C program ismain() { printf("\nab"); printf("\bsi"); printf("\rha"); }
a> absiha
b> ab
c> hai
d> ab siha
Answer: hai Explanation: \n - newline \b - backspace \r linefeed 24> The output for the following C program isenum colors {BLACK,BLUE,GREEN} main() { printf("%d..%d..%d",BLACK,BLUE,GREEN); return(1); }
a> BLACK..BLUE..GREEN
Answer: 0..1..2 Explanation: enum assigns numbers starting from 0, if not explicitly defined. 25> Write down the Output. main() { int i=300; char *ptr = &i; *++ptr=2; printf("%d",i); }
a> 11266 b> 11265 c> 300 d> 556
Answer: 556 Explanation: The integer value 300 in binary notation is: 00000001 00101100. It is stored in memory as: 00101100 00000001. Result of the expression *++ptr = 2 makes the memory representation as: 00101100 00000010. So the integer corresponding to it is 00000010 00101100 => 556.