Understanding Variables
Variables in C are placeholders for storing data that can be modified during program execution. Each variable must be declared with a specific type before use. For example:
int age = 25;
float temperature = 36.6;
char grade = 'A';
Here, age
is an integer, temperature
is a floating-point number, and grade
is a character.
Exploring Data Types
C provides several data types to define the nature of variables. Commonly used types include:
- int: Stores integers (e.g., 1, 2, -5).
- float: Stores floating-point numbers (e.g., 3.14, -0.01).
- char: Stores a single character (e.g., 'A', 'z').
- double: Stores larger floating-point numbers.
Choosing the correct data type is crucial for efficient memory usage.
Introduction to Operators
Operators in C are symbols used to perform operations on variables and values. Some basic operators include:
- Arithmetic: +, -, *, /, %
- Relational: ==, !=, >, <, >=, <=
- Logical: &&, ||, !
For example:
int x = 10, y = 5;
int sum = x + y; // sum is 15
Practical Examples
Here’s a simple program that uses variables, data types, and operators:
#include <stdio.h>
int main() {
int a = 10, b = 20;
int sum = a + b;
printf("The sum of %d and %d is %d\\n", a, b, sum);
return 0;
}
Save the code as example.c
, compile using gcc
, and run it to see the output.
Nice information I really appreciate
ReplyDelete