Lecture 03 Primitive Datatypes

Joseph Haugh

University of New Mexico

Reading

  • Secs. 2.3-2.4 (pp. 60-65)
  • Online Appendix E (On number systems)
  • On the slides (may be found on C manual)
  • Integer datatypes

Programming in C

/* File: simple1.c */
/* Author: Joseph Haugh */

/* Forward slash and asterisk to open/close comments */

#include <stdio.h>

int main() {
    printf("Hello world!\n");
    printf("How are you today?\n");
    return 0;
}
$ gcc simple1.c
$ ./a.out
Hello world!
How are you today?
  • Simple program
  • 2 lines of output

Programming in C

/* File: simple1.c */
/* Author: Joseph Haugh */

/* Forward slash and asterisk to open/close comments */

#include <stdio.h>

int main() {
    printf("Hello world!\n");
    printf("How are you today?\n");
    return 0;
}
$ gcc simple1.c
$ ./a.out
Hello world!
How are you today?
  • Simple program
  • 2 lines of output
  • To understand C we must learn about the:
    • Reserved words
    • Operators

Reserved Words in C

auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while

All lowercase, these words cannot be used for anything else

Reserved Words

/* File: simple1.c */
/* Author: Joseph Haugh */

/* Forward slash and asterisk to open/close comments */

#include <stdio.h>

int main() {
    printf("Hello world!\n");
    printf("How are you today?\n");
    return 0;
}
$ gcc simple1.c
$ ./a.out
Hello world!
How are you today?
  • Which reserved words do you see?
  • Reserved words have special meanings
  • All programs are built-up by combining reserved words and operators

Primitive Data Types

/* File: simple1.c */
/* Author: Joseph Haugh */

/* Forward slash and asterisk to open/close comments */

#include <stdio.h>

int main() {
    printf("Hello world!\n");
    printf("How are you today?\n");
    return 0;
}
$ gcc simple1.c
$ ./a.out
Hello world!
How are you today?
  • We are often interested in data
  • We start with primitive data types
  • These are built-into C using the reserved words
  • An example in this program: int

Primitive Data Types

auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while

Reserved words relating to primitive data types in C

