473,657 Members | 2,667 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1734
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******@tatael xsi.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.li nux.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.l earn.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.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #4
ma******@tatael xsi.co.in (Manohar S) wrote in message news:<26******* *************** ****@posting.go ogle.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******@safeac cess.com (EPerson) wrote in message news:<6e******* *************** ****@posting.go ogle.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********@optu snet.com.au (Bob Crowley) wrote:
es******@safea ccess.com (EPerson) wrote in message news:<6e******* *************** ****@posting.go ogle.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.c om/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**********@s pamcop.net> wrote in message news:<jv******* *************** **********@4ax. com>...
On 29 Jan 2004 05:55:19 -0800, in comp.lang.c ,
bo********@optu snet.com.au (Bob Crowley) wrote:
es******@safea ccess.com (EPerson) wrote in message news:<6e******* *************** ****@posting.go ogle.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.c om/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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2073
by: Rachel Forder | last post by:
Hi All, I have a problem related to the sort function provided by STL. class A{ A(string, string, int); string itemA; string itemB; int itemC; };
31
2166
by: muralipmanohar | last post by:
Hello all , I need a help on this code kindly help me out for the below code I worked on the Turboc the result I was expecting was different from what has been printed I have indicated the line with " /*this one*/" line no 10 I did expected some junk for the first %ld and 6553 for the second %d ,but it printed in different way I am also pasting the result printed , it is below the code . I am totally confused because I got one answer...
16
1879
by: Guenther Sohler | last post by:
I have following code: int main(void) { printf("%.3lf\n",-2158470*0.001); } it prints -2158.470
2
2304
by: Jude | last post by:
here is the source code: #include<stdio.h> int main() { float f; scanf("%d%f",&f); printf("The float is %10.5f\n",f); return 0; } when I input 12345.11111,the output is 12345.11133.
2
1850
by: ThunderMusic | last post by:
hi, I have 2 services running, one doing a job and the other monitoring the job is done and that the other service (the one doing the job) is still running. The thing is, the 1st service fire some events notifying other programs that an alert happened... I want to register to that event in my second service, but I just can't get it working... I receive the following message : Type System.DelegateSerializationHolder and the types...
2
3916
by: ApexData | last post by:
Access2000, using a continuous form. I’m getting a message that say “you cannot add or change a record because a related record is required in table Employee”. This occurs in all my combobox fields that have referential integrity linked to another table. I do not have the field in the table set as required, and I do not want to require the entry. If the record is filled out completely and added to the table, you can go back to the...
0
8315
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8829
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8508
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7341
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6172
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5633
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4323
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2733
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1627
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.