printf

Y

yashwant pinge

The following statement
printf("%d %c");

gives the garbage value

Why it is...?
 
R

Richard Tobin

yashwant pinge said:
The following statement
printf("%d %c");

gives the garbage value

Why it is...?

Because you didn't tell it what to print.

-- Richard
 
C

Chris Dollin

yashwant said:
The following statement
printf("%d %c");

gives the garbage value

Why it is...?

You're seriously asking?

OK, it's because the compiler isn't obliged (and in general
can't tell) that you haven't specified enough arguments to
satisfy a format string -- your format string above wants
two arguments (an int for the `%d` and a character [1] for
the `%c`) and you supplied none. At this point the behaviour
becomes undefined, the implementation can do whatever is
convenient, which usually means pretending nothing is wrong
and fetching data from inappropriate locations, yeilding
gibberish.

What /did/ you expect to happen?

[1] which will have been promoted to int.
 
E

Eric Sosman

yashwant pinge wrote On 05/08/07 08:44,:
The following statement
printf("%d %c");

gives the garbage value

Why it is...?

For the same reason that atan2(t) doesn't work, or
strcmp(p) or calloc(42): You didn't provide all the
argument values the function requires, and anything can
happen. Test your math: What is the square root of?

If you have #included the right headers, the compiler
will detect the incorrect argument lists for atan2 and
strcmp and so on. But because printf is a function that
can be called with many different argument lists, the
compiler is not always able to tell when the list you
provide is wrong.
 
K

Kenny McCormack

You're seriously asking?

I think the real problem is simply that he forgot to double the % signs
in the format string. Maybe he doesn't know that % is a "magic"
character in printf (and related functions) format strings, and that to
get a literal %, you have to double it.

To the OP: Did you try:

printf("%%d %%c\n"); /* Note also the 'no trailing newline' nitpick */

Does it do what you want it to?
 
R

ramesh nellore1 nellore

The following statement
printf("%d %c");

gives the garbage value

Why it is...?

In C lang the % character has a specific functionality. This can be
used while printing any data which is stored in variables like that.
Here in this statement " printf("%d %c"); " u not mentioned the
variables, which stores the data( need to print). And u just specified
the format strings.

Thanks & Regards,
 
R

Richard Bos

ramesh nellore1 nellore said:
In C lang the % character has a specific functionality.

Yes, it does, but...
This can be used while printing any data which is stored in variables
like that.

....no, it can't. At least, not in general. In all other contexts in C,
the % character is either part of a digraph, if you have C99, or the
modulus operator, in any version of the language. _Only_ in the specific
context of the format string for the *scanf() and *printf() families
does the % character mean "do an in-/output conversion", and only in the
*printf()s does it (in most cases!) mean "print some data".

It's important to realise that this only works because that's how
printf() are defined, and not because of something special about the %
character itself. For example, puts("%d %c") will work just fine, and it
will print two percentage characters, two letters, one space and one
new-line; the %s will not magically cause the printing of other data.
Here in this statement " printf("%d %c"); " u not mentioned the
variables, which stores the data( need to print).

This, however (except for the schoolboy abbreviation; don't do that if
you want to be taken seriously) is correct.

Richard
 
K

Keith Thompson

Charles Richmond said:
Try it this way:

printf("%%d %%c\n");

You're assuming that the OP wanted to print the percent signs.

He didn't give us enough information to guess what he was actually
trying to do.
 
C

Charles Richmond

Keith said:
You're assuming that the OP wanted to print the percent signs.

He didn't give us enough information to guess what he was actually
trying to do.

Yup, he did *not* give enough information to indicate
what he was trying to accomplish. So I figured I could
make whatever assumption I wanted to make.

If he wants a better answer, he will have to ask a
better question. ;-)
 
K

Kenny McCormack

Yup, he did *not* give enough information to indicate
what he was trying to accomplish. So I figured I could
make whatever assumption I wanted to make.

Try to imagine for a minute what it must be *like* to be KT.

OK, now that you've recovered from what I just put you through, ...

Show a little compassion.
 

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
473,952
Messages
2,570,111
Members
46,695
Latest member
Juliane58C

Latest Threads

Top