C Language
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, and double.
  • Type modifiers like signed, unsigned, short, and long adjust 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 printf and scanf how 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 TypeStandard Size (Typical 64-bit)Value RangeFormat Specifier
char1 byte (8 bits)-128 to 127 (or 0 to 255)%c (char) or %d (ASCII)
unsigned char1 byte (8 bits)0 to 255%c or %u
short int2 bytes (16 bits)-32,768 to 32,767%hd
unsigned short2 bytes (16 bits)0 to 65,535%hu
int4 bytes (32 bits)-2,147,483,648 to 2,147,483,647%d or %i
unsigned int4 bytes (32 bits)0 to 4,294,967,295%u
long int4 or 8 bytes-9.22e18 to +9.22e18%ld
long long int8 bytes (64 bits)-(2^63) to (2^63 - 1)%lld
float4 bytes (single precision)~6-7 decimal digits precision%f
double8 bytes (double precision)~15-17 decimal digits precision%lf
long double10, 12, or 16 bytesExtended precision floating point%Lf
void0 bytesRepresents absence of value or untyped pointerN/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, and TOTALSCORE are 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)?

Beginner
2

Which of the following is an INVALID variable name in C?

Beginner