Learn C - C Variable

A variable is a specific piece of memory that holds one or more contiguous bytes.

Every variable in a program has a name, which will correspond to the memory address for the variable.

We use the variable name to store a data value in memory or retrieve the data that the memory contains.

The following code hardcoded the value 10 in the string. It is not using variable.

#include <stdio.h> 
int main(void) 
      printf("My salary is $10."); 
      return 0; 

The code above generates the following result.

Naming Variables

A variable name is a sequence of one or more uppercase or lowercase letters, digits, and underscore characters (_) that begin with a letter.

Examples of legal variable names are as follows:

Radius diameter Current_May my_Result D123

A variable name must not begin with a digit, so 8_value and 6_size aren't legal names.

A variable name must not include characters other than letters, underscores, and digits.

Declaring Variable

To declare a variable called myvar1, write the following:

int myvar1;

int is data type.

The following is the list of common data type you can use in C language.


We can also write many variables as follows:

int myvar1, myvar2;

Once declared, these variables can be used to store data based on its data type.

Assigning Variables

Variable that you declare can be assigned by a value.

It can done using the equals sign (=).

For example, variable myvar1 will assign the number 100, you would write this:

int myvar1 = 100;

You also declare as below

int myvar1; 
myvar1 = 100; 

You must assign a value on a variable properly based on data type.

If not, you will get error on compiling process.

Example: Using Variable

You can rewrite the previous program to use a variable of type int, which is an integer type:

    #include <stdio.h> 
    int main(void) { 
      int salary;                               // Declare a variable called salary 
      salary = 10000;                           // Store 10000 in salary 
      printf("My salary is %d.\n", salary); 
      return 0; 

The code above generates the following result.


The statement that identifies the memory that you're using to store your salary is the following:

int salary;  // Declare a variable called salary 

This statement is called a variable declaration because it declares the name of the variable.

The name, in this case, is salary.

The variable declaration ends with a semicolon.

The variable declaration also specifies the type of data that the variable will store.

You've used the keyword int to specify that the variable, salary, will be used to store an integer value of type int.

The keyword int precedes the name of the variable.

This is just one of several different types you can use to store integers.

Keywords are words that are reserved in C with a special meaning.

You must not use keywords as names for variables or other entities in your code.

If you do, your compiler will produce error messages.

The next statement is:

salary = 10000;                           // Store 10000 in salary 

This is a simple arithmetic assignment statement.

It takes the value to the right of the equal sign and stores it in the variable on the left of the equal sign.

The = symbol is called the assignment operator because it assigns the value on the right to the variable on the left.

Note on printf()

The printf() statement:

printf("My salary is %d.\n", salary); 

There are now two arguments inside the parentheses, separated by a comma.

An argument is a value that's passed to a function.

The two arguments to the printf() function are:

The first argument is a control string, and it controls how the output specified by the following argument or arguments is to be presented.

It is also referred to as a format string.

The second argument is the name of the variable, salary.

The control string in the first argument determines how the value of salary will be displayed.

%d is called a conversion specifier for the value of the variable.

Conversion specifiers determine how variable values are displayed on the screen.

A d is a decimal specifier that applies to integer values.

It means that the second argument, salary, will be represented and output as a decimal (base 10) number.

Conversion specifiers always start with a % character so that the printf() function can recognize them.

Because a % in a control string always indicates the start of a conversion specifier, you must use the escape sequence %% when you want to output a % character.

The following code shows how to use two Conversion specifiers.

    #include <stdio.h> 
      //from  w  ww.java2  s . c o  m
    int main(void) 
      int chair;                              
      int table;                                
      chair = 2;                              
      table = 3;                                
      // Display some output 
      printf("%d table for %d chair\n", table, chair); 
      return 0; 

You first declare two variables, table and chair, with these statements:

int chair;                                // Declare a variable called chair 
int table;                                // and a variable called table 

You specify both variables as type int so they both can only store integer values.

They have been declared in separate statements.

Because they are both of the same type, you could have saved a line of code and declared them together like this:

int chair, table; 

The next two statements assign the value to each variable:

chair = 2;                          
table = 3;                          

The code above generates the following result.

Example: Using Variables

This program does a simple calculation using the values of the variables:

    #include <stdio.h> 
      //from   w w  w. j a va 2  s.com
    int main(void) 
      int total_count; 
      int birds; 
      int tigers; 
      int pigs; 
      int others; 
      birds = 26; 
      tigers = 13; 
      pigs = 541; 
      others = 46; 
      // Calculate the total number
      total_count = birds + tigers + pigs + others; 
      printf("We have %d in total\n", total_count);   // Output the result 
      return 0; 

The code above generates the following result.

Initializing Variables

In the previous example, you declared each variable with a statement such as this:

int birds;                         // The number of birds as pets 

You set the value of the variable birds using this statement:

birds = 2; 

This sets the value of birds to 2.

The first statement creates the variable called birds, but its value will be whatever was left in memory from the last program that used this memory.

We can initialize the variable when we declare it.

You can do this with the following statement:

int birds = 2; 

This statement declares birds as type int and sets its initial value to 2.