-1

I'm making a program for image treatment and i have this variables initialized:

int minR, minG, minB = 255; int maxR, maxG, maxB = 0;
  printf("%d", maxG);

And when I print this, instead of getting a 0 as it should be, I get 16384 as the value of maxG. Yet, if I do this:

int minR, minG, minB = 255; int maxR, maxB = 0;
  int maxG = 0; printf("%d", maxG);

Then everything goes OK.

Does anyone know why this could be? Thank you.

Varu
  • 311
  • 1
  • 15

6 Answers6

5

The initializer only applies to the one declarator which it follows, not any of the other declarators in the list!

So int a = 10, b, * c = NULL; only initializes a and c, but b remains uninitialized.

(By the way, reading an uninitialized variable has undefined behaviour.)

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • "By the way, reading an uninitialized variable has undefined behaviour." -- That's true in C89. It is not necessarily true in C99: it depends on whether the type has trap representations, which most integer types on most implementations don't. Not sure about C11 though. –  Nov 27 '14 at 11:21
  • 1
    [(Why) is using an uninitialized variable undefined behavior in C?](http://stackoverflow.com/questions/11962457/why-is-using-an-uninitialized-variable-undefined-behavior-in-c) – 2501 Nov 27 '14 at 11:29
  • @hvd: You need to read around a few corners in the Standard, but it is UB. – Kerrek SB Nov 27 '14 at 11:31
  • Okay, so Jens Gustedt's answer to that other question shows that reading an uninitialised variable in C11 is undefined if the type has trap representations, or the variable never has its address taken, but *is* defined if the type has no trap representations and the variable has its address taken. It's still not true that reading an uninitialised variable necessarily has undefined behaviour. –  Nov 27 '14 at 11:32
  • And searching for the new wording brings up [DR #338](http://www.open-std.org/jtc1/sc22/wg14/www/docs/dr_338.htm), which acknowledges what I claimed in my comment: as of C99, reading an uninitialised variable is no longer undefined by itself. –  Nov 27 '14 at 11:35
  • 1
    @hvd From your link: *If the lvalue designates an object of automatic storage duration that could have been declared with register storage class (never had its address taken), and that object is uninitialized (not declared with an initializer, and no assignment to it has been performed prior to the use), the behavior is undefined.* – 2501 Nov 27 '14 at 11:39
  • @2501 Yes, exactly. "and", not "or". Both conditions have to be met for the behaviour to be undefined. –  Nov 27 '14 at 11:40
  • 1
    @hvd Both conditions are met. – 2501 Nov 27 '14 at 11:41
  • @2501 In this question, both conditions are met, and I agree that the behaviour is undefined in C89 and in C11 (but not necessarily in C99). That doesn't make the more general claim in this answer correct, though. –  Nov 27 '14 at 11:42
4
int maxR, maxG, maxB = 0;

This becomes:

int maxR;
int maxG;
int maxB = 0;

So MaxR and MaxG are not initialized.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
Gopi
  • 19,784
  • 4
  • 24
  • 36
3
int maxR, maxG, maxB = 0;   

Here only maxB is initialized to 0.

Easier ways to detect this:
Compile with -Wall or -Wextra.

int minR, minG, minB = 255; int maxR, maxG, maxB = 0;
printf("%d", maxG); 

$ gcc -o exe -Wextra
warning: ‘maxG’ is used uninitialized in this function [-Wuninitialized]

Easier Way to do this:

int maxR, maxG, maxB;
maxR = maxG = maxB = 0;

Where the first line is declaration.
Following line is assignment, evaluated left-to-right.

(maxR = (maxG = (maxB = 0)));
Saurabh Meshram
  • 8,106
  • 3
  • 23
  • 32
  • That is incorrect. The second line is assignment. As a general rule one should always prefer initialization over assignment. – David Heffernan Nov 27 '14 at 11:26
  • But does is make it incorrect ?. Isnt it matter of choice ?. Its initialization of `maxB`, followed by assignment of `maxR` and `maxG`. – Saurabh Meshram Nov 27 '14 at 11:28
  • Your use of terminology is incorrect. There is no initialization in that excerpt. – David Heffernan Nov 27 '14 at 11:43
  • Isn't `maxB = 0` termed initialization ? after `int maxB` (declaration). Kindly correct me in that case. – Saurabh Meshram Nov 27 '14 at 11:46
  • No. That is assignment. These are formal terms defined by the language standard. – David Heffernan Nov 27 '14 at 11:50
  • @SaurabhMeshram After the assignment `maxB = 0`, `maxB` is considered initialised, despite `maxB = 0` not being initialisation. Initialisation specifically refers to initialisers, which are part of the declaration: `int maxB = 0;`. David Heffernan is correct that what you're doing is not called initialisation. I don't know if the general rule he gives, that initialisation should be preferred over assignment, applies, though: it certainly applies to C++, but there are more situations in C where assignment instead of initialisation wins for readability or compatibility. –  Nov 27 '14 at 11:51
1
int minR, minG, minB = 255; int maxR, maxG, maxB = 0;
  printf("%d", maxG);

is like

int minR;
int minG;
int minB = 255;
int maxR;
int maxG;
int maxB = 0;
printf("%d", maxG);

but maxG is not initialized so it points somewhere in your memory. 16384 is the real value maxG when you tried to print it but it can be anything!

imagine my memory is like this XXXXXXXX919A9F7B62A526XXXXXX

imagine that when you did

int maG;

the allocated memory for it points here

XXXXXXXX919A9F7B62A526XXXXXX
          ^

since I assume that you system is telling that sizeof(int) is 4 * 8 bits, the printf will print

XXXXXXXX919A9F7B62A526XXXXXX
          ^^^^^^^^
          1o2o3o4o

9A9F7B62 (in hexadecimal) = 2594143074 (in decimal)

So you need to initialize maxG to something before printing it ! :)

Charlon
  • 363
  • 2
  • 16
0

You need to initialize all the variables.

int minR = 255, minG = 255, minB = 255; int maxR = 0, maxG = 0, maxB = 0;
vz0
  • 32,345
  • 7
  • 44
  • 77
0

The statement should be like this:

int minR = 255, minG = 255, minB = 255; int maxR = 0, maxG = 0, maxB = 0;

maxG in your statement remains uninitialized..

whoan
  • 8,143
  • 4
  • 39
  • 48