Unit 1: Basics of C Programming - Comprehensive
Notes with Examples and Code
This comprehensive guide covers all the fundamental concepts of C programming with real-life
examples, practical code demonstrations, and detailed explanations based on extensive
research from authoritative sources.
1. What is C?
C is a general-purpose, procedural programming language developed by Dennis Ritchie in 1972
at Bell Labs. It was originally created to write the UNIX operating system and has since become
one of the most influential programming languages in computer science. [1] [2] [3] [4]
Key Characteristics:
Efficiency: Direct hardware access and minimal runtime overhead
Portability: Code can run on different platforms with minimal changes
Foundation Language: Influenced C++, Java, Python, and many other modern
languages [5] [1]
Real-life Applications:
Operating systems (UNIX, Linux) [3] [5]
Embedded systems and microcontrollers
Database management systems
Compilers and interpreters
System software development
// Simple C program demonstrating basic structure
#include <stdio.h>
int main() {
printf("Hello, World! This is C programming.\n");
return 0;
}
,2. Structure of a C Program
Every C program follows a well-defined structure that includes several essential components: [6]
[7] [8]
Components of C Program Structure:
1. Preprocessor Directives - Include header files and define macros
2. Global Declarations - Variables and constants visible throughout the program
3. main() Function - Entry point of program execution
4. Variable Declarations - Local variables within functions
5. Executable Statements - Actual program logic
6. User-defined Functions - Optional custom functions
#include <stdio.h> // Preprocessor directive
#include <math.h>
#define PI 3.14159 // Global constant definition
int globalVar = 100; // Global variable
// Function declaration
int calculateArea(int radius);
int main() { // Main function - entry point
int radius = 5; // Local variable declaration
int area; // Variable declaration
// Executable statements
printf("Enter radius: ");
scanf("%d", &radius);
area = calculateArea(radius);
printf("Area of circle: %d\n", area);
return 0; // Return statement
}
// Function definition
int calculateArea(int radius) {
return PI * radius * radius;
}
Real-life Analogy: A C program is like a recipe - it has ingredients list (header files), preparation
steps (variable declarations), main cooking process (main function), and optional helper
procedures (user-defined functions). [7]
,3. Keywords
Keywords are reserved words in C that have predefined meanings and cannot be used as
identifiers. C has 32 standard keywords that serve specific purposes in the language. [9] [10]
Categories of Keywords:
Category Keywords Purpose
int, float, double, char, void, short, long, signed,
Data Types Define variable types
unsigned
if, else, switch, case, default, for, while, do, break, Control program
Control Flow
continue execution
Storage Classes auto, register, static, extern Define variable storage
Modify variable
Type Qualifiers const, volatile
behavior
User-Defined Create custom data
struct, union, enum
Types types
Operators sizeof, typedef Special operations
Jump Statements return, goto Control program flow
#include <stdio.h>
int main() {
// Keywords in action
const int MAX_SIZE = 100; // const keyword
static int counter = 0; // static keyword
register int fastVar = 10; // register keyword
if (fastVar > 5) { // if keyword
printf("Value is greater than 5\n");
} else { // else keyword
printf("Value is 5 or less\n");
}
for (int i = 0; i < 3; i++) { // for keyword
counter++; // Increment using ++
if (counter == 2) {
break; // break keyword
}
}
return 0; // return keyword
}
Real-life Analogy: Keywords are like traffic signs - they have fixed meanings that everyone
must follow, and you cannot use them for other purposes. [11]
, 4. Identifiers
Identifiers are names given to variables, functions, arrays, structures, and other program
elements. They allow programmers to reference and manipulate program entities. [12] [13]
Rules for Naming Identifiers: [14] [13] [12]
1. Must start with a letter (A-Z, a-z) or underscore (_)
2. Can contain letters, digits (0-9), and underscores
3. Case-sensitive (myVar and myvar are different)
4. Cannot be keywords
5. No spaces or special characters allowed
6. Should be meaningful and descriptive
Examples:
#include <stdio.h>
int main() {
// Valid identifiers
int studentAge = 20;
float _salary = 50000.50;
char firstName[^20] = "John";
int count123 = 0;
// Invalid identifiers (these will cause errors)
// int 2names; // Cannot start with digit
// int student-age; // Hyphen not allowed
// int if; // Cannot use keywords
// int student name; // Spaces not allowed
printf("Student age: %d\n", studentAge);
printf("Salary: %.2f\n", _salary);
printf("Name: %s\n", firstName);
return 0;
}
Naming Conventions: [14]
Use descriptive names: studentGrade instead of sg
Use camelCase: calculateTotal or underscore_case: calculate_total
Constants in UPPERCASE: MAX_SIZE, PI_VALUE
Real-life Analogy: Identifiers are like name tags at a conference - they help identify and
distinguish different people (variables/functions) in the same room (program). [14]
Notes with Examples and Code
This comprehensive guide covers all the fundamental concepts of C programming with real-life
examples, practical code demonstrations, and detailed explanations based on extensive
research from authoritative sources.
1. What is C?
C is a general-purpose, procedural programming language developed by Dennis Ritchie in 1972
at Bell Labs. It was originally created to write the UNIX operating system and has since become
one of the most influential programming languages in computer science. [1] [2] [3] [4]
Key Characteristics:
Efficiency: Direct hardware access and minimal runtime overhead
Portability: Code can run on different platforms with minimal changes
Foundation Language: Influenced C++, Java, Python, and many other modern
languages [5] [1]
Real-life Applications:
Operating systems (UNIX, Linux) [3] [5]
Embedded systems and microcontrollers
Database management systems
Compilers and interpreters
System software development
// Simple C program demonstrating basic structure
#include <stdio.h>
int main() {
printf("Hello, World! This is C programming.\n");
return 0;
}
,2. Structure of a C Program
Every C program follows a well-defined structure that includes several essential components: [6]
[7] [8]
Components of C Program Structure:
1. Preprocessor Directives - Include header files and define macros
2. Global Declarations - Variables and constants visible throughout the program
3. main() Function - Entry point of program execution
4. Variable Declarations - Local variables within functions
5. Executable Statements - Actual program logic
6. User-defined Functions - Optional custom functions
#include <stdio.h> // Preprocessor directive
#include <math.h>
#define PI 3.14159 // Global constant definition
int globalVar = 100; // Global variable
// Function declaration
int calculateArea(int radius);
int main() { // Main function - entry point
int radius = 5; // Local variable declaration
int area; // Variable declaration
// Executable statements
printf("Enter radius: ");
scanf("%d", &radius);
area = calculateArea(radius);
printf("Area of circle: %d\n", area);
return 0; // Return statement
}
// Function definition
int calculateArea(int radius) {
return PI * radius * radius;
}
Real-life Analogy: A C program is like a recipe - it has ingredients list (header files), preparation
steps (variable declarations), main cooking process (main function), and optional helper
procedures (user-defined functions). [7]
,3. Keywords
Keywords are reserved words in C that have predefined meanings and cannot be used as
identifiers. C has 32 standard keywords that serve specific purposes in the language. [9] [10]
Categories of Keywords:
Category Keywords Purpose
int, float, double, char, void, short, long, signed,
Data Types Define variable types
unsigned
if, else, switch, case, default, for, while, do, break, Control program
Control Flow
continue execution
Storage Classes auto, register, static, extern Define variable storage
Modify variable
Type Qualifiers const, volatile
behavior
User-Defined Create custom data
struct, union, enum
Types types
Operators sizeof, typedef Special operations
Jump Statements return, goto Control program flow
#include <stdio.h>
int main() {
// Keywords in action
const int MAX_SIZE = 100; // const keyword
static int counter = 0; // static keyword
register int fastVar = 10; // register keyword
if (fastVar > 5) { // if keyword
printf("Value is greater than 5\n");
} else { // else keyword
printf("Value is 5 or less\n");
}
for (int i = 0; i < 3; i++) { // for keyword
counter++; // Increment using ++
if (counter == 2) {
break; // break keyword
}
}
return 0; // return keyword
}
Real-life Analogy: Keywords are like traffic signs - they have fixed meanings that everyone
must follow, and you cannot use them for other purposes. [11]
, 4. Identifiers
Identifiers are names given to variables, functions, arrays, structures, and other program
elements. They allow programmers to reference and manipulate program entities. [12] [13]
Rules for Naming Identifiers: [14] [13] [12]
1. Must start with a letter (A-Z, a-z) or underscore (_)
2. Can contain letters, digits (0-9), and underscores
3. Case-sensitive (myVar and myvar are different)
4. Cannot be keywords
5. No spaces or special characters allowed
6. Should be meaningful and descriptive
Examples:
#include <stdio.h>
int main() {
// Valid identifiers
int studentAge = 20;
float _salary = 50000.50;
char firstName[^20] = "John";
int count123 = 0;
// Invalid identifiers (these will cause errors)
// int 2names; // Cannot start with digit
// int student-age; // Hyphen not allowed
// int if; // Cannot use keywords
// int student name; // Spaces not allowed
printf("Student age: %d\n", studentAge);
printf("Salary: %.2f\n", _salary);
printf("Name: %s\n", firstName);
return 0;
}
Naming Conventions: [14]
Use descriptive names: studentGrade instead of sg
Use camelCase: calculateTotal or underscore_case: calculate_total
Constants in UPPERCASE: MAX_SIZE, PI_VALUE
Real-life Analogy: Identifiers are like name tags at a conference - they help identify and
distinguish different people (variables/functions) in the same room (program). [14]