C Programming for Problem Solving - Notes
1. Introduction to C Programming
C is a high-level, structured programming language developed by Dennis Ritchie at Bell Labs in
1972. It combines the power of low-level programming (like assembly) with the simplicity of
high-level logic.
Features of C
- Simple and efficient
- Portable (runs on many systems)
- Structured language
- Rich library of functions
- Fast execution
- Supports pointers and dynamic memory
Structure of a C Program
#include <stdio.h> int main() { printf("Hello, World!"); return 0; } Basic structure:
1. Header files
2. main() function
3. Variable declaration
4. Statements
5. Return statement
2. Basic Concepts
Keywords & Identifiers
- Keywords are reserved words (e.g., int, for, if, return).
- Identifiers are names given to variables, functions, arrays, etc.
Variables & Constants
- Variable: A name given to a memory location.
- Constant: Fixed value that cannot be changed.
Type Example Size
int 10 2 or 4 bytes
float 12.5 4 bytes
char 'A' 1 byte
double 15.5555 8 bytes
3. Control Statements
Used to control the flow of execution.
Types:
1. Decision-making: if, if-else, nested if, switch
2. Looping: for, while, do-while
3. Jumping: break, continue, goto, return
4. Functions and Arrays
Functions help divide a program into smaller, manageable parts.
Syntax:
return_type function_name(parameters) { // code return value; } Arrays store multiple values of the
same type in contiguous memory locations.
Syntax:
int arr[5] = {1, 2, 3, 4, 5};
5. Pointers
Pointers are variables that store memory addresses of other variables.
Syntax:
int a = 10; int *p; p = &a; They allow dynamic memory allocation and efficient array handling.
6. File Handling
Used to store and retrieve data permanently.
File operations:
1. fopen() – Opens a file
2. fprintf()/fscanf() – Write/Read formatted data
3. fgetc()/fputc() – Read/Write a character
4. fclose() – Closes the file
5. feof() – Checks end of file
7. Problem Solving Using C
Steps to solve problems:
1. Understand the problem statement
2. Identify inputs and outputs
3. Design an algorithm
4. Write pseudocode
5. Implement in C
6. Test and debug the program
Example: Program to find factorial of a number
#include <stdio.h> int main() { int n, i, fact = 1; printf("Enter a number: "); scanf("%d", &n;); for(i = 1; i
<= n; i++) fact = fact * i; printf("Factorial = %d", fact); return 0; }