C wars: Chapter two: The data wars
Chapter two: The Data wars
Hello again, learners.
This is your teacher, the Sourcerer. I trust that you have finshed your homework...
If you forgot, or are joining for the first time, you need to read Chapter one: The segfault menace
Your lesson today is about
data, and how to store it.
First, let's review:
- How to print to the screen
- What role semicolons play in your Source code
I'm going to teach you how to create data, output it, change it, and store it.
Remember, C code has two parts:
In Chapter one, we looked at how C programs are formed. Now, today, you get to wield those powers today.
Today, we will:
- Learn how to make variables
- Learn the basics of format string
- Learn how to get user input
Make another C repl and add the barebones:
There are three main data types:
int stores whole numbers.
Examples of ints:
Examples of not-ints:
double stores numbers that have a decimal place.
Examples of doubles:
Examples of non-doubles:
There are two types of chars:
A char holds 1 character
These are wrapped in a single quote (
Examples of chars:
Examples of non-chars:
char* is a lot of chars. They are also called Strings
They are wrapped in double quotes (
Examples if char*s:
Examples of non-char*s:
Now, let's make some data!
But, first I want to ask you something:
What is this:
Most of you say it is the equals sign.
=) is the
assignment operator or
DO NOT SAY EQUALS!
Now, with that out of the way:
You declare a variable like this:
type var_name = value;
int a = 42;
double b = 3.14159;
char inital = 's'
char* name = "R. Daneel Olivaw"
Let's rework our C repl.
Please make a var,
age that holds the age of your longest-lived pet. (If you have no pet, use your phone. If you don't have a phone, use your repl.it account)
Done? Good. Here's what that should look like:
Back to the theroy
First, before we print out our age, we need to talk about
In Chapter One, we had
The first argument,
"Hello world" is the format string.
"Wait!" You say. "You told us that there was no formatting!"
Well, I had to keep some stuff secret. I mean, I have to keep this going, right?
Well, format strings are normal
char*s except they stole the % char.
You can use this to print out variables, provided after the format string:
Types of stolen %'s:
%d: Prints out a digit (int)
%f: Prints out a double
%c: Prints out a char
%s: Prints out a string (char*)
You can do stuff like:
"My name is: %s"
"I am %d years old"
So, to print out your var,
age, you would do:
Your code should now be:
There is a function in
It's a very complex one, but all you need to know is that, to get a var, you do this:
scanf("%c, &var_name) (You replace the %c with the format string type of var_name)
For example: (The are snippets and will not work by copy-and-paste)
Note: You can use
\n to avoid printing a new line.
More in Chapter Three.
Your mission, if you choose to accept (and you will):
Make a program that asks for a int, x, and prints out the value of x^2.
When will you release
Part 3: Revenge of the...? I really enjoy this, it is clear and concise!
Quick note: Generally, it's a better practice to use floats instead of doubles. Doubles are rarely used and are generally only used for super scientific programs. Nine times out of ten, floats will work just fine with a fraction of the memory cost. Great tutorial though!