Sample Video Frame
Exercise 7: Variables and Types
You should be getting a grasp of how a simple C program is structured, so let's do the next simplest thing and make some variables of different types:
#include <stdio.h>
int main(int argc, char *argv[])
{
int distance = 100;
float power = 2.345f;
double super_power = 56789.4532;
char initial = 'A';
char first_name[] = "Zed";
char last_name[] = "Shaw";
printf("You are %d miles away.\n", distance);
printf("You have %f levels of power.\n", power);
printf("You have %f awesome super powers.\n", super_power);
printf("I have an initial %c.\n", initial);
printf("I have a first name %s.\n", first_name);
printf("I have a last name %s.\n", last_name);
printf("My whole name is %s %c. %s.\n",
first_name, initial, last_name);
int bugs = 100;
double bug_rate = 1.2;
printf("You have %d bugs at the imaginary rate of %f.\n",
bugs, bug_rate);
long universe_of_defects = 1L * 1024L * 1024L * 1024L;
printf("The entire universe has %ld bugs.\n", universe_of_defects);
double expected_bugs = bugs * bug_rate;
printf("You are expected to have %f bugs.\n", expected_bugs);
double part_of_universe = expected_bugs / universe_of_defects;
printf("That is only a %e portion of the universe.\n",
part_of_universe);
// this makes no sense, just a demo of something weird
char nul_byte = '\0';
int care_percentage = bugs * nul_byte;
printf("Which means you should care %d%%.\n", care_percentage);
return 0;
}
In this program, we're declaring variables of different types and then printing them using different printf
format strings. I can break it down as follows:
- ex7.c:1-4: The usual start of a C program.
- ex7.c:5-7: Declare an
int
,float
, anddouble
for some fake calculations. - ex7.c:8-10: Declare some character data. First a single character, then two character array strings.
- ex7.c:12-19: Use
printf
to print out each of these declared variables. - ex7.c:21-22: Declare an integer and a double variable to show you don't have to declare variables at the top of a function.
- ex7.c:24-25: Print those variables using
printf
again. - ex7.c:27-28: Calculate a really large number using a
long
integer type. Notice the use ofL
notation to specify long integer constants (1L, 1024L). - ex7.c:30-35: Print out some calculations and math using the variables we have.
- ex7.c:37-40: This is actually not good form, but demonstrate that C will let you use
char
variables in math expressions by multiplying achar
by anint
, then print it. - ex7.c:42-43: The end of the
main
function.
This source file demonstrates how some math works with different types of variables. At the end of the program, it also demonstrates something you see in C but not in many other languages. To C, a character is just an integer. It's a really small integer, but that's all it is. This means you can do math on them, and a lot of software does just that, -for good or bad.
This last bit is your first glance at how C gives you direct access to the machine. We'll be exploring that more in later exercises.
What You Should See
As usual, here's what you should see for the output:
View Source file ex7.sh-session Only
$ make ex7
cc -Wall -g ex7.c -o ex7
$ ./ex7
You are 100 miles away.
You have 2.345000 levels of power.
You have 56789.453200 awesome super powers.
I have an initial A.
I have a first name Zed.
I have a last name Shaw.
My whole name is Zed A. Shaw.
You have 100 bugs at the imaginary rate of 1.200000.
The entire universe has 1073741824 bugs.
You are expected to have 120.000000 bugs.
That is only a 1.117587e-07 portion of the universe.
Which means you should care 0%.
How to Break It
Again, go through this and try to break the printf
by passing in the wrong arguments. See what happens if you try to print out the nul_byte
variable along with %s
versus %c
. When you break it, run it under the debugger to see what it says about what you did.
Extra Credit
- Make the number you assign to
universe_of_defects
various sizes until you get a warning from the compiler. - What do these really huge numbers actually print out?
- Change
long
tounsigned long
and try to find the number that makes it too big. - Go search online to find out what
unsigned
does. - Try to explain to yourself (before I do in the next exercise) why you can multiply a
char
and anint
.
Register for Learn C the Hard Way
Register today for the course and get the all currently available videos and lessons, plus all future modules for no extra charge.