This document discusses variables and fundamental data types in C programming. It explains that variables are used to store temporary values during program execution and must be declared before use. It also covers the different fundamental data types like int, char, float, and double, and how they are represented and used. Integer, floating point, and character constants are also explained.
2. Structure of C program
Start with #include <..> #include <stdio.h>
void main()
All statements locate {
between “void main() {“
int x ;
and “}”
scanf( “%d”, &x ) ;
All statements end with “;”
printf( “%dn”, x*x ) ;
return ;
Case sensitive
}
– “Printf” and “printf” are not the
same
2
3. Structure of C program
#include <stdio.h>
void main() Variable declaration
{
int x ; Input
scanf( “%d”, &x ) ;
printf( “%dn”, x*x ) ; Output
return ;
}
3
4. 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
#include <stdio.h> …
void main() inches
{ feet
int inches, feet, fathoms;
fathoms
… …
}
4
5. Variables
Variables Naming Rule
– Composed of English alphabets, numbers, _(underbar)
– First character should be an English alphabet or _(underbar)
[Ex]
available variable name: times10, get_next_char, _done
Not available variable name: 10times, get-next-char, int
– maximum length: 255
– Reserved words are not available as variable names
5
6. Variables
Reserved Words
– Words reserved for C language
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
6
7. Variables
What is int ahead of variables?
#include <stdio.h>
void main()
Types of values {
variables can store int inches, feet, fathoms;
…
}
7
8. The Fundamental Data Types
Data Types in C
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
– ‘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
8
12. The Integral Types
Example : 4 byte machine
2147483645
int i = 2147483645, j ;
2147483646
for( j = 0 ; j < 5 ; j++ ) 2147483647
printf( “%dn”, i + j ) ; -2147483648
-2147483647
2147483645
unsigned int i = 2147483645, j ;
2147483646
for( j = 0 ; j < 5 ; j++ ) 2147483647
printf( “%un”, i + j ) ; 2147483648
2147483649
12
13. The Integral Types
Example : 4 byte machine
-1 4294967295
int i = -1 ; -1 -1
unsigned u = -1 ; 4294967295 4294967295
printf( “%d %un”, i, u ) ;
printf( “%d %dn”, i, u ) ;
printf( “%u %un”, i, u ) ;
13
15. Integer Constants
Example
#include <stdio.h>
17 15 23
int main(void) {
int i = 17, j = 017, k =0x17 ;
printf( “%d %d %dn”, i, j, k ) ;
return 0 ;
}
#include <stdio.h>
15 17 f F
int main(void) {
int i = 15;
printf( “%d %o %x %Xn”, i, i, i, i ) ;
return 0 ;
}
15
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 */
16
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 */
17
18. The Data Type char
[Ex]
char c;
c= ‘A’+5;
F 70
printf(“%c %dn”, c, c);
[Ex]
c = ‘A’;
B 66
c++;
printf(“%c %dn”, c, c);
[Ex]
for( c = ‘A’; c <= ‘Z’; c++ ) A B C D E…Z
printf(“%ct”,c);
18
19. The Data Type char
Escape sequence
Nonprinting and hard-to-print characters
Name of character Written in C Integer value
alert a 7
backslash 92
backspace b 8
carriage return r 13
double quote ” 34
formfeed f 12
horizontal tab t 9
newline n 10
null character 0 0
single quote ’ 39
vertical tab v 11
19
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
20
21. The Floating Types
float
– 4bytes memory allocation (4 byte machine)
– Can store 6~7 significant digits
– Range of float type: 10-38 ~ 1038
[Ex] float a = 123.451234;
double
– 8bytes memory allocation
– Can store 15~16 significant digits
– Range of double type : 10-308 ~ 10308
[Ex] double a = 123.45123451234512345;
21
22. The Floating Types
Float type operation
– Up to 6~7 significant digits (also approximation)
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 == ?
22
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
23
25. Data Types: Operations with Different Type
Rounded up, Comparison
Chopping float f = 1.23456789 ;
int n1, n2;
float f = 1.2 ; if( f == 1.23456789 )
printf( “Yesn” ) ;
n1 = f + 0.5 ; else
n2 = f ; printf( “Non” ) ;
25
26. 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
2 + 1 == ? 2.0 + 1.0 == ? 2 + 1.0 == ? 2<1 ?
2 * 1 == ? 2.0 * 1.0 == ? 2.0 * 1 == ? 2.0 > 1 ?
3 / 2 == ? 3.0 / 2.0 == ? 3 / 2.0 == ? 2.0 <= 1.0 ?
3 % 2 == ? 3.0 % 2.5 == ? 3 % 2.0 == ?
26
27. Casts
Casts
– Operand type converted in expression
– (type)expression
[Ex1] int a=3, b=2;
double c = a / b;
printf(“c=%fn”, c); c=1.000000
[Ex2] int a=3, b=2;
double c = (double) a / b;
printf(“c=%fn”, c);
c=1.500000
27