Learn C Language

Learn C Programming Language Step By Step. It’s easy to learn c programming language with real-life examples and hands-on. We are trying our best to deliver quality content.

Variables In C

« Previous Page                                                                                                                               Next Page »

Variables are the name of memory locations where we store our data. When the C program in under execution, the operating system allocates memory to store instructions of the program and also allocates space to the stored data.

Well, in other words, you can say that ” Variables in c is nothing but a name given to a storage area that our programs can manipulate”. Each variable in C has a specific type, which determines the size and layout of the variable’s memory. Variables also determine the range of values that can be stored within that memory and the set of operations that can be applied to the variable.

A programmer has to decide how much space he wants to store data. Also, a programmer specifies the names of memory locations in his program code. These names are called variables. These locations can contain integer, real and character constants.

We can change its value according to the programming requirement.

Rules To Name Variables

You have to follow the three simple steps to name a variable. You can remember the chemistry where we used to follow a convention (Certain Rules) to name a compound. A similar thing happens here. These three steps are:

  • The variable name may contain alphabets, digits or underscore.
  • No other symbol or blank in allowed.
  • A valid variable name never starts with a digit.

The name of a variable in c can be composed of letters, digits, and the underscore character. It must begin with either a letter or an underscore. Upper and lowercase letters are distinct because C is case-sensitive. 

C programming language also allows defining various other types of variables, which we will cover in subsequent chapters like Enumeration, Pointer, Array, Structure, Union, etc. For this chapter, let us study only basic variable types.

Types Of Variables

The types of variables based on basic data types or primitive data types are given below.

  • Char
  • Int
  • Float
  • Double
  • Void (absence of data type)

We already discussed in detail about the above data types in the previous chapter.

Variable Definition

A variable definition tells the compiler where and how much storage to create for the variable. A variable definition specifies a data type and contains a list of one or more variables of that type as follows:

Here, datatype must be a valid C data type including char, int, float, double, bool, or any user-defined object and variable_list may consist of one or more identifier names separated by commas. Let’s see some valid variable declaration:

The line int a; declares and defines the variable a which instructs the compiler to create variables named a of type int. Similar things happen for rest.

Variables can be initialized (assigned an initial value) in their declaration. The initializer consists of an equal sign followed by a constant expression as follows:

For definition without an initializer: variables with static storage duration are implicitly initialized with NULL (all bytes have the value 0); the initial value of all other variables are undefined.

Variable Declaration

A variable declaration provides assurance to the compiler that there exists a variable with the given type and name so that the compiler can proceed for further compilation without requiring the complete detail about the variable. A variable definition has its meaning at the time of compilation only, the compiler needs actual variable definition at the time of linking the program.

In addition, the variable declaration is useful when you are using multiple files and you define your variable in one of the files which will be available at the time of linking of the program. You will use the keyword extern to declare a variable at any place. Though you can declare variable multiple times in your C program, it can be defined only once in a file, a function, or a block of code. Let’s take an example to understand better:

When you will compile the above code and execute, it will produce an output:

Lvalues and Rvalues

There are two kinds of expressions in C:

  • lvalue − Expressions that refer to a memory location are called “lvalue” expressions. An lvalue may appear as either the left-hand or right-hand side of an assignment.
  • rvalue − The term rvalue refers to a data value that is stored at some address in memory. An rvalue is an expression that cannot have a value assigned to it which means an rvalue may appear on the right-hand side but not on the left-hand side of an assignment.

Variables are lvalues and so they may appear on the left-hand side of an assignment. Numeric literals are rvalues and so they may not be assigned and cannot appear on the left-hand side.

Top Book Suggestion

The two books on C Programming Language that I Personally Recommend. I really loved reading these books. These books are listed for quality content, easy steps, and affordable price. You can get it from Amazon and Flipkart. 

Check The Price On Amazon:

1. C Programming Beginner’s Guide

2. Head First C: A Brain-Friendly Guide

Check The Price On Flipkart:

1. Introduction to C Programming 

2. Head First C: A Brain-Friendly Guide

« Previous Page                                                                                                                               Next Page »

Buy Premium Courses At Lowest Price. Become A Certified Developer Today!

Best Books Selected by Our Experts

Buy Premium Projects At Lowest Price

Ebooks Written by Our Experts


Buy Premium Courses At Lowest Price. Grab Golden Opportunity To Become A Certified Developer Today!

Useful Links

Pin It on Pinterest

Share This