Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

tricky question on strings!! please comment

44 views
Skip to first unread message

Rohit Dhamija

unread,
Aug 21, 2004, 12:13:03 AM8/21/04
to
Couldn't come up with the logic of the below program
What will be the output of the following program :
int main()
{
printf("%d"+1,123);
return(0);
}
Ans comes out to be 2


Regards,
Rohit
--
comp.lang.c.moderated - moderation address: cl...@plethora.net

Martin Ambuhl

unread,
Aug 26, 2004, 3:39:57 PM8/26/04
to
Rohit Dhamija wrote:

> Couldn't come up with the logic of the below program
> What will be the output of the following program :
> int main()
> {
> printf("%d"+1,123);
> return(0);
> }
> Ans comes out to be 2

Turn the compiler diagnostics back on and fix your mistakes.

Jonathan Leffler

unread,
Aug 26, 2004, 3:40:11 PM8/26/04
to
Rohit Dhamija wrote:

> Couldn't come up with the logic of the below program
> What will be the output of the following program :
> int main()
> {
> printf("%d"+1,123);
> return(0);
> }
> Ans comes out to be 2


Are you sure? It should produce a 'd', all on its own, with no
newline after it (and does on MacOS X). Technically, it is malformed
since printf() is a varargs function and must, therefore, have a
prototype in scope (#include <stdio.h>), but otherwise OK, if a little
odd.

The string plus one effectively means that the pointer passed to
printf() is the second character of the string - the 'd'. And, since
there is no % in front of the 'd' (as far as printf() can tell), it
just copies that letter to its output.

--
Jonathan Leffler #include <disclaimer.h>
Email: jlef...@earthlink.net, jlef...@us.ibm.com
Guardian of DBD::Informix v2003.04 -- http://dbi.perl.org/

Barry Schwarz

unread,
Aug 26, 2004, 3:41:05 PM8/26/04
to
On 21 Aug 2004 04:13:03 GMT, rohit_...@rediffmail.com (Rohit
Dhamija) wrote:

>Couldn't come up with the logic of the below program
>What will be the output of the following program :
> int main()
> {
> printf("%d"+1,123);
> return(0);
> }
>Ans comes out to be 2

Not on my system. The output is d as expected. Evaluate the first
argument to determine why.


<<Remove the del for email>>

Douglas A. Gwyn

unread,
Aug 26, 2004, 3:41:10 PM8/26/04
to
Rohit Dhamija wrote:
> What will be the output of the following program :
> int main()
> {
> printf("%d"+1,123);
> return(0);
> }
> Ans comes out to be 2

Really? I would have expected the letter "d" (and
no terminating newline). Maybe the platform you're
running this on goes bonkers since you neglected to
#include <stdio.h>, so the wrong linkage is used
for the printf function.

Walter Briscoe

unread,
Aug 26, 2004, 3:41:15 PM8/26/04
to
In message <clcm-2004...@plethora.net> of Sat, 21 Aug 2004
04:13:03 in comp.lang.c.moderated, Rohit Dhamija
<rohit_...@rediffmail.com> writes

>Couldn't come up with the logic of the below program
>What will be the output of the following program :
> int main()
> {
> printf("%d"+1,123);
> return(0);
> }
>Ans comes out to be 2

I think the moderator needs authority to reject that sort of rubbish!

Your file should read:
#include <stdio.h>
int main()
...

In what environment - compiler and machine - does it output 2?
It should output d.
Lacking that #include line, you are pushing your luck as your call is
equivalent to printf((int)("%d"+1),(int)123);
I see no reason why d is not output and see no reason to guess.
I suspect you have sizeof(int) != sizeof(char*).
You may care to use printf("%s","%d"+1)
It shows that "%d"+1 is equivalent to "d".
The construct is stupid at first sight.
--
Walter Briscoe

Brian Inglis

unread,
Aug 26, 2004, 3:41:22 PM8/26/04
to
On 21 Aug 2004 04:13:03 GMT in comp.lang.c.moderated,
rohit_...@rediffmail.com (Rohit Dhamija) wrote:

>Couldn't come up with the logic of the below program
>What will be the output of the following program :
> int main()
> {
> printf("%d"+1,123);
> return(0);
> }
>Ans comes out to be 2

If you take a pointer to the string "%d" and increment it, what will
the pointer point to? What happens when you pass that pointer to
printf()?

--
Thanks. Take care, Brian Inglis Calgary, Alberta, Canada

Brian....@CSi.com (Brian dot Inglis at SystematicSw dot ab dot ca)
fake address use address above to reply

Steve Greenland

unread,
Aug 26, 2004, 3:41:24 PM8/26/04
to
According to Rohit Dhamija <rohit_...@rediffmail.com>:

> Couldn't come up with the logic of the below program
> What will be the output of the following program :
> int main()
> {
> printf("%d"+1,123);
> return(0);
> }
> Ans comes out to be 2

Aside from the fact that it's not valid C (no declaration of "printf()"
via stdio.h, bad declaration of main()), I get what you probably
expected: the single character 'd' (with gcc 3.3.4, on a Linux box). But
from strictly legalistic point, it can be anything at all, since the
code is invalid.

