Unit I: Fundamentals of C Programming
1. Introduction to C Programming
The C programming language is a foundational tool in computer science. This unit introduces the basic building blocks required to write and implement simple C programs.
2. Data Types and Expressions
Data types define the type of data a variable can hold.
- Basic Data Types: Include int (integers), float (floating-point numbers), char (characters), and double (double-precision floating point).
- Expressions: A combination of operators, constants, and variables that are evaluated to produce a value.
- Operations: Arithmetic (e.g., +, -), Relational (e.g., >, <), and Logical (e.g., &&, ||) operations form the basis of calculation.
3. Input and Output Operations
To interact with a program, we use specific functions for data input and output.
- printf(): Used to display formatted output to the console.
- scanf(): Used to accept formatted input from the user.
Utility: Input/Output operations allow the program to process dynamic data provided by the user during execution.
4. Control Structures
Control structures manage the flow of execution within a program.
Selection Statements
- IF-ELSE: Executes a block of code if a condition is true; otherwise, it executes another block.
- SWITCH: A multi-way branch statement that selects one of many code blocks to be executed based on a variable's value.
Iterative Statements (Loops)
- WHILE: Repeats a block of code as long as a condition is true.
- DO-WHILE: Similar to while, but the block is executed at least once before the condition is checked.
- FOR: Used for a fixed number of iterations, often using a counter.
Jump Statements
- BREAK: Terminates the current loop or switch statement immediately.
- CONTINUE: Skips the remaining code in the current iteration and moves to the next loop iteration.
- GOTO: Transfers control to a labeled statement elsewhere in the function.
5. Problem Solving in Mathematics and Statistics
C is widely used for solving elementary programming problems in various application areas.
- Mathematics: Calculating factorials, finding prime numbers, or solving quadratic equations using control structures.
- Statistics: Calculating mean, variance, or processing frequency distributions through arrays and loops.
Exam Tips
- Syntax: Always remember that C is case-sensitive. "Int" is not the same as "int".
- Semicolons: A very common mistake is forgetting the semicolon (;) at the end of statements.
- Loops: Ensure every loop has a clear exit condition to avoid "Infinite Loops".
Mnemonic: WDF
To remember the three main types of loops in C: While, Do-While, For.
Frequently Asked Questions
Q1: What is the difference between WHILE and DO-WHILE?
The while loop checks the condition first and may never run. The do-while loop runs at least once because it checks the condition at the end.
Q2: Why is the GOTO statement generally discouraged?
It makes programs harder to read, debug, and maintain by creating "spaghetti code" that jumps unpredictably.