C Variables and Data Types
Variables and data types are fundamental concepts in C programming. Variables allow you to store and manipulate data, while data types define what kind of data you can store and how it's represented in memory.
Why This Matters
C is a statically-typed language, which means you must declare the type of each variable before using it. Understanding data types is crucial because:
- It affects how much memory is allocated for the variable
- It determines what operations you can perform
- It influences the accuracy and range of values
- It impacts program efficiency and performance
Variables in C
A variable is a named location in memory that stores a value of a specific type. Think of it as a labeled box where you can put a certain kind of data.
Declaring Variables
In C, you must declare a variable before using it. The general syntax is:
1data_type variable_name;23// Examples:4int age; // Declares an integer variable named 'age'5float temperature; // Declares a floating-point variable named 'temperature'6char grade; // Declares a character variable named 'grade'
Initializing Variables
You can assign a value to a variable when you declare it (initialization) or later in the program:
1// Initialization during declaration2int age = 25;3float temperature = 98.6;4char grade = 'A';56// Declaration first, assignment later7int count;8count = 10;910// Multiple variables of the same type11int x, y, z;12int a = 5, b = 10, c = 15;
Basic Data Types in C
C provides several built-in data types to handle different kinds of data:
Data Type | Description | Size (typical) | Range (typical) |
---|---|---|---|
char | Single character or small integer | 1 byte | -128 to 127 or 0 to 255 |
int | Integer (whole number) | 4 bytes | -2,147,483,648 to 2,147,483,647 |
float | Single-precision floating-point | 4 bytes | ~1.2E-38 to 3.4E+38 (6-7 digits precision) |
double | Double-precision floating-point | 8 bytes | ~2.3E-308 to 1.7E+308 (15-16 digits precision) |
⚠️ Important Note
The exact size and range of data types may vary depending on the platform and compiler. The C standard only guarantees minimum ranges.
Type Modifiers
C allows you to modify the basic types using modifiers like short
, long
,signed
, and unsigned
:
1short int smallNumber; // Uses less memory than int, smaller range2long int bigNumber; // Uses more memory than int, larger range3unsigned int positiveOnly; // Only non-negative values (0 and positive)4long double highPrecision; // Extra precision floating-point
Working with Different Data Types
1. Characters (char
)
Characters in C are stored as small integers representing their ASCII values:
1#include <stdio.h>23int main() {4 char letter = 'A';56 // Characters are stored as their ASCII values7 printf("The character %c has ASCII value %d\n", letter, letter);89 // We can do arithmetic with characters10 char nextLetter = letter + 1;11 printf("The next letter is %c\n", nextLetter);1213 return 0;14}1516// Output:17// The character A has ASCII value 6518// The next letter is B
2. Integers (int
, short
, long
)
Integers are used for whole numbers with no fractional parts:
1#include <stdio.h>23int main() {4 int count = 10;5 int negative = -42;67 // Basic arithmetic8 int sum = count + 5;9 int product = count * 3;1011 printf("Sum: %d, Product: %d\n", sum, product);1213 // Using unsigned for non-negative values only14 unsigned int students = 30;15 // unsigned int invalid = -5; // This would cause issues!1617 // Using different integer sizes18 short smallNumber = 123;19 long bigNumber = 1234567890L; // 'L' suffix for long literals2021 printf("Small: %hd, Big: %ld\n", smallNumber, bigNumber);2223 return 0;24}2526// Output:27// Sum: 15, Product: 3028// Small: 123, Big: 1234567890
3. Floating-Point Numbers (float
, double
)
Floating-point types store numbers with decimal points:
1#include <stdio.h>23int main() {4 float price = 10.99f; // 'f' suffix for float literals5 double precise = 3.14159265359;67 printf("Price: $%.2f\n", price); // %.2f means 2 decimal places8 printf("Pi: %.10lf\n", precise); // %lf for double, 10 decimal places910 // Be careful with floating-point comparisons11 float a = 0.1f + 0.2f;12 float b = 0.3f;1314 printf("0.1 + 0.2 = %.20f\n", a);15 printf("0.3 = %.20f\n", b);1617 if (a == b) {18 printf("Equal\n");19 } else {20 printf("Not equal due to floating-point precision!\n");21 }2223 return 0;24}2526// Output:27// Price: $10.9928// Pi: 3.141592653629// 0.1 + 0.2 = 0.3000000119209289550830// 0.3 = 0.3000000119209289550831// Not equal due to floating-point precision!
Constants in C
Constants are values that cannot be changed during program execution:
1#include <stdio.h>23// Using #define directive (symbolic constant)4#define PI 3.141595#define MAX_USERS 10067int main() {8 // Using const keyword (type-safe constant)9 const double gravity = 9.81;10 const char newline = '\n';1112 // Literal constants13 printf("Integer: %d\n", 42); // Integer literal14 printf("Float: %f\n", 3.14f); // Float literal15 printf("Character: %c\n", 'X'); // Character literal16 printf("String: %s\n", "Hello"); // String literal1718 // Using our defined constants19 double circleArea = PI * 5 * 5;20 printf("Area of circle with radius 5: %.2f\n", circleArea);2122 return 0;23}2425// Output:26// Integer: 4227// Float: 3.14000028// Character: X29// String: Hello30// Area of circle with radius 5: 78.54
Type Conversion
C allows conversion between different data types:
Implicit Conversion (Automatic)
C automatically converts values in certain operations, typically when converting from a smaller to a larger data type.
int i = 10;double d = i; // int to double, no data losschar c = 'A';int ascii = c; // char to int, stores ASCII value
Explicit Conversion (Casting)
You can explicitly convert values using type casting, which tells the compiler to convert a value to a specific type.
double pi = 3.14159;int rounded = (int)pi; // Explicitly cast to int (3)float sum = 5.5f + (float)10; // Convert 10 to float
Practice Example: Temperature Converter
Let's create a simple program that converts temperature from Celsius to Fahrenheit using variables of different types:
1#include <stdio.h>23int main() {4 // Declare and initialize variables5 float celsius, fahrenheit;6 int kelvin;78 // Get temperature in Celsius9 printf("Enter temperature in Celsius: ");10 scanf("%f", &celsius);1112 // Convert to Fahrenheit: F = (C * 9/5) + 3213 fahrenheit = (celsius * 9.0 / 5.0) + 32.0;1415 // Convert to Kelvin: K = C + 273.15 (truncated to integer)16 kelvin = (int)(celsius + 273.15);1718 // Display results19 printf("%.2f Celsius = %.2f Fahrenheit\n", celsius, fahrenheit);20 printf("%.2f Celsius = %d Kelvin (integer part)\n", celsius, kelvin);2122 return 0;23}2425// Example Output:26// Enter temperature in Celsius: 2527// 25.00 Celsius = 77.00 Fahrenheit28// 25.00 Celsius = 298 Kelvin (integer part)
🎯 Try it yourself!
Practice working with variables and data types by creating programs that:
- Calculate the area and perimeter of a rectangle using integer and float variables
- Convert a lowercase letter to uppercase using the ASCII relationship
- Display the memory sizes of different data types using the
sizeof
operator
Summary
In this tutorial, you've learned:
- How to declare and initialize variables in C
- The basic data types in C and their characteristics
- How to work with characters, integers, and floating-point numbers
- Different ways to define constants
- How type conversion works in C
Variables and data types form the foundation of C programming. In the next tutorials, you'll learn how to perform input and output operations and control the flow of your programs.
Related Tutorials
Introduction to C
Learn about the C programming language, its history, and importance.
Continue learningSetting Up Your C Environment
Set up your development environment for C programming.
Continue learningBasic Input and Output in C
Learn how to handle basic input and output operations in C.
Continue learning