Before posting, you really should run the code through -Wall (or your
compilers equivalent "whine about everything" mode).

Steve
--
Steve Greenland
The irony is that Bill Gates claims to be making a stable operating
system and Linus Torvalds claims to be trying to take over the
world. -- seen on the net

Kenneth Brody

unread,
Aug 26, 2004, 3:41:26 PM8/26/04
to
Rohit Dhamija wrote:
>
> Couldn't come up with the logic of the below program
> What will be the output of the following program :
> int main()
> {
> printf("%d"+1,123);
> return(0);
> }
> Ans comes out to be 2

Technically, this program produces undefined behavior, as the last thing
output does not end in '\n', so I wouldn't says your compiler is "broken".
But, if that's really what you get, it's probably seriously funky.

The above program is equivalent to:

int main()
{
printf("d",123);
return(0);
}

On most systems, you would probably end up with a single lower-case "d"
being output. Others would probably end up with no output at all.

For example, my Unix system shows:

# cc -o foo foo.c
# ./foo
d#

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+

Cesar Rabak

unread,
Aug 26, 2004, 3:41:27 PM8/26/04
to
Rohit Dhamija escreveu:

> Couldn't come up with the logic of the below program
> What will be the output of the following program :
> int main()
> {
> printf("%d"+1,123);
> return(0);
> }
> Ans comes out to be 2
>
Why?

Lucky

unread,
Aug 26, 2004, 3:41:30 PM8/26/04
to
Hello,
When I compile and run I get output as d
I used gcc compiler for windows.

Regards,
Lucky


rohit_...@rediffmail.com (Rohit Dhamija) wrote in message news:<clcm-2004...@plethora.net>...

Roger Leigh

unread,
Aug 26, 2004, 3:41:32 PM8/26/04
to
rohit_...@rediffmail.com (Rohit Dhamija) writes:

> Couldn't come up with the logic of the below program
> What will be the output of the following program :
> int main()
> {
> printf("%d"+1,123);
> return(0);
> }
> Ans comes out to be 2

My answer comes out as 'd', as I expected from reading it:

