printf statement not executed

J

JS

Why is "TEST" not printed when I run this code after comiling?


#include <stdio.h>

int main()
{

int a,b,c,d,e,x;
int *variables[5];

a = 1;
b = a*2;
c = b*2;
d = c*2;
e = d*2;

variables[0] = &a;
variables[1] = &b;
variables[2] = &c;
variables[3] = &d;
variables[4] = &e;


for(x=0; x<5; x++)
printf("Variable %c = %d\n", 'a'+ x, *variables[x]);


printf("TEST");


return(0);
}
 
K

Ken Human

JS said:
Why is "TEST" not printed when I run this code after comiling?


#include <stdio.h>

int main()
{

int a,b,c,d,e,x;
int *variables[5];

a = 1;
b = a*2;
c = b*2;
d = c*2;
e = d*2;

variables[0] = &a;
variables[1] = &b;
variables[2] = &c;
variables[3] = &d;
variables[4] = &e;


for(x=0; x<5; x++)
printf("Variable %c = %d\n", 'a'+ x, *variables[x]);


printf("TEST");


return(0);
}

I ran this code unmodified, "TEST" printed on my console.
 
W

Walter Roberson

The OP missed the \n after TEST, so the output was probably
wiped out by the command prompt.
 
J

Jonathan Bartlett

JS said:
Why is "TEST" not printed when I run this code after comiling?

Because you did not add a "\n" or flush your output. The "f" family of
functions does buffering, and may not print out all of your results
until it hits a "\n" or you tell it to flush.

The printf is executed, it's just that your "TEST" is waiting in a
buffer and hasn't yet been handed to the operating system.

Jon
 
M

Martin Ambuhl

JS said:
Why is "TEST" not printed when I run this code after comiling?

If you don't terminate the last line of output with an end-of-line
character, there is no portable behavior.

[...]
 
L

Lawrence Kirby

Because you did not add a "\n" or flush your output. The "f" family of
functions does buffering, and may not print out all of your results
until it hits a "\n" or you tell it to flush.

The program does flush the output by terminating normally. On normal
program termination all open files are flushed (when that is a meaningful
operation) and closed.
The printf is executed, it's just that your "TEST" is waiting in a
buffer and hasn't yet been handed to the operating system.

If an implementation did that it would be broken. It is true that if
the last line is not terminated by a newline it may not be
considered valid and perhaps even discarded but this is not a
buffering issue. In practice I'm not aware of an implementation that would
discard it. Walter probably has the correct explanation.

Lawrence
 
V

Villy Kruse

The program does flush the output by terminating normally. On normal
program termination all open files are flushed (when that is a meaningful
operation) and closed.

It has been seen before that the output that was actualy written was
overwritten by the next command prompt, and people has in that case
spent hours looking for a bug that wasn't there. Including a newline
would have forced the prompt to the next line, though.


Villy
 
L

Lawrence Kirby

It has been seen before that the output that was actualy written was
overwritten by the next command prompt, and people has in that case
spent hours looking for a bug that wasn't there. Including a newline
would have forced the prompt to the next line, though.

Yes, there was a reference to this in the part of my article that you
snipped, i.e. Walter's explanation.

Lawrence
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,539
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top