Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Variables & Fundamental Data Types

Lesson 5 of 53 in the free Foundation of C & C++ notes on Siksha Sarovar, written by Rohit Jangra.

Memory and Storage

A variable is a named storage location in your computer's RAM. When you declare a variable, the computer reserves a specific block of memory for it based on the "Data Type" you choose.

1. Integer Types (int)

Used for whole numbers without any decimal points.

  • Standard int: Usually 4 bytes. Range: approximately -2.1 Billion to +2.1 Billion.
  • short int: Usually 2 bytes. For when you want to save memory and know the number is small (-32,768 to 32,767).
  • long int: 4 or 8 bytes. For larger whole numbers.
  • unsigned int: Only positive numbers. It effectively doubles the positive range (0 to 4.2 Billion) by using the bit usually reserved for the negative sign.

2. Floating-Point Types

Used for numbers with decimal points.

  • float: 4 bytes. Precision: about 6-7 decimal places. Always use an f suffix like 3.14f to tell C it's a float.
  • double: 8 bytes. Precision: about 15-16 decimal places. This is the default type for decimal numbers in C. Use this for most scientific or financial calculations.
  • long double: 10, 12, or 16 bytes depending on the hardware. For extreme precision.

3. Character Type (char)

  • char: 1 byte. Stores a single character.
  • ASCII Encoding: Crucially, C stores characters as numbers. For example, the character 'A' is actually stored as the number 65 in memory. This means you can do math with characters! 'A' + 1 is equal to 'B'.

Variable Declaration & Initialization

In C, you must declare a variable's type before you use it. This allows the compiler to know how much memory to reserve.

int age;           // Declaration (reserves 4 bytes)
age = 25;          // Initialization (stores 25 in those bytes)
float salary = 4500.50f; // Declaration + Initialization
char grade = 'A';  // Single characters always use single quotes

Rules for Identifiers (Names)

  • Must start with a letter (a-z, A-Z) or an underscore (_).
  • Cannot start with a digit.
  • Can contain letters, digits, and underscores.
  • Case-sensitive: myVar, MyVar, and myvar are three different variables!
  • Cannot be a reserved keyword (like int or while).

Constants

Constants are values that are "read-only" and cannot be changed after they are defined.

  1. const keyword: const float PI = 3.14159;
  2. #define directive: #define MAX_SPEED 120 (Note: no semicolon or equals sign here).

Format Specifiers Summary

When you want to print or read these types, you use these special codes:

TypeSpecifierDescription
int%dDecimal Integer
float%fFloating point
double%lfLong Float (Double)
char%cSingle Character
char[]%sString (Array of characters)
long%ldLong Integer
unsigned%uUnsigned Integer
Garbage Values: C does not automatically initialize local variables to zero. A new variable contains "Garbage Value"—whatever random bits were left in that memory spot by a previous program. Always initialize your variables before reading from them!