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

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":>%>);. Commented Oct 6, 2017 at 6:56

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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.

Comments

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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.