Expert Insight
🤔 Expert Insight: Why C is Still Critical (3GL)
C is often called a "middle-level" language. Why? Because while it's a 3GL (high-level structure), its efficiency and features allow for direct hardware manipulation and memory access, traditionally reserved for lower-level languages. This efficiency is why it forms the core of operating systems like UNIX and Windows.
Problem-Solving Tools: Algorithm & Flowchart
Before writing a single line of code, you must design a solution. Algorithms and Flowcharts are the two essential tools for planning.
Algorithm: A finite sequence of well-defined, unambiguous steps to solve a specific problem. It's written in plain English or pseudocode.
Example: Algorithm to calculate the sum of two numbers.
// C program to add two numbers
#include <stdio.h>
int main() {
int a, b, sum = 0;
// Read two numbers from the user
printf("Enter two integers: ");
scanf("%d %d", &a, &b);
// Calculate the addition of a and b
// using '+' operator
sum = a + b;
printf("Sum: %d", sum);
return 0;
}
Enter two integers: 5 3
Sum: 8
Flowchart: A graphical representation of an algorithm, using standard symbols to depict the flow of logic.
Pro Tip: Always define your algorithm first. A faulty plan leads to faulty code!
The C Compilation Process
C is a compiled language. This means the entire source code is translated into machine code before execution. This is generally faster than interpreted languages (like Python, where code is translated line-by-line during runtime).
The journey from your source code to an executable program has four crucial stages:
Preprocessing: The preprocessor handles directives (lines starting with
#). It includes header files (#include) and handles macro substitutions (#define).Input:
filename.c(Source Code)
Compilation: The compiler translates the preprocessed code into Assembly Code.
Assembly: The assembler translates the assembly code into Machine Code (binary), generating an Object File.
Output:
filename.obj(Object File)
Linking: The linker combines your program's object file with necessary external libraries (like the standard input/output functions from
stdio.h) to create the final, complete Executable File.Output:
filename.exe(Executable File)
Fundamentals: Building Blocks
The basic elements of any C program are called Tokens. There are six types of tokens, but we'll focus on the most fundamental ones: Keywords, Identifiers, and Constants.
A. C Character Set
The allowed characters in a C source file, including letters (A-Z, a-z), digits (0-9), and special characters (e.g., ,, ;, (, +, etc.).
B. Keywords (Reserved Words)
These are 32 words that have a predefined meaning in the C compiler. You cannot use them as names for variables, functions, or other entities.
Examples:
int,void,if,else,while,for,return.
C. Identifiers (User-Defined Names)
These are the names you give to variables, functions, arrays, etc., to uniquely identify them in the program.
Rules for Identifiers:- Can contain letters (a-z, A-Z), digits (0-9), and the underscore (
_). - Must start with a letter or an underscore (
_). Cannot start with a digit. - C is case-sensitive (e.g.,
Ageandageare considered two different identifiers). - Cannot be a Keyword.
Data Types and Variables
A Variable is a named storage location in memory used to hold a value. Every variable must have a Data Type, which tells the compiler two things:
The size of memory to allocate (e.g., 4 bytes for an
int).The type of value it can store (e.g., integers, decimals, characters).