C_Programming__2_
C_Programming__2_
Lecture 2: Primitive
Data Types & Expressions
12 00001100
-13 11110011
-1 11111111
1 / 71
Outline
2 Data types
4 Variable Input/Output
C Programming 2 / 71
Everything is binary code in computer (1)
C Programming 3 / 71
Everything is binary code in computer (2)
C Programming 4 / 71
Decimal to Binary, Octal and Hexadecimal (1)
1 49 1 49 1 49
0 24 6 3
0 12 10 → A
11 → B
0 6 12 → C
13 → D
1 3 14 → E
15 → F
1
C Programming 5 / 71
Decimal to Binary, Octal and Hexadecimal (2)
C Programming 6 / 71
Decimal to Binary, Octal and Hexadecimal (2)
C Programming 7 / 71
Decimal to Binary, Octal and Hexadecimal (3)
0 60 4 60 C 60
0 30 7 3
1 15
10 → A
1 7 11 → B
12 → C
1 3 13 → D
14 → E
1 15 → F
C Programming 8 / 71
Decimal to Binary, Octal and Hexadecimal (4)
0 0.3137
1 0.6274
0 0.2548
1 0.5096
0 0.0192
0 0.0384
C Programming 9 / 71
Binary, Octal and Hexadecimal to Decimal
1 × 25 + 1 × 24 + 1 × 23 + 1 × 22 + 0 × 21 + 0 × 20 = 60
7 × 81 + 4 × 80 = 60
3 × 161 + 12 × 160 = 60
C Programming 10 / 71
Data in the memory (1)
Data in the memory is kept in binary form
Given an integer 49, its binary code is 110001(2)
It is kept in following form
0 1 1 0 0 0 1
1 0 1 1 0 0 0 1
C Programming 11 / 71
Data in the memory (2)
0000 1 0 1 1 0 0 0 1
0001 0 0 1 1 0 0 1 1
0002 0 1 0 1 1 1 0 1
0003 . .
C Programming 12 / 71
Data in the memory (3)
00 0 0 0 0 0 0 0 0
01 0 0 0 0 0 0 0 1
02 0 0 0 0 0 0 1 0
FF 1 1 1 1 1 1 1 1
C Programming 13 / 71
Data in the memory (4)
Now, think about how many different numbers we have if one bit is
reserved for sign
????
C Programming 14 / 71
Data in the memory (5)
Now, think about how many different numbers we have if one bit is
reserved for sign
2×27 − 1 = 255
Only 127 positive numbers (1 ∼ 127)
127 negatives (-1 ∼ -127)
Some numbers can only be approximately represented by binary code
For example, 3.3137
11.0101(2)
C Programming 15 / 71
One’s complement and Two’s Complement
C Programming 16 / 71
Outline
2 Data types
4 Variable Input/Output
C Programming 17 / 71
Data Types Supported in C
int
char float
Primitive
double
enum
Pointers
E.g., float *p
C Programming 18 / 71
Integer numbers
5 u n s i g n e d s h o r t b1 ; /∗ Range : 0 t o 6 5 , 5 3 5 ∗/
C Programming 19 / 71
Integer numbers
Keywords: int, short, long
Can be signed (default) or unsigned
Actual size of int, short, long depends on architecture
short
int
long
5 u n s i g n e d s h o r t b1 ; /∗ Range : 0 t o 6 5 , 5 3 5 ∗/
C Programming 20 / 71
Integer numbers
i n t main ( )
{
short a = 0 x8000 ;
short b = 0 x7FFF ;
short c = 0xFFFE ;
char d = 0 x80 ;
p r i n t f ( ”a = %d , b = %d , c = %d\n” , a , b , c ) ;
p r i n t f ( ”d = %d\n” , d ) ;
return 0;
}
C Programming 21 / 71
The Problem of Overflow (1)
C Programming 22 / 71
The Problem of Overflow (2)
Given following code, anything wrong??
i n t main ( )
{
unsigned short b = 65537;
return 0;
}
C Programming 23 / 71
The Problem of Overflow (3)
C Programming 24 / 71
Floating point numbers (1)
Keywords: float, double, long double
Real numbers: x ∈ R
Due to limited memory, only 4 bytes/8 bytes are used for float/double
So it will not cover the whole range of R
sign
[3.14159]
0 0000100 110010010000111111001110
ˆ ˆ ˆ
| | |
| | +−−− s i g n i f i c a n d = 0 . 7 8 5 3 9 7 5
| |
| +−−−−−−−−−−−−−−−−−−− e x p o n e n t = 4
|
+−−−−−−−−−−−−−−−−−−−−−−−−− s i g n = 0 ( p o s i t i v e )
C Programming 25 / 71
Floating point numbers (2)
C Programming 26 / 71
Characters (1)
Keyword: char
Can be signed (default) or unsigned
Size: 1 Byte (8 bits) on almost every architecture
Intended to represent a single character
Stores its ASCII number (e.g. ’A’ ⇒ 65)
You can define a char either by its ASCII number or by its symbol:
char a = 65;
c h a r b = ’A ’ ; /∗ u s e s i n g l e q u o t a t i o n marks ∗/
C Programming 27 / 71
Characters (2)
C Programming 28 / 71
Characters (3)
There are some frequently used ones you should know
[Code]
[Output]
#i n c l u d e < s t d i o . h>
i n t main ( )
{
p r i n t f ( ”A : %d %c \n” , ’A ’ , ’A ’ ) ;
A: 65 A
p r i n t f ( ” 1 : %d %c \n” , ’1 ’ , ’1 ’ ) ;
1: 49 1
p r i n t f ( ”B : %d %c \n” , 66 , 66) ;
B: 66 B
p r i n t f ( ” 2 : %d %c \n” , 50 , 50) ;
2: 50 2
}
C Programming 29 / 71
Data type and its size
char
short
int
long
float
double
1 2 3 4 8
C Programming 30 / 71
Outline
2 Data types
4 Variable Input/Output
C Programming 31 / 71
Variable: valid identifiers (1)
Consist of English letters (a-z, A-Z), numbers (0-9) and underscore ( )
Start with a letter (a-z, A-Z) or underscore ( )
Are case sensitive (number differs from Number)
Must not be reserved words (e.g int, return)
Check which are valid identifiers
distance
milesPerHour
x-ray
2ndGrade
$amount
2nd
two&four
hi
return
C Programming 32 / 71
Variable: valid identifiers (1)
Consist of English letters (a-z, A-Z), numbers (0-9) and underscore ( )
Start with a letter (a-z, A-Z) or underscore ( )
Are case sensitive (number differs from Number)
Must not be reserved words (e.g int, return)
Check which are valid identifiers
√
distance
√
milesPerHour
x-ray ×
2ndGrade ×
$amount ×
√
2nd
two&four ×
√
hi
return ×
C Programming 33 / 71
Variable: valid identifiers (2)
Recommended style
Stay in one language (English recommended)
Decide whether to use camelCaseIdentifiers or snake case identifiers
When nesting blocks, indent every inner block by one additional tab!
1 #i n c l u d e < s t d i o . h>
2 i n t main ( )
3 {
4 f l o a t width = 3.0 , height = 5.0 , area = 0 . 0 ;
5 area = width ∗ height ;
6 p r i n t f ( ” Area i s : %f \n” , a r e a ) ;
7 return 0;
8 }
C Programming 34 / 71
Speaking identifiers
1 /∗ c a l c u l a t e volume o f s q u a r e p y r a m i d ∗/
2 int a , b, c ;
3 a = 3;
4 b = 2;
5 c = (1 / 3) ∗ a ∗ a ∗ b ;
⇓
1 /∗ c a l c u l a t e volume o f s q u a r e p y r a m i d ∗/
2 i n t l e n g t h , h e i g h t , volume ;
3 length = 3;
4 height = 2;
5 volume = ( 1 / 3 ) ∗ l e n g t h ∗ l e n g t h ∗ h e i g h t ;
C Programming 35 / 71
Constants
Put key word ‘const’ before and type of variable definition
The variable(s) become(s) constant(s)
Constant means that you are not allowed to change the value after
the definition
const int a = 5 , b = 6;
const f l o a t c = 2.1;
1 #i n c l u d e < s t d i o . h>
2 i n t main ( )
3 {
4 c o n s t f l o a t PI = 3 . 1 4 1 5 9 ;
5 f l o a t r = 3.0 , area = 0.0;
6 PI = 3 . 1 4 ; /∗ I n v a l i d ∗/
7 a r e a = PI ∗ r ∗ r ; /∗ ’ a r e a ’ h a s been u p d a t e d h e r e ∗/
8 }
C Programming 36 / 71
Variables and Constants
C Programming 37 / 71
Outline
2 Data types
4 Variable Input/Output
C Programming 38 / 71
printf() with placeholders (1)
printf(”%d ...%f ...%ld”, d1, d2, d3)
A function pre-defined by C
It is in charge of print things onto screen
You should organize your things in special format
[Codes]
#i n c l u d e < s t d i o . h>
i n t main ( )
{ [Output]
int a = 1;
a: 1
float b = 3.1;
b: 3.1
char c = ’h ’ ;
c: h
p r i n t f ( ” a : %d\n” , a) ;
a: 1, c: h
p r i n t f ( ”b : %f \n” , b) ;
p r i n t f ( ” c : %c \n” , c) ;
p r i n t f ( ” a : %d , c : %c \n” , a , c ) ;
}
C Programming 39 / 71
printf() with placeholders (2)
C Programming 40 / 71
Supported placeholders
C Programming 41 / 71
printf() by example
C Programming 42 / 71
printf() by example
C Programming 43 / 71
Escape Character in ASCII (1)
C Programming 44 / 71
Escape Character in ASCII (2)
C Programming 45 / 71
Variable input
scanf(”%d...%f”, &a, &b) is another useful function
Like printf(), it is declared in stdio.h
Like printf(), it has a format string with placeholders
You can use it to read values of primitive datatypes from the
command line
Example:
int i ;
s c a n f ( ”%d” , & i ) ;
C Programming 47 / 71
scanf() by example
C Programming 48 / 71
Outline
2 Data types
4 Variable Input/Output
C Programming 49 / 71
Overview about Expressions
C Programming 50 / 71
Vadlid Operators in C
Operators
Arithmetic: +,-,*, /, %
Relational: ==, !=, >,<, <=, >=
Logical: &&, !, ||
Bitwise: &, —, ˆ, ˜
Shift: <<, >>
C Programming 51 / 71
Arithmetic Operators in C
C Programming 52 / 71
Precedence Example
(2 + 3 + 5)/3
5 ∗ ((2 + 6) (1)
i n t avg = ( 2 + 3 + 5 )
i n t avg = 2 + 3 + 5 / 3 ;
/3;
f l o a t x=5∗2+6%2;
f l o a t x =5∗((2+6)%2) ;
Try to use “()” to clarify, if you are uncertain about the precedence
C Programming 53 / 71
Division Operator (1)
Generates a result that is the same data type of the largest operand
used in the operation
Dividing two integers yields an integer result
[Result]
5/2 2
17/5 3
C Programming 54 / 71
Division Operator (2)
Generates a result that is the same data type of the largest operand
used in the operation
Dividing two integers yields an integer result
[Result]
5.0/2 2.5
17.0/5 3.4
C Programming 55 / 71
Modulus Operator %
C Programming 56 / 71
Evaluating Arithmetic Expressions (1)
11/2
11%2
11/2.0 [Result]
5.0/2
C Programming 57 / 71
Evaluating Arithmetic Expressions (2)
C Programming 58 / 71
Arithmetic Expressions (1)
[Arithmetic Expression]
[Expression in C]
a a /b
b 2∗ x
2x ( x −7)/(2+3∗ y )
x −7
2 + 3y
C Programming 59 / 71
Arithmetic Expressions (2)
[Arithmetic Expression]
2 ∗ ( −3)
4 ∗ 5 − 15
4 + 2 ∗ 5
7/2
7 / 2.0
2 / 5
2.0 / 5.0
2 / 5 ∗ 5
2.0 + 1.0 + 5 / 2
5 % 2
4 ∗ 5/2 + 5 % 2
C Programming 60 / 71
Arithmetic Expressions (3)
C Programming 61 / 71
Data Assignment
i n t main ( )
{ [Output]
int a;
a = 2.99; a = 2
p r i n t f ( ” a = %d” , a ) ;
}
C Programming 62 / 71
Shortcut assignment Operators (1)
Assignment Shortcut
d=d-4 d-=4
e = e*5 e *= 5
f = f/3 f /= 3
g = g%9 g %=9
m = m*(5 + 3) m *= 5+3
k = k/(5 + 1) m /= 5+1
k = k/(5*7) k /= 5*7
C Programming 63 / 71
Shortcut assignment Operators (2)
a += 4; /∗ a = a + 4 ; ∗/
a −= 4; /∗ a = a − 4 ; ∗/
a ∗= b; /∗ a = a ∗ b ; ∗/
b /= 4+2; /∗ b = b / (4+2) ; ∗/
b %= 2+3; /∗ b = b % (2+3) ; ∗/
C Programming 64 / 71
Shorthand Operators (1)
Incremental operator: ++
i++ equivalent to i = i+1
Decremental operator: –
i– equivalent to i = i-1
When they are used alone
i++ and ++i behave the same as
i = i+1
Similar comment applies to –
C Programming 65 / 71
Shorthand Operators (2)
i n t main ( )
i n t main ( ) {
{ int a , i = 4;
int a , b , i = 4; a = i;
a = i ++; i = i + 1;
b = ++i ; i = i + 1;
} b = i;
}
C Programming 66 / 71
Shorthand Operators (3)
i n t main ( )
{
int a , b , i = 4; [Output]
a = i −−; a = ?, b = ?
b = −− i ;
p r i n t f ( ” a = %d , b = %d\n” , a , b ) ;
}
C Programming 67 / 71
Conditional Operator
i n t main ( )
{
int a = 2 , b = 3 , i = 4; [Output]
a = b>i ? b : i ; a = 4, b = 2
b = b ==3?2:1;
p r i n t f ( ” a = %d , b = %d\n” , a , b ) ;
}
C Programming 68 / 71
Outline
2 Data types
4 Variable Input/Output
C Programming 69 / 71
Implicit Data Type Casting
a = c + 6 + 3.1 + 1.7e5
char int float double
char c = ’ x ’ ; int
d o u b l e a = c + 5 + 1 . 3 + 1 . 7 3 e4 ; float
double
double
Above type castings are done automatically (implicitly)
Code below is risky, rear part will be truncated
int a = 0;
a = 5.1;
C Programming 70 / 71
Explicit (forceful) Data Type Casting
double
int a = 0; int a = 0;
float b = 5.4; float b = 5.4;
a = ( int )b ; a = ( i n t ) round ( b ) ;
C Programming 71 / 71