#include<stdio.h>
void main()
{
printf(5+"good morning");/*need explanation for this line
return 0;
}
The output of the program is - morning can anyone explain how?
#include<stdio.h>
void main()
{
printf(5+"good morning");/*need explanation for this line
return 0;
}
The output of the program is - morning can anyone explain how?
Prototype of printf
int printf(const char *format, ...);
Here format
is a type of const char*
and point to address of first element of string literal. When you pass 5+"Good morning"
in printf
, what you are really passing is the memory address of the string plus 5
. The plus 5
means printing will start 5
chars
beyond the start of the string, and the space after the word "Good",counts as a char.
when you call with 5+"good morning"
parameter is converted to pointer. That means there is string constant "good morning"
stored somewhere in the executable and compiler pass its pointer. something like this:
const char txt[]="good morning\0";
printf(5+txt);
So the printf
will obtain the evaluated pointer txt+5
which bypassed first 5 characters in the string (as one char is single BYTE and single memory address on 8bit WORD addressing machines).
The output of the program is - morning
printf(5+"good morning");
prints the string inside the " "
, overpassing the first five characters. So the first four characters g
, o
, o
, d
and the fifth character, the space, will be overpassed and the rest of the string will be printed.
Printf()
method, is used to print the text in ()
It prints only "morning" and 5+ bypassed the initial 5 characters that is "g" "o" "o" "d" and a " " (space)