Variables & Fundamental Data Types
Structure of C program Start with #include <..>               #include <stdio.h>                                         ...
Structure of C program#include <stdio.h>void main()                   Variable declaration{    int x ;                   I...
Variables Variables  – Memory space for storing temporary values during the    executing program  – Variable must be decl...
Variables Variables Naming Rule  – Composed of English alphabets, numbers, _(underbar)  – First character should be an En...
Variables Reserved Words  – Words reserved for C language                           Keywords   auto        do         got...
Variables What is int ahead of variables?                       #include <stdio.h>                       void main() Type...
The Fundamental Data Types Data Types in C                           Fundamental data types  char                      si...
The Data Type int int :   – 2 byte machine : -32768(-215) ~ 32767(215-1)   – 4 byte machine : -2147483648(-231) ~ 2147483...
The Integral Types unsigned: positive integer only   – Range of unsigned int (0 ~ 2wordsize-1)       • 2 byte machine: 0 ...
The Integral Types   Binary representation of 2 bytes int          Case of int                            Case of unsinge...
The Integral Types Example : 4 byte machine                                           2147483645  int i = 2147483645, j ;...
The Integral Types Example : 4 byte machine                                    -1 4294967295  int i = -1 ;               ...
Integer Constants Integer Constants :  – In C, integer type is represented as Decimal, Octal,    Hexadecimal    [Ex]    1...
Integer Constants Example#include <stdio.h>                                                   17 15 23int main(void) {   ...
The Data Type char char type   – 8 bits for all machines   – Can represent 256 characters   – Can store a character or a ...
The Data Type char char variables can be handled as int variables   [Ex]   char c; int i;   for ( i = ‘a’ ; i <= ‘z’; ++i...
The Data Type char[Ex] char c; c= ‘A’+5;                                     F 70 printf(“%c %dn”, c, c);[Ex] c = ‘A’;    ...
The Data Type char Escape sequence            Nonprinting and hard-to-print characters Name of character        Written i...
The Floating Types float, double, long double   – Store real number data   – Store approximated values (Not exact values)...
The Floating Types float   – 4bytes memory allocation (4 byte machine)   – Can store 6~7 significant digits   – Range of ...
The Floating Types Float type operation  – Up to 6~7 significant digits (also approximation)     float f1 = 0.1234567, f2...
Floating Constants Float Constants :  – Represented by decimal point numbers  – Represented by exponential forms    [Ex] ...
Floating Constants Example                                                   57.0 57.0 57.0 57.0#include <stdio.h>int mai...
Data Types: Operations with Different Type Rounded up,         Comparison  Chopping             float f = 1.23456789 ;  ...
Data Types: Operations with Different Type Operation between int and float   –   Arithmetic operation of int and int resu...
Casts Casts  – Operand type converted in expression  – (type)expression       [Ex1]   int a=3, b=2;               double ...
Upcoming SlideShare
Loading in …5
×

2 1. variables & data types

674 views

Published on

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
674
On SlideShare
0
From Embeds
0
Number of Embeds
231
Actions
Shares
0
Downloads
7
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

2 1. variables & data types

  1. 1. Variables & Fundamental Data Types
  2. 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. 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. 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. 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. 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. 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. 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
  9. 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) 9
  10. 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) 10
  11. 11. The Integral Types  Binary representation of 2 bytes int Case of int Case of unsinged int0000 0000 0000 0000 -> 0 0000 0000 0000 0000 -> 00000 0000 0000 0001 -> 1 0000 0000 0000 0001 -> 10000 0000 0000 0010 -> 2 0000 0000 0000 0010 -> 20000 0000 0000 0011 -> 3 0000 0000 0000 0011 -> 3… …0111 1111 1111 1110 -> 215-2 0111 1111 1111 1110 -> 215-20111 1111 1111 1111 -> 215-1 0111 1111 1111 1111 -> 215-11000 0000 0000 0000 -> -215 1000 0000 0000 0000 -> 2151000 0000 0000 0001 -> -215+1 1000 0000 0000 0001 -> 215+11000 0000 0000 0010 -> -215+2 1000 0000 0000 0010 -> 215+2… …1111 1111 1111 1101 -> -3 1111 1111 1111 1101 -> 216-31111 1111 1111 1110 -> -2 1111 1111 1111 1110 -> 216-21111 1111 1111 1111 -> -1 1111 1111 1111 1111 -> 216-1 11
  12. 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. 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
  14. 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 */ 14
  15. 15. Integer Constants Example#include <stdio.h> 17 15 23int main(void) { int i = 17, j = 017, k =0x17 ; printf( “%d %d %dn”, i, j, k ) ; return 0 ;}#include <stdio.h> 15 17 f Fint main(void) { int i = 15; printf( “%d %o %x %Xn”, i, i, i, i ) ; return 0 ;} 15
  16. 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. 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. 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. 19. The Data Type char Escape sequence Nonprinting and hard-to-print characters Name of character Written in C Integer valuealert a 7backslash 92backspace b 8carriage return r 13double quote ” 34formfeed f 12horizontal tab t 9newline n 10null character 0 0single quote ’ 39vertical tab v 11 19
  20. 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. 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. 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. 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
  24. 24. Floating Constants Example 57.0 57.0 57.0 57.0#include <stdio.h>int main(void) { float f=57.0, g=5.70E1, h=.57e+02, i=570e-01 ; printf( “%.1f %.1f %.1f %.1fn”, f, g, h, i ) ; return 0 ;} 57.0 5.7e+001 5.7E+001#include <stdio.h>int main(void) { float f=57.0, g=57.0, h=57.0 ; printf( “%.1f %.1e %.1En”, f, g, h ) ; return 0 ;} 24
  25. 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. 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. 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

×