Introduction to C Programming Guide
Introduction to C Programming Guide
C Programming
Contents
1 Introduction to C Programming 2
1.1 What is C Programming? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2 Why Do We Need C Programming? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
10 Operators in C 11
10.1 Operator Table with Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
10.2 Unary Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
10.3 Increment and Decrement Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
10.4 Bitwise Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
10.5 Assignment Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
11 Input/Output Functions 13
12 Built-in Functions 14
13 Compiler vs Assembler 14
1
C Programming
1 Introduction to C Programming
1.1 What is C Programming?
Definition
C is a high-level programming language developed in the early 1970s by Dennis Ritchie at Bell
Labs. It is widely used for system programming, creating operating systems, embedded systems, and
general-purpose applications. C allows you to write programs that are efficient, fast, and portable.
➤ System Programming: Ideal for writing operating systems, compilers, and embedded systems.
➤ Control: Provides low-level access to memory and hardware for detailed control.
Computers excel at solving complex, tedious, and routine problems. To enable a computer to address
a problem, a programmer must devise a solution method and a detailed procedure. The problem-
solving process involves:
2
C Programming
Component Description
Input Data provided to the program (e.g., numbers, strings).
Output Expected results or deliverables from the program.
Processing Computations or operations to transform input into
output.
Example
• Processing: Compute C = A + B.
• Output: C (the sum).
Example
3.2 Flowchart
A flowchart is a diagrammatic representation of an algorithm, illustrating the sequence of operations.
3
C Programming
Example
Start
Read radius
Print area
Stop
3.3 Pseudocode
Pseudocode is a high-level description of an algorithm using natural language and programming constructs,
avoiding language-specific syntax.
Example
BEGIN
READ radius
area = 3.14 * radius * radius
PRINT area
END
4
C Programming
General Syntax
1 Preprocessor Directives
2 Global Declarations
3
4 int main ()
5 {
6 Local Variable Declarations ;
7 Executable Statements ;
8 return 0;
9 }
10
Explanation of Components
Components
❐ Local Declarations: Variables declared inside a function, accessible only within it.
❐ Executable Statements: Instructions performing actions such as input/output or calcula-
tions.
❐ User-defined Functions: Additional functions created by the programmer to perform specific
tasks.
Example Program
17 // Function definition
18 int add ( int a , int b ) {
19 return a + b ;
20 }
5
C Programming
✎ Executable File (.exe / [Link]): Final runnable program created by the linker.
Process Diagram
Compilation Process
6
C Programming
Script Mode
In this mode, you write the whole program in a file, save it, and then run it. The entire program is
executed together after compilation. Example: Running a C program using gcc on your computer.
Explanation: Each line is typed and executed immediately. You can see the output right after entering
the command.
Output:
Sum = 8
Explanation: Even though C is a compiled language, online compilers make it feel interactive because
they automatically compile and run the program when you click Run.
7
C Programming
Definition/Purpose: Comments are notes you write in your code to explain what it does. They
are ignored by the compiler or interpreter, so they do not affect how the program runs.
Why use them:
7.2 Indentation
Indentation
Definition/Purpose: Indentation is adding spaces or tabs at the beginning of a line to show its
structure. Some languages (like Python) require it, others (like C/C++) use it for readability.
Why use it:
✱ Makes code easier to read.
✱ Shows which lines belong together in blocks (like inside if, for, while).
8
C Programming
int main() {
// Declare variables
float weight, price_per_gram, total_price;
return 0;
}
Definition: A mistake in the rules of the programming language. The program cannot compile or
run.
Example:
int x = 10 // Missing semicolon
Error Message: The compiler may say: “expected ’;’ before ’’“
int a = 10, b = 0;
int c = a / b; // Division by zero
Error Message: Division by zero or program crash.
9
C Programming
9.2 Keywords
Keywords are reserved words (32 in C) with predefined meanings, not usable as variable names.
All 32 Keywords in C
The 32 keywords in ANSI C are: auto, break, case, char, const, continue, default, do, double, else,
enum, extern, float, for, goto, if, int, long, register, return, short, signed, sizeof, static, struct, switch,
typedef, union, unsigned, void, volatile, while.
9.4 Variables
A variable is a named memory location whose value can change during execution.
• Example:
1 int sum ; // Declaration
2 float pi = 3.14; // Initialization
10
C Programming
9.5 Constants
A constant is a fixed value that cannot change during execution.
10 Operators in C
Definition
Operators and Operands
11
C Programming
Increment (++) and decrement (--) operators add or subtract 1. They can be:
• Prefix (++x / –x): Increments/decrements first, then uses the new value. Example: int
x=5; int y=++x; → y=6, x=6
• Postfix (x++ / x–): Uses current value, then increments/decrements. Example: int x=5;
int y=x++; → y=5, x=6
Commonly used in loops and counters. Precedence is higher than arithmetic operators.
• NOT (˜
): Inverts bits. Example: ˜5 = -6
• Left Shift («): Shifts bits left. Example: 5 << 1 = 10
• Right Shift (»): Shifts bits right. Example: 5 >> 1 = 2
12
C Programming
11 a ++; ++ b ;
12 int eq = ( a == b ) ;
13 int gt = ( a > b ) ;
14 int logical = ( a > 0 && b > 0) ;
15 int bit_and = a & b ;
16 int left = a << 2;
17 int ternary = ( a > b ) ? a : b ;
18
19 printf ( " add =% d ␣ sub =% d ␣ mul =% d ␣ div =% d ␣ mod =% d \ n " , add , sub , mul , divv , mod ) ;
20 printf ( " eq =% d ␣ gt =% d ␣ logical =% d ␣ bit_and =% d ␣ left =% d ␣ ternary =% d \ n " ,
21 eq , gt , logical , bit_and , left , ternary ) ;
22 return 0;
23 }
11 Input/Output Functions
C I/O operations use function calls from the standard I/O library (<stdio.h>).
13
C Programming
12 Built-in Functions
Built-in functions are predefined in header files and perform specific tasks.
Example
13 Compiler vs Assembler
1. Definitions
Compiler
Assembler
An assembler converts assembly language instructions into machine code (binary).In modern
compilers (like gcc), this step happens automatically inside the compilation process.
2. Simple Example
C Source Code:
14
C Programming
3 int main () {
4 int a = 5 , b = 3 , c ;
5 c = a + b;
6 printf (" Sum = % d \ n " , c ) ;
7 return 0;
8 }
When you compile this using gcc prog.c -o prog, the compiler performs several steps automatically.
2. Compilation: The compiler converts the preprocessed C code into assembly language. Output:
temporary assembly file (e.g., prog.s).
3. Assembly: The assembler converts assembly code into object code (machine code). Output: prog.o
file.
4. Linking: The linker combines the object file with library files (like stdio) to create the final exe-
cutable file. Output: [Link] or the name you specify with -o.
6. Summary Table
Stage Language / Content Example File
1. Source Code Human-readable (C language) prog.c
2. Assembly Code CPU instructions (text form) prog.s (temporary)
3. Object Code Machine instructions (binary) prog.o
4. Executable File Ready-to-run program prog / [Link]
7. Visual Representation
C Source Code (.c)
|
| gcc compiler (includes assembler internally)
v
Object Code (.o)
|
| Linking
v
Executable File (.exe / [Link])
15
C Programming
8. In Simple Words
➢ Compiler: Converts C code to machine code (automatically calls assembler internally).
➢ Assembler: Converts assembly to machine code (used inside the compiler process).
➢ When we use gcc, we don’t have to run the assembler manually.
16