[After adding "#include <stdio.h>]
$ i386-linux-gcc-3.4 -Wall -ansi -pedantic -o test test.c
test.c: In function `main':
test.c:5: warning: too many arguments for format
$ ./test
d

This is because "%d" is a null-terminated string, of type "const char
*", which is a pointer to the first character, i.e. '%'. Adding +1 to
the pointer will make it point to the second character, i.e. 'd'.
This is why 'd' is printed, and it's no longer a format string, which
is why the compiler complains. The printf function sees "d\0" rather
than "%d\0".

I'm unsure why you get '2' printed: I think the code is bad style,
since the second argument is not used, but it's not undefined
behaviour. Which compiler are you using? Are you sure you compiled
the example properly?


Regards,
Roger

--
Roger Leigh

Printing on GNU/Linux? http://gimp-print.sourceforge.net/
GPG Public Key: 0x25BFB848. Please sign and encrypt your mail.

Jack Klein

unread,
Aug 26, 2004, 3:41:36 PM8/26/04
to
On 21 Aug 2004 04:13:03 GMT, rohit_...@rediffmail.com (Rohit
Dhamija) wrote in comp.lang.c.moderated:

> Couldn't come up with the logic of the below program
> What will be the output of the following program :
> int main()
> {
> printf("%d"+1,123);
> return(0);
> }
> Ans comes out to be 2

There is no logic to what you say was output. Assuming that you
actually included <stdio.h> to prototype printf() and avoided
undefined behavior, the output should have been the single character
'd'. On some platforms that would not show up because you did not
terminate it with a newline.

Your printf() call includes the string literal "%d", which exists in
memory as an unnamed static array of three characters:

'%' 'd' '\0'

The expression "%d"+1 creates a pointer to the character 'd' in that
string, which is equivalent to calling printf() like this:

printf("d", 123);

Extra trailing arguments to printf() are ignored. If anything at all
appears on the output, and it is not the single character 'd', your
library's implementation of printf() is broken.

--
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/~ajo/docs/FAQ-acllc.html

Dietmar Schindler

unread,
Aug 26, 2004, 3:41:38 PM8/26/04
to
Rohit Dhamija wrote:
>
> Couldn't come up with the logic of the below program
> What will be the output of the following program :
> int main()
> {
> printf("%d"+1,123);
> return(0);
> }
> Ans comes out to be 2

I don't believe your answer.

David Resnick

unread,
Aug 26, 2004, 3:41:52 PM8/26/04
to
Rohit Dhamija wrote:
> Couldn't come up with the logic of the below program
> What will be the output of the following program :
> int main()
> {
> printf("%d"+1,123);
> return(0);
> }
> Ans comes out to be 2
>
>
> Regards,
> Rohit

"%d"+1 should give you a pointer to a string "d", which should print as
d
Having an excess argument (123) is not a problem, unlike the reverse
case where you don't have enough arguments to satisfy the format
statement. Assuming that you don't get into trouble for not terminating
your string with "\n"...

-David

Kevin D. Quitt

unread,
Aug 26, 2004, 3:43:31 PM8/26/04
to
On 21 Aug 2004 04:13:03 GMT, rohit_...@rediffmail.com (Rohit Dhamija)
wrote:


>Couldn't come up with the logic of the below program
>What will be the output of the following program :
> int main()
> {
> printf("%d"+1,123);
> return(0);
> }
>Ans comes out to be 2

That answer is wrong. What gets printed is "d".

--
#include <standard.disclaimer>
_
Kevin D Quitt USA 91387-4454 96.37% of all statistics are made up
Per the FCA, this address may not be added to any commercial mail list

kyle york

unread,
Aug 26, 2004, 3:43:34 PM8/26/04
to
Greetings,

Rohit Dhamija wrote:
> Couldn't come up with the logic of the below program
> What will be the output of the following program :
> int main()
> {
> printf("%d"+1,123);
> return(0);
> }

"%d"+1 makes this the same as:

printf("d", 123);

so only 'd' is written, but...it's lacking a newline and/or fflush() so
there's no guarentee of any output.

Someone will likely point out the lack of #inclde <stdio.h> but I think
that can be implied.

> Ans comes out to be 2

That's odd.

--
Kyle A. York
Sr. Subordinate Grunt

kgold

unread,
Aug 26, 2004, 3:43:37 PM8/26/04
to
Answer comes to d on my system as I expected.

The format string "%d" is
'%' 'd' '\0'
Adding one to that pointer gives
'd' '\0'
and so it prints the d.

gcc also gives a warning about the extra 123 parameter.

rohit_...@rediffmail.com (Rohit Dhamija) writes:
> Couldn't come up with the logic of the below program
> What will be the output of the following program :
> int main()
> {
> printf("%d"+1,123);
> return(0);
> }
> Ans comes out to be 2

Ceol Mor

unread,
Aug 26, 2004, 3:43:46 PM8/26/04
to
I get the answer to be d.

This is reasonable because +1 in "%d"+1 increments the char pointer to
the next element. So printf("%d"+1,123) is equivelent to printf("d",123)

Rohit Dhamija wrote:
> Couldn't come up with the logic of the below program
> What will be the output of the following program :
> int main()
> {
> printf("%d"+1,123);
> return(0);
> }
> Ans comes out to be 2

Rohit Dhamija

unread,
Aug 26, 2004, 3:43:48 PM8/26/04
to
One correction in my previous post.

The output is d, but donot know why ?

Regards,
Rohit
rohit_...@rediffmail.com (Rohit Dhamija) wrote in message news:<clcm-2004...@plethora.net>...

Magnus Therning

unread,
Aug 26, 2004, 3:44:09 PM8/26/04
to
Rohit Dhamija <rohit_...@rediffmail.com> wrote:
> Couldn't come up with the logic of the below program
> What will be the output of the following program :
> int main()
> {
> printf("%d"+1,123);
> return(0);
> }
> Ans comes out to be 2

I get a "d" on my system:

Debian GNU/Linux
gcc v. 3.3.4

/M

--
-----------------------------------------------------------------------
Magnus Therning Philips Research Laboratories Eindhoven
Phone: +31 40 2745179 (OpenPGP: 0x4FBB2C40)

X-Windows: ...Flaky and built to stay that way.

A. Sinan Unur

unread,
Aug 26, 2004, 3:44:12 PM8/26/04
to
rohit_...@rediffmail.com (Rohit Dhamija) wrote in news:clcm-20040820-
00...@plethora.net:

> Couldn't come up with the logic of the below program
> What will be the output of the following program :

You need

#include <stdio.h>

here if you are going to use printf below.

> int main()

int main(void)

> {
> printf("%d"+1,123);

The line above tells printf to use the string "d" as the format string.
Hence, if the prototype is in scope (you have included stdio.h), you should
only see the letter d printed. This was the case for me with VC++ on Win2K
and gcc 3.3.3 on FreeBSD. gcc also helpfully told me:

t.c: In function `main':


t.c:5: warning: too many arguments for format

> return(0);


> }
> Ans comes out to be 2

