473,326 Members | 2,126 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,326 software developers and data experts.

Printf question

Hi CLCers,
The program below prints some number and i dont
understand why it is behaving like this. I am not supplying
the necessary arguments to the printf function and
according to my understanding the program shiould throw
some error. Please clear my doubts.

#include<stdio.h>
int main()
{
printf("%d");
return 0;
}

Thanks in advance.

Sha
Nov 14 '05 #1
11 1802
an*******@coolgroups.com scribbled the following:
Hi CLCers,
The program below prints some number and i dont
understand why it is behaving like this. I am not supplying
the necessary arguments to the printf function and
according to my understanding the program shiould throw
some error. Please clear my doubts. #include<stdio.h>
int main()
{
printf("%d");
return 0;
}


You are creating undefined behaviour. printf() expects an argument,
because of the format string, but you didn't supply one. This, by
itself, creates undefined behaviour, so anything might happen,
including printing random numbers. Most probably it's reading a
value from the top of the stack, but whatever value that is is
implementation-defined.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Roses are red, violets are blue, I'm a schitzophrenic and so am I."
- Bob Wiley
Nov 14 '05 #2
Joona I Palaste wrote:

an*******@coolgroups.com scribbled the following:
Hi CLCers,
The program below prints some number and i dont
understand why it is behaving like this. I am not supplying
the necessary arguments to the printf function and
according to my understanding the program shiould throw
some error. Please clear my doubts.

#include<stdio.h>
int main()
{
printf("%d");
return 0;
}


You are creating undefined behaviour. printf() expects an argument,
because of the format string, but you didn't supply one. This, by
itself, creates undefined behaviour, so anything might happen,
including printing random numbers. Most probably it's reading a
value from the top of the stack, but whatever value that is is
implementation-defined.


It's undefined.
It need not be documented and it need not be repeatable.

--
pete
Nov 14 '05 #3
pete <pf*****@mindspring.com> scribbled the following:
Joona I Palaste wrote:
an*******@coolgroups.com scribbled the following:
> Hi CLCers,
> The program below prints some number and i dont
> understand why it is behaving like this. I am not supplying
> the necessary arguments to the printf function and
> according to my understanding the program shiould throw
> some error. Please clear my doubts.
> #include<stdio.h>
> int main()
> {
> printf("%d");
> return 0;
> }


You are creating undefined behaviour. printf() expects an argument,
because of the format string, but you didn't supply one. This, by
itself, creates undefined behaviour, so anything might happen,
including printing random numbers. Most probably it's reading a
value from the top of the stack, but whatever value that is is
implementation-defined.

It's undefined.
It need not be documented and it need not be repeatable.


That's why I said "you are creating undefined behaviour", and that's
also why I said "most probably" instead of "certainly".

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"'I' is the most beautiful word in the world."
- John Nordberg
Nov 14 '05 #4
an*******@coolgroups.com spoke thus:
int main()
{
printf("%d");
return 0;
}


Your program produces undefined behavior in two ways - one, by failing
to specify an argument for the %d conversion specifier (as others
noted), and two, by failing to supply a newline at the end of your
program's output. Invoking undefined behavior means that the
implementation is free to do whatever it wants, up to and including
perpetrating terrorist acts.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #5
Christopher Benson-Manica <at***@nospam.cyberspace.org> scribbled the following:
an*******@coolgroups.com spoke thus:
int main()
{
printf("%d");
return 0;
}
Your program produces undefined behavior in two ways - one, by failing
to specify an argument for the %d conversion specifier (as others
noted), and two, by failing to supply a newline at the end of your
program's output. Invoking undefined behavior means that the
implementation is free to do whatever it wants, up to and including
perpetrating terrorist acts.


Failing to supply a newline at the end of the program's output causes
undefined behaviour? Chapter and verse, please?

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"You will be given the plague."
- Montgomery Burns
Nov 14 '05 #6
Joona I Palaste <pa*****@cc.helsinki.fi> spoke thus:
Failing to supply a newline at the end of the program's output causes
undefined behaviour? Chapter and verse, please?


