The C programming language was developed by Dennis Ritchie in 1972 at Bell Labs. It was initially designed for system programming, particularly for writing operating systems. C has since evolved and influenced many other languages, such as C++, Java, and Python.
Key Features of C:
Example: The C language is used in developing operating systems like Unix and embedded systems.
A C program consists of several key components:
#include
includes standard input/output functions.
main()
function. Every C program must have a main()
function.main()
function, you write the code
that performs the actual work of the program.#include <stdio.h> int main() { printf("Hello, World!\\n"); return 0; }
Explanation: This program simply prints "Hello, World!" to the screen. The printf
function
is used for output, and return 0
indicates the program finished successfully.
In C, functions are used to group code that performs a specific task. Functions help organize the program
and make it more modular. Functions can either be built-in (like printf
) or user-defined.
int
for integers or void
for functions that do not return a value.
#include <stdio.h> int add(int a, int b) { return a + b; } int main() { int result = add(5, 3); // Calling the add function printf("Sum: %d\\n", result); return 0; }
Explanation: The function add
takes two integers as input and returns their sum. In the
main
function, the add
function is called with the values 5 and 3, and the
result is printed using printf
.
In C, there are several language fundamentals that every programmer should understand. These include:
The character set of C includes all alphabets (both uppercase and lowercase), digits, special characters,
and whitespace. Characters are represented by single quotes, like 'A'
, 'b'
,
etc.
C tokens are the basic building blocks of a C program. They include:
int
, char
, if
,
while
.
myVar
or
sum
.
5
, 'A'
.
+
, -
, *
,
/
that perform operations on variables.
//
, while block comments use /* */
.
// This is a single-line comment #include <stdio.h> int main() { int num = 10; // Declaring a variable printf("Number: %d\\n", num); // Printing the variable return 0; }
Explanation: The program declares an integer variable num
, assigns it the value 10, and
prints it using the printf
function.
In C, a variable is a container for storing data values, while constants are fixed values that cannot be changed once assigned.
C provides several data types to define the type of data a variable can store. Some of the common data types are:
int num = 10;
float price = 10.99;
char grade = 'A';
double pi = 3.14159;
Constants are values that cannot be changed during program execution. They can be declared using the
const
keyword.
5
, 'A'
,
3.14
.
const
keyword. Example: const int MAX = 100;
.
#include <stdio.h> int main() { int x = 5; // Variable const int y = 10; // Constant printf("x = %d, y = %d\\n", x, y); return 0; }
Explanation: The program declares a variable x
with a value of 5 and a constant
y
with a value of 10. The values are then printed using printf
.
C supports various operators to perform computations and logical operations.
#include <stdio.h> int main() { int a = 5, b = 2; // Arithmetic printf("Add: %d\n", a + b); printf("Modulus: %d\n", a % b); // Relational printf("a == b: %d\n", a == b); // Logical printf("a > 0 && b > 0: %d\n", a > 0 && b > 0); // Assignment a += 3; printf("a after += 3: %d\n", a); // Bitwise printf("a & b: %d\n", a & b); // Conditional int max = (a > b) ? a : b; printf("Max: %d\n", max); return 0; }
Operator precedence determines the order of evaluation. Associativity determines direction.
#include <stdio.h> int main() { int result = 10 + 2 * 3; printf("Result: %d\n", result); // Output: 16 return 0; }
Explanation: *
has higher precedence than +
.
Expression: Any combination of variables and operators (e.g., a + b).
Statement: A complete instruction (e.g., int x = 5;).
Types:
int x;
x = 10;
printf()
, scanf()
if
, for
, while
, switch
#include <stdio.h> int main() { int x = 5; if (x > 0) { printf("Positive\n"); } return 0; }
printf()
: Outputs data to screenscanf()
: Reads input from usergetchar()
: Reads single characterputchar()
: Outputs single charactergetch()
: Reads char (doesn’t show on screen)#include <stdio.h> int main() { char ch; printf("Enter a char: "); ch = getchar(); // Input printf("You entered: "); putchar(ch); // Output return 0; }
Header Files: Contain code for built-in functions.
#include <stdio.h>
: Standard I/O#include <math.h>
: Math functions#include <string.h>
: String functionsPreprocessor Directives:
#include
: Adds header files#define
: Defines constants/macros#include <stdio.h> #define PI 3.14159 int main() { float r = 3; float area = PI * r * r; printf("Area: %.2f\n", area); return 0; }
Executes a block of code if the condition is true.
#include <stdio.h> int main() { int num = 10; if (num > 0) { printf("Positive number"); } return 0; }
Output: Positive number
Executes one block if true, another if false.
#include <stdio.h> int main() { int num = -5; if (num >= 0) { printf("Positive"); } else { printf("Negative"); } return 0; }
Output: Negative
Multiple conditions checked in hierarchy.
#include <stdio.h> int main() { int num = 0; if (num > 0) { printf("Positive"); } else if (num < 0) { printf("Negative"); } else { printf("Zero"); } return 0; }
Output: Zero
Used for multiple selections based on value.
#include <stdio.h> int main() { int choice = 2; switch (choice) { case 1: printf("One"); break; case 2: printf("Two"); break; case 3: printf("Three"); break; default: printf("Invalid"); } return 0; }
Output: Two
Repeats a block while the condition is true.
#include <stdio.h> int main() { int i = 1; while (i <= 5) { printf("%d ", i); i++; } return 0; }
Output: 1 2 3 4 5
Executes first, then checks the condition.
#include <stdio.h> int main() { int i = 1; do { printf("%d ", i); i++; } while (i <= 5); return 0; }
Output: 1 2 3 4 5
Compact loop structure for fixed iterations.
#include <stdio.h> int main() { for (int i = 1; i <= 5; i++) { printf("%d ", i); } return 0; }
Output: 1 2 3 4 5
Loops inside another loop (useful for patterns).
#include <stdio.h> int main() { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { printf("(%d,%d) ", i, j); } printf("\n"); } return 0; }
Output: (1,1) (1,2) (1,3) (2,1) (2,2) (2,3) (3,1) (3,2) (3,3)
Exits a loop or switch early.
#include <stdio.h> int main() { for (int i = 1; i <= 10; i++) { if (i == 5) break; printf("%d ", i); } return 0; }
Output: 1 2 3 4
Skips to the next iteration.
#include <stdio.h> int main() { for (int i = 1; i <= 5; i++) { if (i == 3) continue; printf("%d ", i); } return 0; }
Output: 1 2 4 5
Jumps to a labeled statement (use with caution).
#include <stdio.h> int main() { int num = 1; if (num == 1) { goto label; } printf("This will be skipped\n"); label: printf("Jumped to label!\n"); return 0; }
Output: Jumped to label!
Terminates the program immediately.
#include <stdio.h> #include <stdlib.h> int main() { printf("Before exit\n"); exit(0); printf("This won't be printed\n"); return 0; }
Output: Before exit
#include <stdio.h> int main() { int a = 5, b = 3; printf("Addition: %d\n", a + b); printf("Multiplication: %d\n", a * b); return 0; }
Output: Addition: 8 Multiplication: 15
#include <stdio.h> int main() { int num = -7; if(num > 0) printf("Positive\n"); else if(num < 0) printf("Negative\n"); else printf("Zero\n"); if(num % 2 == 0) printf("Even\n"); else printf("Odd\n"); return 0; }
Output: Negative Odd
// Maximum of 2 numbers #include <stdio.h> int main() { int a = 10, b = 20; if(a > b) printf("Max: %d\n", a); else printf("Max: %d\n", b); return 0; }
Output: Max: 20
// Maximum of 3 numbers #include <stdio.h> int main() { int a = 10, b = 20, c = 15; if(a > b && a > c) printf("Max: %d\n", a); else if(b > c) printf("Max: %d\n", b); else printf("Max: %d\n", c); return 0; }
Output: Max: 20
#include <stdio.h> int main() { int n = 5, sum = 0; for(int i = 1; i <= n; i++) { sum += i; } printf("Sum = %d\n", sum); return 0; }
Output: Sum = 15
#include <stdio.h> int main() { int a = 10, b = 3; printf("Quotient = %d\n", a / b); printf("Remainder = %d\n", a % b); return 0; }
Output: Quotient = 3 Remainder = 1
#include <stdio.h> int main() { int num = 1234, rev = 0; while(num != 0) { rev = rev * 10 + num % 10; num /= 10; } printf("Reversed = %d\n", rev); return 0; }
Output: Reversed = 4321
#include <stdio.h> int main() { int n = 5; for(int i = 1; i <= 10; i++) { printf("%d x %d = %d\n", n, i, n * i); } return 0; }
Output: 5 x 1 = 5 5 x 2 = 10 ... 5 x 10 = 50
#include <stdio.h> int main() { int n = 5, fact = 1; for(int i = 1; i <= n; i++) { fact *= i; } printf("Factorial = %d\n", fact); return 0; }
Output: Factorial = 120
#include <stdio.h> int main() { int num = 7, isPrime = 1; for(int i = 2; i <= num / 2; i++) { if(num % i == 0) { isPrime = 0; break; } } if(isPrime) printf("Prime Number\n"); else printf("Not Prime\n"); return 0; }
Output: Prime Number
#include <stdio.h> int fact(int n) { int f = 1; for(int i = 1; i <= n; i++) f *= i; return f; } int main() { int n = 5; for(int i = 0; i < n; i++) { for(int space = 0; space < n - i; space++) printf(" "); for(int j = 0; j <= i; j++) { printf("%d ", fact(i)/(fact(j)*fact(i-j))); } printf("\n"); } return 0; }
Output: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1
#include <stdio.h> int main() { int a = 36, b = 60, gcd; for(int i = 1; i <= a && i <= b; ++i) { if(a % i == 0 && b % i == 0) gcd = i; } printf("GCD = %d\n", gcd); return 0; }
Output: GCD = 12
#include <stdio.h> int main() { int a = 5, b = 10, temp; temp = a; a = b; b = temp; printf("a = %d, b = %d\n", a, b); return 0; }
Output: a = 10, b = 5
Functions in C are blocks of code that perform a specific task. There are mainly two types:
printf()
,
scanf()
.
Function Declaration: Tells the compiler about the function name, return type, and parameters.
Function Definition: Contains the actual body or implementation of the function.
// Declaration int add(int, int); // Definition int add(int a, int b) { return a + b; }
To use a function, we call it from another function (typically main()
).
#include <stdio.h> int add(int a, int b) { return a + b; } int main() { int sum = add(5, 3); printf("Sum = %d\\n", sum); return 0; }
Sum = 8
#include <stdio.h> int multiply(int a, int b) { return a * b; } int main() { int result = multiply(4, 5); printf("Product = %d\\n", result); return 0; }
Product = 20
There are two types of parameter passing:
#include <stdio.h> void change(int x) { x = 100; } int main() { int a = 5; change(a); printf("Value of a = %d\\n", a); return 0; }
Value of a = 5
#include <stdio.h> void change(int *x) { *x = 100; } int main() { int a = 5; change(&a); printf("Value of a = %d\\n", a); return 0; }
Value of a = 100
#include <stdio.h> int global = 50; // Global int main() { int local = 10; // Local printf("Global = %d, Local = %d\\n", global, local); return 0; }
Global = 50, Local = 10
Storage classes define scope, lifetime, and linkage of variables.
#include <stdio.h> void counter() { static int count = 0; count++; printf("Count = %d\\n", count); } int main() { counter(); counter(); counter(); return 0; }
Count = 1 Count = 2 Count = 3
Recursion is when a function calls itself to solve a smaller part of the problem.
#include <stdio.h> int factorial(int n) { if(n == 0) return 1; else return n * factorial(n - 1); } int main() { int num = 5; printf("Factorial of %d = %d\\n", num, factorial(num)); return 0; }
Factorial of 5 = 120