§ — — Computer Programming 1
C is a general-purpose, structured programming language known for efficiency and close-to-hardware control. It uses economy of expression, modern control structures, and a rich set of operations. Often called a "systems programming language" because it is well-suited for writing operating systems and compilers.
When you write a C program and want to run it, it goes through these stages:
Source code (.c file)
↓
Preprocessor ← handles #include, #define directives
↓
Compiler ← converts to object code; catches syntax errors
↓
Object code
↓
Linker ← combines object code with library code
↓
Executable code
An identifier is a name used to reference a variable, function, or other user-defined element.
Rules for naming identifiers in C:
_).Sname, SNAME, and sname are three different identifiers.int, float, if, while).C provides several fundamental (scalar) data types:
| Type | Size | Range | Use |
|---|---|---|---|
short int | 2 bytes | −32,768 to +32,767 | Small integers |
int | 4 bytes | −2,147,483,648 to +2,147,483,647 | General integers |
long int | 4 bytes | Same as int | Large integers |
unsigned int | 2–4 bytes | 0 to 65,535+ | Non-negative integers |
float | 4 bytes | ≈ 3.4E−38 to 3.4E+38 | Decimal numbers |
double | 8 bytes | ≈ 1.7E−308 to 1.7E+308 | Higher-precision decimals |
char | 1 byte | −128 to +127 | Single characters |
void | — | — | No value (functions returning nothing) |
A variable is an identifier that holds a value which can change during program execution.
Declaration syntax:
data_type variable1, variable2;
Examples:
int x, y, z;
char a, b;
double s;
float ave;
char Sname[30];
Variables contain garbage values until explicitly assigned. Three ways:
1. Assignment statement:
x = -1;
ch1 = 'A';
2. Using scanf:
scanf("%d", &x);
3. At declaration:
int x = 3;
char y = 'x';
double a, b = 100.00;
| Type | Example |
|---|---|
| Character constant | 'a', 'P' |
| Integer constant | 10, -100 |
| Floating-point | 11.123 |
| String constant | "hello" |
#define | #define PI 3.14159 |
const modifier | const float version = 3.20; |
| Operator | Operation |
|---|---|
* | Multiplication |
/ | Division |
+ | Addition |
- | Subtraction |
% | Modulus (remainder) — integers only |
++ | Increment (add 1) |
-- | Decrement (subtract 1) |
Important: Integer division truncates: 11/2 = 5. Modulus gives remainder: 11 % 2 = 1.
Prefix vs. postfix increment:
x = 10;
y = ++x; // x becomes 11 first, then y = 11
x = 10;
y = x++; // y = 10 first, then x becomes 11
Shorthand operators:
x += y; // x = x + y
x -= y; // x = x - y
x *= y; // x = x * y
x /= y; // x = x / y
x %= y; // x = x % y
| Operator | Meaning |
|---|---|
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
== | Equal to |
!= | Not equal to |
| Operator | Meaning |
|---|---|
&& | AND |
|| | OR |
! | NOT |
In C, true = any non-zero value, false = 0.
expression1 ? expression2 : expression3;
If expression1 is true, result is expression2; otherwise expression3.
! ++ -- (unary)
* / %
+ -
< <= > >=
== !=
&&
||
? :
= += -= *= /=
ProReviewer — locked
Drills, code labs, and full solutions.
ProReviewer — locked
Drills, code labs, and full solutions.
ProReviewer — locked
Drills, code labs, and full solutions.
Run the code below. Try changing the values and adding more variables.
Done with this module? Track it — your progress shows on the subject list.
Up next
Lesson 3: Input/Output and Program Structure→←Previous: Lesson 1: Programming Concepts