Um... can I quote from the book of Revelation? ;( Apparently I
(badly!) mis-recalled previous discussion on the topic. Thanks for
the cluon...

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #7
Joona I Palaste wrote:

pete <pf*****@mindspring.com> scribbled the following:
Joona I Palaste wrote:
an*******@coolgroups.com scribbled the following:
> Hi CLCers,
> The program below prints some number and i dont
> understand why it is behaving like this. I am not supplying
> the necessary arguments to the printf function and
> according to my understanding the program shiould throw
> some error. Please clear my doubts.

> #include<stdio.h>
> int main()
> {
> printf("%d");
> return 0;
> }

You are creating undefined behaviour. printf() expects an argument,
because of the format string, but you didn't supply one. This, by
itself, creates undefined behaviour, so anything might happen,
including printing random numbers. Most probably it's reading a
value from the top of the stack, but whatever value that is is
implementation-defined.

It's undefined.
It need not be documented and it need not be repeatable.


That's why I said "you are creating undefined behaviour",
and that's also why I said "most probably" instead of "certainly".


There is nothing about the value at the top of the stack
(assuming a stack)
which suggests that it is an implementation defined value.

--
pete
Nov 14 '05 #8
Joona I Palaste wrote:

Christopher Benson-Manica <at***@nospam.cyberspace.org> scribbled the following:
an*******@coolgroups.com spoke thus:
int main()
{
printf("%d");
return 0;
}

Your program produces undefined behavior in two ways
- one, by failing
to specify an argument for the %d conversion specifier (as others
noted), and two, by failing to supply a newline at the end of your
program's output. Invoking undefined behavior means that the
implementation is free to do whatever it wants, up to and including
perpetrating terrorist acts.


Failing to supply a newline at the end of the program's output causes
undefined behaviour? Chapter and verse, please?


It's from the definition of "stream".

Whether or not the last line of a text stream needs to be
newline terminated is implementation defined.

If it isn't newline terminated, then it isn't a portable stream.
If it isn't a stream, then what is it ?

--
pete
Nov 14 '05 #9
In <bu*********@oravannahka.helsinki.fi> Joona I Palaste <pa*****@cc.helsinki.fi> writes:
an*******@coolgroups.com scribbled the following:
Hi CLCers,
The program below prints some number and i dont
understand why it is behaving like this. I am not supplying
the necessary arguments to the printf function and
according to my understanding the program shiould throw
some error. Please clear my doubts.

#include<stdio.h>
int main()
{
printf("%d");
return 0;
}


You are creating undefined behaviour. printf() expects an argument,
because of the format string, but you didn't supply one. This, by
itself, creates undefined behaviour, so anything might happen,
including printing random numbers. Most probably it's reading a
value from the top of the stack, but whatever value that is is
implementation-defined. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

^^^^^^^^^^^^^^^^^^^^^^
Nope, it ain't. It's *unspecified*, but this doesn't matter, because
undefined behaviour has *already* been invoked.

Be *very* careful when using terms like "implementation-defined", because
their meaning is precisely defined by the C standard (and the definition
is not always what one would intuitively expect).

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #10
In <bu**********@oravannahka.helsinki.fi> Joona I Palaste <pa*****@cc.helsinki.fi> writes:
Christopher Benson-Manica <at***@nospam.cyberspace.org> scribbled the following:
an*******@coolgroups.com spoke thus:
int main()
{
printf("%d");
return 0;
}

Your program produces undefined behavior in two ways - one, by failing
to specify an argument for the %d conversion specifier (as others
noted), and two, by failing to supply a newline at the end of your
program's output. Invoking undefined behavior means that the
implementation is free to do whatever it wants, up to and including
perpetrating terrorist acts.


Failing to supply a newline at the end of the program's output causes
undefined behaviour? Chapter and verse, please?


2 A text stream is an ordered sequence of characters composed into
lines, each line consisting of zero or more characters
plus a terminating new-line character. Whether the last line
requires a terminating new-line character is implementation-defined.

So, if the implementation requires it, but the program doesn't supply it
you have undefined behaviour by lack of specification of behaviour.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #11
Groovy hepcat Christopher Benson-Manica was jivin' on Mon, 19 Jan 2004
14:54:48 +0000 (UTC) in comp.lang.c.
Re: Printf question's a cool scene! Dig it!
an*******@coolgroups.com spoke thus:
int main()
{
printf("%d");
return 0;
}
Your program produces undefined behavior in two ways - one, by failing

^^^
Would you believe three ways?
to specify an argument for the %d conversion specifier (as others
noted), and two, by failing to supply a newline at the end of your


Three, failing to provide a prototype for printf().

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
Nov 14 '05 #12

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

Similar topics

9
by: Wenjie | last post by:
Hello, Is printf("%d", var); OK for the unsigned long var = SOME_VAR? Or should some other print format be specified? Thanks and best regards, Wenjie
14
by: Mantorok Redgormor | last post by:
Would this by any chance invoke undefined behavior? extern int printf(const char *, ...); int main(void) { printf("Hello\n"); return 0; } That is, providing my own printf prototype would...
188
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
31
by: DeltaOne | last post by:
#include<stdio.h> typedef struct test{ int i; int j; }test; main(){ test var; var.i=10; var.j=20;
12
by: John Devereux | last post by:
Hi, I would like to know if their is a conversion specifier, or other method, that will allow me to not use particular arguments to printf. Failing that, is it permissable to simply "not use"...
11
by: rahul8143 | last post by:
Does printf output depends on compilers or operating systems? I have one program int main() { int i=5; printf("%d %d %d",i++,i++,i++); return 0; } Output of this program on DOS (TC) 7 6 5...
27
by: jacob navia | last post by:
Has anyone here any information about how arrays can be formatted with printf? I mean something besides the usual formatting of each element in a loop. I remember that Trio printf had some...
7
by: Martin Jørgensen | last post by:
Hi, Perhaps a stupid question but I wonder if this isn't possible using C: printf("\nOr push <enter> for default (which is theta=%ld)%s", theta, ": "); It would be nicer to have it on one...
5
by: Why Tea | last post by:
int printf(const char *fmt,...) The fmt is const, indicating that it can't be changed during runtime. If I want to print a series of integers that I only know the largest value beforehand,...
7
by: Rajesh S R | last post by:
printf("%hhd",89);/*Assume char has 8 bits and is signed*/ Is it valid? I know that it has been discussed in comp.std.c. ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.