what is the output and why

T

Tushar

hello all,

i would like to know why this code compiles and gives the output

Code 1:
----------
int main(){
printf("String" + 2);
}

Output: ring

Code 2:
----------
int main(){
printf("String", +2);
}

Output: String

I used gcc 3.2 on redhat 8.0 to compile my code

Regards,
Tushar
 
B

Barry Schwarz

hello all,

i would like to know why this code compiles and gives the output

They both compile because there is no syntax error in either.

"String" is an array of 7 char (including the terminating \0). In
this context, the expression "String" evaluates to a pointer to the
first char in the array, the S. Adding 2 to this value produces a
pointer to the third char in the array, the r. Since the first
argument to printf is a string, it prints starting at this character
until it reaches the terminating \0.
}

Output: ring

Code 2:

Here, it prints the first argument as a string. Since there is no
format specification in this string, it never looks at the second
argument.
}

Output: String

I used gcc 3.2 on redhat 8.0 to compile my code
Shouldn't matter.


<<Remove the del for email>>
 
P

Philip Willoughby

Tushar said:
I used gcc 3.2 on redhat 8.0 to compile my code

I'd suggest that for the purpose of learning C you add the following
flags to the gcc command line used to compile your programs:

-Wall -Werror

so instead of using:

gcc -o test test.c

you use:

gcc -o test -Wall -Werror test.c

With these flags, your second example would not compile, and you would
have seen a (slightly) helpful message explaining what went wrong.

Regards,

Phil
 
J

Joona I Palaste

#include <stdio.h>
This is undefined behavior, since there are no
format specifiers, yet you pass an additional
argument.
According to the C standard, this program's
behavior could be anything at all.

No, you're wrong. It's perfectly safe to pass *more* arguments to
printf() than it expects. Passing *less*, however, yields undefined
behaviour.

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"Last year he disrespected me - and then he showed lack of respect."
- Anthony Mason
 
D

Dan Pop

In said:
i would like to know why this code compiles and gives the output

Code 1:

Hard to answer, since this code invokes undefined behaviour, by calling
printf without a proper declaration in scope. Furthermore, due to the
missing newline character after the last line of output, it need not
produce any output at all.
Output: ring

Code 2:

The same comments as above.
Output: String

I'll fix the first example and comment on it. The second one is so
easy that, if you can't figure out the answer by yourself, you should
stop messing with C.

#include <stdio.h>

int main()
{
printf("String\n" + 2);
return 0; /* main is a function returning int, right? */
}

A string literal appearing in a normal expression (other than as the
argument of sizeof) is replaced by a pointer to its first char, 'S' in
our case. You're adding 2 to this pointer, obtaining a pointer pointing
2 char's past 'S', i.e. to 'r'. This pointer is passed to printf as its
argument, which interprets it as its format string and behaves
accordingly. In other words, your printf call is equivalent to
printf("ring\n"), which explains the observed behaviour.

Dan
 
D

Dan Pop

In said:
I'd suggest that for the purpose of learning C you add the following
flags to the gcc command line used to compile your programs:

-Wall -Werror

so instead of using:

gcc -o test test.c

you use:

gcc -o test -Wall -Werror test.c

With these flags, your second example would not compile, and you would
have seen a (slightly) helpful message explaining what went wrong.

The two programs contain the same number of bugs (undefined behaviour +
gratuitous reliance on implementation-defined behaviour); there is nothing
special about the second one.

Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,077
Messages
2,570,566
Members
47,202
Latest member
misc.

Latest Threads

Top