problem related to printf!! why is it so???

M

Manohar S

It is a problem related to printf, and buffering to the printf statements
Look through this program.
main()
{
int pid;
int loop,max=3;

for(loop = 0; loop <max;loop++)
{
pid = fork();
if(pid < 0)
printf("\nThe greatest fork error of the decade");
else if(pid==0)
{
printf("\nI am a child");
goto out;
}
else
{
printf("\nI have grown up");
}
}
out:
}

output:
I am a child
I have grown up
I am a childI have grown up
I have grown up
I am a childI have grown up
I have grown up

the same program with '\n' placed at the end of printf
main()
{
int pid;
int loop,max=3;

for(loop = 0; loop <max;loop++)
{
pid = fork();
if(pid < 0)
printf("The greatest fork error of the decade\n");
else if(pid==0)
{
printf("I am a child\n");
goto out;
}
else
{
printf("I have grown up\n");
}
}
out:
}

output:
I am a child
I have grown up
I am a child
I have grown up
I am a child
I have grown up

why is this difference???
could anyone please explain.....
 
V

Vijay Kumar R Zanvar

It is a problem related to printf, and buffering to the printf statements
Look through this program.
main()
{
int pid;
int loop,max=3;

for(loop = 0; loop <max;loop++)
{
pid = fork();
if(pid < 0)
printf("\nThe greatest fork error of the decade");
else if(pid==0)
{
printf("\nI am a child");
goto out;
}
else
{
printf("\nI have grown up");
}
}
out:
}

output:
I am a child
I have grown up
I am a childI have grown up
I have grown up
I am a childI have grown up
I have grown up

the same program with '\n' placed at the end of printf
main()
{
int pid;
int loop,max=3;

for(loop = 0; loop <max;loop++)
{
pid = fork();
if(pid < 0)
printf("The greatest fork error of the decade\n");
else if(pid==0)
{
printf("I am a child\n");
goto out;
}
else
{
printf("I have grown up\n");
}
}
out:
}

output:
I am a child
I have grown up
I am a child
I have grown up
I am a child
I have grown up

why is this difference???
could anyone please explain.....

Yes I can explain:

If you understand the concept of buffering, you will come
know to why the above programs behaves like this.

Streams are of two types: buffered and unbuffered. Buffered
streams are flushed when any of the following conditions are met:

* The buffer is full
* When '\n' is encounterd, if the stream is line buffered
* When a function to read from stdin is invoked
* When the program exits
* If the default flushing, i.e., buffering, behaviour is modified by
the setvbuf(3) or setbuf(3) library functions.

Whereas, unbufferd stream are flushed as soon as the data arrive.
Keeping above facts in mind, try tracing your program again.

[OT]
After forking, which process (parent or child) will execute first can
not be anticipated.
[/OT]
 
J

Jack Klein

