Incorrect format specifier with gcc compiler

1.1k views Asked by At

This is a simple program in C to illustrate how C allocates memory to variables. The idea is that there will be a new memory address shown each time I re-compile the program.

Here is the program:

#include<stdio.h>

main()
{
        int a;
        int b = 10;
        float c = 5.8;

        printf("the address is %u \n", &a);
        printf("the address is %u \n", &b);
        printf("the address is &u \n", &c);

        getch();
}

When I run this gcc program_name.c, I get a series of errors:

program_name.c:3:1: warning: type specifier missing, defaults to 'int'
      [-Wimplicit-int]
main()
^
program_name.c:9:33: warning: format specifies type 'unsigned int' but the
      argument has type 'int *' [-Wformat]
        printf("the address is %u \n", &a);
                               ~~      ^~
program_name.c:10:33: warning: format specifies type 'unsigned int' but the
      argument has type 'int *' [-Wformat]
        printf("the address is %u \n", &b);
                               ~~      ^~
program_name.c:11:33: warning: format specifies type 'unsigned int' but the
      argument has type 'float *' [-Wformat]
        printf("the address is %u \n", &c);
                               ~~      ^~
program_name.c:13:2: warning: implicit declaration of function 'getch' is
      invalid in C99 [-Wimplicit-function-declaration]
        getch();
        ^
5 warnings generated.
Undefined symbols for architecture x86_64:

I cannot understand why these errors are being thrown. Surely %u is an entirely appropriate format specifier. What am I missing? How can I reformat the code to stop these errors?

EDIT: Based on the responses, it appears the correct code should be

#include<stdio.h>

int main()
{
        int a;
        int b = 10;
        float c = 5.8;

        printf("the address is %p \n", &a);
        printf("the address is %p \n", &b);
        printf("the address is %p \n", &c);

        return 0;
}

That does run without errors.

0

There are 0 answers