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.

Categories: C Programming. No Comments on Variables in C Programming Language

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.

 

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

 

 

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) Declare the variable

2) Initialize 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:

 

Variables in C

 

The “type” 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-init

 

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

 

Example:

variable-init-ex

 

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

 

var-init-decl

 

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?

Suryateja Pericherla

Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.

He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.

He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.

Leave a Reply

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