C Variables
Variables and data types are fundamental building blocks of any C program. Understanding how to declare variables, what types of data C supports, and how to work with them is essential for programming in C.
What is a Variable?
A variable is a named storage location in memory that holds a value. The value stored in a variable can be changed during program execution.
int age = 25; // 'age' is a variable that stores the integer value 25
Variable Declaration in C
In C, you must declare variables before using them. The basic syntax for declaring a variable is:
data_type variable_name;
You can also initialize a variable at the time of declaration:
data_type variable_name = initial_value;
Multiple Variable Declaration
You can declare multiple variables of the same type in a single statement:
int x, y, z;
int a = 10, b = 20, c = 30;
Variable Naming Rules
When naming variables in C, follow these rules:
- Names can contain letters, digits, and underscores
- Names must begin with a letter or underscore
- Names are case-sensitive (
count
andCount
are different variables) - Reserved keywords cannot be used as variable names
- Names should be meaningful and descriptive
// Valid variable names
int age;
float _temperature;
char studentName[50];
// Invalid variable names
int 1count; // Cannot start with a digit
float if; // Cannot use reserved keyword
char student-name[50]; // Cannot use hyphen
Basic Data Types in C
C provides several basic data types to represent different kinds of data:
Integer Types
Integer types store whole numbers without decimal points.
Type | Size (typical) | Range (typical) |
---|---|---|
char | 1 byte | -128 to 127 or 0 to 255 |
short | 2 bytes | -32,768 to 32,767 |
int | 4 bytes | -2,147,483,648 to 2,147,483,647 |
long | 4 or 8 bytes | Varies by system |
long long | 8 bytes | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
char grade = 'A';
short small_number = 123;
int count = 1000;
long big_number = 1000000L;
long long very_big_number = 9000000000LL;
Floating-Point Types
Floating-point types store numbers with decimal points.
Type | Size | Precision |
---|---|---|
float | 4 bytes | 6-7 decimal digits |
double | 8 bytes | 15-16 decimal digits |
long double | 10-16 bytes | 18-19 decimal digits |
float temperature = 98.6f;
double pi = 3.14159265359;
long double very_precise = 3.14159265359L;
Void Type
The void
type specifies that no value is available. It is used:
- As function return type for functions that don't return a value
- For functions with no parameters
- For generic pointers
void function_without_return_value(void);
Type Modifiers
C provides type modifiers to alter the range of data types:
Modifier | Effect |
---|---|
signed | Allows positive and negative values (default for most types) |
unsigned | Allows only positive values, doubling the positive range |
unsigned int positive_only = 4000000000; // Regular int couldn't hold this value
signed char temperature = -15; // Can hold negative values
The sizeof
Operator
The sizeof
operator returns the size (in bytes) of a data type or variable.
#include <stdio.h>
int main() {
printf("Size of int: %lu bytes\n", sizeof(int));
printf("Size of float: %lu bytes\n", sizeof(float));
int number = 10;
printf("Size of variable 'number': %lu bytes\n", sizeof(number));
return 0;
}
Type Qualifiers
Type qualifiers provide additional information about variables:
const
: Variable cannot be modified after initializationvolatile
: Value might change unexpectedlyrestrict
: Pointer is the only way to access an object (C99)
const double PI = 3.14159; // Cannot be modified
volatile int sensor_value; // May change due to external factors
Example: Working with Variables and Data Types
Here's a comprehensive example demonstrating various data types and variables:
#include <stdio.h>
int main() {
// Integer types
char character = 'A';
unsigned char byte = 255;
int number = 42;
unsigned int positive_number = 3000000000;
// Floating-point types
float decimal = 3.14f;
double precise_decimal = 3.14159265359;
// Output variable values
printf("Character: %c (ASCII: %d)\n", character, character);
printf("Byte value: %u\n", byte);
printf("Integer: %d\n", number);
printf("Unsigned integer: %u\n", positive_number);
printf("Float: %f\n", decimal);
printf("Double: %.10lf\n", precise_decimal);
// Output variable sizes
printf("\nSize Information:\n");
printf("Size of char: %lu bytes\n", sizeof(char));
printf("Size of int: %lu bytes\n", sizeof(int));
printf("Size of float: %lu bytes\n", sizeof(float));
printf("Size of double: %lu bytes\n", sizeof(double));
return 0;
}
Best Practices
-
Initialize variables: Always initialize variables when declaring them to avoid undefined behavior.
cint count = 0; // Good practice
int count; // May contain garbage value -
Use appropriate data types: Choose the smallest data type that can hold your data to conserve memory.
-
Use meaningful names: Name variables clearly to improve code readability.
cint age; // Good
int a; // Unclear -
Be aware of type limits: Understand the limits of each data type to avoid overflow.
Summary
Variables in C provide named storage locations for data. C offers various data types like integers, floating-point numbers, and characters to efficiently represent different kinds of information. Understanding these basics is crucial for writing effective C programs.
In the next section, we'll learn about constants and literals in C, which are similar to variables but with values that cannot be changed.
If you spot any mistakes on this website, please let me know at [email protected]. I’d greatly appreciate your feedback! :)