C Programming - Strings - Discussion

Discussion Forum : Strings - Find Output of Program (Q.No. 4)
4.
What will be the output of the program ?
#include<stdio.h>

int main()
{
    printf(5+"Good Morning\n");
    return 0;
}
Good Morning
Good
M
Morning
Answer: Option
Explanation:

printf(5+"Good Morning\n"); It skips the 5 characters and prints the given string.

Hence the output is "Morning"

Discussion:
12 comments Page 1 of 2.

Reshma S Shivalli said:   1 decade ago
Because 5+ acts as a pointer pointing to 6th element in the printf statement, as their are no characters at 5th and above place you will not get any output.

char str[]="HELLO WORLD";
printf(5+"aaaa %s",str);

Now the output will be HELLO WORLD
As the first five elements will be discarded.

Try with 1+, 2+...
You will get a clear idea.

Himanshu said:   1 decade ago
But say I want to take the string from the user, then this is not working. For example,

char str[] = "Hello World";
printf( 5 + "%s", str);

The output should be: "World" but Turbo C is not showing up anything on the output window. why?

SANCHITA said:   1 decade ago
But for above example,
char str[] = "Hello World";
printf( 5 + "%s", str);

Output should be "World" as after space characters are present.

Harsh said:   1 decade ago
"Good Morning\n" returns the base address of this string and hence 5+ increments the address, eg: char* a="Good Morning\n"; a=a+5;

Pragya said:   1 decade ago
It works with:

#include<stdio.h>
int main()
{
char str[]="Hello World";
printf(5+str);
return 0;
}

Output:
World.

Ketan kumhar said:   4 years ago
@All.

Here is my coding;

#include<stdio.h>
int main()
{
char str[]="Hello World";
printf("%s"5+str);
return 0;
}
(1)

Deepak said:   4 years ago
After skipping a+5=0+5=5 It returns the pointer as 6...And 6th letter is 'M'.
Note: Space should be count.
4+1(space).

Ravi said:   1 decade ago
Why is it so? that it leaves the first 5 characters and takes tha last characters?

Ashu khedekar said:   8 years ago
But the size of word "good" is four so how can get only morning output?

Dhananjay Moundekar said:   8 years ago
@Ashu.

Good+space is there so, total 5, hence only morning is print.


Post your comments here:

Your comments will be displayed after verification.