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%

Special Keywords: volatile and restrict

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

Dealing with Hardware and Optimization

C provides two advanced keywords that are essential for systems and embedded programming. They tell the compiler exactly how to optimize (or NOT optimize) your memory access.

1. The volatile Keyword

Normally, a compiler optimizes your code. If it sees:

int status = 1;
while(status == 1) { /* do nothing */ }

The compiler might think: "Wait, status is never changed inside this loop, so I'll just change this to while(true)."

But what if status is a memory address connected to a physical button or a sensor? The hardware could change that value at any second!

  • volatile tells the compiler: "This variable can change unexpectedly. Do not cache it; read it from RAM every single time."

2. The restrict Keyword (C99)

This is a hint to the compiler for optimization. It tells the compiler that a specific pointer is the only way to access that memory block.

void copy(int * restrict src, int * restrict dest);

This allows the compiler to perform much more aggressive optimizations because it knows there's no "aliasing" (two pointers overlapping).

You'll rarely use these in high-level app development, but you'll see them everywhere in Linux kernel source code and microcontroller drivers!