0% found this document useful (0 votes)
3 views

C_Programming__2_

Uploaded by

laihoptuan2006
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

C_Programming__2_

Uploaded by

laihoptuan2006
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 71

C Programming

Lecture 2: Primitive
Data Types & Expressions
12 00001100
-13 11110011
-1 11111111

1 / 71
Outline

1 Basics about Data Representation

2 Data types

3 Variables and Constants

4 Variable Input/Output

5 Data Operators and Expressions

6 Implicit and Forceful Data Type Casting

C Programming 2 / 71
Everything is binary code in computer (1)

Everything in computer is in binary form


Data: integers, real numbers and strings
Instructions
Addresses: sequential numbers for the memory cells
It is therefore necessary to know how the binary code is produced
In addition, for convenience
Octal and Hexadecimal numbers are also used for display

C Programming 3 / 71
Everything is binary code in computer (2)

Anyone watched this movie?

C Programming 4 / 71
Decimal to Binary, Octal and Hexadecimal (1)

Decimal to binary Decimal to octal Decimal to hexadecimal

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

Binary code: 110001(2)


Octal code: 61(8)
Hexadecimal code: 31(16)
Can you figure out the relation between them

C Programming 5 / 71
Decimal to Binary, Octal and Hexadecimal (2)

Try it by yourself to convert 60 to


Binary code:
Octal code:
Hexadecimal code:

C Programming 6 / 71
Decimal to Binary, Octal and Hexadecimal (2)

Try it by yourself to convert 60 to


Binary code: 111100(2)
Octal code: 74(8)
Hexadecimal code: 3C(16)

C Programming 7 / 71
Decimal to Binary, Octal and Hexadecimal (3)

Decimal to binary Decimal to octal Decimal to hexadecimal

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)

Decimal fraction to binary

0 0.3137
1 0.6274
0 0.2548
1 0.5096
0 0.0192
0 0.0384

0×2−1 + 1×2−2 + 0×2−3 + 1×2−4 = 0.3125 ≈ 0.3137

C Programming 9 / 71
Binary, Octal and Hexadecimal to Decimal

Binary code: 111100(2)


Octal code: 74(8)
Hexadecimal code: 3C(16)

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

Given an integer -49, its binary code is 1110001(2)


It is kept in following form
1 1 1 0 0 0 1

The highest bit is reserved for sign


This is true for real numbers later we will see
We use 8 bits (1 byte), 2 bytes or more bytes to keep a number

1 0 1 1 0 0 0 1

C Programming 11 / 71
Data in the memory (2)

Data in the memory is kept in binary form


Given we have several numbers to be kept
They are kept one after another (assume we use 1 byte for one
number)

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)

Now, think about an important issue


Given 1 byte, how many different numbers we can represent
Assuming no sign bit

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

With 1 byte, there are 28 = 256 numbers


Since our memory are limited, we can only represent a limited range
of numbers

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

Original bits One’s Complement Two’s Complement


23 00010111 00010111 00010111
-23 10010111 11101000 11101001
33 00100001 00100001 00100001
-33 10100001 11011110 11011111

One’s complement and two’s complement of positive numbers are the


same as original code
For negative number, we do not inverse its sign bit
Why we do so??
It is very convenient when we do substraction
Substraction is converted to add operation
Now please work out one’s complement and two’s complement of -17

C Programming 16 / 71
Outline

1 Basics about Data Representation

2 Data types

3 Variables and Constants

4 Variable Input/Output

5 Data Operators and Expressions

6 Implicit and Forceful Data Type Casting

C Programming 17 / 71
Data Types Supported in C

int
char float
Primitive
double
enum

Array, e.g., int a[3]


Constructive struct
Data type
union

Pointers
E.g., float *p

C Programming 18 / 71
Integer numbers

Keywords: int, short, long


Can be signed (default) or unsigned
Actual size of int, short, long depends on architecture

1 i n t a ; /∗ Range : −2 ,147 ,483 ,648 t o 2 , 1 4 7 , 4 8 3 , 6 4 7 ∗/


2 short b ; /∗ Range : −32 ,768 t o 3 2 , 7 6 7 ∗/
3 l o n g c ; /∗ Range : −2 ,147 ,483 ,648 t o 2 , 1 4 7 , 4 8 3 , 6 4 7 ∗/
4 u n s i g n e d i n t a1 ; /∗ Range : 0 t o 4 , 2 9 4 , 9 6 7 , 2 9 7 ∗/

5 u n s i g n e d s h o r t b1 ; /∗ Range : 0 t o 6 5 , 5 3 5 ∗/

int and long take 4 bytes (32 bits system)


short takes 2 bytes

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

1 i n t a ; /∗ Range : −2 ,147 ,483 ,648 t o 2 , 1 4 7 , 4 8 3 , 6 4 7 ∗/


