C Programming Variables

Variables are nothing more than the name given to a storage that our programs can modify.Each variable in C has a specific type that determines the size and layout of the variable's memory; the range of values that can be stored in that memory; and the set of operations that can be applied to the variable.

The name of a variable can consist of letters, numbers, and underscore characters.The name of the variable must begin with a letter or underscore.Since C is case sensitive, uppercase and lowercase letters are different.Depending on the base types described in the previous section, there will be the following basic variable types:

Sr.No.Types and Descriptions
1Char
A single octet (one byte).An integer type.
2Int
The most natural integer size for the machine.
3Float
Single precision floating point value.
4Double
Double-precision floating point value.
5Void
Represents the absence of the type.
Variables

The C programming language also allows to define other types of variables that we will consider in later sections such as Enumeration, Pointer, Array, Structure, Union, etc. For this section, we will only examine the basic variable types.

Defining Variables in C

The variable definition tells the compiler where and how much storage space to create for the variable.The variable definition specifies a data type and contains a list of one or more variables of this type, such as:

type variable_list;

Where type must be a valid C data type, including char, w_char, int, float, double, bool, or any user-defined object; and değişken_listesi comma separated by one or more identifier names.

int    i, j, k;
char   c, ch;
float  f, circuit;
double d;

int i, j, k; the line declares and defines the i, j, and k variables; this tells the compiler to create variables of type int named i, j, and k.

Variables can be initialized in their declarations (an initial value can be assigned).The initiator consists of a constant expression after the equal sign, such as:

type degisken_ismi = value;

Some examples:

extern int d = 3, f = 5; 
int d = 3, f = 5;           
byte z = 22;                
char x = 'x';               

For identification without initiator: variables with static storage time are implicitly initialized with NULL (all bytes have a value of 0); the initial value of all other variables is undefined.

Variable Declaration in C

A variable declaration provides the compiler with assurance that a variable with the given type and name is present, so that the compiler can continue compiling without the need for full detail about the variable.If the variable definition has meaning only during compilation, the compiler program needs the actual variable definition at the time of binding.

Variable declaration is useful when you use multiple files, and you define your variable in one of the files that will be available during the program's connection.You will use the extern keyword to declare a variable anywhere.Although you can declare a variable multiple times in your program C, it can only be defined once in a file, in a function, or in a block of code.

Reminder

If a variable or function defined within a source file can be accessed not only within the source file in which it is defined, but also in the other source files that make up the project, the external linkage of the object or function is called "external linkage". If a variable or function defined within a source file can only be accessed within the source file in which it is defined, if it cannot be accessed in the other source files that make up the project, the internal linkage of the object or function is called "internal linkage". The extern keyword is a keyword used to notify the compiler that the variable is defined outside. When compiler sees the extern keyword, it does not allocate space for the variable.

Example

Try the following example, where variables are declared at the top but defined and started within the main function:

#include <stdio.h></stdio.h>  // variable declaration:extern int a, b;
extern int c;
extern float f;

int main () {

/* variable identification */int a, b;
   int c;
   float f;
 
/* actual startup */
   a = 10;
   b = 20;
  
c = a + b;
   printf("value of c : %d n", c);

f = 70.0/3.0;
   printf("value of f : %f n", f);
 
return 0;
}



   

When the above code is compiled and executed, it produces the following result:

Value of c : 30
value of f : 23.333334

The same concept applies to function declaration, where you provide a function name during declaration and the actual definition can be given anywhere else.

Identification of // function
int func();

int main() {

   Calling // function
   int i = func();
}

// function definition
int func() {
   return 0;
}

Lvalue and Rvalue Values in C

There are two types of expressions in C:

Lvalue: Expressions that refer to a memory location are called "lvalue".A value can appear as the left or right side of an assignment.

Rvalue: The term "rvalue" refers to a data value stored in some addresses in memory.A value is an expression that cannot have a value assigned to it; this means that a value may appear on the right side of an assignment but not on its left.

Variables are values and therefore can appear on the left side of an assignment.Numeric literals are values and therefore cannot be assigned and cannot be seen on the left side.Take a look at the following valid and invalid expressions:

int g = 20; // current definition

10 = 20; // invalid identification may cause problems during compilation

In our next article, we will examine The Constants and Immutables.