Lecture 1 - Fundamentals of C
Lecture 1 - Fundamentals of C
int main()
return 0;
2
Printing strings
3
Printing strings
printf(“To C or not to C, that is the question.”);
● Will print anything enclosed between double quotation marks (“ ”)
● Things to be careful about:
○ Don’t forget the closing brackets
○ Don’t forget the semicolon (;)
4
Pop Quiz
5
Printing strings
6
Escape sequences
Sometimes, it is necessary to use characters that cannot be typed or has some special
meaning in C programming. To use those, escape sequences are used.
\t Horizontal \” Double
tab quotation
\v Vertical \0 Null
tab
\\ Backslash \b backspace
7
Comments
8
Comments
Multiline Comments (C89 Style)
/* int main()
{
This is an example of a printf(“Hello!”); /*Comment*/
}
multiline comment.
*/
9
Comments
Single line Comments (C99 Style)
● Begins with //
● Ends when the line ends
● No need for a closing symbol
10
Comments
Write comments that are helpful. Do not write comments that state the obvious
11
Variables
12
Just a fancy name for
containers
13
Variables
int, float, char, double, short int, unsigned int, long int
And many more. But for now, we are concerned with: int, float
int float
15
Declaring variables
All variables must be declared before usage
datatype identifier;
Examples:
int count;
float salary;
16
Assigning variables
Variables can be given a value to store through assignment
salary = 12.4f;
17
Best practices
sum = 0.0;
angle = 45.0;
18
Pop Quiz
19
Printing strings
What is stored in the variable?
height = 8;
int height;
21
Printing variables
22
Printing variables
printf(“%d”, total);
23
Printing variables
printf(“%.3f”, volume)
24
Taking input
25
Taking input
26
Identifiers
27
Rules of Identifier Specification
Identifiers are just the names of the variables
● May contain letters, digits, and underscores
● Must begin with a letter or underscore
● C identifiers are Case Sensitive. Age, age, AGE, agE are
all different identifiers
● C places no limit on the maximum length of an identifier
28
Pop Quiz
29
Rules of Identifier Specification
Are these identifiers legal?
30
Keywords
● Keywords are words that have special meaning to the C compiler
● They can’t be used as identifiers
● Some keywords are
31
Reading Tasks
● C Programming - A Modern Approach | Section 2.1 ~ 2.7
● Teach yourself C | Section 1.1, 1.3 ~ 1.4
32
Thank You
33