-1

Most uninitialised errors in C seem to come from scope as in this answer: Uninitialized Structures in C.

However in this example I have declared all my variables in the same scope. I cannot find a direct answer to why the gcc compiler would warn about uninitialised variables. How to initialise my variables without errors?

#include <stdio.h>

int main() {
  int a,b,c;
  a =+ 3;
  b -=2;
  c = 0;
  printf("a = %d, b = %d\n",a,b );
  return 0;
}
Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56
sayth
  • 6,696
  • 12
  • 58
  • 100
  • 3
    None of these values have been initialised - local variables left uninitialised take on an *indeterminate* value, while global variables left uninitialised take a "default" value (0 in most cases). – hnefatl Feb 09 '18 at 22:38
  • What are the inicial values of `a` and `b`? – Xam Feb 09 '18 at 22:39
  • my confusion has come from this SO answer https://stackoverflow.com/a/1597426/461887 specifically static variables. I expected a default value of 0 to apply but it does not. – sayth Feb 09 '18 at 22:51
  • The referenced question doesn't answer directly my question. However I have no idea how to edit my question to help people in the future. – sayth Feb 09 '18 at 22:55
  • 1
    @sayth The relevant quote from that answer is: "Non-static variables (**local variables**) are **indeterminate**. Reading them prior to assigning a value results in undefined behavior". Your `a`,`b`,`c` variables are non-static local variables, and so are not initialised to `0` automatically. – hnefatl Feb 09 '18 at 22:56
  • @hnefatl i agree in hindsight but to a beginner in C not so obvious. – sayth Feb 10 '18 at 01:29

2 Answers2

2

You declared your variables a and b but they are not initialized when you use them.

Non-static local variables need to be initialized. It means you need to assign a value to these variables after declaring them.

You should have something like this:

int main()
{
  int a = 0;
  int b = 0;
  int c;

  a =+ 3;
  b -=2;
  c = 0;
  printf("a = %d, b = %d\n", a, b);
  return (0);
}

Otherwise, when you do a =+ 3, your a variable is declared but still does not hold any value. The same happens with b at the moment when you do b -= 2.

Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56
  • Ok I see my error. In this stackoverflow answer it shows that variables are defaulted to zero which must not be true https://stackoverflow.com/a/1597426/461887 – sayth Feb 09 '18 at 22:47
  • 2
    Here, you are declaring non-static local variables. As explained in this SO answer, these variables have an indeterminate state when declared. As opposed to static local variables and global variables, which have a default value of 0. – Ronan Boiteau Feb 09 '18 at 22:49
1
int a,b,c;
a =+ 3;

a is uninitialized and then initialize it 31. The variable b however is uninitialized and you subtract 2 from an uninitialized value. c is declared but later initialized (with 0).

The results are meaningless because this is undefined behaviour. Of course GCC will warn about it. What did you expect?

edit

Only global variables and static variables are initialized with 0 before the main function run. Local variables are not initialized. In your example a, b and c are local variables, hence you have to initialize them.


fotenotes

1I initially misread the code and interpeted the second line as a += 3. Then my explanation would be correct, that it would add 3 to an uninitialized value. But in the case of a =+3; it is the same as a=3; and this would initialize a with 3. I don't know if you intended to do that or you wanted to to += and incorrectly entered =+.

Pablo
  • 13,271
  • 4
  • 39
  • 59
  • I expected that int a,b,c; initialised all values to a int default of zero. – sayth Feb 09 '18 at 22:45
  • Well, that's not what happens. Only global variables are initialized with 0, local variables are not initialized. `a` and `b` are local variables. – Pablo Feb 09 '18 at 22:46
  • I have commented this on another answer but I got that impression from another SO answer which shows it does https://stackoverflow.com/a/1597426/461887 – sayth Feb 09 '18 at 22:49
  • 2
    @sayth read my answer. I said that only global variables and static variables (in a function) are initialized with 0. `a`, `b` and `c` are local variables but they are neither global nor static. – Pablo Feb 09 '18 at 22:51
  • `a =+ 3` doesn't add 3 to `a`, it sets `a` to 3. In other words, the compiler sees that line as `a = (+3)`. – user3386109 Feb 09 '18 at 23:02
  • `a=+3;` sets `a` to `+3`, which is perfectly fine. `a+=3;` would attempt to add `3` to an uninitialised value. – rici Feb 09 '18 at 23:04
  • @user3386109 @rici I gee, I misread that as `a += 3`, you are right. – Pablo Feb 09 '18 at 23:13