SlideShare a Scribd company logo
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
Ad

Recommended

C tutorial
C tutorial
Anurag Sukhija
 
Keygenning using the Z3 SMT Solver
Keygenning using the Z3 SMT Solver
extremecoders
 
C tutorial
C tutorial
Patruni Chidananda Sastry
 
DISE - Windows Based Application Development in Java
DISE - Windows Based Application Development in Java
Rasan Samarasinghe
 
DITEC - Programming with C#.NET
DITEC - Programming with C#.NET
Rasan Samarasinghe
 
Spf Chapter5 Conditional Logics
Spf Chapter5 Conditional Logics
Hock Leng PUAH
 
Lập trình C
Lập trình C
Viet NguyenHoang
 
Micro Blaze C Reference
Micro Blaze C Reference
iuui
 
Think sharp, write swift
Think sharp, write swift
Pascal Batty
 
State Monad
State Monad
Philip Schwarz
 
C C++ tutorial for beginners- tibacademy.in
C C++ tutorial for beginners- tibacademy.in
TIB Academy
 
Test2 Sum05
Test2 Sum05
guestc66a38
 
Lesson 17. Pattern 9. Mixed arithmetic
Lesson 17. Pattern 9. Mixed arithmetic
PVS-Studio
 
C++ Quick Reference Sheet from Hoomanb.com
C++ Quick Reference Sheet from Hoomanb.com
FrescatiStory
 
Spf Chapter4 Variables
Spf Chapter4 Variables
Hock Leng PUAH
 
DEF CON 23 - Atlas - fun with symboliks
DEF CON 23 - Atlas - fun with symboliks
Felipe Prado
 
Rx: Curing Your Asynchronous Programming Blues | QCon London
Rx: Curing Your Asynchronous Programming Blues | QCon London
Jiby John
 
1
1
guestc66a38
 
DISE - Windows Based Application Development in C#
DISE - Windows Based Application Development in C#
Rasan Samarasinghe
 
Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02
CIMAP
 
oop Lecture 17
oop Lecture 17
Anwar Ul Haq
 
OpenGL ES 3 Reference Card
OpenGL ES 3 Reference Card
The Khronos Group Inc.
 
Concept_of_NAN_IND_INF_DEN_Using_C++
Concept_of_NAN_IND_INF_DEN_Using_C++
Mohammed Nisamudheen
 
Arrays in C++
Arrays in C++
Ilio Catallo
 
L05if
L05if
Kgr Sushmitha
 
Ruby Topic Maps Tutorial (2007-10-10)
Ruby Topic Maps Tutorial (2007-10-10)
Benjamin Bock
 
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]
Chris Adamson
 
Mesics lecture 3 c – constants and variables
Mesics lecture 3 c – constants and variables
eShikshak
 
C language (Collected By Dushmanta)
C language (Collected By Dushmanta)
Dushmanta Nath
 

More Related Content

What's hot (20)

Think sharp, write swift
Think sharp, write swift
Pascal Batty
 
State Monad
State Monad
Philip Schwarz
 
C C++ tutorial for beginners- tibacademy.in
C C++ tutorial for beginners- tibacademy.in
TIB Academy
 
Test2 Sum05
Test2 Sum05
guestc66a38
 
Lesson 17. Pattern 9. Mixed arithmetic
Lesson 17. Pattern 9. Mixed arithmetic
PVS-Studio
 
C++ Quick Reference Sheet from Hoomanb.com
C++ Quick Reference Sheet from Hoomanb.com
FrescatiStory
 
Spf Chapter4 Variables
Spf Chapter4 Variables
Hock Leng PUAH
 
DEF CON 23 - Atlas - fun with symboliks
DEF CON 23 - Atlas - fun with symboliks
Felipe Prado
 
Rx: Curing Your Asynchronous Programming Blues | QCon London
Rx: Curing Your Asynchronous Programming Blues | QCon London
Jiby John
 
1
1
guestc66a38
 
DISE - Windows Based Application Development in C#
DISE - Windows Based Application Development in C#
Rasan Samarasinghe
 
Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02
CIMAP
 
oop Lecture 17
oop Lecture 17
Anwar Ul Haq
 
OpenGL ES 3 Reference Card
OpenGL ES 3 Reference Card
The Khronos Group Inc.
 
Concept_of_NAN_IND_INF_DEN_Using_C++
Concept_of_NAN_IND_INF_DEN_Using_C++
Mohammed Nisamudheen
 
Arrays in C++
Arrays in C++
Ilio Catallo
 
L05if
L05if
Kgr Sushmitha
 
Ruby Topic Maps Tutorial (2007-10-10)
Ruby Topic Maps Tutorial (2007-10-10)
Benjamin Bock
 
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]
Chris Adamson
 
Think sharp, write swift
Think sharp, write swift
Pascal Batty
 
C C++ tutorial for beginners- tibacademy.in
C C++ tutorial for beginners- tibacademy.in
TIB Academy
 
Lesson 17. Pattern 9. Mixed arithmetic
Lesson 17. Pattern 9. Mixed arithmetic
PVS-Studio
 
C++ Quick Reference Sheet from Hoomanb.com
C++ Quick Reference Sheet from Hoomanb.com
FrescatiStory
 
Spf Chapter4 Variables
Spf Chapter4 Variables
Hock Leng PUAH
 
DEF CON 23 - Atlas - fun with symboliks
DEF CON 23 - Atlas - fun with symboliks
Felipe Prado
 
Rx: Curing Your Asynchronous Programming Blues | QCon London
Rx: Curing Your Asynchronous Programming Blues | QCon London
Jiby John
 
