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.
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 "=".
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.
By following these rules, you can create meaningful and easy-to-understand variable names that will make your code more readable and maintainable.
int age;
float temperature;
double _pi;
char name[20];
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
In C programming language, there are three types of variables:
#include<stdio.h>
int main() {
int x = 10; //local variable
printf("The value of x is: %d", x);
return 0;
}
#include<stdio.h>
int x; //global variable
int main() {
x = 10;
printf("The value of x is: %d", x);
return 0;
}
#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;
}
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;
}