0

I start to learn c programming language, when I do an exercise I see a very singular error, for be sure that this is the really error , and not something else into the program , I wrote it in another project

the code is :

  1 #include <stdio.h>
  2 
  3 int main (){
  4         printf("ciao come va");
  5         for(;;);
  6         return 0;
  7 }

gcc -o test main.c ; ./test ---> and the return is(TAN TAN TAN) nothing ! On my shell didn't appeare anything!

and the process do not return , on my system monitor I saw it , so it means it is into the for loop forever

my first question is : why i do not print anything? printf is call befor loop !

with gcc -S I compile and not Assamble and also the Assembly code seams to be correct

  1         .file   "main.c"
  2         .section        .rodata
  3 .LC0:
  4         .string "ciao come va"
  5         .text
  6         .globl  main
  7         .type   main, @function
  8 main:
  9 .LFB0:
 10         .cfi_startproc
 11         pushq   %rbp
 12         .cfi_def_cfa_offset 16
 13         .cfi_offset 6, -16
 14         movq    %rsp, %rbp
 15         .cfi_def_cfa_register 6
 16         movl    $.LC0, %edi
 17         movl    $0, %eax
 18         call    printf
 19 .L2:
 20         jmp     .L2
 21         .cfi_endproc
 22 .LFE0:
 23         .size   main, .-main
 24         .ident  "GCC: (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609"
 25         .section        .note.GNU-stack,"",@progbits

I don't know very well assembly, but I saw that printf is called befor the loop!

So how could it is possible?

second question : if I add an "\n" to the static string it work as I expect!

  1 #include <stdio.h>
  2 
  3 int main (){
  4         printf("ciao come va\n");
  5         for(;;);
  6         return 0;
  7 }

The result is : ciao come va

and the process don't return because the for loop , as I expect

The Assembly code is :

  1         .file   "main_con_new_line.c"
  2         .section        .rodata
  3 .LC0:
  4         .string "ciao come va"
  5         .text
  6         .globl  main
  7         .type   main, @function
  8 main:
  9 .LFB0:
 10         .cfi_startproc
 11         pushq   %rbp
 12         .cfi_def_cfa_offset 16
 13         .cfi_offset 6, -16
 14         movq    %rsp, %rbp
 15         .cfi_def_cfa_register 6
 16         movl    $.LC0, %edi
 17         call    puts
 18 .L2:
 19         jmp     .L2
 20         .cfi_endproc
 21 .LFE0:
 22         .size   main, .-main
 23         .ident  "GCC: (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609"
 24         .section        .note.GNU-stack,"",@progbits

so , how you could see at 17 line, the function that was called is puts and not printf!

so the second question is : why the second code works and the first not? and why if I call printf in my c program in assembly one is called puts? and why only I write the new line "\n" character in the string?

Abhishek Aryan
  • 19,936
  • 8
  • 46
  • 65

1 Answers1

2

It doesn't show up because the standard output is line-buffered, and you didn't include a newline. The data is stuck in the buffer until a newline is printed (or the program ends, but yours doesn't).

To fix, add a newline:

printf("ciao come va\n");
                    ^
                    |
                  boom!
unwind
  • 391,730
  • 64
  • 469
  • 606
  • ok, if I undestood well, when i call printf i do not really print on my screen but I'am writing on a buffer in my memory, is it correct? Is the kernel of my OS than print on the screen when press : ENTER or my programm end? – Alessandro Domeneghetti Feb 17 '17 at 16:33