2

I have a main.c file compiled successfully with dependencies. The code of the main is:

#include "includer.h"

int main(int argc, char *argv[]){
  int i;
  printf("The value of argc is %d\n", argc);
  /*if (argc>1){
    printf("inside the if");
    for (i = 1; i < argc; i++){
      assembler(argv[i]);
    }
  }*/
  printf("Now calling to testList");
  testList();
  /*printTable();*/
  return 0;
}

This is what "includer.h" looks like:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "functions.h"
#include "constants.h"
#include "datatypes.h"

When I run the main, I get the first printf on the screen, and then basically nothing happens, it looks as if it's running but it doesn't do anything, not even the second printf.

Can anyone point me to where the problem is, if there is one?

BoBTFish
  • 19,167
  • 3
  • 49
  • 76
Elad Edri
  • 331
  • 2
  • 9
  • 3
    What happens if you add a new-line character to the second `printf` or print the string with `puts`? – M Oehm Mar 18 '16 at 13:47
  • 3
    `stdout` is normally line buffered by default, so if you forget to include a newline in your `printf`, you may not see it at all until a newline is printed elsewhere in your program, or until the program exits. – Tom Karzes Mar 18 '16 at 13:49
  • 1
    Try fflush(stdout) after each printf() call – Dmitry Mar 18 '16 at 13:52
  • 2
    Looks like the stdout buffer is not flushed. You are saying that "basically nothing happens". However I believe that your code is executing properly even after second printf but you are not getting any feedback from printf. This link might help: http://stackoverflow.com/questions/1716296/why-does-printf-not-flush-after-the-call-unless-a-newline-is-in-the-format-strin – Sunil Shahu Mar 18 '16 at 13:57
  • Just to clarify what's happening: (1) The second `printf` *is* being executed, but the output is being buffered because there is no newline to cause it to be flushed, and (2) After the second `printf`, it's calling `testList` which apparently is never returning. – Tom Karzes Mar 18 '16 at 14:00
  • The buffer was the problem, solved now. Thanks... – Elad Edri Mar 18 '16 at 14:10
  • stdout is line buffered by default when it is associated with a tty (or pseudo-tty). If it is associated with a regular file, it is fully buffered by default. – William Pursell Aug 26 '16 at 02:07

1 Answers1

3

The stdout stream is buffered, so it will only display what's in the buffer after it reaches a newline (or when it's told to). You can force it to print by:

Flushing stdout

printf("Now calling to testList");
fflush(stdout);
testList();

Disabling buffering on stdout

setbuf(stdout, NULL);

Apart from that your testList() call is never returning, which we can't help with since there's no code for it, causing your main to never de facto 'end' (meaning, reach your return 0;)

Bernardo Meurer
  • 2,295
  • 5
  • 31
  • 52
  • 1
    Bernard, I suggest to remove the `\n` so as to demonstrate when to use `fflush`. In your example `fflush` is not needed. – Paul Ogilvie Mar 18 '16 at 14:10
  • 1
    I think your example should be: `printf("Now calling to testList"); fflush(stdout);` Your current example don't need a flush – Support Ukraine Mar 18 '16 at 14:11
  • @PaulOgilvie You're right, didn't remember to take that off when I copied that line from OP's post. – Bernardo Meurer Mar 18 '16 at 14:12
  • @4386427 Nice idea, changed my answer. – Bernardo Meurer Mar 18 '16 at 14:13
  • If stdout is associated with a tty, then it is line-buffered by default. If stdout is associated with a regular file, then its is fully buffered by default (meaning it is not necessarily this way). If stdout is fully buffered, adding a newline won't cause the data to be written. – William Pursell Aug 26 '16 at 02:06