It is a problem related to printf, and buffering to the printf statements
Look through this program.
main()
{
int pid;
int loop,max=3;

for(loop = 0; loop <max;loop++)
{
pid = fork();

[snip]

Your question is off-topic here, as there is no such function as
fork() in the C language or library. It is a non-standard extension
provided by your particular compiler and operating system, nothing at
all to do with the language.

Questions about extensions such as this should be asked in a support
group for that particular platform. In this particular case I would
suggest either or
depending you your platform.
 
J

Jack Klein

[snip non-standard, off-topic code]
Yes I can explain:

[snip]

Please don't, as it is completely off-topic here. The proper thing to
do with off-topic posts about non-standard extensions is to direct the
poster to a group that supports the compiler/OS combination that
provides them, not to provide off-topic answers here.
[OT]
After forking, which process (parent or child) will execute first can
not be anticipated.
[/OT]

Since you know it is off-topic and there is no forking in the C
language, why do you pollute the group?
 
E

EPerson

It is a problem related to printf, and buffering to the printf statements
Look through this program.
main()

It's best to be more explicit here:

int main(void)
{
int pid;
int loop,max=3;

for(loop = 0; loop <max;loop++)
{
pid = fork();

Non-standard function (off-topic in comp.lang.c).
if(pid < 0)
printf("\nThe greatest fork error of the decade");

Undefined behavior. #include said:
else if(pid==0)
{
printf("\nI am a child");
goto out;

The effect of the goto can be achieved using a break statement.
}
else
{
printf("\nI have grown up");
}
}
out:

Syntax error. You have a label without a statement.

You should return 0 (or other suitable value) here.

[snip output and alternative program]
why is this difference???
could anyone please explain.....

Take this to an appropriate group. The semantics of fork() are off-topic here.
 
V

Vijay Kumar R Zanvar

[..]
Please don't, as it is completely off-topic here. The proper thing to
do with off-topic posts about non-standard extensions is to direct the
poster to a group that supports the compiler/OS combination that
provides them, not to provide off-topic answers here.
[OT]
After forking, which process (parent or child) will execute first can
not be anticipated.
[/OT]

Since you know it is off-topic and there is no forking in the C
language, why do you pollute the group?

My aim was not to pollute. Next time I would do as you have
adviced, or will try mailing the answer personally. Thank you.
 
B

Bob Crowley

Sorry to get off the topic, but as an absolute beginner teaching
myself to program in C, I would like to know why I can get numbers to
add together either as declared variables, or as mathematical
functions in "printf", but find I cannot get them to multiply or
divide.

Is there an additional switch I need. I have included <math.h> but I
don't think it is necessary for these simple operations.

I'd appreciate advice.

Bob Crowley.
 
J

Jeremy Yallop

Bob said:
Sorry to get off the topic, but as an absolute beginner teaching
myself to program in C, I would like to know why I can get numbers to
add together either as declared variables, or as mathematical
functions in "printf", but find I cannot get them to multiply or
divide.

It's not clear (to me, at least) what you mean. Could you post some
code to illustrate the problem?

Jeremy.
 
M

Mark McIntyre

On 29 Jan 2004 05:55:19 -0800, in comp.lang.c ,
Sorry to get off the topic, but as an absolute beginner teaching
myself to program in C, I would like to know why I can get numbers to
add together either as declared variables, or as mathematical
functions in "printf", but find I cannot get them to multiply or
divide.

You're using the operators * and / to do multiplication and division,
right?
 
B

Bob Crowley

Mark McIntyre said:
On 29 Jan 2004 05:55:19 -0800, in comp.lang.c ,


You're using the operators * and / to do multiplication and division,
right?

The problem is in a text book I am using, namely to find out
"5/12*68".

My coding is as follows

#include <stdio.h>
#include <math.h> COMMENT - should not be necessary

main(){

float answer;

answer = 5/12*68;

printf("Five eighths of sixty eight is %f\n", answer);

}


END OF CODING

The first time I tried it I did not use a variable, but simply had
5/12*68 in the printf statement where the variable name "answer" is
now.

I either got 0.000000 or -1.9xxxxx as the answer, whereas it should be
28+.

I also changed the / and * signs to + to see what would happen, and
got 85 which is correct. So the program worked when I used the +
signs for addition, but not when I used / or * for division and
multiplication.

I am using Redhat Linux 8, with the gcc C compiler, and compiling with

gcc -o object.o source.c


As I said, I'd appreciate knowing why the program won't allow * or /
to work.

Bob Crowley.
 
J

Joona I Palaste

The problem is in a text book I am using, namely to find out
"5/12*68".
My coding is as follows
#include <stdio.h>
#include <math.h> COMMENT - should not be necessary

float answer;
answer = 5/12*68;

The problem here is that 5/12*68 is an integer expression. 5/12 is
an integer division, and it's perfomed as such, yielding a result of
0. You then multiply 0 by 68, yielding 0.
Then this 0 is assigned to a float variable. Assigning it to a float
does not magically go back and redo all the calculations as a float
expression.
You need to change the integer expression into a float expression.
This can be done in several ways. Either cast the 5 or the 12 to
float, or write 5.0 or 12.0 instead of 5 or 12.
printf("Five eighths of sixty eight is %f\n", answer);

Shouldn't that be five twelfths?
 
D

Dan Pop

In said:
The problem is in a text book I am using, namely to find out
"5/12*68".

My coding is as follows

#include <stdio.h>
#include <math.h> COMMENT - should not be necessary

It is NOT necessary!
main(){

float answer;

answer = 5/12*68;
^^^^^^^
This is an integer expression and it is evaluated as such. 5 / 12 yields
0 in integer arithmetic, so the final result is 0. This result is
converted to float and assigned to answer.

Use floating point constants instead, to force the evaluation of the
expression as a floating point expression.
printf("Five eighths of sixty eight is %f\n", answer);

}


END OF CODING

The first time I tried it I did not use a variable, but simply had
5/12*68 in the printf statement where the variable name "answer" is
now.

That's MUCH worse: %f expects a double argument, not an integer one.
If you used gcc correctly, it would have warned you.
I either got 0.000000 or -1.9xxxxx as the answer, whereas it should be
28+.

Nope, the correct result in C is 0.
I also changed the / and * signs to + to see what would happen, and
got 85 which is correct. So the program worked when I used the +
signs for addition, but not when I used / or * for division and
multiplication.

Depending on the actual expression, even * and / give the expected
result when misused. Try

answer = 60 / 5 * 3;

and I bet you'll get the expected result, because 60 / 5 yields the same
value as 60.0 / 5.0 (but with a different type).
I am using Redhat Linux 8, with the gcc C compiler, and compiling with

gcc -o object.o source.c

It is highly misleading to call object.o an executable file, which is
what your command will produce in the absence of the -c option.

NEVER omit the -Wall -O options when compiling with gcc.

fangorn:~/tmp 259> cat test.c
#include <stdio.h>

int main()
{
printf ("%f\n", 5/12*68);
return 0;
}
fangorn:~/tmp 260> gcc test.c
fangorn:~/tmp 261> gcc -Wall test.c
test.c: In function `main':
test.c:5: warning: double format, different type arg (arg 2)

And, of course, NEVER ignore any warning produced by gcc -Wall -O.
As I said, I'd appreciate knowing why the program won't allow * or /
to work.

They do work, but they work as specified by the C language definition,
not according to your misconceptions. C doesn't support the arithmetic
you've learned in grade school. It supports two different types of
arithmetic (ignoring pointer arithmetic): integer arithmetic and floating
point arithmetic and neither of them has the semantics of arithmetic on
real numbers.

Never forget: when two operands have the same arithmetic type, the
+, -, * and / operators yield a value of the *same* type, even if the
value is used as initialiser to an object of a different arithmetic type.

Dan
 
D

Dan Pop

In said:
Why do you recommend the -O flag?

Without it, gcc is not able to report the usage of uninitialised
variables:

fangorn:~/tmp 279> cat test.c
int main()
{
int rc;
return rc;
}
fangorn:~/tmp 280> gcc -Wall test.c
fangorn:~/tmp 281> gcc -Wall -O test.c
test.c: In function `main':
test.c:3: warning: `rc' might be used uninitialized in this function

And this particular class of errors occurs quite often in beginner
programs.
What about the -W flag?

From the gcc man page:

The following -W... options are not implied by -Wall. Some of
them warn about constructions that users generally do not consider
questionable, but which occasionally you might wish to check for;
others warn about constructions that are necessary or hard to
avoid in some cases, and there is no simple way to modify the code
to suppress the warning.

-W Print extra warning messages for these events:
...

I couldn't agree more with the gcc people: some of the -W warnings are
about perfectly good constructs and the only way to shut them up is by
reducing the quality/maintainability of the code (e.g. by introducing
*unneeded* casts).

Whoever decided not to enable everything with -Wall certainly knew
what he was doing.

Dan
 
P

Peter Pichler

statements
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[snip non-standard, off-topic code]
Yes I can explain:

[snip]

Please don't, as it is completely off-topic here. The proper thing to
do with off-topic posts about non-standard extensions is to direct the
poster to a group that supports the compiler/OS combination that
provides them, not to provide off-topic answers here.

Jack, would you please cool down. fork() is OT here indeed, but the OP's
question was about printf(), to which Vijay gave a perfectly topical
answer.

The moral is that the question may still be topical, even if the context
is not.

Peter
 
L

Lew Pitcher

Bob said:
(e-mail address removed) (EPerson) wrote in message

Sorry to get off the topic, but as an absolute beginner teaching
myself to program in C, I would like to know why I can get numbers to
add together either as declared variables, or as mathematical
functions in "printf", but find I cannot get them to multiply or
divide.

Is there an additional switch I need. I have included <math.h> but I
don't think it is necessary for these simple operations.

No, <math.h> isn't necessary for addition, subtraction, multiplication or division.

~/code/aaa $ cat math.c
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
printf("10 + 7 = %d\t",(10 + 7));
printf("10. + 7. = %f\n",(10. + 7.));
printf("10 - 7 = %d\t",(10 - 7));
printf("10. - 7. = %f\n",(10. - 7.));
printf("10 * 7 = %d\t",(10 * 7));
printf("10. * 7. = %f\n",(10. * 7.));
printf("10 / 7 = %d\t",(10 / 7));
printf("10. / 7. = %f\n",(10. / 7.));

return EXIT_SUCCESS;
}

~/code/aaa $ cc -o math math.c
~/code/aaa $ math
10 + 7 = 17 10. + 7. = 17.000000
10 - 7 = 3 10. - 7. = 3.000000
10 * 7 = 70 10. * 7. = 70.000000
10 / 7 = 1 10. / 7. = 1.428571

Seems simple enough, though.

HTH

--
Lew Pitcher

Master Codewright and JOAT-in-training
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
 
M

Mark McIntyre

On 30 Jan 2004 02:24:48 -0800, in comp.lang.c ,
The problem is in a text book I am using, namely to find out
"5/12*68".

My coding is as follows

answer = 5/12*68;

This is a FAQ. Summary: 5,12, and 68 are all integers. So the copmiler
does integer maths. 5/12 = 0.
If you want to do floating point maths, use floating point values.
answer = 5.0/12.0*68.0;
As I said, I'd appreciate knowing why the program won't allow * or /
to work.

They're working perfectly, you're jusst using them wrong !
 
B

Bob Crowley

Lew Pitcher said:
No, <math.h> isn't necessary for addition, subtraction, multiplication or division.

~/code/aaa $ cat math.c
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
printf("10 + 7 = %d\t",(10 + 7));
printf("10. + 7. = %f\n",(10. + 7.));
printf("10 - 7 = %d\t",(10 - 7));
printf("10. - 7. = %f\n",(10. - 7.));
printf("10 * 7 = %d\t",(10 * 7));
printf("10. * 7. = %f\n",(10. * 7.));
printf("10 / 7 = %d\t",(10 / 7));
printf("10. / 7. = %f\n",(10. / 7.));

return EXIT_SUCCESS;
}

~/code/aaa $ cc -o math math.c
~/code/aaa $ math
10 + 7 = 17 10. + 7. = 17.000000
10 - 7 = 3 10. - 7. = 3.000000
10 * 7 = 70 10. * 7. = 70.000000
10 / 7 = 1 10. / 7. = 1.428571

Seems simple enough, though.

HTH


Thanks for your help everybody. I simply changed my input to include
a decimal place eg. 5.0, 12.0, 68.0 and it worked perfectly. I'm a
bit bamboozled why the compiler does not assume floating point maths
when the integer is declared floating point, but I'll learn that as
time goes by. Obviously the problem was included for this reason.

I also tried including the -Wall switch as suggested, but seemed to
come up with a lot of verbiage which meant little to me in my
programming infancy, and the program worked when I tried it anyway.

Bob Crowley
 
C

CBFalconer

Bob said:
.... snip ...

Thanks for your help everybody. I simply changed my input to include
a decimal place eg. 5.0, 12.0, 68.0 and it worked perfectly. I'm a
bit bamboozled why the compiler does not assume floating point maths
when the integer is declared floating point, but I'll learn that as
time goes by. Obviously the problem was included for this reason.

If you pour an arbitrary liquid into your car engine, does it
magically change to oil or water depending on which orifice you
are filling?
I also tried including the -Wall switch as suggested, but seemed to
come up with a lot of verbiage which meant little to me in my
programming infancy, and the program worked when I tried it anyway.

Find out what that verbiage means and fix it, or you will remain
in your infancy.
 

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,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top