Syllabus topics (RTMNU)
- Design methods
- Programming language
- Translators
- Introduction to C
- C character set and keywords
- Escape sequence
- Constants and variables
- Data types
- Conversion specification
- Input and output statements in C
- Operators and expressions (Arithmetic, Relational, Logical, Assignment, Ternary, Bit Wise and Increment & Decrement Operator)
Design Methods
Definition: A design method is a planned way of solving a problem before writing the program. It breaks the problem into clear steps so that coding becomes easy and errors are fewer.
- Top-down design: start with the main problem and divide it into smaller sub-problems (modules) until each part is simple. Example: a billing program is divided into input, calculation of GST and printing of bill.
- Bottom-up design: first build small, basic modules (like a function to add tax) and then join them to make the full program.
- Modular design: the program is made of independent modules (functions in C). Each module does one job and can be tested separately.
- Structured programming: uses only three control structures – sequence, selection (if/switch) and iteration (loops) – and avoids goto. C supports this style.
Tools of design: (1) Algorithm – step-by-step solution written in simple language; (2) Flowchart – diagram of the algorithm using symbols (oval = start/stop, parallelogram = input/output, rectangle = process, diamond = decision, arrows = flow); (3) Pseudocode – English-like code.
Programming Language and Translators
Programming language: a set of words, symbols and rules used to give instructions to a computer.
- Machine language (low level): only 0s and 1s; directly understood by the CPU; very fast but hard to write.
- Assembly language: uses short codes called mnemonics (ADD, MOV, SUB); needs an assembler.
- High level language: English-like words (C, C++, Java, Python); easy to write and portable; needs a compiler or interpreter.
Translator: a program that converts a program written in assembly or high level language (source code) into machine language (object code).
| Point | Compiler | Interpreter |
|---|---|---|
| Working | Translates the whole program at once | Translates and runs line by line |
| Speed of execution | Fast (object file is created once) | Slow (translation every time) |
| Errors | Shows all errors after compiling | Stops at the first error |
| Object file | Creates an executable file | No separate file |
| Examples | C, C++ | BASIC, Python |
Assembler converts assembly language into machine code. Other helpers: the linker joins object code with library code, and the loader loads the program into memory for running.
Introduction to C
C was developed by Dennis Ritchie at AT&T Bell Laboratories, USA, in 1972. It came from the earlier languages BCPL and B. The UNIX operating system was written mainly in C. It is called a middle level language because it has high level features and can also work close to hardware.
Features of C:
- Simple and structured – program is divided into functions.
- Portable – a C program can run on different machines with little change.
- Fast and efficient – compiled code runs quickly.
- Rich library – many built-in functions (printf, sqrt, strlen).
- Pointers – direct access to memory.
- Extensible – users can add their own functions.
#include <stdio.h> /* header file */
int main() /* execution starts here */
{
printf("Welcome to C\n");
return 0;
}
Basic structure: documentation (comments), link section (#include), definition section (#define), global declarations, main() function, user-defined functions.
C Character Set and Keywords
Character set: the characters allowed in C – letters (A–Z, a–z), digits (0–9), special symbols (+ - * / % = ; , { } ( ) & # etc.) and white spaces (blank, tab, newline).
Keywords: reserved words with a fixed meaning; they cannot be used as variable names. ANSI C has 32 keywords, all in lowercase, e.g. auto, break, case, char, const, continue, default, do, double, else, enum, extern, float, for, goto, if, int, long, register, return, short, signed, sizeof, static, struct, switch, typedef, union, unsigned, void, volatile, while.
Identifiers are names given by the programmer to variables and functions. Rules: start with a letter or underscore, no spaces or special symbols, not a keyword, case-sensitive (total and Total are different).
Escape Sequence
An escape sequence is a backslash (\) followed by a character, used inside strings to represent special (non-printing) characters.
| Escape sequence | Meaning |
|---|---|
| \n | New line |
| \t | Horizontal tab |
| \b | Backspace |
| \r | Carriage return |
| \a | Alert (beep) |
| \\ | Backslash |
| \" | Double quote |
| \' | Single quote |
| \0 | Null character (end of string) |
Constants and Variables
Constant: a fixed value that does not change while the program runs. Types: integer constant (25, -100), real/float constant (3.14), character constant ('A'), string constant ("RTMNU"). Symbolic constants are made with #define PI 3.14 or const float rate = 18.0;
Variable: a named memory location whose value can change during execution. It must be declared before use: int qty; float price = 99.50;
Data Types
A data type tells the compiler what kind of value a variable will store and how much memory it needs.
| Data type | Stores | Typical size* | Format specifier |
|---|---|---|---|
| char | Single character | 1 byte | %c |
| int | Whole number | 4 bytes (2 on old 16-bit compilers) | %d |
| float | Decimal number (6 digit precision) | 4 bytes | %f |
| double | Large decimal number (15 digit precision) | 8 bytes | %lf |
| void | No value | – | – |
*Size depends on the compiler; use sizeof() to check. Modifiers short, long, signed and unsigned change the range. Derived types: arrays, pointers, functions. User-defined: struct, union, enum, typedef.
Conversion Specification and Input/Output Statements
A conversion specification (format specifier) starts with % and tells printf/scanf the type of data: %d int, %f float, %c char, %s string, %lf double, %ld long, %u unsigned, %o octal, %x hexadecimal, %e exponent form. Width and precision can be given: %8.2f prints a float in 8 places with 2 decimals.
- printf() – formatted output: printf("Total = %d", t);
- scanf() – formatted input; the address operator & is used before variable names: scanf("%d", &t);
- getchar() / putchar() – read/write one character.
- gets() / puts() – read/write a string (gets is unsafe; fgets is preferred).
#include <stdio.h>
int main() {
int roll;
float marks;
char grade;
printf("Enter roll, marks, grade: ");
scanf("%d %f %c", &roll, &marks, &grade);
printf("\nRoll: %d\tMarks: %.2f\tGrade: %c\n", roll, marks, grade);
return 0;
}
Operators and Expressions
An operator is a symbol that performs an operation on data (operands). An expression is a combination of operands and operators that gives a value, e.g. a + b * 2.
| Type | Operators | Example (a=10, b=3) |
|---|---|---|
| Arithmetic | + - * / % | a/b = 3 (integer division), a%b = 1 |
| Relational | < <= > >= == != | a>b gives 1 (true) |
| Logical | && (AND), || (OR), ! (NOT) | (a>5 && b>5) gives 0 |
| Assignment | = += -= *= /= %= | a += 5 means a = a + 5 |
| Ternary (conditional) | ? : | big = (a>b) ? a : b; gives 10 |
| Bitwise | & | ^ ~ << >> | a&b = 2, a|b = 11, a^b = 9, a<<1 = 20 |
| Increment / Decrement | ++ -- | x = a++ (x=10, a=11); x = ++a (a first increases) |
Pre vs post increment: in ++a the value is increased first and then used; in a++ the old value is used first and then increased. Precedence: * / % are done before + -; brackets have the highest priority.
Likely exam questions
- (Long, 8/16 marks) What is a translator? Explain compiler, interpreter and assembler. Differentiate between compiler and interpreter.
- (Long, 8/16 marks) What is an operator? Explain arithmetic, relational, logical, ternary, bitwise and increment/decrement operators in C with examples.
- (Short, 4 marks) Explain any four escape sequences used in C.
- (Short, 4 marks) What are data types? List the basic data types of C with their format specifiers.
- (Short, 4 marks) Differentiate between a constant and a variable with examples.