01b. C Language Basics
01b. C Language Basics
Outline
1. First C program & Language Syntax
2. Variables and Assignment Operators
3. Identifiers, Reserved Words, Predefined
Identifiers
4. Data Types, Numeric Constants, String Literals
5. Console Input/Output Using scanf() and
printf()
2
1. Overview of C Language
1 #include <stdio.h> Basic structure of a C
2 program:
3 int main(void) Standard
4 { libraries
#include <stdio.h>
to load
5 printf("Hello, world!\n");
6 printf("Hello, universe!\n"); int main(void)
Entrance
{
7 point of C
The start statement_1;
program
8 return 0; of block statement_2;
9 } …
10 The end
return 0; This program
11 of block } is done!
12
Statements are executed
Hello, world!
sequentially.
Hello, universe!
1.1. Language Syntax
4
1 #include <stdio.h>
2 Can you spot
3 int main(void) { the error(s)?
4 Printf("Hello, world!\n");
5 Printf("Hello, universe!\n");
6 return 0;
7 }
8
9
5
1.2. Comments
1 /*
2 This is a comment
3 */
4 #include <stdio.h>
5
6 int main(void) {
7 printf("Hello, world!\n"); // This is also a
8 comment
9 return 0;
}
Comments are used for documentation aid; they are ignored by
compiler.
Two commenting styles
Text starting with /* and ends at the next */
Can span multiple lines
Text begins with // and extend to the end of the line
6
1.3. The printf() function
• You may notice that the most interesting
statement in our very generic example is
printf("Hello, world!\n");
printf(Hello, world!\n);
8
1.4. String Constant
• You are probably thinking: What if I want to print a
double quotation mark? What if I want to print the
"Enter" key for a new line of text?
9
1.4. String Constant
• e.g.: This string constant
"\\ is \"backslash\" and /
is \"slash\""
11
2. Variables
• Variables are used to store data in a program.
12
1 #include <stdio.h>
2 Declaring three variables:
3 int main(void) side, perimeter, and
4 {
5 int side, perimeter, area; area.
6
7 side = 3; The type of the three variables
8 perimeter = 4 * side; is int, indicating that the
9 area = side * side; variables are for storing
10
11 integers.
printf("Side : %d\n", side);
12 printf("Perimeter : %d\n", perimeter);
13 printf("Area : %d\n", area);
14
15 return 0;
16 }
Side : 3
Perimeter : 12
Area : 9
Syntax
type1 var1;
Declaring a single variable of type type1
14
1 #include <stdio.h>
2 When the program runs, the
3 int main(void) statements are executed
4 { sequentially one by one.
5 int side, perimeter, area;
6
7 side = 3;
8 perimeter = 4 * side;
9 area = side * side;
10
11 printf("Side : %d\n", side);
12 printf("Perimeter : %d\n", perimeter);
13 printf("Area : %d\n", area);
14
15 return 0;
16 }
15
1 #include <stdio.h>
2 Immediately after the variables
3 int main(void) are being declared,
4 { their values are undefined (we
5 int side, perimeter, area;
do not know what their values
6
7 side = 3; are).
8 perimeter = 4 * side;
9 area = side * side; We say that these variables are
10 uninitialized.
11 printf("Side : %d\n", side);
12 printf("Perimeter : %d\n", perimeter);
13 printf("Area : %d\n", area);
14
15 return 0;
16 }
• "Side : %d\n"
– Text to print, called the format string
– The format specifier %d specifies that the value of the
corresponding expression is to be printed in the format of a
decimal integer.
• side
– The expression whose value is to be supplied to the format string.
• Compare to:
printf("Hello world!\n");
21
1 #include <stdio.h>
2
3 int main(void)
4 {
5 int side, perimeter, area;
6
7 side = 3; printf() outputs the given
8 perimeter = 4 * side; text (string) but with the %d
9 area = side * side; replaced by the value of side.
10
11 printf("Side : %d\n", side);
12 printf("Perimeter : %d\n", perimeter);
13 printf("Area : %d\n", area);
14
15 return 0;
16 }
Side : 3
22
3. Naming Variables
Key concepts
• How to name variables?
23
3.1. Identifier
• An identifier is a name used to identify variables,
functions, etc. in a program.
24
3.2. Reserved Words
• Reserved words or keywords are names that have
special meaning in the C language. You will eventually
learn all about them later.
auto do goto signed unsigne
d
break double if sizeof void
case else int static volatil
e
char enum long struct while
const extern registe switch
r
continu float return typedef
e
3.3. Predefined Identifiers
• There are also Predefined identifiers that have
already been used as names for standard
usages:
– e.g., main, printf, scanf
26
Naming Variables – Challenge
Which of the following are valid identifiers?
1. $abc 11. printf
2. _1_abc_1_ 12. engg1110
3. 1_1 13. engg_1110
4. Domain-name 14. _
5. URL 15. A100xC200
6. int 16. tab
7. main 17. include
8. Int 18. VARIABLE
9. 32bits 19. www_yahoo_com
10. c 20. Hong Kong
27
3.4. Naming Conventions (Guidelines)
• Be meaningful
– Avoid names like: a, b, c, d, a1, a2, a3, xyz
• Be consistent
interest_rate (Use underscore in place of space)
or
interestRate (camelCase – Mixed case starting with lower
case)
28
5. Console Input and Output
Key concepts
• How to read numbers from a user?
29
1 #include <stdio.h>
2
3 int main(void)
4 {
5 int num1;
6
7 printf("Enter an integer:\n");
8 scanf("%d", &num1);
9
10 printf("num1 = %d\n", num1);
11
12 return 0;
13 }
14
Enter an integer:
123
num1 = 123
30
Example 5.1 explained
• scanf("%d", &num1);
– A single %d in the format string tells scanf() to read one
integer.
– Upon success, the input value is stored in num1.
– & before the variable is a must. You will learn about this operator
later in this course.
• Behavior of scanf()
– (Line 8) Execution is paused here while scanf() waits for user
input.
– Program resumes when the user enters a value follows by pressing
the "Enter" key (denoted by the symbol '' in the sample output).
31
1 #include <stdio.h>
2
3 int main(void)
4 {
5 int num1, num2;
6
7 printf("Enter two integers:\n");
8 scanf("%d%d", &num1, &num2);
9
10 printf("num1 = %d, num2 = %d\n", num1, num2);
11
12 return 0;
13 }
14
A) 3.141593
B) 3.14
C) 3.1415927
Example 5.4: Controlling the # of decimal places for floating point numbers.
36
Notes about scanf() and printf()
• scanf() won't work "properly" if it encounters an
invalid input. For example, a user enters an alphabet
when an integer is expected.
– In this course, unless otherwise stated, you can assume the
input values are always valid.
37
Summary
• Syntax: C, as a programming language, has rules that
programmers must obey.
38
Reading Assignment
• C: How to Program, 8th ed, Deitel and Deitel
• Chapter 2 Introduction to C Programming
– Sections 2.1 – 2.7
39