Variables are containers for storing data values, like numbers and characters.
Declaring (Creating) Variables
To create a variable, specify the C Data Types and assign it a value:
Syntax
You can also declare a variable without assigning the value, and assign the value later:
Variable Names
All C variables must be identified with unique names, these unique names are called identifiers.
Naming Rules:
- A variable name can only have letters (both uppercase and lowercase letters), digits and underscore.
- The first letter of a variable should be either a letter or an underscore.
Output Variables
You learned from C Print Output that you can output values/print text with the printf()
function:
In many other programming languages, you would normally use a print function to display the value of a variable. However, this is not possible in C:
To output variables in C, you must get familiar with something called “C Format Specifiers
Syntax
Change Variable Values
Note: If you assign a new value to an existing variable, it will overwrite the previous value:
Assign Values
You can also assign the value of one variable to another:
Constants
If you want to define a variable whose value cannot be changed, you can use the const
keyword. This will create a constant. For example,