Variable and Constant in C
On this page, we will learn about what a variable is, how to declare variables in C, rules for naming variables, the types of variables in C, and what a constant is in C.
What is a variable?
In computer programming, a variable is a storage location in memory that has a name and a value. It can be used to store and manipulate data within a program. A variable's value can be changed during the program's execution, allowing for dynamic and flexible operations.
Declaring a variable involves specifying its data type and assigning it a name. The name must be unique within the scope of the program. Once a variable is declared, it can be assigned a value using the assignment operator "=".
How to declare variable in c?
data_type variable_name;
For example, in C programming, we can declare an integer variable named "age" as follows:
int age; // declare an integer variable named "age"
age = 25; // assign a value of 25 to the variable "age"
Variables play a crucial role in programming, as they allow programs to store and manipulate data, perform calculations, and make decisions based on input and output.
Rules for Naming variables:
- Variable names can only contain letters (both uppercase and lowercase), digits, and underscore (_) characters.
- Variable names must start with a letter or an underscore.
- Variable names cannot be a C keyword (e.g. int, float, etc.).
- Variable names should be descriptive and not too long.
By following these rules, you can create meaningful and easy-to-understand variable names that will make your code more readable and maintainable.
Valid variable names:
int age;
float temperature;
double _pi;
char name[20];
Invalid variable names:
int 2numbers; // Variable name cannot begin with a number
float avg_temp$; // Variable name contains an invalid character
double class; // Variable name is a reserved keyword in C language
What are the types of variable in C?
In C programming language, there are three types of variables:
-
Local variables:Local variables are defined within a block or a function, and their scope is limited to that block or function. They are not accessible from outside of that block or function. Local variables must be initialized before use, and if they are not initialized, they will contain a garbage value. An example given below:
#include<stdio.h> int main() { int x = 10; //local variable printf("The value of x is: %d", x); return 0; }
-
Global variables: Global variables are defined outside of any function, and they can be accessed from anywhere in the program. Global variables are initialized automatically, so if they are not explicitly initialized, they will contain a default value of 0. Example of a global variable:
#include<stdio.h> int x; //global variable int main() { x = 10; printf("The value of x is: %d", x); return 0; }
-
Static variables:Static variables are defined within a block or a function, but their scope is limited to that block or function. However, unlike local variables, static variables retain their value even after the function or block has ended. Static variables are initialized only once, and their value persists throughout the program. Example of a static variable:
#include<stdio.h> void myFunction(){ static int x = 0; //static variable x++; printf("The value of x is: %d\n", x); } int main() { myFunction(); myFunction(); myFunction(); return 0; }
What is a constant in C?
In programming, a constant is a value that cannot be modified or changed during the execution of a program. Constants are useful for defining values that are known and fixed, such as mathematical constants or program settings.
In C, constants can be of different types, such as integer constants, character constants, floating-point constants, and string literals.
Integer constants are used to represent whole numbers and can be specified as decimal, octal, or hexadecimal numbers. For example, the decimal number 10 can be represented as 10, the octal number 10 can be represented as 012, and the hexadecimal number 10 can be represented as 0xA.
Character constants are used to represent single characters and are enclosed in single quotes. For example, the character 'a' can be represented as 'a'.
Floating-point constants are used to represent fractional numbers and are specified with a decimal point. For example, the floating-point number 3.14 can be represented as 3.14.
String literals are used to represent a sequence of characters and are enclosed in double quotes. For example, the string "hello" can be represented as "hello".
Constants are defined using the const keyword in C. For example, to define an integer constant with the value 10, we can write:
const int MY_CONSTANT = 10;
Constants are useful for making code more readable and maintainable, as they can help avoid hard-coded values that may be used multiple times throughout a program. Additionally, by making values constant, we can ensure that they are not accidentally modified during program execution, which can help prevent bugs and errors. An example given below:
#include<stdio.h>
int main() {
const int a = 3.14159265358979323; // using const keyword
return 0;
}