DISE - Windows Based Application Development in C#
DISE - Windows Based Application Development in C#
Rasan Samarasinghe
 
Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02
CIMAP
 
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)
Benjamin Bock
 
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]
Chris Adamson
 

Similar to 2 1. variables & data types (20)

Mesics lecture 3 c – constants and variables
Mesics lecture 3 c – constants and variables
eShikshak
 
C language (Collected By Dushmanta)
C language (Collected By Dushmanta)
Dushmanta Nath
 
C programming session 01
C programming session 01
AjayBahoriya
 
COM1407: Variables and Data Types
COM1407: Variables and Data Types
Hemantha Kulathilake
 
C the basic concepts
C the basic concepts
Abhinav Vatsa
 
C reference manual
C reference manual
Komal Ahluwalia
 
FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2
rohassanie
 
Data Type in C Programming
Data Type in C Programming
Qazi Shahzad Ali
 
C Programming Assignment
C Programming Assignment
Vijayananda Mohire
 
Datatypes
Datatypes
ZTE Nepal
 
Lecture 2 introduction to Programming languages C.pptx
Lecture 2 introduction to Programming languages C.pptx
muhammadumairsoftwar
 
OOP Chapter 4: Data Type and Expressions
OOP Chapter 4: Data Type and Expressions
Atit Patumvan
 
ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...
ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...
Muhammad Ulhaque
 
structured Programming Unit-2-Basic-Elements-of-C.pptx
structured Programming Unit-2-Basic-Elements-of-C.pptx
SuryaBasnet1
 
Lecture 2
Lecture 2
marvellous2
 
cmp104 lec 8
cmp104 lec 8
kapil078
 
C basics
C basics
Learn By Watch
 
Mesics lecture 5 input – output in ‘c’
Mesics lecture 5 input – output in ‘c’
eShikshak
 
Mesics lecture 5 input – output in ‘c’
Mesics lecture 5 input – output in ‘c’
eShikshak
 
Writing Efficient Code Feb 08
Writing Efficient Code Feb 08
Ganesh Samarthyam
 
Mesics lecture 3 c – constants and variables
Mesics lecture 3 c – constants and variables
eShikshak
 
C language (Collected By Dushmanta)
C language (Collected By Dushmanta)
Dushmanta Nath
 
C programming session 01
C programming session 01
AjayBahoriya
 
COM1407: Variables and Data Types
COM1407: Variables and Data Types
Hemantha Kulathilake
 
C the basic concepts
C the basic concepts
Abhinav Vatsa
 
FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2
rohassanie
 
Data Type in C Programming
Data Type in C Programming
Qazi Shahzad Ali
 
Lecture 2 introduction to Programming languages C.pptx
Lecture 2 introduction to Programming languages C.pptx
muhammadumairsoftwar
 
OOP Chapter 4: Data Type and Expressions
OOP Chapter 4: Data Type and Expressions
Atit Patumvan
 
ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...
ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...
Muhammad Ulhaque
 
structured Programming Unit-2-Basic-Elements-of-C.pptx
structured Programming Unit-2-Basic-Elements-of-C.pptx
SuryaBasnet1
 
cmp104 lec 8
cmp104 lec 8
kapil078
 
Mesics lecture 5 input – output in ‘c’
Mesics lecture 5 input – output in ‘c’
eShikshak
 
Mesics lecture 5 input – output in ‘c’
Mesics lecture 5 input – output in ‘c’
eShikshak
 
Writing Efficient Code Feb 08
Writing Efficient Code Feb 08
Ganesh Samarthyam
 
Ad

More from 웅식 전 (20)

15 3. modulization
15 3. modulization
웅식 전
 
15 2. arguement passing to main
15 2. arguement passing to main
웅식 전
 
13. structure
13. structure
웅식 전
 
12 2. dynamic allocation
12 2. dynamic allocation
웅식 전
 
12 1. multi-dimensional array
12 1. multi-dimensional array
웅식 전
 
11. array & pointer
11. array & pointer
웅식 전
 
10. pointer & function
10. pointer & function
웅식 전
 
7. variable scope rule,-storage_class
7. variable scope rule,-storage_class
웅식 전
 
6. function
6. function
웅식 전
 
5 2. string processing
5 2. string processing
웅식 전
 
5 1. character processing
5 1. character processing
웅식 전
 
15 1. enumeration, typedef
15 1. enumeration, typedef
웅식 전
 
3 2. if statement
3 2. if statement
웅식 전
 
3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib
웅식 전
 
2 3. standard io
2 3. standard io
웅식 전
 
2 2. operators
2 2. operators
웅식 전
 
2 1. variables & data types
2 1. variables & data types
웅식 전
 
15 3. modulization
15 3. modulization
웅식 전
 
15 2. arguement passing to main
15 2. arguement passing to main
웅식 전
 
12 2. dynamic allocation
12 2. dynamic allocation
웅식 전
 
12 1. multi-dimensional array
12 1. multi-dimensional array
웅식 전
 
11. array & pointer
11. array & pointer
웅식 전
 
10. pointer & function
10. pointer & function
웅식 전
 
7. variable scope rule,-storage_class
7. variable scope rule,-storage_class
웅식 전
 
5 2. string processing
5 2. string processing
웅식 전
 
5 1. character processing
5 1. character processing
웅식 전
 
15 1. enumeration, typedef
15 1. enumeration, typedef
웅식 전
 
3 2. if statement
3 2. if statement
웅식 전
 
3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib
웅식 전
 
2 3. standard io
2 3. standard io
웅식 전
 
2 2. operators
2 2. operators
웅식 전
 
2 1. variables & data types
2 1. variables & data types
웅식 전
 
Ad

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