hey I am having confusion in the output of a simple program.
here is the code:-

int main(void)
{
    printf("%d %d",sizeof('A'),sizeof("A"));
    return 0;
}

output on my compiler(gcc):- 4 2.

I thought sizeof("A") is considered as string ending with '\0' so there are two characters i.e,2 bytes a resulting 2 as an output.
and if instead of sizeof("A") if there is sizeof("AS") output is 3.

But why for sizeof('A') output is 4,i.e, its considering the sizeof(int) instead.
Please explain the reason for both.

and also for the code

int main(void)
{
 int i=0;
    printf("%d",++i + ++i + ++i);
}

why output is 7 ?

Try this

#include <stdio.h>

int main(void)
{
    printf("%lu %lu",sizeof((unsigned char)'A'),sizeof("A"));
    return 0;
}

A character literal has type int.

Edited 5 Years Ago by gerard4143: n/a

@gerard4143:

Yep its giving the same output as expected, okay sizeof('A') is acting like sizeof(int).
but what about sizeof("A"), its output= number of character in inverted comaam +1(of '\0' perhaps).??

and also for the code

int main(void)
{
 int i=0;
    printf("%d",++i + ++i + ++i);
}

why output is 7 ?

It's undefined behavior because you're modifying an object multiple times between sequence points. The output could be anything.

but what about sizeof("A")

It's the size of an array of char initialized with two characters ('A' and '\0'). The underlying type of a string literal is an array, and sizeof works in object context, so this is one of the few places where an array isn't converted to a pointer to the first element.

The output could be anything.

I didn't understand.

Output is 7 after every compilation.
it should be 6 but output is 6+1=7.

if instead code is:-

int main(void)
{
 int i=0;
    printf("%d",++i + ++i + ++i);
}

output is 37

as output should be 36 + 1 = 37.

why this 1 is added if two pre increment operators are added.??

it should be 6 but output is 6+1=7

What makes 6 any better than 7? The reason it's undefined is there are several different ways to interpret the expression, all with different results, and all equally good.

The code is broken, it's as simple as that. You're trying to analyze garbage output.

To quote one famous comment made many years ago, undefined behavior could mean that the compiler causes demons to fly out of your nose, if the compiler writer so chooses and is able to implement it.

Undefined means just that: that the compiler can give whatever results the compiler writer chooses, and there is no way to predict the behavior short of specific knowledge of the compiler itself. As far as the standard is concerned, code such as this could return a random result, or no result, or raise a compile error, it's all dependent on the way the compiler writers implemented that 'feature'.

This isn't to say that a given compiler won't have a consistent result, just that the result is not bound by the language standard. In a case like this one, the most likely compiler output would be something like this:

increment x         -> x == 1
add x to x          -> x == 2
increment x         -> x == 3
add x to x          -> x == 6
increment x         -> x == 7

However, that's just an accident of the particular compiler output, and the choices the compiler writer made about the order of events; it could just as easily have been:

add x to x          -> x == 0
increment x         -> x == 1
add x to x          -> x == 2
increment x         -> x == 3
add x to x          -> x == 6
increment x         -> x == 7

or

add x to x          -> x == 0
add x to x          -> x == 0
add x to x          -> x == 0
increment x         -> x == 1
increment x         -> x == 2
increment x         -> x == 3

Do you see the issue here? Because the standard does not mandate the order of operations for post-increment, you can't tell ahead of time when the post-increment will take place, beyond the basic premise of it coming after the value is returned.

Edited 5 Years Ago by Schol-R-LEA: n/a

Schoil-R-LEA and Narue : means its an undefined value.

But Why this value is only chosen in any compiler when compiled several times ???

Undefined is not the same as random. I suppose the apparent predictability on one or more implementations is what makes this concept so difficult to grasp.

int main(void)
{
 int i=0;
    printf("%d",++i + ++i + ++i);
}

This code perfectly produces 6 when compiled with Tiny C Compiler V0.9.25.

int main(void)
{
 int i=0;
    printf("%d",++i + ++i + ++i);
}

This code perfectly produces 6 when compiled with Tiny C Compiler V0.9.25.

This is what my compiler say about your code...

warning: operation on ‘i’ may be undefined
warning: operation on ‘i’ may be undefined

Plus it displays 7 on my machine and not 6.

This article has been dead for over six months. Start a new discussion instead.