🏠 Homepage | Next Chapter → |
What is 'C'?
'C' is a language we use to talk to computers.
It was made by a smart man named Dennis Ritchie in 1972.
We use it to make:
Computer brains (like Windows not the house windows.)
Smart machines (like washing machines, microwaves)
-
Fun games (like Mario or car racing games)
In English:
A B C... → Make Words → Form Sentences → Write Stories
In C:
A, 1, +, = → Make Variables → Write Instructions → Build Programs
Building Blocks of C
C Character Set:
-
Letters: A-Z, a-z
-
Numbers: 0–9
-
Symbols: +, -, =, ;, { }
-
Space, Tab, Enter
Letters: A-Z, a-z
Numbers: 0–9
Symbols: +, -, =, ;, { }
Space, Tab, Enter
Constants (Fixed Values)
Bonus types (used later): arrays, pointers, etc.
What is a Variable?
A variable is like a box
You can store values and change them later!
Types:
-
int
→ whole numbers (10, -5) -
float
→ decimal numbers (3.14) -
char
→ single letters ('A')
Naming Variables (Rules)
Good: marks, total_1, _score
Bad: 1mark, my-name, rate$
What are Keywords?
Examples: int
, if
, for
, return
You can’t use them as your own variable names!
First Program in C
/* Simple Interest Calculator */
{
int p, n;
float r, si;
p = 1000;
t = 3;
r = 8.5;
si = p * t * r / 100;
printf("%f", si);
}
{
int p, n;
float r, si;
p = 1000;
t = 3;
r = 8.5;
si = p * t * r / 100;
printf("%f", si);
}
What’s Happening?
-
int
, float
– We make boxes (variables)
-
/* */
– Comments for humans
-
si = p * t * r / 100;
– Math formula
-
printf()
– Show the answer on screen!
int
, float
– We make boxes (variables)
/* */
– Comments for humans
si = p * t * r / 100;
– Math formula
printf()
– Show the answer on screen!
printf() Examples
printf("%d", 5); // Show number
printf("%f", 5.5); // Show decimal
printf("%c", 'A'); // Show letter
printf("%d", 5); // Show number
printf("%f", 5.5); // Show decimal
printf("%c", 'A'); // Show letter
\n = Go to new line
Example: printf("Hi\nBye");
Output:
Hi
Bye
Expressions
Expressions = Math or logic stuff
Examples:
-
3
-
a + b
-
x * y / 100
You can also print them:
printf("%d", 5 + 3);
→ 8
No comments:
Post a Comment