Variables in C are names that can hold a value of a given type. Once created, the type of the variable cannot be changed.
Variables are placed in the computer memory. When you give that variable a value, its place in the memory saves the value. Later you can change or read that value, using the name of the variable.
Advertise on this site. I promise you will like the rates :)
The term declaration means that we tell the compiler that a certain variable exists. To do that, you need to specify its type and name. Let's create one variable for each type from the previous lesson:
If you want to declare multiple variables of the same type, you can do that in one row. Specify the data type and then list the variables' names, separating them with commas:
For the names of your variables you can use letters(both uppercase and lowercase), digits(0..9) and underscore ( _ ):
The name of the variable has to make it obvious what information it holds. Choosing obscure names for the variables is a bad habit that most beginners have. Names like a,b,c,asd and so on are bad names. Reading a well written code should be almost like reading a book and not like deciphering an encrypted message.
Don't use toooooooo long names like
For longer names use underscores or “camelCase”, where each next word begins with a capital letter:
To give your variable a value place it on the left of the assignment operator “=” and put the value on the right side.
You can do the assignment on immediately when you create the variable
or later in the code
You can also assign it to the value of another variable:
To output the values of variables in C, we will use the printf function. You already know about it, from the previous lessons. To recall, it uses a format string and arguments. Until now we used constant values as arguments. Now we will use the name of the variables that we want to display.
We can use the scanf function to read information. Just like printf, scanf uses a format string and arguments. The format string contains format specifiers that tell what kind of data will be read. The number of specifiers must match the number of arguments. Also, their sequence must be the same – the first argument is for the first specifier and so on.
Finally, the data type of the variable must be compatible with the information that we will read.
Note the & symbol, before the name of the variable. This symbol means that we take the memory address of the variable and not its actual value. Remember – the scanf function needs the address of the variable, not its value!
Let's create a simple echo program. It will read a symbol from the keyboard and then print it back on the screen:
Once you build and execute this, you will be prompted to input a symbol. When the program reaches the scanf function it pauses and waits until you press enter. Then it will read the symbol and the program will continue with the next line.
Remember from the last lesson, that data in C is very flexible. We can interpret the character as integers and this way we will find the ASCII code of a given symbol. Let's modify the above program and make it more useful:
See? The second time the argument “symbol” will be interpreted as a number. If you still haven't, compile and test the above program. It could be useful if you need to quickly find the ASCII code of a symbol.