Variables and Data Types in C
Introduction
In C programming, variables are used to store data, and data types define the type of data that can be stored in a variable. Understanding variables and data types is fundamental to writing C programs. In this tutorial, we will explore different data types and how to declare and use variables in C.
Data Types in C
C provides several data types, including:
- int: Used to store integer values, e.g.,
int age = 25;
- float: Used to store floating-point numbers (real numbers), e.g.,
float salary = 2500.50;
- char: Used to store single characters, e.g.,
char grade = 'A';
- double: Used to store double-precision floating-point numbers, e.g.,
double price = 99.99;
- void: A special data type used for functions that don't return a value, e.g.,
void myFunction();
Declaring Variables
In C, variables must be declared with a data type before they can be used. For example:
int age;
float salary;
char grade;
You can also initialize variables at the time of declaration:
int age = 25;
float salary = 2500.50;
char grade = 'A';
Using Variables
Once declared and initialized, variables can be used in expressions:
int x = 5;
int y = 10;
int sum = x + y;
Here, the variables x
and y
are used to calculate the sum, which is stored in the variable sum
.
Conclusion
Understanding variables and data types is crucial for writing C programs. Proper use of data types ensures that your program stores and manipulates data accurately. You've learned the basics of declaring and using variables with various data types in C, and you're now ready to create more complex programs.