Primitive Data Types

  • Integers: ℤ = {…, −3, −2, −1, 0, 1, 2, 3, …}

  • Natural Numbers: ℕ = {0, 1, 2, 3, …}

  • Real Numbers: $$ \mathbb{R} = \{ \dots, -3.4, 0, \frac{7}{5}, e, \pi, 572.5683, \dots \} $$

  • Characters: {A, B, C, 1, 2, 3, @, #, … (all keyboard symbols)}

  • Booleans: {true, false}

  • Pointers:

How do we represent numbers?

  • Consider the number, or value, 3,682
  • How can we “write down” this value?
  • Ordinary representation:
    • 3,685
    • 3685

How do we represent numbers?

  • Consider the number, or value, 3,682
  • How can we “write down” this value?
  • Ordinary representation:
    • 3,685 (no commas in numbers allowed in C)
    • 3685
  • Roman Numerals: MMMDCLXXXV

How do we represent numbers?

  • Consider the number, or value, 3,682
  • How can we “write down” this value?
  • Ordinary representation:
    • 3,685 (no commas in numbers allowed in C)
    • 3685
  • Roman Numerals: MMMDCLXXXV
  • Scientific Notation:
    • 3.682 * 103
    • 3.682E3
  • All the same value, but different representations

Number Systems

Read: Appendix C

Number Systems

  • Roman (Additive)
    • I, V, X, L, C, D, M
  • Modern, aka Hindu-Arabic (Positional)
    • 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

Number Systems

  • Roman (Additive)
    • I, V, X, L, C, D, M
    • shape of the symbol indicates its value
  • Modern, aka Hindu-Arabic (Positional)
    • 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
    • position of the symbol indicates its value
      • 246: two hundreds, four tens, and six ones
      • 462: four hundreds, six tens, and two ones
      • 402: four hundreds, and two ones
  • Four hundred and two
    • Roman: C C C C I I (don’t need explicit 0)
    • Modern: 402 (need explicit 0)

One Kind of “Zero” in C

auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while

Decimal vs Binary

  • Modern, aka Hindu-Arabic (Positional)
    • 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
    • position of the symbol indicates its value
      • 246: two hundreds, four tens, and six ones
      • 462: four hundreds, six tens, and two ones
      • 402: four hundreds, and two ones
  • Computers are electronic
  • All data is represented with:
    • 0’s
    • 1’s
  • Must use binary, base 2 representation
  • Any value can be represented in different bases

Decimal vs Binary

  • 46210 = 1110011102

Decimal - Base 10

  • 3,642 in base 10 positional notation is:

$$ \begin{aligned} 3 \times 10^3 &= 3 \times 1,000 = 3,000 \\ + 6 \times 10^2 &= 6 \times 100 = 600 \\ + 4 \times 10^1 &= 4 \times 10 = 40 \\ + 2 \times 10^0 &= 2 \times 1 = 2 \\ &= 3,642_{10} \end{aligned} $$

Binary - Base 2

  • 11010 in base 2 positional notation is:

$$ \begin{aligned} 1 \times 2^4 &= 1 \times 16 = 16 \\ + 1 \times 2^3 &= 1 \times 8 = 8 \\ + 0 \times 2^2 &= 0 \times 4 = 0 \\ + 1 \times 2^1 &= 1 \times 2 = 2 \\ + 0 \times 2^0 &= 0 \times 1 = 0 \\ &= 26_{10} \end{aligned} $$

Hexadecimal - Base 16

  • 642 in base 16 positional notation is:

$$ \begin{aligned} 6 \times 16^2 &= 6 \times 256 = 1536 \\ + 4 \times 16^1 &= 4 \times 16 = 64 \\ + 2 \times 16^0 &= 2 \times 1 = 2 \\ &= 1602_{10} \end{aligned} $$

Binary and Hexadecimal

1100110102 = 41010

$$ \begin{aligned} 1 \times 2^8 &= 1 \times 256 = 256 \\ + 1 \times 2^7 &= 1 \times 128 = 128 \\ + 0 \times 2^6 &= 0 \times 64 = 0 \\ + 0 \times 2^5 &= 0 \times 32 = 0 \\ + 1 \times 2^4 &= 1 \times 16 = 16 \\ + 1 \times 2^3 &= 1 \times 8 = 8 \\ + 0 \times 2^2 &= 0 \times 4 = 0 \\ + 1 \times 2^1 &= 1 \times 2 = 2 \\ + 0 \times 2^0 &= 0 \times 1 = 0 \\ &= 410_{10} \end{aligned} $$

Binary numbers require many bits

Binary and Hexadecimal

1100110102 = 41010

$$ \begin{aligned} 1 \times 2^8 &= 1 \times 256 = 256 \\ + 1 \times 2^7 &= 1 \times 128 = 128 \\ + 0 \times 2^6 &= 0 \times 64 = 0 \\ + 0 \times 2^5 &= 0 \times 32 = 0 \\ + 1 \times 2^4 &= 1 \times 16 = 16 \\ + 1 \times 2^3 &= 1 \times 8 = 8 \\ + 0 \times 2^2 &= 0 \times 4 = 0 \\ + 1 \times 2^1 &= 1 \times 2 = 2 \\ + 0 \times 2^0 &= 0 \times 1 = 0 \\ &= 410_{10} \end{aligned} $$

Binary numbers require many bits

19A16 = 41010

$$ \begin{aligned} 1 \times 16^2 &= 1 \times 256 = 256 \\ + 9 \times 16^1 &= 9 \times 16 = 144 \\ + A \times 16^0 &= 10 \times 1 = 10 \\ &= 410_{10} \end{aligned} $$

Hexadecimal is more compact

Easy to convert from binary to hex and vice versa

Ints

Read: Appendix C

Creating Three Int Variables

/* File: 03_int.c */
/* Joseph Haugh */

#include <stdio.h>

int main() {
    int x = 12; /* declare variable */

    int someNumber = 45; /* declare variable */

    int foo_123 = 27455; /* no commas */

    printf("The value of x is %d\nsomeNumber = %d\n"
           "and foo is bigger\nwith value %d\n",
           x, someNumber, foo_123);
}
  • Create three integer variables
  • Print the value of the variables
$ gcc 03_int.c
$ ./a.out
The value of x is 12
someNumber = 45
and foo is bigger
with value 27455

Creating Three Int Variables

/* File: 03_int.c */
/* Joseph Haugh */

#include <stdio.h>

int main() {
    int x = 12; /* declare variable */

    int someNumber = 45; /* declare variable */

    int foo_123 = 27455; /* no commas */

    printf("The value of x is %d\nsomeNumber = %d\n"
           "and foo is bigger\nwith value %d\n",
           x, someNumber, foo_123);
}
  • printf begins with a formatting string (inside double-quotes)
  • this string spills over 2 lines
  • %d indicates a decimal value is to be printed
$ gcc 03_int.c
$ ./a.out
The value of x is 12
someNumber = 45
and foo is bigger
with value 27455

Creating Three Int Variables

/* File: 03_int.c */
/* Joseph Haugh */

#include <stdio.h>

int main() {
    int x = 12; /* declare variable */

    int someNumber = 45; /* declare variable */

    int foo_123 = 27455; /* no commas */

    printf("The value of x is %d\nsomeNumber = %d\n"
           "and foo is bigger\nwith value %d\n",
           x, someNumber, foo_123);
}
  • this string spills over 2 lines
  • %d indicates a decimal value is to be printed
  • For each %d in the printf, we list the corresponding value
$ gcc 03_int.c
$ ./a.out
The value of x is 12
someNumber = 45
and foo is bigger
with value 27455

Creating Three Int Variables

/* File: 03_int.c */
/* Joseph Haugh */

#include <stdio.h>

int main() {
    int x = 12; /* declare variable */

    int someNumber = 45; /* declare variable */

    int foo_123 = 27455; /* no commas */

    printf("The value of x is %d\nsomeNumber = %d\n"
           "and foo is bigger\nwith value %d\n",
           x, someNumber, foo_123);
}
  • Every data element (aka variable) has:
    • Name or identifier
      • what do we call the variable, how do we “refer” to it?
    • Address or location
      • where is this data located in the computer memory?

Creating Three Int Variables

/* File: 03_int.c */
/* Joseph Haugh */

#include <stdio.h>

int main() {
    int x = 12; /* declare variable */

    int someNumber = 45; /* declare variable */

    int foo_123 = 27455; /* no commas */

    printf("The value of x is %d\nsomeNumber = %d\n"
           "and foo is bigger\nwith value %d\n",
           x, someNumber, foo_123);
}
  • Every data element (aka variable) has:
    • Type
      • what type of data is this (int, char, etc). How much memory does it take? ie., size?
    • Value
      • what is the value of the data?

Graphical Representation

/* File: 03_int.c */
/* Joseph Haugh */

#include <stdio.h>

int main() {
    int x = 12; /* declare variable */

    int someNumber = 45; /* declare variable */

    int foo_123 = 27455; /* no commas */

    printf("The value of x is %d\nsomeNumber = %d\n"
           "and foo is bigger\nwith value %d\n",
           x, someNumber, foo_123);
}
  • It will often be useful to “draw” our variables

x

12

someNumber

45

foo_123

27455

Graphical Representation

/* File: 03_int.c */
/* Joseph Haugh */

#include <stdio.h>

int main() {
    int x = 12; /* declare variable */

    int someNumber = 45; /* declare variable */

    int foo_123 = 27455; /* no commas */

    printf("The value of x is %d\nsomeNumber = %d\n"
           "and foo is bigger\nwith value %d\n",
           x, someNumber, foo_123);
}
  • Every data element (aka variable, aka object) has:
    • Name or identifier
    • Address or location
    • Type
    • Value

x

12

someNumber

45

foo_123

27455

Identifiers are made from letters, numbers and _

Cannot start with numbers

Graphical Representation

/* File: 03_int.c */
/* Joseph Haugh */

#include <stdio.h>

int main() {
    int x = 12; /* declare variable */

    int someNumber = 45; /* declare variable */

    int foo_123 = 27455; /* no commas */

    printf("The value of x is %d\nsomeNumber = %d\n"
           "and foo is bigger\nwith value %d\n",
           x, someNumber, foo_123);
}
  • Every data element (aka variable, aka object) has:
    • Name or identifier
    • Address or location
    • Type
    • Value

x

12

someNumber

45

foo_123

27455

Where exactly are these variable stored on the computer??

You will start to learn where in this class.

Graphical Representation

/* File: 03_int.c */
/* Joseph Haugh */

#include <stdio.h>

int main() {
    int x = 12; /* declare variable */

    int someNumber = 45; /* declare variable */

    int foo_123 = 27455; /* no commas */

    printf("The value of x is %d\nsomeNumber = %d\n"
           "and foo is bigger\nwith value %d\n",
           x, someNumber, foo_123);
}
  • Every data element (aka variable, aka object) has:
    • Name or identifier
    • Address or location
    • Type
    • Value

x

12

someNumber

45

foo_123

27455

All three variables are of int

Graphical Representation

/* File: 03_int.c */
/* Joseph Haugh */

#include <stdio.h>

int main() {
    int x = 12; /* declare variable */

    int someNumber = 45; /* declare variable */

    int foo_123 = 27455; /* no commas */

    printf("The value of x is %d\nsomeNumber = %d\n"
           "and foo is bigger\nwith value %d\n",
           x, someNumber, foo_123);
}
  • Every data element (aka variable, aka object) has:
    • Name or identifier
    • Address or location
    • Type
    • Value

x

12

someNumber

45

foo_123

27455

The value is the actual data stored inside the computer memory.