Practical Application for C Programming: Data Types & Variables

Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

In this practical lesson, you will write code. You will develop C code that declares, manipulates, and displays data using various data types in C. You will write, compile, and test your code.

Lesson Overview & Knowledge Required

In order to successfully complete this lesson, you should be able to define the various data types that are used in C: int, float, char. A knowledge of arithmetic operators is also important, including modulus (%) division.

Program Code

First, start a new C program and create variables. Not all of them will be given a value since that can be set later. For example, you often use i and j as temporary counter variables (of local scope instead of global scope), so those tend to be declared right away at the top of the function or subroutine.

Enter the code below, noting that we are creating variables of type int (integer), float, and char (character).

#include <stdio.h>
int main() {
 int i;
 int j;
 //other integers
 int high_score = 9000;
 int low_score = 250;
 float exchange_rate = 3.39843;
 float dollars = 100.25;
 float result;
 //character value
 char menu_option;
 return 0;

We now have a decent set of variables to work with. Next, we will do some math and display output.

Output and Arithmetic

When using the printf function in C, you will use special format specifiers to tell the program how to display the variables involved. These specifiers are enclosed in quotes, and followed by the variables to fill them with. The following table displays the modifiers.

Specifier Description
%d Integer
%f Float
%s String of characters
%lu Unsigned integer (int or long)
%% Print the % character

Next, we will multiply the exchange rate by the dollar amount and then display the results:

result = exchange_rate * dollars;
printf("Result = %f", result);

The output should look as follows:

Result =340.692627

You can further refine how the program will print your number, by tweaking the format specifier and adding a .2 in front which means to display it out to 2 decimal places:

printf("Result = %.2f", result);

This will display:

Result = 340.69

Code Application

Now it's your turn to write code! Given the variables we have created so far, complete the following:

  1. Create a new variable. It will be an integer score that has a value between the high and low scores (you can call it med_score for example)
  2. Next add all the scores together and display the result
  3. Then multiply all the scores and display the result. Uh-oh. What happened? How can you fix it?

Follow-Up Questions

Now that you have created new variables and performed some arithmetic, answer the following questions.

Question 1: Cast and Int to a Float

In the variables created in the Program Code, divide the dollars by the high_score variable. Be sure to type cast the integer value to a float. If you don't cast to float, you will lose data. An integer cannot handle decimal places so you will lose that information.

What is the result? What do you think would happen if you cast a float variable to an int?

Question 2: Modulo

For this exercise, perform modulus division on two integer variables and print the result. Try two operations; one in which the division operation has a remainder, and one without. What does the result mean?

Question 3: How Many Bytes Do the Variables Take Up?

Write code to display the bytes that each variable consumes. Be sure to include the char, int, float, unsigned long, and double variable types.

Answer Key

Following are the answers to the code application and the follow-up questions.

Code Application

Medium-Range Variable

A new score variable could be created as follows:

int med_score = 1500;

Add All Scores

It is good practice to create a new variable to hold the result. The following code uses the variable called result and then displays it on screen.

#include <stdio.h>
int main() {
 int high_score = 9000;
 int low_score = 250;
 int med_score = 1500;
 int result;
 result = (high_score + med_score + low_score);
 printf("Result = %d", result);
 return 0;

The result is:

Result = 10750

Multiply Scores

Following is the code to multiply scores.

result = (high_score * med_score * low_score);

Unfortunately, when the program ran, the result in my C compiler environment was:

Result = -919967296


This is because we exceeded the limits of the int data type by multiplying everything together. The result should be 3,375,000,000. You have a couple of options here:

  • Change everything to double to account for any large numbers
  • Change variables to unsigned long. These are positive integers only (a score can't be negative) and the unsigned long can hold up to 4,294,967,295

The code below shows both options. Note the format specifier for unsigned long!

To unlock this lesson you must be a Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use

Become a member and start learning now.
Become a Member  Back
What teachers are saying about
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 160 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create An Account