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

[adinserter name="Block 7"]

« 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.

[adinserter name=”Block 7″]

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:

[et_pb_dmb_code_snippet code=”ZGF0YXR5cGUgdmFyaWFibGVfbGlzdDs=” _builder_version=”3.29.3″]ZGF0YXR5cGUgdmFyaWFibGVfbGlzdDs=[/et_pb_dmb_code_snippet]

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:

[et_pb_dmb_code_snippet code=”aW50IGE7IC8vIGludGVnZXIgdHlwZQpjaGFyIGM7IC8vY2hhcmFjdGVyIHR5cGUKZmxvYXQgZjsgLy8gZmxvYXQgb3IgZGVjaW1hbCB0eXBlCmRvdWJsZSBkOyAvLyBkZWNpbWFsIHR5cGUgKEJpZyBzaXplKQ==” _builder_version=”3.29.3″]aW50IGE7IC8vIGludGVnZXIgdHlwZQpjaGFyIGM7IC8vY2hhcmFjdGVyIHR5cGUKZmxvYXQgZjsgLy8gZmxvYXQgb3IgZGVjaW1hbCB0eXBlCmRvdWJsZSBkOyAvLyBkZWNpbWFsIHR5cGUgKEJpZyBzaXplKQ==[/et_pb_dmb_code_snippet]

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:

[et_pb_dmb_code_snippet code=”ZGF0YXR5cGUgdmFyaWFibGVfbmFtZSA9IHZhbHVlOwovLyBzb21lIGV4YW1wbGVzIGFyZQppbnQgYT0gMzsKY2hhciB4ID0gJ3knOy8vIHRoZSB2YXJpYWJsZSB4IGhhcyB0aGUgdmFsdWUgJ3knCg==” _builder_version=”3.29.3″]ZGF0YXR5cGUgdmFyaWFibGVfbmFtZSA9IHZhbHVlOwovLyBzb21lIGV4YW1wbGVzIGFyZQppbnQgYT0gMzsKY2hhciB4ID0gJ3knOy8vIHRoZSB2YXJpYWJsZSB4IGhhcyB0aGUgdmFsdWUgJ3knCg==[/et_pb_dmb_code_snippet]

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:

[adinserter name=”Block 7″]

[et_pb_dmb_code_snippet code=”I2luY2x1ZGUgPHN0ZGlvLmg+Ci8vIFZhcmlhYmxlIGRlY2xhcmF0aW9uOgpleHRlcm4gaW50IGEsIGI7IC8vR2xvYmFsIHZhcmlhYmxlIGRlY2xhcmF0aW9uCmV4dGVybiBpbnQgYzsKaW50IG1haW4gKCkgewoKICAgLyogdmFyaWFibGUgZGVmaW5pdGlvbjogKi8KICAgaW50IGEsIGI7CiAgIGludCBjOyAKICAgLyogYWN0dWFsIGluaXRpYWxpemF0aW9uICovCiAgIGEgPSAxMDsKICAgYiA9IDMwOwogIAovL1dlIGNhbiBkZWZpbmUgYW5kIGluaXRpYWxpemUgdG9nZXRoZXIsIGludCBhID0xMCxiPTMwOwogIAogICBjID0gYSArIGI7CiAgIHByaW50ZigidmFsdWUgb2YgYyA6ICVkIAoiLCBjKTsKICAgcmV0dXJuIDA7Cn0K” _builder_version=”3.29.3″]I2luY2x1ZGUgPHN0ZGlvLmg+Ci8vIFZhcmlhYmxlIGRlY2xhcmF0aW9uOgpleHRlcm4gaW50IGEsIGI7IC8vR2xvYmFsIHZhcmlhYmxlIGRlY2xhcmF0aW9uCmV4dGVybiBpbnQgYzsKaW50IG1haW4gKCkgewoKICAgLyogdmFyaWFibGUgZGVmaW5pdGlvbjogKi8KICAgaW50IGEsIGI7CiAgIGludCBjOyAKICAgLyogYWN0dWFsIGluaXRpYWxpemF0aW9uICovCiAgIGEgPSAxMDsKICAgYiA9IDMwOwogIAovL1dlIGNhbiBkZWZpbmUgYW5kIGluaXRpYWxpemUgdG9nZXRoZXIsIGludCBhID0xMCxiPTMwOwogIAogICBjID0gYSArIGI7CiAgIHByaW50ZigidmFsdWUgb2YgYyA6ICVkIAoiLCBjKTsKICAgcmV0dXJuIDA7Cn0K[/et_pb_dmb_code_snippet]

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

[et_pb_dmb_code_snippet code=”dmFsdWUgb2YgYyA6IDQw” _builder_version=”3.29.3″]dmFsdWUgb2YgYyA6IDQw[/et_pb_dmb_code_snippet]

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.

[et_pb_dmb_code_snippet code=”aW50IGEgPTIwOyAvLyBBIHZhbGlkIHN0YXRlbWVudAppbnQgMjAgPTMwOyAvL0ludmFsaWQgc3RhdGVtZW50CjIwPTMwOyAvL0ludmFsaWQgc3RhdGVtZW50CmludCAyMCA9YjsgLy9JbnZhbGlkIHN0YXRlbWVudAovL0FsbCBpbnZhbGlkIHN0YXRlbWVudCB3aWxsIGdlbmVyYXRlIGNvbXBpbGUgdGltZSBlcnJvci4=” _builder_version=”3.29.3″]aW50IGEgPTIwOyAvLyBBIHZhbGlkIHN0YXRlbWVudAppbnQgMjAgPTMwOyAvL0ludmFsaWQgc3RhdGVtZW50CjIwPTMwOyAvL0ludmFsaWQgc3RhdGVtZW50CmludCAyMCA9YjsgLy9JbnZhbGlkIHN0YXRlbWVudAovL0FsbCBpbnZhbGlkIHN0YXRlbWVudCB3aWxsIGdlbmVyYXRlIGNvbXBpbGUgdGltZSBlcnJvci4=[/et_pb_dmb_code_snippet]

 [adinserter name=”Block 7″]

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 »

[adinserter block=”6″]

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

CoursePot.Com

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

Useful Links

Pin It on Pinterest

Share This