Home / Notes / Class 11 / Computer Science / Programming in C
Structure of a C program, data types, variables and constants, operators, input and output with printf and scanf, if and switch statements, loops, and simple programs with common compile errors.
C is a high-level, general-purpose language developed by Dennis Ritchie in the early 1970s. It is fast, close to the hardware and is the base of many other languages. A program written in C must be compiled into machine code before it runs.
Structure of a simple C program:
A first program: #include <stdio.h> then int main() { printf("Hello\n"); return 0; }. The \n is a newline escape sequence. Other common ones are \t for a tab and \\ for a backslash.
Basic data types:
A variable is a named memory location and must be declared before use, for example int age; or float price = 99.5;. Names may have letters, digits and underscore, must not start with a digit, cannot be a keyword such as int or while, and are case sensitive. A constant does not change and is made with const or #define PI 3.14.
Operators in C:
Remember that = assigns a value while == compares two values. Dividing two integers gives an integer, so 7 / 2 is 3, not 3.5. To get 3.5 use 7 / 2.0. Precedence puts * / % before + −, and brackets override it.
Input uses scanf and output uses printf. For example scanf("%d", &n); reads an integer into n. The & gives the address of the variable and must be used for numbers and characters. printf("Sum = %d", s); prints text with the value of s.
The if statement runs code only when a condition is true. if-else gives two paths and else if gives more. The switch statement picks among fixed values of an int or char, with case labels, break to leave the switch, and default for no match.
Loops repeat code. for (i = 1; i <= 5; i++) has initialisation, condition and update in one line. while (condition) tests before each pass. do { } while (condition); runs once first and tests after. break leaves a loop and continue skips to the next pass.
Example, sum of 1 to n: int n, i, s = 0; scanf("%d", &n); for (i = 1; i <= n; i++) s = s + i; printf("%d", s); For n = 5 this prints 15.
Errors are of three kinds. Syntax errors break the language rules, such as a missing semicolon, and the compiler reports them. Runtime errors happen while running, such as division by zero. Logical errors let the program run but give wrong output, for example using + instead of *.
Study tip: Write programs on paper neatly with correct braces and semicolons, and always include the header, main and return 0. Trace output questions step by step with a table of variable values. Learn format codes: %d, %f, %c.
Say the meaning first, then open the card to check.
A program that translates source code into machine code.
A named memory location that stores a value.
The kind of value a variable can hold, such as int or char.
A symbol that performs an operation on values.
A backslash code such as \n that stands for a special character.
A statement that repeats a block of code.
A mistake against the rules of the language.
A reserved word with a fixed meaning in C.
A file such as stdio.h that has ready declarations for library functions.
| Name | Formula | What it means |
|---|---|---|
| Area of circle | area = 3.14159 * r * r; | C statement with float variables |
| Average of three | avg = (a + b + c) / 3.0; | The .0 avoids integer division |
| Even test | n % 2 == 0 | True when n is even |
| Swap using temp | t = a; a = b; b = t; | Exchanges two values |
These are the kind of questions asked for 2, 3 and 5 marks. Try each one before opening the answer.
It is a preprocessor directive that adds the standard input and output header file to the program. This file declares functions such as printf and scanf, which are needed for output and input.
The = operator assigns the value on its right to the variable on its left, as in x = 5. The == operator compares two values and gives 1 if they are equal, otherwise 0, as in x == 5. Using = inside an if condition by mistake is a common error.
A while loop tests its condition before running the body, so the body may never run. A do-while loop runs the body first and tests afterwards, so it always runs at least once. The do-while ends with a semicolon.
int stores whole numbers such as 25. float stores decimal numbers such as 3.5. double stores decimals with higher accuracy such as 3.14159265. char stores one character such as 'Z'. Each takes a fixed amount of memory.
#include <stdio.h> int main() { int n; scanf("%d", &n); if (n > 0) printf("Positive"); else if (n < 0) printf("Negative"); else printf("Zero"); return 0; } The program reads n, then an if-else-if ladder tests the three cases in turn and prints the matching message.
A syntax error breaks the rules of C, for example a missing semicolon, and the compiler stops. A runtime error appears while the program runs, for example dividing by zero. A logical error does not stop the program, but the result is wrong because the logic is wrong, such as using the wrong formula.
Answer: 3 1
Answer: 4 times, with i = 1, 4, 7, 10
Answer: x = 15