473,379 Members | 1,317 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Help me

I HAVE TWO QUESTIONS:

1.WHY DON'T WE NEED TO INCLUDE STDIO.H IN C PROGRAMS?

2.WHAT IS PRINTED WHEN WE EXECUTE THE FOLLOWING PROGRAM:

main()
{
printf("%d %d %d %d");
}

This program actually prints the same value every time you run the
program on a given machine.
What are those values?

Mar 8 '07 #1
9 2315
Nikhil Bokare wrote:
I HAVE TWO QUESTIONS:
All uppercase text is considered rude.
1.WHY DON'T WE NEED TO INCLUDE STDIO.H IN C PROGRAMS?
Because STDIO.H doesn't exist in Standard C. OTOH, stdio.h does,and
it's included in most programs because I/O is a common requirement for
any non-trivial program.
2.WHAT IS PRINTED WHEN WE EXECUTE THE FOLLOWING PROGRAM:

main()
{
printf("%d %d %d %d");
}
It invokes undefined behaviour, so that anything can happen. It might,
on the next run, trash all your music files.
This program actually prints the same value every time you run the
program on a given machine.
What are those values?
The output is irrelevant.

Mar 8 '07 #2
On Mar 8, 9:50 am, "Nikhil Bokare" <nbok...@gmail.comwrote:
I HAVE TWO QUESTIONS:

1.WHY DON'T WE NEED TO INCLUDE STDIO.H IN C PROGRAMS?

2.WHAT IS PRINTED WHEN WE EXECUTE THE FOLLOWING PROGRAM:

main()
{
printf("%d %d %d %d");

}

This program actually prints the same value every time you run the
program on a given machine.
What are those values?
Time to invest in a good C book.
I suggest K&R2. If you can't afford it, get a copy from your local
library.

Mar 8 '07 #3
Nikhil Bokare <nb*****@gmail.comwrote:
I HAVE TWO QUESTIONS:
QUIET DOWN. There's no need to shout here.
2.WHAT IS PRINTED WHEN WE EXECUTE THE FOLLOWING PROGRAM:
"We" aren't executing anything, and apparently neither are you, or you
wouldn't be asking.
main()
{
printf("%d %d %d %d");
}
This program actually prints the same value every time you run the
program on a given machine.
That so? Sounds like you need to find yourself a new instructor.
What are those values?
Well *I* ran the program and it launched nuclear missiles at Moscow,
but I run a DeathStation 9000 and I'm used to such quirks when I run
poorly written C programs.

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Mar 8 '07 #4
Nikhil Bokare <nb*****@gmail.comwrote:
>main()
{
printf("%d %d %d %d");
}
printf() is going to start looking for parameters that correspond to the
%ds, but there are no more, so the actual results could be anything.
>This program actually prints the same value every time you run the
program on a given machine.
For me, it prints different values every time I run it on my machine:

0 -1077652376 134513659 -1208979460
0 -1074653368 134513659 -1208328196
0 -1079993800 134513659 -1208889348
0 -1074364536 134513659 -1208938500

But neither your program's behavior nor my program's behavior should be
entirely surprising, by C99 7.15.1.1p2, "The va_arg macro":

# If there is no actual next argument [...] the behavior is undefined

Anything could have happened on that call to printf(), really.
>What are those values?
Just garbage, presumably whatever was on your stack from before the
call. (But even that depends on the implementation. If you want to get
fun, compile to assembly language and see exactly what it's doing. Um,
but don't post that here. :)

-Beej

Mar 8 '07 #5
Christopher Benson-Manica wrote:
>
Nikhil Bokare <nb*****@gmail.comwrote:
[...]
main()
{
printf("%d %d %d %d");
}
This program actually prints the same value every time you run the
program on a given machine.

That so? Sounds like you need to find yourself a new instructor.
What are those values?

Well *I* ran the program and it launched nuclear missiles at Moscow,
but I run a DeathStation 9000 and I'm used to such quirks when I run
poorly written C programs.
Fortunately, you can run the following on the DS-6000 to cause the
missiles to self-destruct before they arrive at their target:

void main(void) { int i = i++; }

Unfortunately, running that on your DS-9000 will cause the self-
destruct to be disabled.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
Mar 8 '07 #6
Beej Jorgensen <b...@beej.uswrote:
Nikhil Bokare <nbok...@gmail.comwrote:
main()
{
printf("%d %d %d %d");
}

printf() is going to start looking for parameters that correspond
to the %ds, but there are no more, so the actual results could be
anything.
Even if there were arguments, the lack of prototype for printf
means there's no guarantee that it would find them (or even that
printf itself is found!)

<snip>
>
But neither your program's behavior nor my program's behavior
should be entirely surprising, by C99 7.15.1.1p2, "The va_arg
macro":

