0

I am using gcc to compile my file.c to object.o and nm to list symbols of MinGW.

I would like to know : Why do I get puts with nm is_alphabet.o -u when I am using printf with a newline ending with printf("%s\n", c); ? because if I do printf("%s", c);, I will get printf.

is_alphabet.c:

#include <stdio.h>

int is_alphabet(char *c)
{
    printf("%s\n", c);
    // […]
    return (1);
}

See my console output:

E:\GitHub\exemple_rendu (main -> origin)
λ sed '17!d' ex00\is_alphabet.c
        printf("%s\n", c);

E:\GitHub\exemple_rendu (main -> origin)
λ gcc -Wall -Werror -Wextra -c ex00/*.c

E:\GitHub\exemple_rendu (main -> origin)
λ nm is_alphabet.o
0000000000000000 b .bss
0000000000000000 d .data
0000000000000000 p .pdata
0000000000000000 r .rdata$zzz
0000000000000000 t .text
0000000000000000 r .xdata
0000000000000000 T is_alphabet
                 U puts

[…]

E:\GitHub\exemple_rendu (main -> origin)
λ sed '17!d' ex00\is_alphabet.c
        printf("%s", c);

E:\GitHub\exemple_rendu (main -> origin)
λ gcc -Wall -Werror -Wextra -c ex00/*.c

E:\GitHub\exemple_rendu (main -> origin)
λ nm is_alphabet.o
0000000000000000 b .bss
0000000000000000 d .data
0000000000000000 p .pdata
0000000000000000 r .rdata
0000000000000000 r .rdata$zzz
0000000000000000 t .text
0000000000000000 r .xdata
0000000000000000 T is_alphabet
                 U printf

I thought is something like macro replacement before compiling and the macro should be probably in stdio.h but I don't know how to verify my hypothesis and I would like to know how this macro work.

  • 1
    This is optimization by the compiler. `puts(c);` produces the same result as `printf("%s\n", c);` and is probably faster. – Bodo Oct 12 '21 at 13:35
  • Thanks! And I can disable this optimization with `gcc -fno-builtin` flags. https://publicclu2.blogspot.com/2013/05/how-gcc-generates-optimized-code-for.html –  Oct 12 '21 at 13:44
  • And here : https://stackoverflow.com/questions/54281780/what-exactly-is-fno-builtin-doing-here the explanation of `-fno-builtin` flag –  Oct 12 '21 at 13:51

0 Answers0