Data Types, Variables, and Operators
Understanding basic data types (int, float, char, etc.), declaring variables, and using arithmetic, relational, and logical operators.
Variables and Data Types in C Programming
Introduction to Variables
In C programming, a variable is a named storage location in the computer's memory that holds a value. Think of it as a labeled container that can store different types of data. The value stored in a variable can be changed during program execution.
Variables are essential for storing and manipulating data, allowing programs to perform calculations, make decisions, and interact with the user.
Variables in C
What is a Variable? A variable is a named location in memory used to store data. It has a name (identifier), a type (which determines the kind of data it can hold), and a value.
Purpose of Variables: Variables allow you to:
- Store data that will be used later in the program.
- Perform calculations and store the results.
- Make decisions based on the values of variables.
- Pass data between different parts of the program (functions).
Declaring Variables
Before you can use a variable in C, you must declare it. Declaration involves specifying the variable's name and its data type. The syntax for declaring a variable is:
data_type variable_name;
For example:
int age; // Declares an integer variable named 'age'
float salary; // Declares a floating-point variable named 'salary'
char initial; // Declares a character variable named 'initial'
You can also declare and initialize a variable in the same statement:
int age = 30; // Declares 'age' as an integer and initializes it to 30
float pi = 3.14159; // Declares 'pi' as a float and initializes it to 3.14159
char grade = 'A'; // Declares 'grade' as a char and initializes it to 'A'
Rules for Naming Variables:
- Variable names can contain letters (A-Z, a-z), digits (0-9), and underscores (_).
- They must start with a letter or an underscore.
- C is case-sensitive, so
myVar
andmyvar
are different variables. - Keywords (e.g.,
int
,float
,if
,else
) cannot be used as variable names. - Choose descriptive names that clearly indicate the purpose of the variable. This greatly improves code readability.
Data Types in C
The data type of a variable determines the type of data it can store and the operations that can be performed on it. C provides several fundamental data types:
Basic Data Types
int
: Used to store integers (whole numbers) without decimal points. Examples:-10
,0
,100
. Typically occupies 4 bytes (but can vary depending on the system).int count = 10;
float
: Used to store single-precision floating-point numbers (numbers with decimal points). Examples:3.14
,-2.5
,0.0
. Typically occupies 4 bytes.float price = 99.99;
double
: Used to store double-precision floating-point numbers. Provides greater precision thanfloat
. Examples:3.1415926535
,-123.456789
. Typically occupies 8 bytes.double pi = 3.141592653589793;
char
: Used to store single characters. Characters are enclosed in single quotes. Examples:'A'
,'z'
,'5'
. Typically occupies 1 byte.char grade = 'B';
Derived Data Types
- Arrays: A collection of elements of the same data type stored in contiguous memory locations.
int numbers[5] = {1, 2, 3, 4, 5};
- Pointers: A variable that stores the memory address of another variable.
int num = 10; int *ptr = # // ptr stores the address of num
- Structures: A collection of variables of different data types grouped together under a single name.
struct Student { char name[50]; int age; float gpa; };
- Unions: Similar to structures, but all members share the same memory location. Only one member can be active at a time.
union Data { int i; float f; char str[20]; };
Type Qualifiers
Type qualifiers modify the properties of basic data types:
const
: Specifies that the variable's value cannot be changed after initialization.const int MAX_VALUE = 100;
volatile
: Indicates that the variable's value can be changed by external factors (e.g., hardware interrupts). This prevents the compiler from performing certain optimizations.volatile int sensorValue;
static
: Depending on where it is used,static
can have different meanings:- Inside a function: A static variable retains its value between function calls.
- Outside a function: A static variable has internal linkage, meaning it is only accessible within the file it is declared in.
static int counter = 0; // Example within a function
extern
: Used to declare a variable that is defined in another file.extern int globalVariable;
Size Modifiers
Size modifiers influence the amount of memory allocated for integer types:
short
: Reduces the amount of memory used by an integer (typically 2 bytes).long
: Increases the amount of memory used by an integer (typically 4 or 8 bytes).long long
: Increases the amount of memory used by an integer even further (typically 8 bytes).
Example:
short int smallNumber;
long int largeNumber;
long long veryLargeNumber;
Signed and Unsigned
These modifiers are used to specify whether an integer variable can store negative values or only non-negative values:
signed
: Can store both positive and negative values (default forint
,short
,long
).unsigned
: Can only store non-negative values (zero and positive numbers).
Example:
signed int temperature; // Can store negative temperatures
unsigned int age; // Age cannot be negative