C wars: Chapter two: The data wars
h
CodeLongAndPros (1632)

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

You are viewing a single comment. View All
EpicGamer007 (1755)

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

CodeLongAndPros (1632)

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