TransWikia.com

why '' is not pushed to stack to indicate the end of string?

Stack Overflow Asked by user11224591 on December 13, 2021

I’m new to C, sorry if my question sounds dumb.
We know that for string like this below(from https://www.tutorialspoint.com/cprogramming/c_strings.htm:

char greeting[] = "Hello";

C will append special character in the memory to indicate the end of the string as picture below shows:
enter image description here

So I just did an experiment to prove is in memory, below is my code:

C code:

int main()
{
   char str[] = "dd";
   printf("%s", str);
   return 0;
}

assembly code:

//only show relevant code
main:
   subq $24, %rsp
   movw $25700, (%rsp)  //decimal 25700 is 0x6464 where hex 64(decimal 100) is 'd' in ascii
   movb $0, 2(%rsp)
   ...
   call printf
   ...

so you can see that the assembly code only push "dd" to stack, what about ? it doesn’t get pushed to stack? then how does C know the second ‘d’ is the end of the string?

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP