Introduction to Programming in C

Index   |   Using VC++
 
Introduction
Basic Output
3 Variables
Conditionals
Loops
Example 1
More on Conditionals
Further Exploration
Algorithm Design
10 Arrays
 
< Previous

Variables and Calculations

Next >
  • Create a project called Math.
  • Create a source file called main.c.
  • Type in (or copy/paste) the following code:
  • #include <stdio.h>
    
    int main()
    {
    	int a;
    	int b;
    
    	a = 6;
    	printf("The value of a is %d.\n", a);
    
    	b = a + 3;
    	printf("The value of b is %d.\n", b);
    
    	return 0;
    }
    
  • Save your file.
  • Build your program (F7).
  • Check the Output window for errors.
  • Run your program (Ctrl-F5).

Explanations

  • "int a;" is a variable declaration, which lets the compiler know that our program wants memory to store an integer, and will refer to that memory with the name "a".
  • "a = 6;" is a variable initialization; read this as "a gets 6" -- it's best not to think of "=" as "equals" since you cannot reverse the statement. ("6 gets a" would not make sense, either to you or the compiler!)
  • There is a new special character in this example of printf: %. The % character tells printf to place the value of a variable at this location. The character after the % indicates the type of value, in this case d for a decimal (base 10) integer. The variable to use is passed as another parameter, after the string.
  • The basic arithmetic operators are
    + addition
    -subtraction
    *multiplication
    /division

Definitions

Variable
A named location where a value can be stored (and modified).
Variable Declaration
A statement that lets the compiler know what type of variable we need (and thus how much memory) and what we want to call it.
Variable Initialization
Giving a value to a variable. There is no default value assigned when a variable is declared.

Exercises

Practice with calculations and output:

  • Change the initial value for a.
  • Change what's added to b.
  • Try things like
    b = a * 5;
    or
    b = 2 + a * 5;
    You can use parentheses for more complex operations, just as in math.
  • Make the output appear all on one line, e.g.
    The value of a is 6 and the value of b is 9.
    (Hint: Why are you getting two lines? It's not because there are two printf statements.)

Changes

Make the following changes to your program:

  • Initialize a with the value 7 and change the line setting b to "b = a / 2;" What do you expect to happen? Build, check for errors, then run your program. You will see that the result is 3, not 3.5! Since we are using integers, there are no fractional values. The rule for division using integers is that the result is truncated, not rounded.
  • You can output multiple variables in one printf statement by specifying %d in each location that you want a value. You will need to pass as many variables as you have %d in the string, e.g.
    printf("a is %d and b is %d.\n", a, b);

Exercises

  • Experiment with division to get a good understanding of how truncation works. Try things like 1 / 3, 4 / 3, 16 / 5, and 19 / 5.

When you are done working with this project, select File/Close Solution.

Summary

  • You need to declare variables before you can use them.
  • Use operators + - * and / to perform mathematical operations.
  • Division (/) of integers will truncate (not round) the result.
  • printf uses "%d" to output an integer variable.
< Previous Next >