C Variable Syntax
data_type variable_name;
Explanation
- data_type: Specifies the type of data the variable will store (e.g., int, float, char).
- variable_name: The name used to reference the variable in the program.
Example
int age; // Declares an integer variable named 'age'
float salary; // Declares a floating-point variable named 'salary'
char grade; // Declares a character variable named 'grade'
Basic Variable Types
1. char
C uses char kind to store characters and letters. The char type is integer type because C stores integer numbers rather than characters. In C programming, char values are stored in 1 byte in memory, ranging from -128 to 127 or 0 to 255.
2. int
Int, short for "integer," is a basic variable type built into the compiler and used to define numeric variables retaining whole numbers. ... C, C++, C# and many different programming languages understand int as a data type.
3. float
Float is a shortened period for "floating point." using the definition. It is a fundamental data type built into the compiler. It is indeed used to define numeric values with floating decimal points. C, C++, C#, and many different programming languages understand float as a data type.
4. double
A double is a data type in c language that stores high-precision floating-point data or numbers in computer memory. It is called double data type because it can hold the double size of data compared to the float data kind. A double has 8 bytes, the same as 64 bits in length.
5. void
In computer programming, while void is used as a function return type, it indicates that the function does now not return a value. While void appears in a pointer statement, it specifies that the pointer is universal. when used in a function's parameter listing, void indicates that the function takes no parameters.
Also see, Short int in C Programming
Variable Declaration in C
A variable declaration presents a guarantee to the compiler that there exists a variable with the given type and name so the compiler can continue for a similar compilation without requiring the complete detail about the variable. A variable definition has its that means at the time of collection only. The compiler desires actual variable definition at the time of linking this system.
A variable declaration is helpful when you use multiple files, and also you define your variable in one of the files so that it will be available at the time of linking the program. You will be using the keyword extern to declare a variable at any place. Although you can declare a variable multiple times to your C program, it could be described only once in a particular file, a function, or a block of code.
Also read - Bit stuffing program in c
Example
C
#include <bits/stdc++.h>
using namespace std;
// Variable declaration:
extern int a, b;
extern int c;
extern float f;
int main ()
{
/* variable definition: */
int a, b;
int c;
float f;
/* actual initialization */
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;
}
You can also try this code with Online C Compiler
Run Code
Output
value of c: 30
value of f: 23.333334
How to Use Variables in C?
Variables in C are used to store data that can be manipulated or accessed throughout a program. To use a variable, you must declare it, initialize it, and optionally modify it.
Steps to Use Variables
- Declare: Define the variable with its type.
- Initialize: Assign a value to the variable.
- Use: Perform operations or display the variable's value.
Example
C
#include <stdio.h>
int main() {
int age = 25; // Declaration and initialization
printf("Age: %d", age); // Using the variable
return 0;
}
You can also try this code with Online C Compiler
Run Code
Rules for Naming Variables in C
When naming variables in C, follow these rules to ensure proper functionality and readability:
- Variable names must start with a letter or an underscore (_) but cannot begin with a digit.
- Variable names can contain letters, digits, and underscores but no special characters.
- Reserved keywords of C (e.g., int, return) cannot be used as variable names.
- Variable names are case-sensitive (age and Age are different).
- Choose descriptive names for better readability, such as totalMarks instead of tm.
C Variable Types
C provides several types of variables to store different kinds of data. Here are the primary types:
- Integer (int): Stores whole numbers.
Example: int age = 30;
printf("Age: %d", age); - Floating-point (float): Stores decimal numbers.
Example: float price = 99.99;
printf("Price: %.2f", price); - Double (double): Stores large decimal numbers with double precision.
Example: double distance = 12345.6789;
printf("Distance: %.4lf", distance); - Character (char): Stores a single character.
Example: char grade = 'A';
printf("Grade: %c", grade); - Boolean (_Bool): Stores true (1) or false (0).
Example: #include <stdbool.h>
_Bool isPassed = 1;
printf("Passed: %d", isPassed); - Void (void): Represents no value, often used for functions with no return type.
Example: void displayMessage() {
printf("Hello, World!");
}
Lvalues and Rvalues in C
Two types of expressions in C −
Lvalue
"l-value" refers to a memory location that is identifies an object. l-value can also seem as both left-hand or right-hand sides of an assignment operator(=). l-value frequently represents an identifier—a facet of an assignment.
Rvalue
Rvalue" refers to the data value saved at a few addresses in memory. A rvalue is an expression that couldn't have a value assigned to it. This means that rvalue can seem on the right but not on the left-hand side of an assignment operator(=).
You can also read about the memory hierarchy,
Frequently Asked Questions
How to represent variables in C?
- Variable call ought to start with a letter or underscore.
- Variables are case sensitive
- They may be constructed with digits letters.
- No special symbols are allowed other than underscore.
- sum, height, _value are a few examples for variable name
How to declare variables?
- Variables must be declared in the C program before applying.
- Memory space isn't allocated for a variable at the same time as a declaration. It takes place only on the variable definition.
- Variable initialization approach assigning a value to the variable.
How to access Variables in C?
Variables that might be declared inside a function or block are known as local variables. They can be used most effectively by means of statements that can be inside that function or block of code. Local variables aren't recognized to functions outside their personal.
Conclusion
Variables in C play a fundamental role in programming, serving as containers for data that can be manipulated and accessed throughout a program. By understanding how to declare, initialize, and use variables effectively, programmers can write efficient and organized code.