b = 2 / a[2]

N

noname_is_me

Sorry for this simple question.

int main(void)
{
unsigned int a[4]={3,5,7},b;
b = 2 / a[2];
printf("%d %d\n",sizeof a,b);
return 0;
}

Is this a UB. I searched through the std and could not able to find out.
 
R

Richard Bos

noname_is_me said:
int main(void)
{
unsigned int a[4]={3,5,7},b;
b = 2 / a[2];
printf("%d %d\n",sizeof a,b);
return 0;
}

Is this a UB.

Apart from the lack of <stdio.h> and the wrong printf() specifier for
sizeof, no. Corrected to

#include <stdio.h>
int main(void)
{
unsigned int a[4]={3,5,7}, b;
b = 2 / a[2];
printf("%d %d\n", (int)sizeof a, b);
return 0;
}

it should always print 4*sizeof(int) and 0; that is, on most desktop
machines, it will print "16 0".

Why would you think there is any UB?

Richard
 
N

noname_is_me

Richard said:
int main(void)
{
unsigned int a[4]={3,5,7},b;
b = 2 / a[2];
printf("%d %d\n",sizeof a,b);
return 0;
}

Is this a UB.

Apart from the lack of <stdio.h> and the wrong printf() specifier for
sizeof, no. Corrected to

#include <stdio.h>
int main(void)
{
unsigned int a[4]={3,5,7}, b;
b = 2 / a[2];
printf("%d %d\n", (int)sizeof a, b);
return 0;
}

it should always print 4*sizeof(int) and 0; that is, on most desktop
machines, it will print "16 0".

Why would you think there is any UB?
Apart from including stdio.h and wrong format specified in the printf
I would like to know what happens when Multiplicative operators,
additive operators
used like the above assignment of the variable b?
 
C

CBFalconer

noname_is_me said:
Sorry for this simple question.

int main(void)
{
unsigned int a[4]={3,5,7},b;
b = 2 / a[2];
printf("%d %d\n",sizeof a,b);
return 0;
}

Is this a UB. I searched through the std and could not able to find out.

Yes. You are using %d to print something of type size_t. In C99
use %z. In C90 use %lu and cast the value to unsigned long, i.e:

printf("%lu %d\n", (unsigned int)sizeof a, b);

Lying about the type of parameters in a variatic function call is
always UB.
 
J

Joona I Palaste

noname_is_me said:
Richard said:
noname_is_me said:
int main(void)
{
unsigned int a[4]={3,5,7},b;
b = 2 / a[2];
printf("%d %d\n",sizeof a,b);
return 0;
}

Is this a UB.

Apart from the lack of <stdio.h> and the wrong printf() specifier for
sizeof, no. Corrected to

#include <stdio.h>
int main(void)
{
unsigned int a[4]={3,5,7}, b;
b = 2 / a[2];
printf("%d %d\n", (int)sizeof a, b);
return 0;
}

it should always print 4*sizeof(int) and 0; that is, on most desktop
machines, it will print "16 0".

Why would you think there is any UB?
Apart from including stdio.h and wrong format specified in the printf
I would like to know what happens when Multiplicative operators,
additive operators
used like the above assignment of the variable b?

What do you think happens? The multiplicative operators and additive
operators work the same way in assignments as they do in any other
expressions. Why should C have a weird exception rule for assignments?
 
M

Michael Mair

Hiho,

Yes. You are using %d to print something of type size_t. In C99
use %z. In C90 use %lu and cast the value to unsigned long, i.e:

z actually is a length modifier which specifies that the argument
is of type size_t (for conversion specifiers for unsigned
integer types) the corresponding signed type, i.e. you want
to use %zu (for conversion specifiers for signed integer types).
printf("%lu %d\n", (unsigned int)sizeof a, b);

Rather:
printf("%lu %u\n", (unsigned int)sizeof a, b);
or
printf("%zu %u\n", sizeof a, b);
as b also is of type unsigned int in this example.

Lying about the type of parameters in a variatic function call is
always UB.

Yep; usually, the UB bites the people when they are using scanf...


Cheers,
Michael
 
M

Michael Mair

Hiho,

> Yes. You are using %d to print something of type size_t. In C99
> use %z. In C90 use %lu and cast the value to unsigned long, i.e:

z actually is a length modifier which specifies that the argument
is of type size_t (for conversion specifiers for unsigned
integer types) the corresponding signed type, (for conversion
specifiers for signed integer types). So, in this case you want
to use %zu.

> printf("%lu %d\n", (unsigned int)sizeof a, b);

Rather:
printf("%lu %u\n", (unsigned int)sizeof a, b);
or
printf("%zu %u\n", sizeof a, b);
as b also is of type unsigned int in this example.

> Lying about the type of parameters in a variatic function call is
> always UB.

Yep; usually, the UB bites the people when they are using scanf...


Cheers,
Michael
 
J

j

Michael Mair said:
Hiho,



z actually is a length modifier which specifies that the argument
is of type size_t (for conversion specifiers for unsigned
integer types) the corresponding signed type, i.e. you want
to use %zu (for conversion specifiers for signed integer types).


Rather:
printf("%lu %u\n", (unsigned int)sizeof a, b);

You should cast that to ``unsigned long''.

7.19.6.1#9
If a conversion specification is invalid, the behavior is undefined. 239)
If any argument is not the correct type for the corresponding conversion
specification, the behavior is undefined.
 

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
474,146
Messages
2,570,832
Members
47,374
Latest member
EmeliaBryc

Latest Threads

Top