YouTip LogoYouTip

C Variables

# C Variables A variable 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, the range of values that can be stored within that memory, and the set of operations that can be applied to the variable. The name of a variable 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. Based on the basic types explained in the previous chapter, there are the following basic variable types: | Type | Description | | --- | --- | | char | Typically a single byte (eight bits). It is an integer type. | | int | The most natural size of integer for the machine. 4 bytes, range from -2147483648 to 2147483647. | | float | A single-precision floating point value. Single precision follows the format: 1 bit sign, 8 bits exponent, 23 bits mantissa. !(#) | | double | A double-precision floating point value. Double precision follows the format: 1 bit sign, 11 bits exponent, 52 bits mantissa. !(#) | | void | Represents the absence of type. | The C language also allows us to define various other types of variables, which we will cover in subsequent chapters, such as enumeration, pointer, array, structure, union, etc. For this chapter, let us focus on the basic variable types. ## Variable Definition in C 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: type variable_list; Here, **type** must be a valid C data type including char, w_char, int, float, double, bool, or any user-defined object, etc., and **variable_list** may consist of one or more identifier names separated by commas. Here are some examples of valid declarations: Define integer variables: int age; In the code above, age is defined as an integer variable. Define floating-point variables: float salary; In the code above, salary is defined as a floating-point variable. Define character variables: char grade; In the code above, grade is defined as a character variable. Define pointer variables: int *ptr; In the code above, ptr is defined as an integer pointer variable. Define multiple variables: int i, j, k; **int i, j, k;** declares and defines the variables **i**, **j**, and **k**; this instructs the compiler to create variables named **i**, **j**, and **k** of type **int**. ### Variable Initialization In C, variable initialization means assigning an initial value to a variable at the time of definition. Variables can be initialized at the time of definition or in subsequent code. Initialization consists of an equal sign followed by a constant expression, as follows: type variable_name = value; Here, **type** represents the data type of the variable, **variable_name** is the name of the variable, and **value** is the initial value of the variable. Here are some examples: int x = 10; // Integer variable x initialized to 10float pi = 3.14; // Floating-point variable pi initialized to 3.14char ch = 'A'; // Character variable ch initialized to the character 'A'int d = 3, f = 5; // Define and initialize d and fbyte z = 22; // Define and initialize z// Declare external variablesextern int d;extern int f; **Subsequent initialization of variables:** After a variable is defined, you can assign a new value to it using the assignment operator =. type variable_name; // Variable definition variable_name = new_value; // Variable initialization Examples: int x; // Define integer variable x x = 20; // Initialize variable x to 20float pi; // Define floating-point variable pi pi = 3.14159; // Initialize variable pi to 3.14159char ch; // Define character variable ch ch = 'B'; // Initialize variable ch to the character 'B' Note that variables should be initialized before use. An uninitialized variable has an undefined value and may contain arbitrary garbage values. Therefore, to avoid undefined behavior and errors, it is recommended to initialize variables before using them. ### Variables Without Initialization In C, if a variable is not explicitly initialized, its default value depends on the type of the variable and its scope. For global variables and static variables (static variables defined inside functions and global variables defined outside functions), their default initial value is zero. Here are the default values for different types of variables when not explicitly initialized: * Integer variables (int, short, long, etc.): default value is 0. * Floating-point variables (float, double, etc.): default value is 0.0. * Character variables (char): default value is '', i.e., the null character. * Pointer variables: default value is NULL, meaning the pointer does not point to any valid memory address. * Compound type variables such as arrays, structures, and unions: their elements or members will be default-initialized according to the corresponding rules, which may include recursively applying the default rules to elements. Note that local variables (non-static variables defined inside functions) are not automatically initialized to default values β€” their initial values are undefined (containing garbage values). Therefore, before using a local variable, you should explicitly assign it an initial value. In summary, the default value of a variable in C depends on its type and scope. Global and static variables default to **0**, character variables default to '', pointer variables default to NULL, and local variables have no default value β€” their initial value is undefined. ## Variable Declaration in C A variable declaration provides assurance to the compiler that a variable of a specified type and name exists so that the compiler can proceed for further compilation without requiring complete details about the variable. A variable declaration has its meaning at the time of compilation only; the compiler needs the actual variable declaration at the time of linking the program. There are two situations for variable declarations: * 1. A declaration that allocates storage space. For example: int a β€” storage space is allocated at the time of declaration. * 2. A declaration that does not allocate storage space, using the extern keyword to declare the variable name without defining it. For example: extern int a β€” the variable a can be defined in another file. Unless the extern keyword is present, all declarations are also definitions. extern int i; // Declaration, not definitionint i; // Declaration, also a definition > For more information about extern, please refer to: (#). ### Example Try the following example, where the variables are declared at the top but defined and initialized inside the main function: ## Example #include// Define variables x and y outside functions int x; int y; int addtwonum(){// Declare x and y as external variables inside the function extern int x; extern int y; // Assign values to the external (global) variables x and y x = 1; y = 2; return x+y; }int main(){int result; // Call function addtwonum result = addtwonum(); printf("result is: %d",result); return 0; } When the above code is compiled and executed, it produces the following result: result is: 3 If you need to reference a variable defined in another source file, you simply need to declare the variable with the extern keyword in the referencing file. ## addtwonum.c file code: #include/*External variable declaration*/extern int x ; extern int y ; int addtwonum(){return x+y; } ## test.c file code: #include/*Define two global variables*/int x=1; int y=2; int addtwonum(); int main(void){int result; result = addtwonum(); printf("result is: %dn",result); return 0; } When the above code is compiled and executed, it produces the following result: $ gcc addtwonum.c test.c -o main $ ./main result is: 3 ## Lvalues and Rvalues in C There are two types of expressions in C: 1. **Lvalue:** An expression that refers to a memory location is called an lvalue expression. An lvalue may appear on either the left-hand or right-hand side of an assignment. 2. **Rvalue:** The term rvalue refers to a data value that is stored at some address in memory. An rvalue is an expression that cannot be assigned to, meaning an rvalue may appear on the right-hand side of an assignment but not on the left-hand side. Variables are lvalues and so may appear on the left-hand side of an assignment. Numeric literals are rvalues and so cannot be assigned to and cannot appear on the left-hand side. The following is a valid statement: int g = 20; But the following is not a valid statement and would generate a compile-time error: 10 = 20;
← Angularjs ServicesAngularjs Select β†’