[Solved] C – int is a variable so why not use a variable instead of an int?


Couldn’t you just do this?:

variable = 12; //Instead of putting int variable before this?

No you can’t do this because you must declare it’s type first. This

int variable;

defines a variable that can hold an int. If you assign a string, “asdsa2”, or a float, 34.5, you will get a compilation error. Because we can assign only variables of type int to the variable.

So char is defining the variable as a string and int is defining it as
an integer.

If you write:

char firstLetter;

defines a variable of type char. That means the variable firstLetter can hold a character.

This is firstLetter="c"; valid. While this firstLetter="21"; is wrong.

I see now, but couldn’t you do this instead? int variable
= 12; or char variable = “string”;

Of course you do. This is actually the usual way we assign a value to a variable, think it like below

  1. We define the type.
  2. We set the name of the variable.
  3. We place the equal sign, =.
  4. We set the value.
  5. We terminate the assignment with a semicolon, ;.

Follow 1 through 5 from left to right.

4

solved C – int is a variable so why not use a variable instead of an int?