0

I am trying to understand how variables are stored in the memory stack and what the printf statements are printing. Your insight in this will be highly appreciated. Thank you.

Ideone link http://ideone.com/uWvPpX

#include <stdio.h>

int main(void) {

    foo("%x");
    return 0;
}

void foo(char *str)
{

    char c='c';
    printf(str);

    printf("\n%x",&c);
}
  • 2
    This question can not be answered in a generic way, but depends on what platform (OS and hardware) you run the code, and what compiler and optimization you use. For x86 perhaps look at [this link](http://www.csee.umbc.edu/~chang/cs313.s02/stack.shtml) – Jens Aug 12 '15 at 18:51
  • 1
    The second one will print the address of `c`, or at least an integer's-worth of that address. The first one is undefined. Changing compilers, or even compiler options, can change the stack contents. – Paul Roub Aug 12 '15 at 18:51
  • 1
    Yeah. More rubbish code with UB 'cos printf specifiers don't match with arguments. – Martin James Aug 12 '15 at 18:54
  • 2
    @MartinJames of course it compiles; C isn't here to baby sit you, so if you want to shoot yourself in the foot, it'll let you. – Jeremy Rodi Aug 12 '15 at 21:33
  • @JeremyRodi Any compiler from this millenium would bury you in warnings though. If you enabled them, that is. – Quentin Aug 12 '15 at 21:36
  • @JeremyRodi - would it not need a forward declaration for foo()? – Martin James Aug 12 '15 at 22:06
  • @MartinJames To compile? [No](http://stackoverflow.com/q/2575153/1015722). In fact, [this compiles](https://eval.in/416033). The code in the question [also compiles](https://eval.in/416034). – Jeremy Rodi Aug 12 '15 at 22:31

2 Answers2

1

%x is a format specifier indicates you want to print in lower-case hexadecimal. So when you don't specify data in the first printf the result is undefined. Even though the code compiles - it is incomplete!

So let us fix the code first- here is the revised code

#include <stdio.h>

void foo(char *str);

int main(int argc, char* argv[])
{

    foo("%x");
    return 0;
}

void foo(char *str)
{

    char c='c';
    printf(str,c);

    printf("\n%x",&c);
}

Now to answer your questions "how variables are stored in the memory stack" The stack pointer registers the top of the stack,every time a value is pushed on to or popped out of the stack - the stack pointer is adjusted to point to the free memory. And then there is the stack frame, it corresponds to a call to the function which has not yet terminated with a return. enter image description here

the other part was "what the printf statements are printing." The first parameter in the printf is a format specifier, the second parameter onwards are the data that are to be used for those format specifier(s). When you did not have the c in your first original printf - it just picked up the adjacent int and printed that for %x that was specified. In the revised first printf I made it print he cvalue of c in hexadecimal. In your second printf you make it print the address of C variable that is in the stack.

Check these links for further details - https://en.wikipedia.org/wiki/Call_stack#Structure Also other Stackoverflow Q&As that show up on the right pane.

abRao
  • 2,787
  • 1
  • 25
  • 37
0

TO 8086 platforms: As you can read here, stack is the runtime/call stack, each function local vars are stored in the current procedure stack frame. read more about runtime/call stack here

ArBel
  • 79
  • 10