Or 42.

Sinan.

Anuradha

unread,
Aug 26, 2004, 3:44:14 PM8/26/04
to
rohit_...@rediffmail.com (Rohit Dhamija) wrote in message news:<clcm-2004...@plethora.net>...

> Couldn't come up with the logic of the below program
> What will be the output of the following program :
> int main()
> {
> printf("%d"+1,123);
> return(0);
> }
> Ans comes out to be 2
>
>
> Regards,
> Rohit


For me the output is only d and not 2. hope this answers your question ?

-anu

Bill Wendling

unread,
Aug 26, 2004, 3:44:15 PM8/26/04
to
Rohit Dhamija wrote:
> Couldn't come up with the logic of the below program
> What will be the output of the following program :
> int main()
> {
> printf("%d"+1,123);
> return(0);
> }
> Ans comes out to be 2
>
I got d when I compiled it with GCC, which is what I'd expect. Don't
know why you're getting 2. Maybe it's having problems with the printf
pushing something on the stack that it's not using? It's really just
guess work right at the mo...

-bw

Mark Weaver

unread,
Aug 26, 2004, 3:44:17 PM8/26/04
to
Rohit Dhamija wrote:

> Couldn't come up with the logic of the below program
> What will be the output of the following program :
> int main()
> {
> printf("%d"+1,123);
> return(0);
> }

d

Basically you are adding 1 to the pointer to the start of the string
"%d", advancing it by one character, hence giving you the string "d".

This can be seen more clearly by rewriting the program as:

#include <stdio.h>

int main()
{
char *fmt = "%d";
char *fmt_1 = fmt + 1; /* so fmt_1 = "d" */
printf(fmt_1, 123);
return (0);
}

> Ans comes out to be 2

Did you try it?

Kenneth Brody

unread,
Aug 30, 2004, 12:59:48 AM8/30/04
to
Rohit Dhamija wrote:
>
> One correction in my previous post.
>
> The output is d, but donot know why ?
[...]

> > int main()
> > {
> > printf("%d"+1,123);
> > return(0);
> > }
[...]

Numerous people have explained why "d" is the correct output. What would
you expect instead, and why would you expect it?

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+

Jack Klein

unread,
Aug 30, 2004, 1:00:54 AM8/30/04
to
On 26 Aug 2004 19:41:24 GMT, ste...@moregruel.net (Steve Greenland)
wrote in comp.lang.c.moderated:

> According to Rohit Dhamija <rohit_...@rediffmail.com>:
> > Couldn't come up with the logic of the below program
> > What will be the output of the following program :
> > int main()
> > {
> > printf("%d"+1,123);
> > return(0);
> > }
> > Ans comes out to be 2
>
> Aside from the fact that it's not valid C (no declaration of "printf()"
> via stdio.h, bad declaration of main()), I get what you probably

