SlideShare une entreprise Scribd logo
1  sur  27
Télécharger pour lire hors ligne
Variables & Fundamental Data Types
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
Structure of C program

#include <stdio.h>

void main()                   Variable declaration
{

    int x ;                   Input

    scanf( “%d”, &x ) ;

    printf( “%dn”, x*x ) ;   Output

    return ;
}


                                                     3
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
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
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
Variables
 What is int ahead of variables?


                       #include <stdio.h>

                       void main()
 Types of values       {
variables can store        int inches, feet, fathoms;

                           …
                       }




                                                        7
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
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
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
The Integral Types

   Binary representation of 2 bytes int
          Case of int                            Case of unsinged int
0000   0000   0000   0000   ->   0        0000   0000   0000   0000   ->   0
0000   0000   0000   0001   ->   1        0000   0000   0000   0001   ->   1
0000   0000   0000   0010   ->   2        0000   0000   0000   0010   ->   2
0000   0000   0000   0011   ->   3        0000   0000   0000   0011   ->   3
…                                         …
0111   1111   1111   1110   ->   215-2    0111   1111   1111   1110   ->   215-2
0111   1111   1111   1111   ->   215-1    0111   1111   1111   1111   ->   215-1
1000   0000   0000   0000   ->   -215     1000   0000   0000   0000   ->   215
1000   0000   0000   0001   ->   -215+1   1000   0000   0000   0001   ->   215+1
1000   0000   0000   0010   ->   -215+2   1000   0000   0000   0010   ->   215+2
…                                         …
1111   1111 1111 1101 -> -3               1111   1111 1111 1101 -> 216-3
1111   1111 1111 1110 -> -2               1111   1111 1111 1110 -> 216-2
1111   1111 1111 1111 -> -1               1111   1111 1111 1111 -> 216-1

                                                                                   11
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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

Contenu connexe

Tendances

Think sharp, write swift
Think sharp, write swiftThink sharp, write swift
Think sharp, write swiftPascal Batty
 
C C++ tutorial for beginners- tibacademy.in
C C++ tutorial for beginners- tibacademy.inC C++ tutorial for beginners- tibacademy.in
C C++ tutorial for beginners- tibacademy.inTIB Academy
 
Lesson 17. Pattern 9. Mixed arithmetic
Lesson 17. Pattern 9. Mixed arithmeticLesson 17. Pattern 9. Mixed arithmetic
Lesson 17. Pattern 9. Mixed arithmeticPVS-Studio
 
C++ Quick Reference Sheet from Hoomanb.com
C++ Quick Reference Sheet from Hoomanb.comC++ Quick Reference Sheet from Hoomanb.com
C++ Quick Reference Sheet from Hoomanb.comFrescatiStory
 
Spf Chapter4 Variables
Spf Chapter4 VariablesSpf Chapter4 Variables
Spf Chapter4 VariablesHock Leng PUAH
 
DEF CON 23 - Atlas - fun with symboliks
DEF CON 23 - Atlas - fun with symboliksDEF CON 23 - Atlas - fun with symboliks
DEF CON 23 - Atlas - fun with symboliksFelipe Prado
 
Rx: Curing Your Asynchronous Programming Blues | QCon London
Rx: Curing Your Asynchronous Programming Blues |  QCon LondonRx: Curing Your Asynchronous Programming Blues |  QCon London
Rx: Curing Your Asynchronous Programming Blues | QCon LondonJiby John
 
DISE - Windows Based Application Development in C#
DISE - Windows Based Application Development in C#DISE - Windows Based Application Development in C#
DISE - Windows Based Application Development in C#Rasan Samarasinghe
 
Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02CIMAP
 
Concept_of_NAN_IND_INF_DEN_Using_C++
Concept_of_NAN_IND_INF_DEN_Using_C++Concept_of_NAN_IND_INF_DEN_Using_C++
Concept_of_NAN_IND_INF_DEN_Using_C++Mohammed Nisamudheen
 
Ruby Topic Maps Tutorial (2007-10-10)
Ruby Topic Maps Tutorial (2007-10-10)Ruby Topic Maps Tutorial (2007-10-10)
Ruby Topic Maps Tutorial (2007-10-10)Benjamin Bock
 
Multidimensional arrays in C++
Multidimensional arrays in C++Multidimensional arrays in C++
Multidimensional arrays in C++Ilio Catallo
 
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]Chris Adamson
 

Tendances (20)

Think sharp, write swift
Think sharp, write swiftThink sharp, write swift
Think sharp, write swift
 
State Monad
State MonadState Monad
State Monad
 
C C++ tutorial for beginners- tibacademy.in
C C++ tutorial for beginners- tibacademy.inC C++ tutorial for beginners- tibacademy.in
C C++ tutorial for beginners- tibacademy.in
 
