Startertutorials Blog
Tutorials and articles related to programming, computer science, technology and others.
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.
Home » Programming » C Programming » Variables in C Programming Language
Suryateja Pericherla Categories: C Programming. No Comments on Variables in C Programming Language
0
(0)

In this article we will learn about variables in C. First we will see the definition of a variable and then learn how to declare and initialize a variable in C.

 

Visit our C programming tutorial for more topics.

 

In a C program, we need to store values in the memory and perform operations on those values. This can be achieved in C, by using the concept of variables.

 

 


Subscribe to our monthly newsletter. Get notified about latest articles, offers and contests.


What is a variable in C?

A variable is a placeholder for holding a value in the main memory (RAM).

 

As the name implies, the value in the variable can change at any point of execution of the program.

 

 For using variables in our programs, there are essentially two steps:

1) Declaring the variable

2) Initializing the variable 

 

Declaring a Variable

Before using a variable in the program, we have to declare the variable. The  syntax for declaring a variable  in a program is as shown below:

datatype variable_name;
C

 

The “datatype” in the above syntax represents the data type. The “variable-name” is the identifier.

 

 There are certain rules that must be followed while writing the variable name.  They are as follows:

  1. A variable name must always start with an alphabet (letter) or an underscore ( _ ).
  2. The variable name must not be more than 31 characters. The suggested length of a variable name is 8 characters.
  3. C is case sensitive. So, the variable name “average” is different from “AVERAGE”.
  4. Keywords must not be used for declaring variables.
  5. White spaces are not allowed within the variable name.

 

Initializing a Variable

After declaring the variable, we can assign a value to the variable. This process of assigning a value to the variable is known as initialization.

 

 Syntax for initializing a variable  is as shown below:

variable_name = value;
C

 

The value we assign to the variable depends on the data type of the variable.

 

Example for declaring and initializing a variable is given below:

int a;
a = 10;
C

 

The declaration and initialization can be combined into a single line as shown below:

int a = 10;
C

 

In the next article, let’s learn about C data types.

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Leave a Reply

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

Facebook
Twitter
Pinterest
Youtube
Instagram
Blogarama - Blog Directory