471,073 Members | 1,172 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,073 software developers and data experts.

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

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.....
Nov 14 '05 #1
18 1601
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]

--
Vijay Kumar R Zanvar
My Home Page - http://www.geocities.com/vijoeyz/
Nov 14 '05 #2
On 28 Jan 2004 01:54:36 -0800, ma******@tataelxsi.co.in (Manohar S)
wrote in comp.lang.c:
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 news:comp.unix.programmer or
news:comp.os.linux.development.apps, depending you your platform.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #3
On Wed, 28 Jan 2004 15:55:18 +0530, "Vijay Kumar R Zanvar"
<vi*****@hotpop.com> wrote in comp.lang.c:
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 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?

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #4
ma******@tataelxsi.co.in (Manohar S) wrote in message news:<26**************************@posting.google. com>...
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 <stdio.h> above.
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.

--
Eric Schmidt
Nov 14 '05 #5
[..]
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.

--
Vijay Kumar R Zanvar
Nov 14 '05 #6
es******@safeaccess.com (EPerson) wrote in message news:<6e**************************@posting.google. com>...


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.
Nov 14 '05 #7
Bob Crowley wrote:
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.
Nov 14 '05 #8
On 29 Jan 2004 05:55:19 -0800, in comp.lang.c ,
bo********@optusnet.com.au (Bob Crowley) wrote:
es******@safeaccess.com (EPerson) wrote in message news:<6e**************************@posting.google. com>...


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?
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #9
Mark McIntyre <ma**********@spamcop.net> wrote in message news:<jv********************************@4ax.com>. ..
On 29 Jan 2004 05:55:19 -0800, in comp.lang.c ,
bo********@optusnet.com.au (Bob Crowley) wrote:
es******@safeaccess.com (EPerson) wrote in message news:<6e**************************@posting.google. com>...


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?
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---


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.
Nov 14 '05 #10
Bob Crowley <bo********@optusnet.com.au> scribbled the following:
Mark McIntyre <ma**********@spamcop.net> wrote in message news:<jv********************************@4ax.com>. ..
On 29 Jan 2004 05:55:19 -0800, in comp.lang.c ,
bo********@optusnet.com.au (Bob Crowley) wrote:
>es******@safeaccess.com (EPerson) wrote in message news:<6e**************************@posting.google. com>...
>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?

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;
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?

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"All that flower power is no match for my glower power!"
- Montgomery Burns
Nov 14 '05 #11
In <ad************************@posting.google.com> bo********@optusnet.com.au (Bob Crowley) writes:
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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #12
Dan Pop wrote:
NEVER omit the -Wall -O options when compiling with gcc.


Why do you recommend the -O flag?

What about the -W flag?

Nov 14 '05 #13
In <bv**********@news-rocq.inria.fr> Grumble <in*****@kma.eu.org> writes:
Dan Pop wrote:
NEVER omit the -Wall -O options when compiling with gcc.
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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #14
"Jack Klein" <ja*******@spamcop.net> wrote in message
news:nb********************************@4ax.com...
On Wed, 28 Jan 2004 15:55:18 +0530, "Vijay Kumar R Zanvar"
<vi*****@hotpop.com> wrote in comp.lang.c:
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 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
Nov 14 '05 #15
On 30 Jan 2004 02:24:48 -0800, in comp.lang.c ,
bo********@optusnet.com.au (Bob Crowley) wrote:
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 !

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #16
Bob Crowley wrote:
es******@safeaccess.com (EPerson) wrote in message news:<6e**************************@posting.google. com>...
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.

Nov 14 '05 #17
Lew Pitcher <lp******@sympatico.ca> wrote in message news:<er***********@merlin.l6s4x6-4.ca>...
Bob Crowley wrote:
es******@safeaccess.com (EPerson) wrote in message news:<6e**************************@posting.google. com>...
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

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
Nov 14 '05 #18
Bob Crowley wrote:
.... 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.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #19

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Rachel Forder | last post: by
31 posts views Thread by muralipmanohar | last post: by
16 posts views Thread by Guenther Sohler | last post: by
2 posts views Thread by Jude | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.