[snip]

No it is not, it is a perfectly valid declaration of main(). It does
not provide a prototype, but is perfectly legal.

Jack Klein

unread,
Aug 30, 2004, 1:00:57 AM8/30/04
to
On 26 Aug 2004 19:41:26 GMT, Kenneth Brody <kenbro...@spamcop.net>
wrote in comp.lang.c.moderated:

> Rohit Dhamija wrote:
> >
> > Couldn't come up with the logic of the below program
> > What will be the output of the following program :
> > int main()
> > {
> > printf("%d"+1,123);
> > return(0);
> > }
> > Ans comes out to be 2
>
> Technically, this program produces undefined behavior, as the last thing
> output does not end in '\n', so I wouldn't says your compiler is "broken".

[snip]

That's implementation-defined, not undefined behavior. The missing
prototype for printf(), if truly not present, causes undefined
behavior.

Jack Klein

unread,
Aug 30, 2004, 1:01:01 AM8/30/04
to
On 26 Aug 2004 19:44:12 GMT, "A. Sinan Unur"
<1u...@llenroc.ude.invalid> wrote in comp.lang.c.moderated:

> rohit_...@rediffmail.com (Rohit Dhamija) wrote in news:clcm-20040820-
> 00...@plethora.net:
>
> > Couldn't come up with the logic of the below program
> > What will be the output of the following program :
>
> You need
>
> #include <stdio.h>
>
> here if you are going to use printf below.
>
> > int main()
>
> int main(void)

Perfectly correct as written by the OP. Your correction without
additional explanation suggest it is some sort of error, which it is
not.

Thad Smith

unread,
Aug 30, 2004, 1:01:30 AM8/30/04
to
Walter Briscoe wrote:

> >What will be the output of the following program :
> > int main()
> > {
> > printf("%d"+1,123);
> > return(0);
> > }
> >Ans comes out to be 2
>
> I think the moderator needs authority to reject that sort of rubbish!

He has it.



> Your file should read:
> #include <stdio.h>
> int main()
> ...
>
> In what environment - compiler and machine - does it output 2?
> It should output d.
> Lacking that #include line, you are pushing your luck as your call is
> equivalent to printf((int)("%d"+1),(int)123);

No, it is equivalent to printf ("%d"+1, 123), with a definition of
int printf (char*, int);

An incompatible calling mechanism may be used with a variadic function.

> The construct is stupid at first sight.

I have learned a lot in the past by trying stupid or invalid things and
seeing what happens. The question was not "what is the best way to code
X?", but rather "why does this simple code do X?". It is one way to
gain an understanding (appreciation?) for the choices of implementations
and the language standard.

Thad

Kenneth Brody

unread,
Sep 2, 2004, 2:22:03 PM9/2/04
to
Jack Klein wrote:
[...]

> > Technically, this program produces undefined behavior, as the last thing
> > output does not end in '\n', so I wouldn't says your compiler is "broken".
>
> [snip]
>
> That's implementation-defined, not undefined behavior. The missing
> prototype for printf(), if truly not present, causes undefined
> behavior.

Ah. Gotcha.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+

Dave Thompson

unread,
Sep 10, 2004, 1:15:37 AM9/10/04
to
On 30 Aug 2004 05:00:57 GMT, Jack Klein <jack...@spamcop.net> wrote:

> On 26 Aug 2004 19:41:26 GMT, Kenneth Brody <kenbro...@spamcop.net>
> wrote in comp.lang.c.moderated:

<snip>


> > Technically, this program produces undefined behavior, as the last thing
> > output does not end in '\n', so I wouldn't says your compiler is "broken".
>
> [snip]
>

> That's implementation-defined, not undefined behavior. <snip>

Nit: It's implementation-defined (and hence documented) whether the
implementation "requires a terminating new-line [on the last line]".

Nothing says it's implementation-defined, or anything else, what
happens if it is required and the program tries to violate it. And
although clearly not a constraint, this is not worded as a "shall"
whose violation would undisputably be UB. I think we have UB by
omission (the most annoying kind); and I think an even half decent
implementation that uses this rather rare and unexpected to many C
programmers option should document it as QoI, but that's not official.

- David.Thompson1 at worldnet.att.net

0 new messages