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;
}
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.
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;
}
Here is my coding;
#include<stdio.h>
int main()
{
char str[]="Hello World";
printf("%s"5+str);
return 0;
}
(1)
Ravi said:
1 decade ago
Why is it so? that it leaves the first 5 characters and takes tha last characters?
Sanjeev said:
1 decade ago
5+ means what?
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;
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?
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?
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.
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.
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.
char str[] = "Hello World";
printf( 5 + "%s", str);
Output should be "World" as after space characters are present.
Pragya said:
1 decade ago
It works with:
#include<stdio.h>
int main()
{
char str[]="Hello World";
printf(5+str);
return 0;
}
Output:
World.
#include<stdio.h>
int main()
{
char str[]="Hello World";
printf(5+str);
return 0;
}
Output:
World.
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.
Good+space is there so, total 5, hence only morning is print.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers