1. Technology

Variables in C

Hello World

By

This is the second tutorial in the series Online C Tutorial for beginners and is about variables. The Online C Tutorial.

This is a rewriting of the original C tutorials into single page bite size tutorials which can usually be run on either codepad.org or ideone.com.

Note these online services and others are provided free by third party providers so please don't abuse them. I've used them because they save you the hassle of installing a C compiler. You might consider doing that and running them instead because with many users hitting these services the response may get slow.

In this tutorial we'll look at variables in C. These are places to hold values which are usually of these types:

  • int - this holds an integer - usually numbers from -2 billion to 2 billion (-2147483647 to +2147483647)
  • float - this holds a single precision floating point number between 1.175494351 E – 38 and 3.402823466 E + 38
  • double - this holds a double precision floating point number in the range 2.2250738585072014 E – 308 to 1.7976931348623158 E + 308
  • char * - this is a string in c holding text values like "David".
  • enums - this is an int type which a range of values- we'll look at those in a later tutorial.

In this tutorial we'll stick to just int and float variables. Enums will take a tutorial of their own and strings will take several. A double is like a float but holds larger numbers with more decimal places of precision (floats are only good for 6 or 7 digits of precision, doubles up to 15). So feel free to use double instead of float.

Declaring Variables

Before you can use a variable to hold numbers, you must declare them. A declaration is very simple:

int a,b;
int c;
int d =42;

This declares four variables, all of type int. They are called a,b,c and d. You can declare a single variable on a line or multiple ones. You can even declare an initial value. d is set to 42 in the example above.

Where can I declare variables?

There are two places you can declare them. There's actually a third (in another file) but ignore that for now. The places are:

  1. Globally
  2. At the start of a function

Globally means outside of any functions. We'll see both places used in this example below. You can see it on ideone.com with Example 1.

/* Tutorial 2 on variables (c) 2012 David Bolton http://cplus.about.com */

#include <stdio.h>

/* declared globally */
float leftpocket = 10.50;
float rightpocket = 3.75;
float totalwealth;

int main(int argc, char* argv[])
{
    /* day is declared local to main */
    int day = 4;
    day++;
    day += 1;
    day = day + 1;

    totalwealth = leftpocket + rightpocket;

    printf("%d\n\r",day) ; /* should be 7 */
    printf("%f\n\r",totalwealth) ;
    printf("On day %d I had %f in my left pocket and %f in my right pocket\n     \r",day,leftpocket,rightpocket) ;
    printf("On day %d I had $%4.2f in my left pocket and $%4.2f in my right pocket\n
    \r",day,leftpocket,rightpocket) ;
    return 0;
}

Here I've declared three float variables globally: leftpocket, rightpocket and totalwealth, two with initial values. Inside main, I've declared day with an initial value of four.

Remember that when you declare a variable inside a function (we'll comes to those in a future tutorial but main is also a function) you must declare variables before any code that uses them.

The GCC C compilers allow variables to be declared later in a function but strict ANSI-C (and Microsoft Compilers) don't allow it. So it's easiest to remember to declare variables at the start of functions.

Using a Variable

After a variable is declared, it can be used by any code that occurs after the declaration. The three statements after the day declaration all alter day by adding 1. They are just three different ways of adding 1 to day.

The float declarations declare the three variables and assign initial values to two of them. Inside main() the leftpocket and rightpocket values are added and stored in totalwealth.

Finally the four printf statements are used to output the value of the four variables using the %d (for int) and %f (for float) format specifiers. Bookmark this page handling numbers from the old C tutorials as it lists all the different format specifiers. The other pages in that tutorial give a lot more detail to printing numbers.

I copied the last line to show how you can control the formatting of floats. Instead of %f I changed it to $%5.2f in the last line to show the numbers without the trailing 0s that %f provides. 5 is the whole width (4 digits + decimal point) and 2 means 2 digits after the decimal point.

The \n\r at the end of each printf string output a line feed and carriage return so each line is printed on its own line. Without that all four would output on a single line.

The output from this is:

7
14.250000
On day 7 I had 10.500000 in my left pocket and 3.750000 in my right pocket
On day 7 I had $10.50 in my left pocket and $3.75 in my right pocket

In the next tutorial I'll look at enums.

  1. About.com
  2. Technology
  3. C / C++ / C#
  4. C Programming
  5. C Tutorials
  6. Online C Programming Tutorial for Beginners - Lesson Two - Variables

©2014 About.com. All rights reserved.