C-programming notes
Chapter 1: Introduction to C
1. Pseudocode
Definition: Informal list of steps in English-like statements intended for human reading.
Usage: Used in planning computer program development to sketch program structure before coding.
Advantages of Pseudocode:
Easy to write.
Readable and understandable.
Simple to modify.
Disadvantages of Pseudocode:
No standard format exists.
Variations exist for the same problem statement.
Examples:
Addition of two numbers:
Begin
Read two numbers a, b
Calculate sum = a + b
Display sum
End
Area of rectangle:
Begin
Read length, breadth
Calculate area = l * b
Display area
End
2. History of C
Overview: C is a general-purpose, procedural, structured programming language developed by Dennis Ritchie in 1972 at AT&T Bell Labs.
Development: Initially designed to write UNIX system software.
Predecessor: Successor to the B programming language.
C Standards: Includes K&R C, ANSI C, ISO C.
Characteristics of C:
Easy to learn.
General-purpose language.
Procedural and structured.
Portable and extensible.
Examples of C Applications:
Operating systems.
Language compilers.
Assemblers.
Text editors.
Databases.
3. Basic C Characters
Definition: C character set defines valid characters in a source program.
Components:
Letters:
Uppercase: A, B, C, ..., Z
Lowercase: a, b, c, ..., z
Digits: 0, 1, 2, ..., 9
Special Characters: !, ., #, $, (, ), {, }, etc.
White Spaces: Blank space, horizontal tab, carriage return, new line character ( ), form feed.
4. C Tokens
Definition: Smallest individual unit in a C program, forming the building blocks of the program.
Types of C Tokens: i. Keywords ii. Identifiers iii. Constants iv. Strings v. Operators vi. Special Symbols
4.1 Keywords:
Definition: Reserved words with predefined meanings. Must be in lowercase and cannot be identifiers.
Examples: auto, double, int, struct, break, do, else, long, switch, goto, sizeof, if, case, enum, register, typedef, default, char, extern, return, union, volatile, const, float, short, unsigned, while, continue, for, signed, void, static.
5. Identifiers
Definition: Names for objects such as variables, functions, and macros.
Rules:
Must contain alphabets (A-Z, a-z), digits (0-9), and underscores (_).
Must start with an alphabet or underscore.
No special symbols except for underscore.
No spaces allowed.
Cannot be keywords.
Case sensitive with a maximum length of 31 characters.
Valid/Invalid Examples:
Valid: integer, minimum, sum_total, row1, _cpps
Invalid: float (keyword), 123_Abc (starts with number), N1 + n2 (contains symbol).
6. Constants
Definition: Values that do not change during program execution.
Types:
Numeric Constants: Integer, Real constants.
Character Constants: Single and String constants.
6.1 Numeric Constants:
Integer Constant: Whole numbers without decimals. Can be decimal, octal (preceded by 0), or hexadecimal (preceded by 0x).
Real Constant: Decimal numbers, also known as floating-point constants, can be expressed in exponential form.
6.2 Character Constants:
Single Character Constant: Enclosed within single quotes, has corresponding ASCII values.
String Constant: Group of characters enclosed within double quotes, ends with NULL character ( ).
7. Operators in C
Definition: Symbols that tell the compiler to perform specific operations.
Types of Operators:
Arithmetic Operators: +, -, *, /, %
Relational Operators: <, >, <=, >=, ==, !=
Logical Operators: NOT (!), AND (&&), OR (||)
Assignment Operators: =, +=, -=, etc.
Bitwise Operators: &, |, ^, ~, <<, >>
Unary Operators: ++, -- (increment/decrement)
Conditional Operator: ?:
Special Operators: sizeof, comma operator.
7.1 Arithmetic Operators Example:
Examples:
Addition:
Syntax: a = b + c
Subtraction:
Syntax: a = b - c
Multiplication:
Syntax: a = b * c
8. Expressions
Definition: Combination of operands and operators.
Precedence and Associativity of operators determines the order of evaluation.
9. Data Types
Definition: Defines types of data stored in a variable.
Types:
Primary: int, float, double, char, void.
Derived: arrays, structures.
User-defined: enum, typedef.
10. Variables
Definition: Named memory location whose value can change.
Declaration:
Syntax: datatype variable_list;
Initialization:
Syntax: datatype variable_name = value;
11. Type Conversion
Definition: Converting one data type to another.
Types:
Automatic (Implicit) Conversion
Manual (Explicit) Conversion
12. Structure of a C Program
Components:
Documentation Section: Comments about the program.
Link Section: Links library functions.
Definition Section: Defines constants.
Global Declaration Section: Declares global variables.
Main Section: Contains executable code.
Subprogram Section: Additional user-defined functions.
13. Example Programs
Example 1: Area and Perimeter of Rectangle:
#include <stdio.h> void main() { int l, b, area, peri; printf("Enter length and breadth\n"); scanf("%d%d", &l, &b); area = l * b; peri = 2 * (l + b); printf("The area is %d\n", area); printf("The perimeter is %d\n", peri); }Example 2: Simple Interest:
#include <stdio.h> void main() { float p, t, r, si; printf("Enter principle, time and rate\n"); scanf("%f%f%f", &p, &t, &r); si = p * t * r / 100; printf("The Simple Interest is %f", si); }
14. Managing Input and Output Operations
Reading/Writing Data: Using functions such as
scanfandprintffor data input and output.Character Functions: Using
getcharfor single character input and output withputchar.
15. Decision Making and Branching
Decision Making: Using
if,switch,gotostatements for control flow.Example of if statement:
if (condition) { // code block if condition is true } else { // code block if condition is false }Example of switch statement:
switch (variable) { case x: // code block break; default: // default code block }
16. Loops
Definition: Control structures for executing statements repeatedly based on a condition.
Types of Loops:
while loop: Pre-test loop.
do...while loop: Post-test loop.
for loop: Entry-controlled loop.
17. Arrays
Definition: Collection of homogeneous elements accessed using subscript/index.
Types:
One-Dimensional Arrays
Two-Dimensional Arrays
Multi-Dimensional Arrays
18. Strings
Definition: Group of characters enclosed in double quotes, ending with NULL character.
19. User Defined Functions
Definition: Custom functions created by programmers to perform specific tasks.
Example:
void exampleFunction() { // function body }
20. Structures
Definition: Collection of heterogeneous elements of different data types.
Syntax:
struct structureName { datatype member1; datatype member2; ... };Array of Structures: Allow storage of multiple records.
21. Pointers
Definition: Variable that stores the address of another variable.
Usage: For dynamic memory management and efficient array handling.
22. Conclusion
C programming provides foundational constructs for structured programming, data management, and memory handling essential for building efficient software applications.