Joseph Haugh
University of New Mexico
/* 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?
/* 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?
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
/* 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?
/* 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?
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
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:
Read: Appendix 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 |
$$ \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} $$
$$ \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} $$
$$ \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} $$
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
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
Read: Appendix C
/* 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);
}
$ gcc 03_int.c
$ ./a.out
The value of x is 12
someNumber = 45
and foo is bigger
with value 27455
/* 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);
}
$ gcc 03_int.c
$ ./a.out
The value of x is 12
someNumber = 45
and foo is bigger
with value 27455
/* 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);
}
$ gcc 03_int.c
$ ./a.out
The value of x is 12
someNumber = 45
and foo is bigger
with value 27455
/* 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);
}
/* 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);
}
/* 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);
}
x
12
someNumber
45
foo_123
27455
/* 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);
}
x
12
someNumber
45
foo_123
27455
Identifiers are made from letters, numbers and _
Cannot start with numbers
/* 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);
}
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.
/* 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);
}
x
12
someNumber
45
foo_123
27455
All three variables are of int
/* 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);
}
x
12
someNumber
45
foo_123
27455
The value is the actual data stored inside the computer memory.