Skip to main content

3. Variables and Data Types

3.1 Variable Declaration

Python vs C:

Python C
x = 5 int x = 5;
name = "John" char name[] = "John";
pi = 3.14 float pi = 3.14f;

3.2 Basic Data Types

3.2.1 Integer Types

Type Size (bytes) Range Usage
char 1 -128 to 127 Small integers, characters
short 2 -32,768 to 32,767 Small integers
int 4 -2,147,483,648 to 2,147,483,647 Standard integers
long 4/8 System dependent Large integers
long long 8 Very large range Very large integers

Unsigned Variants:

unsigned char uc;    // 0 to 255
unsigned int ui;     // 0 to 4,294,967,295
unsigned short us;   // 0 to 65,535

3.2.2 Floating-Point Types

Type Size Precision Range
float 4 bytes ~7 digits ±3.4 × 10^±38
double 8 bytes ~15 digits ±1.7 × 10^±308
long double 12/16 bytes Extended precision System dependent

3.2.3 Character and String Types

Single Characters:

char letter = 'A';        // Single character
char digit = '5';         // Character representation of digit
char newline = '\n';      // Escape sequence

Strings (Character Arrays):

char name[20] = "John";           // Fixed-size array
char message[] = "Hello World";   // Size determined by initializer
char buffer[100];                 // Uninitialized array

Python vs C String Comparison:

Python C
name = "John" char name[] = "John";
len(name) strlen(name)
name[0] name[0]
name + " Doe" strcat(name, " Doe");

3.3 Variable Declaration Rules

  1. Must declare before use (unlike Python)
  2. Case-sensitive (ageAge)
  3. Cannot start with digits (2x is invalid)
  4. Cannot use keywords (int, if, while, etc.)
  5. Should use meaningful names (student_count not sc)

3.4 Constants

// Method 1: #define preprocessor directive
#define PI 3.14159
#define MAX_SIZE 100

// Method 2: const keyword
const int ARRAY_SIZE = 50;
const float GRAVITY = 9.81f;

Python vs C Constants:

Python C
PI = 3.14159 #define PI 3.14159
PI = 3.14159 const float PI = 3.14159f;