03 - Variables, Data Types & Placeholders

A variable in programming is an alias to store information. In C, the type of information a variable stores must be defined. In Python, you could just create a variable with a name and then later set it to be a number or string as you see fit. In C, you must declare a variable to have only one specific data type. Below are the most common data types that we will use. Here is a link for further reading about all of the possible data types in the C Programming Language: link.

To setup a variable, you must first define/declare it. The following code defines a variable of the type "integer" with the name "num".

int num;

Then you can assign it a value. The following code assigns the "num" variable to have a value of 5.

num = 5;

Instead of two separate statements, you can define/declare the variable and assign variable at the same time.

int num = 5;

You can only define a variable once. The following code will have an error because you are defining it twice. When you try to create a new "num" variable, the compiler will tell you something akin to: "redefinition of num. Previous definition ..."

int num = 3;

int num = 5; // ERROR!

The correct way to change the value of the "num" variable from 3 to 5 is as follows.

int num = 3;

num = 5;

The following code demonstrates creating and assigning a few variables of various data types. Note that the program does not print anything to the console window - It merely demonstrates how to create new variables and assign them values.

Scope of Variables in C (e.g. Global vs Local)

As opposed to Python, which is largely scope-less, variables in C have very specific regions of code for which they exist. A general rule you can use is, a variable you create exists only until the next closing bracket '}'

Examples of different scopes:

Entire File - A variable created outside of the "int main (void) { }" function has scope of the entire file, like a "global" variable. It can be referenced from any function in the entire file you are writing code in.

Main Function - A variable created inside the "int main (void) { }" function only exists inside this function, and could not be referenced from other functions in your code.

If/Else - A variable created inside an if statement only exists until that if statement is over.

Loop - A variable created inside a loop only exists until the loop ends

Functions - A variable created as an input to the function, or in the body of the function, only exists in the function.

Placeholders Inside "printf"

Place holders are used inside the "printf" command to represent where values/variables should be inserted into sentences. Here is a simple example:

printf("This is the number %d", 6);

The code above prints "This is the number 6". The %d is saying, I will insert an "integer" here. Then after the sentence ends, using comma and respective order, you list the values and/or variables to be inserted. In this case, there is a single %d representing a single integer - so when the sentence ends, there is a single comma and the value "6".

These place holders are used in C programming instead of commas or plus signs putting together chunks of text like in Python. The below codes are equivalent but in different languages.

printf("This is the number %d", 6); // C Programming

print("This is the number", 6) # Python

print("This is the number " + str(6)) # Python

There are many types of placeholders that are specific to each different data type. Below is a table of the most common place holders we will use. Here is a link for further reading on all of the possible place holders in C: Link

The following code defines/declare three variables of different data types. It assigns them each a value. It then uses the appropriate placeholders in the "printf" statement to display the values in combination with a string (sentence).

Activity 1

Create 4 variables of different data types and print them each on a separate line of code. You can use this resource as a guide for the placeholders: Link

Solution - Link (with examples of formatting decimals)

Activity 2

Print the "My favorite number is <your favorite number>". Use an integer placeholder for your favorite number.

Print my "My favorite color is <your favorite color>". Use a string placeholder for your favorite color.

Solution - Link (with string variable example)

Activity 3

Modify the printf command from Activity 2 to use only a single printf command on a single line of code, yet be displayed on two separate lines in the console window.

See solution above

Formatting Data Types

The "printf" function has a lot of control over the format in which your data types are displayed. Here is a link to read more about this in detail - Link.

The general syntax for formatting a placeholder in a printf statement is as follows:

%[flags][width][.precision][length]type

In simple terms, these mean:

Here are some simple examples:

To read more about this, check out this great explanation - Link

Common Mistakes

1. Mixing up numerical data types - If you declare/create a number of one data type and then call upon it as though it were another data type you get a seemingly random number. This is because the way in which integers and decimals are stored in memory is very different. Examples below:

For reference, here is a table of the common ASCII values - Link. This is what you see when you print a character as an int. This is the value that a computer stores for each single character in 1's and 0's. It can be stored as a decimal, as an octal (base-8 integer) or as a hexadecimal (base-16 integer which uses letter a-f or A-F as the numbers 10 through 15. This is common in HTML and many other formats - stores bigger values with less characters).