On many systems printf is buffered, i.e. when you call printf the output is placed in a buffer instead of being printed immediately. The buffer will be flushed (aka the output printed) when you print a newline \n.

On all systems, your program will print despite the missing \n as the buffer is flushed when your program ends.

Typically you would still add the \n like:

printf ("%s\n", a);

An alternative way to get the output immediately is to call fflush to flush the buffer. From the man page:

For output streams, fflush() forces a write of all user-space buffered data for the given output or update stream via the stream's underlying write function.

Source: http://man7.org/linux/man-pages/man3/fflush.3.html

EDIT

As pointed out by @Barmar and quoted by @Alter Mann it is required that the buffer is flushed when the program ends.

Quote from @Alter Mann:

If the main function returns to its original caller, or if the exit function is called, all open files are closed (hence all output streams are flushed) before program termination.

See calling main() in main() in c

Top answer
1 of 3
55

On many systems printf is buffered, i.e. when you call printf the output is placed in a buffer instead of being printed immediately. The buffer will be flushed (aka the output printed) when you print a newline \n.

On all systems, your program will print despite the missing \n as the buffer is flushed when your program ends.

Typically you would still add the \n like:

printf ("%s\n", a);

An alternative way to get the output immediately is to call fflush to flush the buffer. From the man page:

For output streams, fflush() forces a write of all user-space buffered data for the given output or update stream via the stream's underlying write function.

Source: http://man7.org/linux/man-pages/man3/fflush.3.html

EDIT

As pointed out by @Barmar and quoted by @Alter Mann it is required that the buffer is flushed when the program ends.

Quote from @Alter Mann:

If the main function returns to its original caller, or if the exit function is called, all open files are closed (hence all output streams are flushed) before program termination.

See calling main() in main() in c

2 of 3
4

Strangely enough, it seems that nobody posted the adjusted code where the buffer is flushed yet...:

#include <stdio.h>

int main (void)
{
    char a[]="abcde";
    printf ("%s", a);
    fflush(stdout);
    //On some systems the line above will fail, in that case use: fflush(NULL);
}

Also note that this code probably doesn't do what you actually want to do.
What I assume you really want to do is:

#include <stdio.h>

int main (void)
{
    char a[]="abcde";
    printf ("%s\n", a);
   //The '\n' makes sure the next thing you print will be on the following line
}
🌐
Reddit
reddit.com › r/c_programming › printf not showing
r/C_Programming on Reddit: printf not showing
October 25, 2022 -

Hi guys,

I have an issue while trying to use the printf. For any reason is not showing no matter in my code.

Could you have a fast look and give me advice of why it's not working? Program simply exits with 0 exit code.

https://i.imgur.com/tQi2QLO.png

I've tried even using printf after the int main(){ but neither is printing anything after running the code through command line!!

Discussions

