C Language: Integer Variables
This C tutorial explains how to declare and use integer variables with syntax and examples.
Description
There are the following integer types available in the C Language:
- short int
- unsigned short int
- int
- unsigned int
- long int
- unsigned long int
For the purposes of this tutorial, we will focus on the basic int type.
Syntax
The syntax for declaring integer variables is:
int variable_name1 [= value1];
Or the syntax for declaring multiple integer variables is:
int variable_name1 [= value1] [, variable_name2 [= value2], ... variable_name_n [= value_n]];
Parameters or Arguments
- variable_name1
- The name of the first variable to declare.
- value1
- Optional. If provided, it is the value to assign to variable_name1.
- variable_name2, ... variable_name_n
- Optional. These are additional variables that will be declared with the same C type.
- value2, ... value_n
- Optional. These are the values that you wish to assign to variable_name2 through variable_name_n.
To declare an integer variable with a type other than int, simply replace the int keyword in the syntax above with your selected type.
For example:
short int variable_name1 [= value1];
Example - Declaring a variable
Let's look at an example of how to declare an integer variable in the C language.
For example:
int age;
In this example, the variable named age would be defined as an int.
Below is an example C program where we declare this variable:
#include <stdio.h> int main() { int age; age = 10; printf("TechOnTheNet.com is over %d years old.\n", age); return 0; }
This C program would print "TechOnTheNet.com is over 10 years old."
Example - Declaring a variable and assigning a value
You can define a variable as an integer and assign a value to it in a single declaration.
For example:
int age = 10;
In this example, the variable named age would be defined as an integer and assigned the value of 10.
Below is an example C program where we declare this variable and assign the value:
#include <stdio.h> int main() { int age = 10; printf("TechOnTheNet.com is over %d years old.\n", age); return 0; }
This C program would print "TechOnTheNet.com is over 10 years old."
Example - Declaring multiple variables in a statement
If your variables are the same type, you can define multiple variables in one declaration statement.
For example:
int age, reach;
In this example, two variables called age and reach would be defined as integers.
Below is an example C program where we declare these two variables:
#include <stdio.h> int main() { int age, reach; age = 10; reach = 100; printf("TechOnTheNet.com is over %d years old and reaches over %d countries.\n", age, reach); return 0; }
This C program would print "TechOnTheNet.com is over 10 years old and reaches over 100 countries."
Example - Declaring multiple variables in a statement and assigning values
If your variables are the same type, you can define multiple variables in one declaration statement. You can also assign the variables a value in the declaration statement.
For example:
int age = 10, reach = 100;
In this example, two variables called age and reach would be defined as integers and be assigned the values 10 and 100, respectively.
Below is an example C program where we declare these two variables and assign their values:
#include <stdio.h> int main() { int age = 10, reach = 100; printf("TechOnTheNet.com is over %d years old and reaches over %d countries.\n", age, reach); return 0; }
This C program would print "TechOnTheNet.com is over 10 years old and reaches over 100 countries."
Advertisements