Progress: 3 of 16 topics18%

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:

c
1data_type variable_name;
2
3// 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:

c
1// Initialization during declaration
2int age = 25;
3float temperature = 98.6;
4char grade = 'A';
5
6// Declaration first, assignment later
7int count;
8count = 10;
9
10// Multiple variables of the same type
11int 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 TypeDescriptionSize (typical)Range (typical)
charSingle character or small integer1 byte-128 to 127 or 0 to 255
intInteger (whole number)4 bytes-2,147,483,648 to 2,147,483,647
floatSingle-precision floating-point4 bytes~1.2E-38 to 3.4E+38 (6-7 digits precision)
doubleDouble-precision floating-point8 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:

c
1short int smallNumber; // Uses less memory than int, smaller range
2long int bigNumber; // Uses more memory than int, larger range
3unsigned 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:

c
1#include <stdio.h>
2
3int main() {
4 char letter = 'A';
5
6 // Characters are stored as their ASCII values
7 printf("The character %c has ASCII value %d\n", letter, letter);
8
9 // We can do arithmetic with characters
10 char nextLetter = letter + 1;
11 printf("The next letter is %c\n", nextLetter);
12
13 return 0;
14}
15
16// Output:
17// The character A has ASCII value 65
18// The next letter is B

2. Integers (int, short, long)

Integers are used for whole numbers with no fractional parts:

c
1#include <stdio.h>
2
3int main() {
4 int count = 10;
5 int negative = -42;
6
7 // Basic arithmetic
8 int sum = count + 5;
9 int product = count * 3;
10
11 printf("Sum: %d, Product: %d\n", sum, product);
12
13 // Using unsigned for non-negative values only
14 unsigned int students = 30;
15 // unsigned int invalid = -5; // This would cause issues!
16
17 // Using different integer sizes
18 short smallNumber = 123;
19 long bigNumber = 1234567890L; // 'L' suffix for long literals
20
21 printf("Small: %hd, Big: %ld\n", smallNumber, bigNumber);
22
23 return 0;
24}
25
26// Output:
27// Sum: 15, Product: 30
28// Small: 123, Big: 1234567890

3. Floating-Point Numbers (float, double)

Floating-point types store numbers with decimal points:

c
1#include <stdio.h>
2
3int main() {
4 float price = 10.99f; // 'f' suffix for float literals
5 double precise = 3.14159265359;
6
7 printf("Price: $%.2f\n", price); // %.2f means 2 decimal places
8 printf("Pi: %.10lf\n", precise); // %lf for double, 10 decimal places
9
10 // Be careful with floating-point comparisons
11 float a = 0.1f + 0.2f;
12 float b = 0.3f;
13
14 printf("0.1 + 0.2 = %.20f\n", a);
15 printf("0.3 = %.20f\n", b);
16
17 if (a == b) {
18 printf("Equal\n");
19 } else {
20 printf("Not equal due to floating-point precision!\n");
21 }
22
23 return 0;
24}
25
26// Output:
27// Price: $10.99
28// Pi: 3.1415926536
29// 0.1 + 0.2 = 0.30000001192092895508
30// 0.3 = 0.30000001192092895508
31// Not equal due to floating-point precision!

Constants in C

Constants are values that cannot be changed during program execution:

c
1#include <stdio.h>
2
3// Using #define directive (symbolic constant)
4#define PI 3.14159
5#define MAX_USERS 100
6
7int main() {
8 // Using const keyword (type-safe constant)
9 const double gravity = 9.81;
10 const char newline = '\n';
11
12 // Literal constants
13 printf("Integer: %d\n", 42); // Integer literal
14 printf("Float: %f\n", 3.14f); // Float literal
15 printf("Character: %c\n", 'X'); // Character literal
16 printf("String: %s\n", "Hello"); // String literal
17
18 // Using our defined constants
19 double circleArea = PI * 5 * 5;
20 printf("Area of circle with radius 5: %.2f\n", circleArea);
21
22 return 0;
23}
24
25// Output:
26// Integer: 42
27// Float: 3.140000
28// Character: X
29// String: Hello
30// 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.

c
int i = 10;
double d = i; // int to double, no data loss
char 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.

c
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:

c
1#include <stdio.h>
2
3int main() {
4 // Declare and initialize variables
5 float celsius, fahrenheit;
6 int kelvin;
7
8 // Get temperature in Celsius
9 printf("Enter temperature in Celsius: ");
10 scanf("%f", &celsius);
11
12 // Convert to Fahrenheit: F = (C * 9/5) + 32
13 fahrenheit = (celsius * 9.0 / 5.0) + 32.0;
14
15 // Convert to Kelvin: K = C + 273.15 (truncated to integer)
16 kelvin = (int)(celsius + 273.15);
17
18 // Display results
19 printf("%.2f Celsius = %.2f Fahrenheit\n", celsius, fahrenheit);
20 printf("%.2f Celsius = %d Kelvin (integer part)\n", celsius, kelvin);
21
22 return 0;
23}
24
25// Example Output:
26// Enter temperature in Celsius: 25
27// 25.00 Celsius = 77.00 Fahrenheit
28// 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 learning

Setting Up Your C Environment

Set up your development environment for C programming.

Continue learning

Basic Input and Output in C

Learn how to handle basic input and output operations in C.

Continue learning