2 short b ; /∗ Range : −32 ,768 t o 3 2 , 7 6 7 ∗/
3 l o n g c ; /∗ Range : −2 ,147 ,483 ,648 t o 2 , 1 4 7 , 4 8 3 , 6 4 7 ∗/
4 u n s i g n e d i n t a1 ; /∗ Range : 0 t o 4 , 2 9 4 , 9 6 7 , 2 9 7 ∗/

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)

Given following code, anything wrong??


i n t main ( )
{
unsigned short b = 65537;
return 0;
}

C Programming 22 / 71
The Problem of Overflow (2)
Given following code, anything wrong??
i n t main ( )
{
unsigned short b = 65537;
return 0;
}

b will never reach to 65537


In this case, it is 65535
Guess the value of b in following code
i n t main ( )
{
short b = 65537;
return 0;
}

C Programming 23 / 71
The Problem of Overflow (3)

The same problem exists for all primitive data types


Because, we only use limited bytes to represent the data
Be careful when you assign big value to a variable
Tricks: estimate how big it could be

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

float exponent 2nd 3rd 4th

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)

Keywords: float, double, long double


float x = 0.125; /∗ P r e c i s i o n : 7 t o 8
d i g i t s ∗/
double y = 111111.111111; /∗ P r e c i s i o n : 15 t o 16
d i g i t s ∗/

Now you should know a very useful operator sizeof(.)


#i n c l u d e < s t d i o . h>
i n t main ( )
{
float x = 0.125;
double y = 111111.111111;
p r i n t f ( ” f l o a t : %d , d o u b l e : %d” , s i z e o f ( x ) , s i z e o f ( y ) ) ;
}

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)

Essentially, char uses 1 byte to represent 255 characters


Each integer is associated with a character
American Standard Code for Information Interchange (ASCII)

C Programming 28 / 71
Characters (3)
There are some frequently used ones you should know

ASCII value ASCII value


0∼9 48∼57 A∼Z 65∼90
a∼z 97∼122 ⌞⌟ 32
\n 10 \t 9

[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

You should clearly know what is the use of your data


One should not define data in double/long double just for convenience
It wastes a lot of memory
String: an array of chars

C Programming 30 / 71
Outline

1 Basics about Data Representation

2 Data types

3 Variables and Constants

4 Variable Input/Output

5 Data Operators and Expressions

6 Implicit and Forceful Data Type Casting

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

01010100 01010011 01010010 01010001


int a;
a
01010100 01010011 01010010 01010001
int a=3; 0 0 0 00000011
a
01010100 01010011 01010010 01010001
const int a=3; 0 0 0 00000011
a

C Programming 37 / 71
Outline

1 Basics about Data Representation

2 Data types

3 Variables and Constants

4 Variable Input/Output

5 Data Operators and Expressions

6 Implicit and Forceful Data Type Casting

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)

“%x” is called placeholder


It holds/occupies the place that is replaced by output data
Different output data require different placeholders
The order of placeholders corresponds to the order of output
The number of placeholders corresponds to the number of output
[Codes]
#i n c l u d e < s t d i o . h>
i n t main ( ) [Output]
{
a: 3
int a = 3;
b: 5
int b = 5;
c : 7.4
float c = 7.4;
p r i n t f ( ” a : %d\ nb : %d\ nc : %f \n” , a , b , c ) ;
}

C Programming 40 / 71
Supported placeholders

The placeholder determines how the value is interpreted.


type description type of argument
%c single character char, int (if <= 255)
%d decimal number char, int
%u unsigned decimal number unsigned char, unsigned int
%x hexadecimal number char, int
%ld long decimal number long
%f floating point number float, double
%lf double number double

C Programming 41 / 71
printf() by example

printf(”%d ...%f ...%ld”, d1, d2, d3)


A function pre-defined by C
[Codes]
#i n c l u d e < s t d i o . h>
i n t main ( )
{
i n t a = 79;
char b = ’n ’ ; [Output]
p r i n t f ( ” a : %d , b : %d\n” , a , b ) ;
p r i n t f ( ” a : %c , b : %c \n” , a , b ) ;
p r i n t f ( ” a : %x , b : %x \n” , a , b ) ;
}

C Programming 42 / 71
printf() by example

printf(”%d ...%f ...%ld”, d1, d2, d3)


A function pre-defined by C
[Codes]
#i n c l u d e < s t d i o . h>
i n t main ( ) [Output]
{
i n t a = 79; a : 7 9 , b : 110
char b = ’n ’ ; a : O, b : n
p r i n t f ( ” a : %d , b : %d\n” , a , b ) ; a : 4f , b : 6e
p r i n t f ( ” a : %c , b : %c \n” , a , b ) ;
p r i n t f ( ” a : %x , b : %x \n” , a , b ) ;
}

C Programming 43 / 71
Escape Character in ASCII (1)

There are some special character to be print out


