-3
#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?

Anonymous
  • 1
  • 1
  • Welcome to C, we have obfuscation here. Now try to explain what this does: `printf((char*restrict const const const const)??<&5??("good morning":>%>);`. – Lundin Oct 06 '17 at 06:56

4 Answers4

1

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.

msc
  • 33,420
  • 29
  • 119
  • 214
1

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).

Spektre
  • 49,595
  • 11
  • 110
  • 380
0

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.

Marievi
  • 4,951
  • 1
  • 16
  • 33
0

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)

iamsankalp89
  • 4,607
  • 2
  • 15
  • 36