# If there is no actual next argument [...] the behavior is
undefined
Although it's not an unreasonable assumption, the standard
doesn't actually require printf (et al) to use the va_ macros.
The behaviour is still undefined though because the fprintf
specifications specifically require an int argument to supplied
when the %d conversion specifier is used.
Anything could have happened on that call to printf(), really.
What are those values?

Just garbage, presumably whatever was on your stack from before
the call. (But even that depends on the implementation. If you
want to get fun, compile to assembly language and see exactly
what it's doing.
That's probably what the OP did, and probably why they stated
that all implementations will behave in a certain way. Looking
at assembly to learn C or C behaviour, even for broken programs,
is really is the worst thing you can encourage a newbie to do.
It's just too easy to develop misguided assumptions.

Unfortunately, human nature will make newbies do it anyway.

--
Peter

Mar 8 '07 #7
Peter Nilsson <ai***@acay.com.auwrote:
>Looking at assembly to learn C or C behaviour, even for broken
programs, is really is the worst thing you can encourage a newbie to
do.
Bah. I can think of worse. :)

I agree that trying to learn C behavior from the resulting assembly is a
bad idea in general. I mean, think of how much implementation-defined
behavior is in there! They need to know that the assembly result is
merely one compiler's interpretation of C, and not at all canon.

However, I still think looking at the assembly results of a C program is
a good thing in general. One can learn a lot about systems and
compilers. If he wants to know where that garbage output comes from, no
amount of standard C will answer that question.

Maybe I read him wrong, but my take on this guy was that he wasn't just
flailing at random, but was pushing the limits to see into the black
box.

Besides, if he's going to look in there anyway, might as well offer a
small amount of direction.

-Beej

Mar 9 '07 #8
Beej Jorgensen wrote:
Peter Nilsson <ai***@acay.com.auwrote:
Looking at assembly to learn C or C behaviour, even for broken
programs, is really is the worst thing you can encourage a newbie to
do.
<snip>
Maybe I read him wrong, but my take on this guy was that he wasn't just
flailing at random, but was pushing the limits to see into the black
box.
You're being sarcastic aren't you?

It looked like some sort of trick question, albeit a very poor one.

Mar 9 '07 #9
On Thu, 8 Mar 2007 11:50:11 -0500, Nikhil Bokare wrote
(in article <11*********************@v33g2000cwv.googlegroups. com>):
I HAVE TWO QUESTIONS:
I have one more, did you compose the first half of this on a Commodore
64?
1.WHY DON'T WE NEED TO INCLUDE STDIO.H IN C PROGRAMS?
That depends, for some programs, your question is a trick question, and
for those that you don't need it, the answer is obvious, because you
don't need it. Example: A program (or at least a compilation unit)
where you use none of the things provided which require stdio.h.
2.WHAT IS PRINTED WHEN WE EXECUTE THE FOLLOWING PROGRAM:

main()
{
printf("%d %d %d %d");
}
It depends. It would be nice if the implementation said:

Stupid programmer detected: fire him immediately.

Alas, I doubt it will.
This program actually prints the same value every time you run the
program on a given machine.
For you, maybe.
What are those values?
Why should we guess what happens on your computer?


--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw

Mar 19 '07 #10

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

Similar topics

21
by: Dave | last post by:
After following Microsofts admonition to reformat my system before doing a final compilation of my app I got many warnings/errors upon compiling an rtf file created in word. I used the Help...
9
by: Tom | last post by:
A question for gui application programmers. . . I 've got some GUI programs, written in Python/wxPython, and I've got a help button and a help menu item. Also, I've got a compiled file made with...
6
by: wukexin | last post by:
Help me, good men. I find mang books that introduce bit "mang header files",they talk too bit,in fact it is my too fool, I don't learn it, I have do a test program, but I have no correct doing...
3
by: Colin J. Williams | last post by:
Python advertises some basic service: C:\Python24>python Python 2.4.1 (#65, Mar 30 2005, 09:13:57) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> With...
7
by: Corepaul | last post by:
Missing Help Files When I enter "recordset" as the keyword and search the Visual Basic Help index, I get many topics of interest in the resulting list. But there isn't any information available...
5
by: Steve | last post by:
I have written a help file (chm) for a DLL and referenced it using Help.ShowHelp My expectation is that a developer using my DLL would be able to access this help file during his development time...
8
by: Mark | last post by:
I have loaded Visual Studio .net on my home computer and my laptop, but my home computer has an abbreviated help screen not 2% of the help on my laptop. All the settings look the same on both...
10
by: JonathanOrlev | last post by:
Hello everybody, I wrote this comment in another message of mine, but decided to post it again as a standalone message. I think that Microsoft's Office 2003 help system is horrible, probably...
1
by: trunxnirvana007 | last post by:
'UPGRADE_WARNING: Array has a new behavior. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"' 'UPGRADE_WARNING: Couldn't resolve...
0
by: hitencontractor | last post by:
I am working on .NET Version 2003 making an SDI application that calls MS Excel 2003. I added a menu item called "MyApp Help" in the end of the menu bar to show Help-> About. The application...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.