2. Tokens, Variables & Data TypesBeginner9 min read
C Variables, Data Types & Format Specifiers
Master primary data types (int, float, double, char), type modifiers, sizeof operator, variable naming conventions, and format specifiers.
Key Takeaways & Core Concepts
- C is a statically-typed language: every variable must have an explicit type declared before use.
- Primitive types include
int,char,float, anddouble. - Type modifiers like
signed,unsigned,short, andlongadjust value range and memory footprint. sizeof()is a compile-time operator that returns the memory size of a type or variable in bytes.- Format specifiers tell
printfandscanfhow to interpret raw binary data in memory.
Primitive Data Types in C
C provides fundamental data types for representing characters, whole integers, and floating-point real numbers:
| Data Type | Standard Size (Typical 64-bit) | Value Range | Format Specifier |
|---|---|---|---|
| char | 1 byte (8 bits) | -128 to 127 (or 0 to 255) | %c (char) or %d (ASCII) |
| unsigned char | 1 byte (8 bits) | 0 to 255 | %c or %u |
| short int | 2 bytes (16 bits) | -32,768 to 32,767 | %hd |
| unsigned short | 2 bytes (16 bits) | 0 to 65,535 | %hu |
| int | 4 bytes (32 bits) | -2,147,483,648 to 2,147,483,647 | %d or %i |
| unsigned int | 4 bytes (32 bits) | 0 to 4,294,967,295 | %u |
| long int | 4 or 8 bytes | -9.22e18 to +9.22e18 | %ld |
| long long int | 8 bytes (64 bits) | -(2^63) to (2^63 - 1) | %lld |
| float | 4 bytes (single precision) | ~6-7 decimal digits precision | %f |
| double | 8 bytes (double precision) | ~15-17 decimal digits precision | %lf |
| long double | 10, 12, or 16 bytes | Extended precision floating point | %Lf |
| void | 0 bytes | Represents absence of value or untyped pointer | N/A (void*) |
Variable Declaration & Naming Rules
A variable is a named storage location in memory. When declaring variables in C:
- Names may contain letters (
a-z,A-Z), digits (0-9), and underscores (_). - Names must begin with a letter or an underscore. They cannot begin with a number.
- C is strictly case-sensitive:
totalScore,TotalScore, andTOTALSCOREare three distinct variables. - Cannot use any of the 32 reserved C keywords (e.g.,
int,return,while,break).
Data Types and sizeof() in Action
1#include <stdio.h>
2#include <stdbool.h> // C99 bool support
3
4int main(void) {
5 char grade = 'A';
6 int score = 98;
7 float percentage = 98.45f;
8 double preciseValue = 123456.789012;
9 bool isPassed = true;
10
11 printf("Grade: %c (Size: %zu byte, ASCII: %d)\n", grade, sizeof(grade), grade);
12 printf("Score: %d (Size: %zu bytes)\n", score, sizeof(score));
13 printf("Percentage: %.2f (Size: %zu bytes)\n", percentage, sizeof(percentage));
14 printf("Double: %.6lf (Size: %zu bytes)\n", preciseValue, sizeof(preciseValue));
15 printf("Bool: %d (Size: %zu byte)\n", isPassed, sizeof(isPassed));
16
17 return 0;
18}
Click code or "Edit Code" to edit
Interactive Code Block•Tab = 4 spaces•Ctrl + Enter = Run
18 lines
Note: The sizeof operator returns size_t, which is printed using the %zu format specifier. Notice grade 'A' has an ASCII numeric value of 65.
Practice & AssessmentC Programming Complete Course
Test Your Understanding
Reinforce core concepts with interactive multiple-choice questions, live coding challenges, and top technical interview questions.
Your Current Score
0 / 2 correct(0 attempted)
1
What is guaranteed by the C standard regarding sizeof(char)?
Beginner2
Which of the following is an INVALID variable name in C?
Beginner