Test2 Sum05
Test2 Sum05Test2 Sum05
Test2 Sum05
 
Lesson 17. Pattern 9. Mixed arithmetic
Lesson 17. Pattern 9. Mixed arithmeticLesson 17. Pattern 9. Mixed arithmetic
Lesson 17. Pattern 9. Mixed arithmetic
 
C++ Quick Reference Sheet from Hoomanb.com
C++ Quick Reference Sheet from Hoomanb.comC++ Quick Reference Sheet from Hoomanb.com
C++ Quick Reference Sheet from Hoomanb.com
 
Spf Chapter4 Variables
Spf Chapter4 VariablesSpf Chapter4 Variables
Spf Chapter4 Variables
 
DEF CON 23 - Atlas - fun with symboliks
DEF CON 23 - Atlas - fun with symboliksDEF CON 23 - Atlas - fun with symboliks
DEF CON 23 - Atlas - fun with symboliks
 
Rx: Curing Your Asynchronous Programming Blues | QCon London
Rx: Curing Your Asynchronous Programming Blues |  QCon LondonRx: Curing Your Asynchronous Programming Blues |  QCon London
Rx: Curing Your Asynchronous Programming Blues | QCon London
 
1
11
1
 
DISE - Windows Based Application Development in C#
DISE - Windows Based Application Development in C#DISE - Windows Based Application Development in C#
DISE - Windows Based Application Development in C#
 
Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02
 
oop Lecture 17
oop Lecture 17oop Lecture 17
oop Lecture 17
 
OpenGL ES 3 Reference Card
OpenGL ES 3 Reference CardOpenGL ES 3 Reference Card
OpenGL ES 3 Reference Card
 
Concept_of_NAN_IND_INF_DEN_Using_C++
Concept_of_NAN_IND_INF_DEN_Using_C++Concept_of_NAN_IND_INF_DEN_Using_C++
Concept_of_NAN_IND_INF_DEN_Using_C++
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
 
L05if
L05ifL05if
L05if
 
Ruby Topic Maps Tutorial (2007-10-10)
Ruby Topic Maps Tutorial (2007-10-10)Ruby Topic Maps Tutorial (2007-10-10)
Ruby Topic Maps Tutorial (2007-10-10)
 
Multidimensional arrays in C++
Multidimensional arrays in C++Multidimensional arrays in C++
Multidimensional arrays in C++
 
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
 

Similaire à 2 1. variables & data types

Similaire à 2 1. variables & data types (20)

Cbasic
CbasicCbasic
Cbasic
 
Cbasic
CbasicCbasic
Cbasic
 
Cbasic
CbasicCbasic
Cbasic
 
Programming in C Basics
Programming in C BasicsProgramming in C Basics
Programming in C Basics
 
Fundamentals of Programming Constructs.pptx
Fundamentals of  Programming Constructs.pptxFundamentals of  Programming Constructs.pptx
Fundamentals of Programming Constructs.pptx
 
2 BytesC++ course_2014_c9_ pointers and dynamic arrays
2 BytesC++ course_2014_c9_ pointers and dynamic arrays 2 BytesC++ course_2014_c9_ pointers and dynamic arrays
2 BytesC++ course_2014_c9_ pointers and dynamic arrays
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial
 
OpenGurukul : Language : C Programming
OpenGurukul : Language : C ProgrammingOpenGurukul : Language : C Programming
OpenGurukul : Language : C Programming
 
03 and 04 .Operators, Expressions, working with the console and conditional s...
03 and 04 .Operators, Expressions, working with the console and conditional s...03 and 04 .Operators, Expressions, working with the console and conditional s...
03 and 04 .Operators, Expressions, working with the console and conditional s...
 
Interesting facts on c
Interesting facts on cInteresting facts on c
Interesting facts on c
 
the refernce of programming C notes ppt.pptx
the refernce of programming C notes ppt.pptxthe refernce of programming C notes ppt.pptx
the refernce of programming C notes ppt.pptx
 
11 2. variable-scope rule,-storage_class
11 2. variable-scope rule,-storage_class11 2. variable-scope rule,-storage_class
11 2. variable-scope rule,-storage_class
 
Unit1 C
Unit1 CUnit1 C
Unit1 C
 
Unit1 C
Unit1 CUnit1 C
Unit1 C
 
Theory1&amp;2
Theory1&amp;2Theory1&amp;2
Theory1&amp;2
 
C chap08
C chap08C chap08
C chap08
 
C standard library functions
C standard library functionsC standard library functions
C standard library functions
 
dinoC_ppt.pptx
dinoC_ppt.pptxdinoC_ppt.pptx
dinoC_ppt.pptx
 
T02 a firstcprogram
T02 a firstcprogramT02 a firstcprogram
T02 a firstcprogram
 
