Learn to Code via Tutorials on Repl.it!

← Back to all posts
C wars: Chapter two: The data wars
CodeLongAndPros (1644)

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:
You know:

  • What #include is
  • How to print to the screen
  • What role semicolons play in your Source code
  • What int main() is

I'm going to teach you how to create data, output it, change it, and store it.

Remember, C code has two parts:

  • Code
  • Data

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:

  • integer
  • double precision
  • character


An int stores whole numbers.

Examples of ints:

  • 42
  • 7
  • 51

Examples of not-ints:

  • c
  • 134qwf
  • 55.0456


A double stores numbers that have a decimal place.

Examples of doubles:

  • 5.12
  • 3.14159265358979323864264338
  • 12.45
  • 1.0

Examples of non-doubles:

  • 3
  • wasdqe
  • 546
  • @^&


There are two types of chars: char and char*

A char holds 1 character
These are wrapped in a single quote (')

Examples of chars:

  • 'a'
  • '/'
  • '@'

Examples of non-chars:

  • hello
  • 8
  • 5423...4.8485

A char* is a lot of chars. They are also called Strings

They are wrapped in double quotes (")

Examples if char*s:

  • "Scott"
  • "Linux"
  • "Hi"
  • "a"

Examples of non-char*s:

  • asdfasdg
  • 2345
  • 1455
  • Kittens


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.


This (=) is the assignment operator or is


Now, with that out of the way:

You declare a variable like this:

type var_name = value;

For example:

  • 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 format strings.

In Chapter One, we had printf("Hello world");.

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:

printf("%d", age);

Your code should now be:

User input

There is a function in stdio.h, scanf

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 printf without \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.
Example interaction:

Part One: The Segfault menace

Part Two: The data wars

Part Three: Revenge of the if

Part Four: A new loop

Part Five: The Empire points back

Part Six: Return of the function

EpicGamer007 (1794)

When will you release Part 3: Revenge of the...? I really enjoy this, it is clear and concise!

CodeLongAndPros (1644)

@AbhayBhat Yeah, should have done that about 3 months ago...

yekyam (0)

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!

CodeLongAndPros (1644)

@yekyam Most times, more precision is good, and most people have more than 1G of ram.
long long long long double is bad though.

CodeLongAndPros (1644)

@yekyam I would still use a float on say, an Arduino, but on my 16G computer? Double.

DynamicSquid (5068)

I thought 8 was a char

CodeLongAndPros (1644)

@DynamicSquid Well, 56 is a char.

Chars are really just bytes. '8' is a char.

CodeLongAndPros (1644)

@Highwayman It's easy in d:




Highwayman (1537)

Lol I meant like the point when one actually starts to realize how fluid data types are in C/C++ but I will say the first one seems not bad.. @CodeLongAndPros

CodeLongAndPros (1644)

@Highwayman C be like:

char == int

int != char

CodeLongAndPros (1644)

@Highwayman And js:

0 == "0"

"0" == []

[] != 0

Highwayman (1537)

@CodeLongAndPros hm I’m sensing hypocrisy within myself cause I honestly hate js implicitly casting all over the place then again I don’t really understand it so I guess there’s that...

Highwayman (1537)

@DynamicSquid Yessss replace it all with WASM! Which is even worse! Yayyy!!!!