0
#include <stdio.h> int main (void) { char *str = "Hello"; //defining and initializing the str pointer, which is directing to 'H'. printf("%s\n", str); printf("%p\n", str); return 0; } 

The Result is:

Hello 0000000000404000 

My question is where did 0000000000404000 come from?

9
  • 1
    no newline in the output? You should do printf(%p\n", (void*)str); instead, %p expects a void pointer. Commented Dec 14, 2016 at 23:46
  • 3
    That's just where the string happened to end up in the address space. It's completely arbitrary and not something you should rely on. Commented Dec 14, 2016 at 23:46
  • 1
    You have two printfs. What did you expect the second printf to do? Commented Dec 14, 2016 at 23:47
  • 1
    Please explain in the question what output you expected instead Commented Dec 14, 2016 at 23:48
  • 2
    It happens to be the address where "Hello" is stored in memory. It must be stored somewhere – so why not there? Commented Dec 14, 2016 at 23:49

2 Answers 2

2

The format specifier %p will print the address contained within the char* str variable whereas the %s specifier will print the actual string literal Hello. The address in memory 0x00000000 00404000 is where Hello resides.

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

2 Comments

Thank you!! I finally understood what it is.
@ChrisLee if you understand from an answer you should accept it and vote for it
0

In Bjarne Stroustrup words:

%s The argument is taken to be a string (character pointer), and characters from the string are printed until a null character or until the number of characters indicated by the precision specification is reached; however, if the precision is 0 or missing, all characters up to a null are printed;

%p The argument is taken to be a pointer. The representation printed is implementation dependent;

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.