T02 a firstcprogram
T02 a firstcprogramT02 a firstcprogram
T02 a firstcprogram
 

Plus de 웅식 전

15 3. modulization
15 3. modulization15 3. modulization
15 3. modulization웅식 전
 
15 2. arguement passing to main
15 2. arguement passing to main15 2. arguement passing to main
15 2. arguement passing to main웅식 전
 
12 2. dynamic allocation
12 2. dynamic allocation12 2. dynamic allocation
12 2. dynamic allocation웅식 전
 
12 1. multi-dimensional array
12 1. multi-dimensional array12 1. multi-dimensional array
12 1. multi-dimensional array웅식 전
 
11. array & pointer
11. array & pointer11. array & pointer
11. array & pointer웅식 전
 
10. pointer & function
10. pointer & function10. pointer & function
10. pointer & function웅식 전
 
7. variable scope rule,-storage_class
7. variable scope rule,-storage_class7. variable scope rule,-storage_class
7. variable scope rule,-storage_class웅식 전
 
5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing웅식 전
 
5 1. character processing
5 1. character processing5 1. character processing
5 1. character processing웅식 전
 
15 1. enumeration, typedef
15 1. enumeration, typedef15 1. enumeration, typedef
15 1. enumeration, typedef웅식 전
 
3 2. if statement
3 2. if statement3 2. if statement
3 2. if statement웅식 전
 
3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib웅식 전
 
2 3. standard io
2 3. standard io2 3. standard io
2 3. standard io웅식 전
 
2 2. operators
2 2. operators2 2. operators
2 2. operators웅식 전
 
2 1. variables & data types
2 1. variables & data types2 1. variables & data types
2 1. variables & data types웅식 전
 

Plus de 웅식 전 (20)

15 3. modulization
15 3. modulization15 3. modulization
15 3. modulization
 
15 2. arguement passing to main
15 2. arguement passing to main15 2. arguement passing to main
15 2. arguement passing to main
 
14. fiile io
14. fiile io14. fiile io
14. fiile io
 
13. structure
13. structure13. structure
13. structure
 
12 2. dynamic allocation
12 2. dynamic allocation12 2. dynamic allocation
12 2. dynamic allocation
 
12 1. multi-dimensional array
12 1. multi-dimensional array12 1. multi-dimensional array
12 1. multi-dimensional array
 
11. array & pointer
11. array & pointer11. array & pointer
11. array & pointer
 
10. pointer & function
10. pointer & function10. pointer & function
10. pointer & function
 
9. pointer
9. pointer9. pointer
9. pointer
 
7. variable scope rule,-storage_class
7. variable scope rule,-storage_class7. variable scope rule,-storage_class
7. variable scope rule,-storage_class
 
6. function
6. function6. function
6. function
 
5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing
 
5 1. character processing
5 1. character processing5 1. character processing
5 1. character processing
 
15 1. enumeration, typedef
15 1. enumeration, typedef15 1. enumeration, typedef
15 1. enumeration, typedef
 
4. loop
4. loop4. loop
4. loop
 
3 2. if statement
3 2. if statement3 2. if statement
3 2. if statement
 
3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib
 
2 3. standard io
2 3. standard io2 3. standard io
2 3. standard io
 
2 2. operators
2 2. operators2 2. operators
2 2. operators
 
2 1. variables & data types
2 1. variables & data types2 1. variables & data types
2 1. variables & data types
 

2 1. variables & data types

  • 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
  • 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. 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. The Integral Types  Binary representation of 2 bytes int Case of int Case of unsinged int 0000 0000 0000 0000 -> 0 0000 0000 0000 0000 -> 0 0000 0000 0000 0001 -> 1 0000 0000 0000 0001 -> 1 0000 0000 0000 0010 -> 2 0000 0000 0000 0010 -> 2 0000 0000 0000 0011 -> 3 0000 0000 0000 0011 -> 3 … … 0111 1111 1111 1110 -> 215-2 0111 1111 1111 1110 -> 215-2 0111 1111 1111 1111 -> 215-1 0111 1111 1111 1111 -> 215-1 1000 0000 0000 0000 -> -215 1000 0000 0000 0000 -> 215 1000 0000 0000 0001 -> -215+1 1000 0000 0000 0001 -> 215+1 1000 0000 0000 0010 -> -215+2 1000 0000 0000 0010 -> 215+2 … … 1111 1111 1111 1101 -> -3 1111 1111 1111 1101 -> 216-3 1111 1111 1111 1110 -> -2 1111 1111 1111 1110 -> 216-2 1111 1111 1111 1111 -> -1 1111 1111 1111 1111 -> 216-1 11
  • 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
  • 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. 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
  • 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. 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