Unit I: Fundamentals of C Programming
Course: Programming with C
Code: CADSM101
Data Types, Expressions, and Operations
C is a statically typed language, meaning every variable must have a declared type that defines the kind of data it can hold.
Standard Data Types
- int: Used for whole numbers (integers).
- float: Used for single-precision floating-point numbers (decimals).
- double: Used for double-precision floating-point numbers.
- char: Used to store a single character.
Operators in C
Operators are symbols used to perform operations on variables and values.
- Arithmetic: +, -, *, /, % (modulus).
- Relational: ==, !=, >, <, >=, <=.
- Logical: && (AND), || (OR), ! (NOT) .
- Assignment: =.
Standard input and output allow a C program to interact with the user.
- printf(): Function used to display data on the screen.
- scanf(): Function used to read data from the keyboard.
Writing Simple C Programs
Every C program begins execution with the main() function.
Example Structure:
#include <stdio.h>
int main() {
printf("Welcome to C!");
return 0;
}
Control Structures: Decision Making
These structures allow the program to branch in different directions based on conditions.
IF-ELSE Statement
Executes one block of code if a condition is true, and another if it is false.
[Image of if-else flowchart in C programming]
SWITCH Statement
Allows a variable to be tested for equality against a list of values (cases).
Control Structures: Looping
Loops are used to repeat a block of code multiple times.
- WHILE Loop: Repeats as long as a condition is true. The condition is checked before execution.
- DO-WHILE Loop: Similar to while, but the condition is checked after execution, ensuring the loop runs at least once.
- FOR Loop: Best used when the number of iterations is known. It combines initialization, condition, and increment/decrement.
Jump Statements
- BREAK: Exits the current loop or switch immediately.
- CONTINUE: Skips the remaining code in the current iteration and jumps to the next iteration.
- GOTO: Transfers control to a labeled part of the program (use sparingly).
Elementary Problem Solving
C is widely used to solve problems in mathematics and statistics through algorithmic logic.
- Finding factorials.
- Evaluating algebraic expressions.
- Generating series like Fibonacci or geometric progressions.
Exam Focus & Tips
- Exam Tip: Be prepared to write code for finding the sum of a series or checking if a number is prime using nested loops.
- Common Mistake: Forgetting the semicolon after the
while condition in a do-while loop.
- Practical Tip: Always initialize your variables to avoid "garbage values" during calculation.
Frequently Asked Questions
Q: What is the difference between = and ==?
A: = is for assignment; == is for checking equality.
Q: When is a DO-WHILE loop preferred over a WHILE loop?
A: When you need the code block to execute at least once regardless of the condition.