简体   繁体   中英

Why can't I printf() to stdout in C without using fflush()?

I recently started programming in C and something I can't seem to get my head around is the way stdout prints to screen.

When I added all my statements in the main() function and used the printf() function everything worked well, all the printf() statements were able to print to stdout.

From Main.c
...
#include "HeaderFile.h"
int main(int argc, char * argv[]){
 ...
 printf("%c\n", testChar);
 return 0;/*End of execution. Returns 0 value and ends gracefully*/
}
...

But when I started to modularize my code in different functions, I realised that I have to insert the fflush(stdout) function at the end of each printf() function in order for the print function to print out to stdout:

From ReadFile.c
...
#include "HeaderFile.h"
void readFileFunction(char* file){
 ...
 printf("%c\n", testChar);
 fflush(stdout);
 ...
}
...

Header file:

 /*This is the header file used by the Linked list program.*/
/*This is the header file used by the Linked list program.*/
#ifndef HEADERFILE_H   /* Include guard */
#define HEADERFILE_H

#include <stdio.h> /*including the stdio file inside the Main.c file. stdio.h is a header file, where this and other similar functions are defined.*/
#include <string.h>/*including the string file inside the Main.c file. string.h is a header file, where this and other similar functions are defined.*/
#include <time.h>/*including the time file inside the Main.c file. time.h is a header file, where this and other similar functions are defined.*/
#include <stdint.h>/*including the stdint file inside the Main.c file. stdint.h is a header file, where this and other similar functions are defined.*/
#include <stdlib.h>/*including the stdlib file inside the Main.c file. stdlib.h is a header file, where this and other similar functions are defined.*/
#include <errno.h> /*including the errno file inside the Main.c file. errno.h is a header file, where this and other similar functions are defined.*/
#include <regex.h>

extern const char errorString[]; /*A string of characters. Indicates an error message when the program in-counters a problem during execution.*/   

/*String constants used to match user input with a specific function.*/
extern const char *string1;
extern const char *string2;
extern const char *string3;
extern const char *string4;
extern const char *string5;
extern const char *string6;
extern const char *string7;
extern const char *string8;
extern const char *string9;

/*Node structure with character value and the next node.*/
typedef struct node {
    char value;
    char type;
    struct node * next;
} nodeStruct;

/*function prototypes for every function being used in the code.*/
int removeChar(nodeStruct ** head, char value);
void readFileInit(char* file);
void readFileFunction(char *file);
void printList(nodeStruct * head) ;
void push(nodeStruct ** head, char value) ;
char tail(nodeStruct * head) ;
char head(nodeStruct * head) ;
int length(nodeStruct * head) ;
int pop(nodeStruct ** head) ;
int regularExpr (const char *patt, char *str) ;
void append(nodeStruct ** head, char value) ;
int insertAfter(nodeStruct ** head, char value, char value2) ;
int insertBefore(nodeStruct ** head, char value, char value2) ;

#endif // HEADERFILE_H

Would you please explain in details why this sudden difference?

The function prototype for fflush is this:

int fflush ( FILE * stream );

It flushes the file pointer to the stream ensuring its written.

Depending on the environment where the code is executing on, the stdout in this case could well be buffered, implying that it does not get written straight away. fflush alleviates that and ensures it is flushed out.

Another thing, the kernel could be under a load at the point of execution thus delaying the printing to the console or terminal in this case, in which case, ending up judiciously sprinkling fflush all over the place.

It might help to enclose a SCCE example as looking at the OP's question, it is not easy to discriminate as to why, more of rather what is happening.

Edit:

The code can specify that the output is automatically written without buffering by including this snippet

setbuf(stdout, NULL);

It would be good practice, to save the state of buffer control at the start, switch off the buffering, and at end of code execution restore the buffer control.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM