C Programming Variables

The variable is nothing but the name given to the storage area that our programs can manipulate. Each variable in C programming language has the specific type, which determines the size and the layout of the variable’s memory, the range of the values that can be stored within that memory, and the set of the operations that can be applied to the variables.

The name of the variable can be composed of the letters, digits, and the underscore character, it’s depends. It must begin with either the letter or the underscore as you required. The Upper and lowercase letters are distinct because C programming language is case-sensitive programming language. Based on the basic data types explained in the previous chapter of C programming language, there will be the following basic variable types to learn further −

Variable Type Variable Description
char Typically the single octet (one byte). This is also an integer type.
int The most generic size of integer for the machine.
float For single precision floating point value.
double For double precision floating point value.
void To Represents the absence of type.

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

Variable Definition in C Programming Language

The variable definition tells the compiler where and how much memory storage need to create for the variable. The variable definition specifies the data type and contains the list of one or more variables of that type as follows −

Here, the type must be the valid C programming data type including int, char, w_char, float, double, bool, or any other user-defined object and variable_list may consist of one or more of identifier names separated by commas. Some of valid declarations are shown here −

In the line int i, j, k; declares and defines as the variables i, j, and k, which instruct to the compiler to create the variables named as i, j and k of data type int.

The variables can be initialized or (assigned an initial value) in their declaration process. The initializer consists of an equal sign followed by the constant expression as the follows −

Here some examples are −

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

Variable Declaration in C Programming Language

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

The variable declaration is very useful when you are using the multiple files and you define your variable in one of the files which will be available at the time of the linking of the program. You will use the keyword extern to declare the variable at any of place. Though you can declare the variable multiple times in your C program file, it can be defined only once in the file, the function, or the block of code.

Check Example

Try the following of example, where the variables have been declared at the top, but they have been defined and initialized inside the main function body −

When the above code is compiled and to be executed, it produces the following result which you can see −

The same concept applies on the function declaration where you provide the function name at the time of the declaration and its actual definition can be given anywhere else in the program. For the example −

The Lvalues and Rvalues in C Programming language

There are two kinds of expressions in C programming language −

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

The Variables are lvalues and so they may appear on the left-hand side of the assignment. The numeric literals are the rvalues and so they may not be assigned and cannot appear on the left hand side. Take the look at the following valid and invalid statements −

Join Us

1,300FansLike
1,997FollowersFollow
0SubscribersSubscribe

Enter your email address:

Get Latest Post Feeds Direct Into Your Email Box, No Spam and Only High Quality Content

Recommended Books To Learn Advanced Aspect of C Programming Language

If You Enjoyed This Post
Join My Newsletter
Subscribe
Give it a try, you can unsubscribe anytime.
Close
Premium SEO Tool Backlink Beast in Just 1$ Limited Time Offer ! Don't Let it go..
Shop Now