printf conversion specification

B

bauran

Acc. to C99, in printf(), if any argument is not the correct type for
correspnding conversion specifier,
the behaviour is undefined.

I want to know that does that happen everytime.
I mean in this program
int main()
{
int num=32;
printf("%ld",num);
}

Is it ub?
 
B

Ben Bacarisse

bauran said:
Acc. to C99, in printf(), if any argument is not the correct type for
correspnding conversion specifier,
the behaviour is undefined.

I want to know that does that happen everytime.
I mean in this program
int main()
{
int num=32;
printf("%ld",num);
}

Is it ub?

Yes, several times over! Without a prototype in scope, calling printf
is UB; the type mismatch is another UB; and when a program's final
line does not end with a newline character the effect is unknown --
you may get no output at all. There is also a highly technical debate
about the acceptability of "int main()" rather than "int main(void)".

Being less fussy for a moment, then even this

#include <stdio.h>

int main(void)
{
int num = 32;
printf("%ld\n", num);
}

is UB in C99 which is the question you had in mind.
 
B

bauran

Thank you for your explanation.
But I want to know in this program
int main(void)
{
int num=65;
printf("%c\n",num);
}

num is integer and printf expects char so is it also ub?
I just asked this question because we often print ascii value of an
integer by this method so is it also ub?
 
B

Ben Bacarisse

bauran said:
Thank you for your explanation.
But I want to know in this program
int main(void)
{
int num=65;
printf("%c\n",num);
}

num is integer and printf expects char so is it also ub?
I just asked this question because we often print ascii value of an
integer by this method so is it also ub?

What type does your textbook or C reference say should be passed
when the format is %c? (If you don't have a C reference to work from,
you will have a lot of questions to post!)

[You should include stdio.h, of course, but I suspect you are trying
to be helpful by abbreviating your program.]
 
S

Seebs

Acc. to C99, in printf(), if any argument is not the correct type for
correspnding conversion specifier,
the behaviour is undefined.

I want to know that does that happen everytime.
I mean in this program
int main()
{
int num=32;
printf("%ld",num);
}

Is it ub?

Yes, for two reasons.

1. The argument does not match the conversion specifier. (And yes, even
if two types have the same representation, they can be "different types".)
2. You called a function taking variable arguments without a prototype in
scope.

-s
 
S

Seebs

Thank you for your explanation.
But I want to know in this program
int main(void)
{
int num=65;
printf("%c\n",num);
}
num is integer and printf expects char so is it also ub?
I just asked this question because we often print ascii value of an
integer by this method so is it also ub?

Not normally, but it is when you omit the prototype for printf.

The reason being that printf doesn't expect a char, it expects an int,
because functions taking variable arguments assume the default promotions,
and a character constant is of type int anyway.

So printf takes an int argument, converts it to unsigned char, and writes
the corresponding character.

-s
 

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

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top