Variables serve as containers for holding data values, such as numbers and characters.
In the C programming language, various types of variables are defined using distinct keywords.
To define a variable, indicate its type and assign it a value.
type variableName = value; |
Where “type” represents one of the C data types (like int), and “variableName” is the identifier for the variable (such as x or myName).
The equal sign is employed for assigning a value to the variable.
To define a variable intended to store a number, consider the following example:
Declare a variable named myNum with the type int and initialize it with the value 15.
int myNum = 15; |
It’s also possible to declare a variable without immediately assigning a value to it, and assign the value later on.
// Declare a variable int myNum; // Assign a value to the variable |
You’ve discovered that you can output values or print text using the printf() function:
printf(“Hello World!”); |
In numerous other programming languages such as Python, Java, and C++, it’s common to utilize a print function to showcase the value of a variable.
Nonetheless, this capability is not available in C.
int myNum = 15; printf(myNum); // Nothing happens |
In order to display variables in C, it’s essential to become acquainted with a concept known as “format specifiers,” which will be covered in the upcoming chapter.