edited by
440 views
2 votes
2 votes

What is the output of this program?

#include <stdio.h>
int main()
{
char *ptr;
char string[] = "How are you?";
ptr = string;
ptr += 4;
printf("%s",ptr);
return 0;
}
  1. How are you?
  2. are you?        
  3. are      
  4. error in program
edited by

2 Answers

Best answer
1 votes
1 votes

here 1) ptr = string;  : it will point to base address of string .

now  2) ptr += 4 ;  : it means ptr = ptr + 4*(size of char data type) = ptr + 4*(1) = it will point 4th index of char array (that is a) .

3)printf("%s",ptr);  :  %s will print the content of array untill it finds null. (that is "are you?") .

so ans would be B.

selected by
1 votes
1 votes

scanf() doesn't recognise spaces, but printf() can.

Be careful about differentiating space and NULL character, though.

A NULL character, if not explicitly specified, can only be at the end.

 

Here, are you? will be printed, not just are. printf() recognises spaces.

After are you? there's an implicit NULL character (\0). This is how printf() knows when to stop. So, it'll stop after printing are you?

 

Option B
Answer:

Related questions

3 votes
3 votes
1 answer
1
Bikram asked May 14, 2017
421 views
Assume that $a[4]$ is a one-dimensional array of $4$ elements, $p$ is a pointer variable and $p = a$ is performed. Now, which among these expressions is illegal?$p == a[0...
0 votes
0 votes
1 answer
2
Bikram asked May 14, 2017
366 views
Spot the error(s) in this code snippet :int n=2; // Line 1 switch(n) { case 1.5: printf( "gate"); break; case 2: printf( "overflow"); break; case 'A': printf("gateoverflo...
0 votes
0 votes
1 answer
3
Bikram asked May 14, 2017
221 views
#include<stdio.h int K = 10; int main() { foo(); foo(); return 0; } int foo() { static int k= 1; printf("%d ",k); k++; return 0; }What is the output of the above code sni...
0 votes
0 votes
1 answer
4
Bikram asked May 14, 2017
220 views
What is the output of the following program? int fun (int z) { if( z==0 || z==1 ) return 1; else return fun(z-1) ; } int main() { int y; y=fun(8); printf(“%d”...