View
638
Download
9
Category
Preview:
DESCRIPTION
Citation preview
Variables & Fundamental Data Types
Structure of C program
Start with #include <..>
All statements locate between “void main() {“ and “}”
All statements end with “;”
Case sensitive – “Printf” and “printf” are not the
same
2
#include <stdio.h> void main() { int x ; scanf( “%d”, &x ) ; printf( “%d\n”, x*x ) ; return ; }
Structure of C program
3
#include <stdio.h> void main() { int x ; scanf( “%d”, &x ) ; printf( “%d\n”, x*x ) ; return ; }
Variable declaration
Input
Output
Variables
Variables
– Memory space for storing temporary values during the executing program
– Variable must be declared before use
– Variables are created in memory during the executing program
4
#include <stdio.h>
void main()
{
int inches, feet, fathoms;
…
}
…
inches
feet
fathoms
…
Variables
Variables Naming Rule
– Composed of English alphabets, numbers, _(underbar)
– First character should be an English alphabet or _(underbar)
– maximum length: 255
– Reserved words are not available as variable names
5
[Ex]
available variable name: times10, get_next_char, _done
Not available variable name: 10times, get-next-char, int
Variables
Reserved Words
– Words reserved for C language
6
Keywords
auto do goto signed unsigned
break double if sizeof void
case else int static volatile
char enum long struct while
const extern register switch
continue float return typedef
default for short union
Variables
What is int ahead of variables?
7
#include <stdio.h>
void main()
{
int inches, feet, fathoms;
…
}
Types of values variables can store
8
The Fundamental Data Types
Data Types in C
– ‘signed’ keyword can be ignored
• int and signed int, long and signed long, each pair has the
same meaning
– ‘int’ keyword can be ignored in short int, long int, unsigned
int
• Simply short, long, unsigned are OK
Fundamental data types
char signed char unsigned char
signed short int signed int signed long int
unsigned short int unsigned int unsigned long int
float double long double
9
The Data Type int
int : – 2 byte machine : -32768(-215) ~ 32767(215-1) – 4 byte machine : -2147483648(-231) ~ 2147483647(231-1) – 8 byte machine : -2147483648(-231) ~ 2147483647(231-1)
short
– 2 byte machine : -32768(-215) ~ 32767(215-1) – 4 byte machine : -32768(-215) ~ 32767(215-1) – 8 byte machine : -32768(-215) ~ 32767(215-1)
long – 2 byte machine : -2147483648(-231) ~ 2147483647(231-1) – 4 byte machine : -2147483648(-231) ~ 2147483647(231-1) – 8 byte machine : -263 ~ (263-1)
10
The Integral Types
unsigned: positive integer only
– Range of unsigned int (0 ~ 2wordsize-1)
• 2 byte machine: 0 ~ 65535(216-1)
• 4 byte machine: 0~ 42949647295(232-1)
• 8 byte machine: 0~ 42949647295(232-1)
– Range of unsigned long
• 2 byte machine: 0~ 42949647295(232-1)
• 4 byte machine: 0~ 42949647295(232-1)
• 8 byte machine: 0 ~ (264-1)
11
The Integral Types
Binary representation of 2 bytes int
0000 0000 0000 0000 -> 0
0000 0000 0000 0001 -> 1
0000 0000 0000 0010 -> 2
0000 0000 0000 0011 -> 3
…
0111 1111 1111 1110 -> 215-2
0111 1111 1111 1111 -> 215-1
1000 0000 0000 0000 -> -215
1000 0000 0000 0001 -> -215+1
1000 0000 0000 0010 -> -215+2
…
1111 1111 1111 1101 -> -3
1111 1111 1111 1110 -> -2
1111 1111 1111 1111 -> -1
0000 0000 0000 0000 -> 0
0000 0000 0000 0001 -> 1
0000 0000 0000 0010 -> 2
0000 0000 0000 0011 -> 3
…
0111 1111 1111 1110 -> 215-2
0111 1111 1111 1111 -> 215-1
1000 0000 0000 0000 -> 215
1000 0000 0000 0001 -> 215+1
1000 0000 0000 0010 -> 215+2
…
1111 1111 1111 1101 -> 216-3
1111 1111 1111 1110 -> 216-2
1111 1111 1111 1111 -> 216-1
Case of int Case of unsinged int
12
The Integral Types
Example : 4 byte machine
int i = 2147483645, j ; for( j = 0 ; j < 5 ; j++ ) printf( “%d\n”, i + j ) ;
unsigned int i = 2147483645, j ; for( j = 0 ; j < 5 ; j++ ) printf( “%u\n”, i + j ) ;
2147483645 2147483646 2147483647 -2147483648 -2147483647
2147483645 2147483646 2147483647 2147483648 2147483649
13
The Integral Types
Example : 4 byte machine
int i = -1 ; unsigned u = -1 ; printf( “%d %u\n”, i, u ) ; printf( “%d %d\n”, i, u ) ; printf( “%u %u\n”, i, u ) ;
-1 4294967295 -1 -1 4294967295 4294967295
14
Integer Constants
Integer Constants :
– In C, integer type is represented as Decimal, Octal, Hexadecimal
[Ex] 17 /* decimal integer constant */ 017 /* octal integer constant : 17(8) = 15 */ 0x17 /* hexadecimal integer constant 17(16)= 23 */ -17 /* negative decimal integer constant */
15
Integer Constants
Example
#include <stdio.h> int main(void) { int i = 17, j = 017, k =0x17 ; printf( “%d %d %d\n”, i, j, k ) ; return 0 ; }
17 15 23
#include <stdio.h> int main(void) { int i = 15; printf( “%d %o %x %X\n”, i, i, i, i ) ; return 0 ; }
15 17 f F
16
The Data Type char
char type
– 8 bits for all machines
– Can represent 256 characters
– Can store a character or a small integer number
[Ex]
printf(“%c”, ‘a’ ); /* a is printed */
printf(“%c%c%c”, ‘A’, ‘ B’, ‘C’ ); /* ABC is printed */
printf(“%c”, 97 ); /* a is printed */
printf(“%c”, ‘a’+1 ); /* b is printed */
printf(“%d”, ‘a’ ); /* 97 is printed */
17
The Data Type char
char variables can be handled as int variables
[Ex]
char c; int i;
for ( i = ‘a’ ; i <= ‘z’; ++i )
printf(“%c”, i); /* abc … z is printed */
for ( c = 65; c <= 90 ; ++c )
printf(“%c”, c); /*ABC … Z is printed */
for ( c = ‘0’; c <= ‘9’ ; ++c )
printf(“%d ”, c); /* 48 49 50… 57 is printed */
18
The Data Type char
[Ex]
char c;
c= ‘A’+5;
printf(“%c %d\n”, c, c);
[Ex]
c = ‘A’;
c++;
printf(“%c %d\n”, c, c);
[Ex]
for( c = ‘A’; c <= ‘Z’; c++ )
printf(“%c\t”,c);
F 70
B 66
A B C D E … Z
19
The Data Type char
Nonprinting and hard-to-print characters
Name of character Written in C Integer value
alert
backslash
backspace
carriage return
double quote
formfeed
horizontal tab
newline
null character
single quote
vertical tab
\a
\\
\b
\r
\”
\f
\t
\n
\0
\’
\v
7
92
8
13
34
12
9
10
0
39
11
Escape sequence
20
The Floating Types
float, double, long double
– Store real number data
– Store approximated values (Not exact values)
– Exponential notation possible
[Ex] 1.234567e5 = 1.234567 x 105 integer : 1 fraction : 234567 exponent : 5
21
The Floating Types
float
– 4bytes memory allocation (4 byte machine)
– Can store 6~7 significant digits
– Range of float type: 10-38 ~ 1038
double
– 8bytes memory allocation
– Can store 15~16 significant digits
– Range of double type : 10-308 ~ 10308
[Ex] double a = 123.45123451234512345;
[Ex] float a = 123.451234;
The Floating Types
Float type operation
– Up to 6~7 significant digits (also approximation)
22
float f1 = 0.1234567, f2 = 0.00000008 ;
f1 + f2 == ?
float f1 = 12345670.0, f2 = 8.0 ;
f1 + f2 == ?
float f1 = 123.4567, f2 = 100000.0 ;
f1 + f2 == ?
23
Floating Constants
Float Constants :
– Represented by decimal point numbers
– Represented by exponential forms
[Ex] 57.0 /* Decimal point */
5.70E1 /*Exponential form */ .57e+02 570.e-01
24
Floating Constants
Example
#include <stdio.h> int main(void) { float f=57.0, g=5.70E1, h=.57e+02, i=570e-01 ; printf( “%.1f %.1f %.1f %.1f\n”, f, g, h, i ) ; return 0 ; }
57.0 57.0 57.0 57.0
#include <stdio.h> int main(void) { float f=57.0, g=57.0, h=57.0 ; printf( “%.1f %.1e %.1E\n”, f, g, h ) ; return 0 ; }
57.0 5.7e+001 5.7E+001
Data Types: Operations with Different Type
Rounded up, Chopping
Comparison
25
int n1, n2;
float f = 1.2 ;
n1 = f + 0.5 ;
n2 = f ;
float f = 1.23456789 ;
if( f == 1.23456789 )
printf( “Yes\n” ) ;
else
printf( “No\n” ) ;
Data Types: Operations with Different Type
Operation between int and float
– Arithmetic operation of int and int results in int
– Arithmetic operation of float and float results in float
– Arithmetic operation of int and float results in float
– Comparison operations between two types are done as you expect
26
2 + 1 == ?
2 * 1 == ?
3 / 2 == ?
3 % 2 == ?
2.0 + 1.0 == ?
2.0 * 1.0 == ?
3.0 / 2.0 == ?
3.0 % 2.5 == ?
2 + 1.0 == ?
2.0 * 1 == ?
3 / 2.0 == ?
3 % 2.0 == ?
2 < 1 ?
2.0 > 1 ?
2.0 <= 1.0 ?
27
Casts
Casts
– Operand type converted in expression
– (type)expression
[Ex1] int a=3, b=2; double c = a / b; printf(“c=%f\n”, c);
[Ex2] int a=3, b=2; double c = (double) a / b; printf(“c=%f\n”, c);
c=1.000000
c=1.500000
Recommended