“Tab”, “Enter”, “backspace”
We want to express it by one character in ASCII
But....
All characters have their own use
If we want to use them to express different meaning
We use ‘⧹’

C Programming 44 / 71
Escape Character in ASCII (2)

All characters have their own use


If we want to use them to express different meaning
We use ‘\’
ESC their charactor
‘\t’ Tab
‘\b’ back one character
‘\r’ return to the start if a line
‘\n’ go to the next line
‘\\’ \
‘\” single quote: ’
‘\”’ double quote: ”
Remember that it is one character: \”
It is valid: ’\b’

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 ) ;

Notice that there is “&” before the variable


This operator takes the address of the variable
When buy goods online, you should put your the address
The postman will transfer the goods (value) to your mailbox
(variable)
C Programming 46 / 71
Notes for scanf

scanf() uses the same placeholders as printf()


You must type an & before each variable identifier
If you read a number (using %d, %u etc.), interpretation
Starts at first digit
Ends before last non digit character
E.g: 2 2.3
If you use %c, the first character of the user input is taken

C Programming 47 / 71
scanf() by example

scanf(”%d ...%f ...%ld”, &d1, &d2, &d3)


A function pre-defined by C
[Codes]
#i n c l u d e < s t d i o . h>
i n t main ( ) [Output]
{
i n t a = 79; a : 79 , b : 0.1
float b = 0.1; I n p u t a and b : xx xx .
p r i n t f ( ” a : %d , b : %f \n” , a , b ) ; xx
p r i n t f ( ” I n p u t a and b : ” ) ; a : xx , b : xx . xx
s c a n f ( ”%d%f ” , &a , &b ) ;
p r i n t f ( ” a : %d , b : %f \n” , a , b ) ;
}

C Programming 48 / 71
Outline

1 Basics about Data Representation

2 Data types

3 Variables and Constants

4 Variable Input/Output

5 Data Operators and Expressions

6 Implicit and Forceful Data Type Casting

C Programming 49 / 71
Overview about Expressions

Legal expressions consist of legal combinations of


Constants: const float PI = 3.14
Variables: int a, b;
Operators: +,-
Function alls, printf(”%d”, a)

C Programming 50 / 71
Vadlid Operators in C

Operators
Arithmetic: +,-,*, /, %
Relational: ==, !=, >,<, <=, >=
Logical: &&, !, ||
Bitwise: &, —, ˆ, ˜
Shift: <<, >>

C Programming 51 / 71
Arithmetic Operators in C

Rules for operator precedence

Operator Operation Precedence


() Parenthese Evaluated first
*,/ or % multiplication, division evluated second
+ or - addition, substraction evaluated last

Take average of three numbers


1+2+4/3 ??

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 %

Modulus Operator % returns the remainder


Dividing two integers yields an integer result
[Result]
5%2 1
17%5 2
12%3 0

C Programming 56 / 71
Evaluating Arithmetic Expressions (1)

See whether you can work out the answer

11/2
11%2
11/2.0 [Result]
5.0/2

C Programming 57 / 71
Evaluating Arithmetic Expressions (2)

Check your answer


[Result]
11/2 5
11%2 1
11/2.0 5.5
5.0/2 2.5

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)

[Arithmetic Expression] [Results]


2 ∗ ( −3) −6
4 ∗ 5 − 15 5
4 + 2 ∗ 5 14
7/2 3
7 / 2.0 3.5
2 / 5 0
2.0 / 5.0 0.4
2 / 5 ∗ 5 0
2.0 + 1.0 + 5 / 2 5.0
5 % 2 1
4 ∗ 5/2 + 5 % 2 11

C Programming 61 / 71
Data Assignment

Assig value to variable in accordance with its type

i n t main ( )
{ [Output]
int a;
a = 2.99; a = 2
p r i n t f ( ” a = %d” , a ) ;
}

Comments: above expression is valid, but NOT suggested

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)

When they appear in a compound expression, things are different


a=i++ will be different from a=++i
In a=i++, i contributes its value to a first, then self-increments
In a=++i, i self-increments first, then contributes its value to a
Similiar comments apply to i– and –i

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)

Now verify how much you understand

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

Conditional Operator: logic exp1?exp2:exp3


Three operands
If logic exp1 is none zero, takes exp2
If logic exp1 is zero, takes exp3

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

1 Basics about Data Representation

2 Data types

3 Variables and Constants

4 Variable Input/Output

5 Data Operators and Expressions

6 Implicit and Forceful Data Type Casting

C Programming 69 / 71
Implicit Data Type Casting

See whether you can work out the answer

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

See whether you can work out the answer

a = (float)c + (float)6 + 3.1 + 1.7e5


float float float double
char c = ’ x ’ ;
int
double a = ( f l o a t ) c + ( f l o a t )5 +
float float
1 . 3 + 1 . 7 3 e4 ;
float

double

Above type castings are done forcefully


Again it is risky sometimes

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

You might also like