in Programming edited by
178 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
in Programming edited by
by
178 views

2 Answers

1 vote
1 vote
Best answer

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

4 Comments

sir how null and space are represented?
0
0
Space by 00010000 and NULL by 00000000 in binary assuming ASCII representation which is decided by the environment.
1
1

what will be the output if : 

printf("%s",*ptr);
0
0
1 vote
1 vote

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