speller- tried using printf to debug -printf not working - CS50 Stack Exchange
The answer provided here explains it. If the added debugging printfs are "not executing" it could be for the same reason, since the first "\n" that is encountered would likely be after "MISSPELLED WORDS". More on cs50.stackexchange.com
🌐 cs50.stackexchange.com
March 3, 2020
c - printf not printing to screen - Stack Overflow
Data is not yet written to the controlled sequence. Instead of fiddling with the buffer setting you could call fflush after each write to profit from the buffer and still enforce the desired behavior/display explicitly. printf( "Enter first integer\n" ); fflush( stdout ); scanf( "%d", &i1 ); More on stackoverflow.com
🌐 stackoverflow.com
C - printf() not working but puts() is working fine - Stack Overflow
Because printf() does not flush the output stream automatically. On the other hand puts() adds a new line '\n' at the end of the passed string. So it's working because the '\n' flushes de stdout. ... Try using the new line character ('\n') at the end of your statement, also make sure you have ... More on stackoverflow.com
🌐 stackoverflow.com
c - printf not printing on console - Stack Overflow
Yes - this would work and is suggested in the link i posted in the correct answer. 2014-03-08T11:52:37.593Z+00:00 ... You could try writing to stderr, rather than stdout. fprintf(stderr, "Hello, please enter your age\n"); You should also have a look at this relevant thread. ... Mr T. Over a year ago · im taking this C course in my university and because is a 101 course then i can't use stuff that isn't in the material (i can only use for now printf ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
STMicroelectronics Community
community.st.com › t5 › stm32cubeide-mcus › printf-not-working-write-never-gets-called › td-p › 276659
Solved: printf() not working - _write() never gets called - STMicroelectronics Community
August 19, 2019 - I know the hardware works, because the same implementation of _write() works in an other project (the one provided here). I am probably missing some compiler setting but I can't for the life of mine figure out what it is. Can someone please point me in the right direction? Best regards. ... Solved! Go to Solution. ... This discussion is locked. Please start a new topic to ask your question. ... I checked your code just now and found your printf message does not contain newline "\n" so your printf doesn't hit lower implementation of __io_putchar since stdout is buffered.
🌐
LinuxQuestions.org
linuxquestions.org › questions › programming-9 › c-gcc-printf-not-printing-4175598404
[SOLVED] C, gcc, printf Not Printing
January 27, 2017 - I'm using the native gcc in Mac OS X. gcc -v returns: Code: couldn't understand kern.osversion `14.5.0' Using built-in specs. Target: i686-apple-darwin
Find elsewhere
🌐
Unix.com
unix.com › applications › programming
why printf don't work? - Programming - Unix Linux Community
September 18, 2009 - I use Solaris 10, I use following code: #include int main(void){ printf("----------testing-----------"); if(signal(SIGUSR1,sig_usr)==SIG_ERR) err_sys("can't catch SIGUSR1"); for(;;) pause(); sig_user(int signo){ ..... } when I run above code,it print nothing in screen.Why?
🌐
Reddit
reddit.com › r/c_programming › printf not working
r/C_Programming on Reddit: Printf not working
May 29, 2023 -

code

# include<stdio.h>

int main()

{

int size;

printf("Enter the number of elements\n");

scanf("%d",&size);

int a[size];

printf("Enter the elements\n");

for(int i = 0 ;i<size;i++)

{

scanf("%d",&a[i]);

}

printf("Before sorting\n");

for(int i = 0 ;i<size;i++)

{

printf("%d\n",a[i]);

}

for(int i = 0 ;i<size;i++)

{

printf("%d\n",a[i]);

}

return 0;

int min;

for(int i=0;i<size;i++)

{

for(int j=i;j<size;j++)

{

if(a[i]<a[j])

{

min = a[i];

}

else

{

min = a[j];

}

}

a[i]=min;

}

printf("After sorting\n"); // This is't working

for(int i = 0 ;i<size;i++)

{

printf("%d\n",a[i]);

}

return 0;

}

Output

Enter the number of elements

5

Enter the elements

23

432

535

234

552

Before sorting

23

32

35

234

552

23

32

35

234

552

Thank you for your time

🌐
GitHub
github.com › purduesigbots › pros › issues › 75
printf not working without newline · Issue #75 · purduesigbots/pros
October 15, 2018 - Expected Behavior: printf will print to the serial port without a newline in its format string Actual Behavior: nothing happens Steps to reproduce: call printf in a PROS c project from any source f...
Author   sealj553
🌐
Qt Forum
forum.qt.io › home › qt development › qt creator and other tools › [solved] printf/scanf not working...?
[solved] printf/scanf not working...? | Qt Forum
October 27, 2022 - Your program works fine...don't know what the problem with mine is, but at least it's not Qt, so I can drop discussing it here. I didn't realize that about scanf...I thought you could provide input from the console. EDIT: I just removed the scanf statements from this program, and the printf statements then worked.
🌐
Mbed OS
forums.mbed.com › mbed os
The function printf does not print anything on the console - Mbed OS - Arm Mbed OS support forum
October 2, 2020 - Hi all, Recently, the function printf does not print any data to the console anymore. I did not face this issue before. I’ve read the threads with similar problems on this forum. However, not any solution worked for me. For example, I’ve added a ‘\n’ character at the end of the string, ...
🌐
Cprogramming
cboard.cprogramming.com › c-programming › 156958-printf-doesnt-print-screen.html
printf doesn't print on screen
May 23, 2013 - If I understand correctly - if your "printf()" strings do not end in a newline, the output does not necessarily have to be printed immediately (there are some caveats, of course). ... If I remove line 12 then it compiles and runs fine. Did you try to run the program from a terminal outside your IDE ?. I remember that there is/was a bug in the terminal that comes with eclipse Kurt ... Thank you, Zuk & Matticus. I've had some problems with Eclipse, but it seems to work ...
🌐
Texas Instruments E2E
e2e.ti.com › support › tools › code-composer-studio-group › ccs › f › code-composer-studio-forum › 494395 › printf-not-working
printf not working - Code Composer Studio forum - TI E2E
March 1, 2016 - The printf support level is at full under the C2000 compiler advanced options. <stdio.h> is included. And I have looked at 5 other postings related to "printf not working" on this forum, but I can't find the source of the problem in my system.
🌐
Reddit
reddit.com › r/c_programming › printf not printing %c
r/C_Programming on Reddit: printf not printing %c
June 23, 2016 -

EDIT: As most are pointing out, it probably has to do with non-printable characters (eg, going above ASCII value 122) because I'm remembering that at on point it was able to print out the character "h" (number 104) so I'll work on that soon.

Hi, this is probably a very beginner question but it´s all about C so I didn´t know if to post in r/learnprogramming. The answers on Google seem to be for similar but more complicated issues.

Some of you might recognize this as a CS50 exercise from week 2 but essentially I cannot get the integer encryptedtext[i] to be recognized and printed out as a character. Below is my code, it is an early draft so right now I´m only looking for help around (I think) line 36:

#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, string argv[])
{
    // user may only input 2 command-line arguments
    if(argc != 2)
    {
        printf("Restart program and input one key only. \n");
        return 1;
    }
    
    string key = argv[1];
    int keylength = strlen(key);
    for(int i = 0; i < keylength; i++)
    {
        if(key[i] < 65 || (key[i] > 90 && key[i] < 97) || key[i] > 122)
        {
            printf("Restart program and input letters only. \n");
            return 1;
        }
    }    
    
    printf("Give me something to encrypt: ");
    string plaintext = GetString();
   
    //looping through key and plaintext
    int plaintextlength = strlen(plaintext);
    int encryptedtext[plaintextlength];
    for(int i = 0; i < plaintextlength; i++)
    {
        encryptedtext[i] = plaintext[i]+key[i]; 
        //printf("Your plaintext %c added to key %c becomes: %c \n", plaintext[i], key[i], encryptedtext[i]);
        printf("%c is %i. \n", encryptedtext[i], encryptedtext[i]);  
    }
    return 0;
}

As far as I know, encryptedtext[i], plaintext[i], and key[i] are all integers, or can be treated as such. In fact, it prints the ASCII number value of the letter but not the letter itself. It probably has to do with me not understanding exactly how characters and integers can be used interchangeable (edit: actually it probably has to do with unprintable ASCII values) but I've tried declaring encryptedtext[] as an array of characters and I've played around with different references (%c, %s, %i, %d) but I can´t get it to print out the character! Any help would be appreciated.

Thanks!