-1

I am confused about how to distinguish the use of "int" when declaring something an integer versus using it to initialize a variable.

In the following example I believe I am initializing the variables n,m,y:

int main(void)
    {
    int n,m,y ;
}

How can I tell between "integer" and "initialize"

Thank you

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • *"I believe I am initializing the variables n,m,y"* - so, to which value are you initializing them? Think about it. – Marco Bonelli Mar 22 '21 at 15:27
  • 1
    `n,m,y` in your example is declared locally and not marked as `static`, so they have *automatic storage duration* and are not initialized. – MikeCAT Mar 22 '21 at 15:27
  • You are *declaring* three integer variables but not initializing them. But I'm not entirely clear about what you are asking, in terms of the use of the "int" term. – Adrian Mole Mar 22 '21 at 15:28
  • 1
    Initializing is `int n = 0;` It is also a declaration and the first assignment. Get into the habit of declaring your variables on individual lines, one variable per line. – Robert Harvey Mar 22 '21 at 15:28
  • Do you mean the difference between `int x` and `int x = N`? – tadman Mar 22 '21 at 15:28
  • 1
    @RobertHarvey Technically (I think) [initialization is not assignment](https://stackoverflow.com/q/35662831/10871073). – Adrian Mole Mar 22 '21 at 15:31
  • @AdrianMole: Initialization is the *first* assignment, in this case. – Robert Harvey Mar 22 '21 at 15:47
  • 1
    @RobertHarvey It's a bit nitpicking, but an initialization is technically not an assignment. The rules for assignment and initialization varies. – klutt Mar 22 '21 at 15:49
  • @RobertHarvey Not something worth getting into an argument about, but there are some subtle differences (IIRC) in terms of what compilers are allowed to do with initializations versus assignments. – Adrian Mole Mar 22 '21 at 15:49
  • Does it have any relevance to the code in the OP? – Robert Harvey Mar 22 '21 at 15:53
  • @RobertHarvey Depends, but now when we have argued about it I think it's clear to OP :) – klutt Mar 22 '21 at 15:55

2 Answers2

3
int foobar; //this is a declaration of an integer variable 

foobar = 42; // this is assignment of the variable 

int x = 52; // this is declaration and initialization in one line so you perform two things in this line: declaration and initialization to value 52
Sara Bean
  • 139
  • 8
1

There is no distinction to be done here.

You have two mention of int in your code.

int main(... indicate that you have a main fonction whose return value is an integer.

int n, m, y; indicate that you have created 3 integer variables named n, m and y.

None of these 3 variables are initialized as you didn't set a value for any of those.

int n = 0, m = 1, y = 3 here we have three initialized integer variables. n initialized to 0, m initialized to 1 and y initialized to 3.

momolechat
  • 155
  • 11