Learn to Code via Tutorials on Repl.it!

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

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

Setup

Make another C repl and add the barebones:

#include <stdio.h>

int main() 
{

    return 0;
}

Theory

There are three main data types:

  • integer
  • double precision
  • character

Ints

An int stores whole numbers.

Examples of ints:

  • 42
  • 7
  • 51

Examples of not-ints:

  • c
  • 134qwf
  • 55.0456
  • 192.168.0.0

Doubles

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
  • @^&

Chars

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

Pratice

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.

NOPE

This (=) is the assignment operator or is

DO NOT SAY EQUALS!

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:

#include <stdio.h>

int main()
{
    int age = 2;
    return 0;
}

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:

#include <stdio.h>
int main()
{
    int age = 2;
    printf("%d", age);
    return 0;
}

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)

int a;
printf("Enter a int: ");
scanf("%d", &a)

Or:

char* name;
printf("What is your name: ");
scanf("%s", &a);

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:

$ ./a.out
What do you want to square? 2
2 squared is 4
$

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

Comments
hotnewtop
EpicGamer007 (1735)

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

CodeLongAndPros (1624)

@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 (1624)

@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 (1624)

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

DynamicSquid (4899)

I thought 8 was a char

CodeLongAndPros (1624)

@DynamicSquid Well, 56 is a char.

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

CodeLongAndPros (1624)

@Highwayman It's easy in d:

cast(type)value

or

parse!type(str)

Highwayman (1481)

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 (1624)

@Highwayman C be like:

char == int

int != char

CodeLongAndPros (1624)

@Highwayman And js:

0 == "0"

"0" == []

[] != 0

Highwayman (1481)

@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 (1481)

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