Code before while(1) does not run [duplicate] - c

This question already has answers here:
printf not printing to screen
(3 answers)
Why does printf not flush after the call unless a newline is in the format string?
(10 answers)
Closed 7 years ago.
I have code like this below:
#include <stdio.h>
int main(int argc, char *argv[]) {
printf("hello");
while(1){
// whatever here
}
}
and the question is: why the first instruction is skipped? It runs only the loop, hello is never printed. I compiled it with gcc and g++ with the same result.

It does run, it's just that the output buffer is not flushed before your while.
Use printf("hello\n"); instead. The newline character will flush the buffer so writing the output immediately to your console.

Your assumption is wrong, your code does run, only stdout is not flushed, but buffered.
Use fflush(stdout) after printf("hello"), this forces stdout to be printed.
And, as #Bathsheba pointed out, also a newline character ("\n") within the printf forces it to flush, which is explained in this SO question.

Related

No need of fflush(stdout) with printf in C? [duplicate]

This question already has answers here:
What are the rules of automatic stdout buffer flushing in C?
(5 answers)
printf flush at program exit
(4 answers)
Closed 3 years ago.
Until now, I used fflush(stdout) when using printf without \n, since we have to flush the buffer.
But I found that the below code also works fine.
#include <stdio.h>
int main(void){
printf("hello world");
// without fflush(stdout)
}
// output: "hello world"
Is the outcome differs with the compiler? or am I misunderstand something?

Why does getchar wait after one character is entered? [duplicate]

This question already has answers here:
How to avoid pressing Enter with getchar() for reading a single character only?
(14 answers)
Closed 4 years ago.
Here is my code:
#include <stdio.h>
int main()
{
char a = getchar();
printf("char: %c", a);
}
I'm compiling this with gcc.
When running, the program waits for input even after one character is entered on console. Shouldn't it immediately exit and print the character? I'm running this on Ubuntu for Windows, if that can make any difference.
stdin is your default input stream. The default buffering mode of stdin is line buffered. So it waits for the \n to be entered. Then only there is something to read from the input stream and then getchar gets the input and works with it.
The operating system often buffers console input until a new line is entered, such that your program will not even receive the single character before. Probably nothing you can do about that.

fgets will be ignored probably due to other scanf's [duplicate]

This question already has answers here:
Why scanf("%d", [...]) does not consume '\n'? while scanf("%c") does?
(4 answers)
scanf() leaves the newline character in the buffer
(7 answers)
Closed 6 years ago.
The program won't compile fgets in the teste1 function. Or at least it isn't working properly, it won't let me type the string, the program will end right after it prints "Nome do cliente".
If I disable the other scanf's in the function, it will run without any issue.
How do I make fgets to work?
#include <stdio.h>
void teste1(){
char teste[50];
printf("Nome do cliente\n");
fgets(teste,50,stdin);
}
void teste2(){
teste1();
}
void teste3(){
int opc1,opc2;
printf("\nSeleccione a área desejada\n1- Clientes\n2- Concessionários\n3- Carros de demonstração\n");
scanf("%d",&opc1);
printf("\nSeleccione a área desejada\n1- Inserir\n2- Alterar\n3- Remover\n4- Consultar\n");
scanf("%d",&opc2);
teste2();
}
int main()
{
teste3();
}
The Enter key you pressed for the last scanf input will be left in the input buffer as a newline. The fgets function will read this newline and think it is finished.

Behaviour of stdout and printf [duplicate]

This question already has an answer here:
C's printf and fprintf(stdout,) are not printing
(1 answer)
Closed 6 years ago.
Just see the below program,
#include <stdio.h>
int main()
{
printf("hai");
while(1);
}
The above code is not printing hai and its just waitinG. But if I add
printf("hai\n");
its working.
Can I know what happening internally?
Writing printf("hai\n"); instead will cause, due to the newline character, the output buffer to be flushed, so you'll see the output before entering the infinite loop. There are other ways of flushing the output buffer, but appending the newline character is particularly simple.
Technically the behaviour of a tight loop like while(1); is undefined in C, but that is unlikely to be the cause of your problem.
printf("hai"); won't show something immediateley, you need to flush stdout's buffer with either printf("\n"); or fflush(stdout);
Your standart output stream (stdout) is buffered, so it will only flush (and display what is in it) when it is either manually forced or when it recieves a newline character ('\n').
You can change this behavior with the library function setbuf()
You can also manually flush the buffer (force it to immideatly display whats currently in it) by using fflush(stdout);

Printf() before line causing segmentation fault does not execute [duplicate]

This question already has answers here:
Execution of printf() and Segmentation Fault
(4 answers)
Closed 1 year ago.
When a segmentation fault occurs, the printf() before it does not execute.
main()
{
printf( "something" );
statement; //this statement causes a segmentation fault
}
In the situation above, why does the printf() not execute?
So do I need to use valgrind in such a case(which prints all printf() before the faulty statement).
Make sure you include a newline "\n" in your printf statement. Normally, at least in UNIX systems, stdout is line-buffered so newline character makes the line to appear immediately. You probably omitted "\n" (or your output is not flushed for other reason) and that's why you can't see the printed string.
Another option is to flush the output yourself using fflush(stdout) after calling printf.
An output stream can fail to be output before a program crash but you can force the bytes to be output by flushing them with fflush().
I usually do it with something like this:
if (trace) { fflush(stdout); }
Output via printf() and any other standard I/O function is buffered in the standard C library.
You need to call fflush() to ensure the output is sent to the tty before your program crashes.

Resources