Learning about Variables in C

Welcome to your second tutorial on the C language. In this tutorial, we’ll be discussing about variables, their applications, their necessity both in general terms of programming and for C language as well. Variables in C are really very important. These are the fundamentals and you need to understand how variables in C are used and data in them can be manipulated.

Variables are where you store your data, you can assume them to be pretty much like containers (reserved memory locations) where you can store your things ( data ). In C or any programming language, it is essential that you define that variable is of what kind, ie it’s data type.

The data types can be divided into two broad categories namely Fundamental Data types and Derived data types. Now we’ll look in detail about both of these.

Fundamental Data Types

These are the data types which are built-in and not derived, that’s why the word “Fundamental” has been used. There are five basic kind of Data types :-

  • int – for integer data types
  • float – for floating point numbers (those with decimals)
  • char – for character type
  • double- same as integer type but include float also and is capable of holding more numbers (range is very large)
  • void – this stands for null value

Derived Data Types

These are the data types derived from fundamental data types. Some of the derived data types are :

  • Arrays
  • Pointers
  • Enumeration
  • Structures


Defining a Variable

The following syntax should be used while defining variable in C :

Here, data_type is the type of data which variable shall hold, and variable_name is any legal name you may give to the variable to access the information stored in it.

Now, we’ll define a variable of type integer.

Here, we have defined a variable named sam which is capable of holding any integer type value.
Now, we’ll assign a value to this variable with assignment operator (=)

Now, our variable sam contains value 10.

Now, we’ll see it in the entire program and output the value of variable.

For the above code, we’ll get the following output :

Output of variable in C tutorial code

Output of variable in C tutorial code

(Note: Program are compiled in Turbo C with DosBox instead of gcc)

Note we’ve used %d , this just means that we want to print an integer value. In C, we need to tell the compiler what type of value is coming, so we need to have this %d for integer type value. Similar to %d, we use the following placeholders to instruct the compiler of upcoming data type :

%d – integer (int)
%c – character (char)
%s – strings

%f – float

Observe, that in printf() function, we have used , and the variable name, and when this program is compiled and executed, we get the  output , such that it prints Value of sam is 10 , the %d is now replaced by the value stored in our variable sam. This is because, %d was an instruction for the compiler so that it knows what type of value is coming in, so wherever you place this %d, and output an integer value, it would be replaced by value of that variable.

For example, see the following code snippet :

Here is the output for the above mentioned code snippet

Output of variable in C tutorial code

Outputing with variables

(Note: Program are compiled in Turbo C with DosBox instead of gcc)

Note: Return type of our main function is of int type so we are returning value 0, this can be any value, but since we don’t need to return anything, we generally return 0.

We’ll carry this topic in the next tutorial as well, till then, you can try the codes from this tutorial and play with the code.


Leave a Reply

Your email address will not be published. Required fields are marked *