Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > problem related to printf!! why is it so???

Reply
Thread Tools

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

 
 
Manohar S
Guest
Posts: n/a
 
      01-28-2004
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.....
 
Reply With Quote
 
 
 
 
Vijay Kumar R Zanvar
Guest
Posts: n/a
 
      01-28-2004

> 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/


 
Reply With Quote
 
 
 
 
Jack Klein
Guest
Posts: n/a
 
      01-29-2004
On 28 Jan 2004 01:54:36 -0800, (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
 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      01-29-2004
On Wed, 28 Jan 2004 15:55:18 +0530, "Vijay Kumar R Zanvar"
<> 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
 
Reply With Quote
 
EPerson
Guest
Posts: n/a
 
      01-29-2004
(Manohar S) wrote in message news:<. 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
 
Reply With Quote
 
Vijay Kumar R Zanvar
Guest
Posts: n/a
 
      01-29-2004
[..]
> 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


 
Reply With Quote
 
Bob Crowley
Guest
Posts: n/a
 
      01-29-2004
(EPerson) wrote in message news:<. 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.
 
Reply With Quote
 
Jeremy Yallop
Guest
Posts: n/a
 
      01-29-2004
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.
 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      01-29-2004
On 29 Jan 2004 05:55:19 -0800, in comp.lang.c ,
(Bob Crowley) wrote:

> (EPerson) wrote in message news:<. 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 =---
 
Reply With Quote
 
Bob Crowley
Guest
Posts: n/a
 
      01-30-2004
Mark McIntyre <> wrote in message news:<>. ..
> On 29 Jan 2004 05:55:19 -0800, in comp.lang.c ,
> (Bob Crowley) wrote:
>
> > (EPerson) wrote in message news:<. 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
How should multiple (related) projects be arranged (structured) and configured so that they can share code, have a related package structure and enable proper unittesting, and ensuring no namespace collisions ToddLMorgan@gmail.com Python 14 04-21-2006 04:03 PM
Why am I a t**t? PIX related incident! Marraboy Cisco 1 11-02-2005 09:39 PM
How should threads be terminated? (related to 'Help with thread related tracebacks') Maxwell Hammer Python 7 06-18-2005 04:20 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57