Home arrow C++ arrow Page 3 - First Steps in (C) Programming, introduction

First Steps in (C) Programming, introduction

If you're a beginning programmer and want to get more deeply into programming with variables, you've come to the right place. This article, the first of three parts, is excerpted from chapter two of the book Beginning C, written by Ivor Horton (Apress, 2004; ISBN: 1590592530).

Author Info:
By: Apress Publishing
Rating: 3 stars3 stars3 stars3 stars3 stars / 10
November 17, 2005
  1. · First Steps in (C) Programming, introduction
  2. · What Is a Variable?
  3. · Variables That Store Numbers
  4. · Try It Out: Using More Variables
  5. · Naming Variables
  6. · Initializing Variables
  7. · Basic Arithmetic Operations
  8. · Try It Out: Division and the Modulus Operator

print this article

First Steps in (C) Programming, introduction - Variables That Store Numbers
(Page 3 of 8 )

There are several different types of variables, and each type of variable is used for storing a particular kind of data. Youíll start by looking at variables that you can use to store numbers. There are actually several ways in which you can store numbers in your program, so letís start with the simplest.

Integer Variables

Letís look first at variables that store integers. An integer is any whole number without a decimal point. Examples of integers are as follows:


Youíll recognize these values as integers, but what Iíve written here isnít quite correct so far as your program is concerned. You canít include commas in an integer, so the second value would actually be written in a program as 10999000000.

Here are some examples of numbers that are not integers:


Normally, 2.0 would be described as an integer because itís a whole number, but as far as your computer is concerned it isnít because it contains a decimal point. For your program, you must write the integer 2 as 2 with no decimal point. Integers are always written in a C program without a decimal point; if thereís a decimal point, it isnít recognized an integer. Before I discuss variables in more detail (and believe me, thereís a lot more detail!), letís look at a simple variable in action in a program, just so you can get a feel for how theyíre used.


Try It Out: Using a Variable

Letís go back to your salary. You can try writing the previous program using a variable:

/* Program 2.2 Using a variable */
#include <stdio.h>
void main()
  int salary;     /* Declare a variable called salary */
  salary = 10000; /* A simple arithmetic assignment statement */
  printf(ďMy salary is %d.Ē, salary);

Type in this example and compile, link, and execute it. Youíll get the following output:

My salary is 10000.


The first three lines are exactly the same as in all the previous programs. Letís look at the new stuff.

The statement that identifies the memory that youíre using to store your salary is as follows:

  int salary;     /* Declare a variable called salary   */

This is called a variable declaration because it declares the name of the variable. The name, in this program, issalary.

Notice that the variable declaration ends with a semicolon. If you omit the semicolon, your program will generate an error when you compile it.

The variable declaration also specifies the type of data that the variable will store. Youíve used the keywordintto specify that the variable,salary, will be used to store an integer value. The keywordintprecedes the name of the variable. As youíll see later, declarations for variables that store other kinds of data consist of another keyword specifying a data type followed by a variable name in a similar manner.

Remember, keywords are special C words that mean something specific to the compiler. You must not use them as variable names or your compiler will get confused.

The next statement is

salary = 10000;

This is a simple arithmetic assignment statement. It takes the value to the right of the equal sign and stores it in the variable on the left of the equal sign.

Here youíre declaring that the variablesalarywill have the value10000. Youíre storing the value on the right (10000) in the variable on the left (salary). The=symbol is called the assignment operator because it assigns the value on the right to the variable on the left.

You then have the familiarprintf()statement, but itís a little different from how youíve seen it in action before:

  printf("My salary is %d.", salary);

There are now two arguments inside the parentheses, separated by a comma. An argument is an item of data thatís passed to a function. In this program statement, the two arguments to theprintf()function are as follows:

  1. Argument 1 is a control string, so called because it controls how the output specified by the following argument (or arguments) is to be presented. This is the character string between the double quotes.
  2. Argument 2 is the variablesalary. How this variable will be displayed is determined by the first argumentóthe control string.

The control string is fairly similar to the previous example in that it contains some text to be displayed. However, if you look carefully, youíll see%dembedded in it. This is called a conversion specifier for the variable.

Conversion specifiers always start with a%character. Because a%in a control string always indicates the start of a conversion specifier, if you want to output a%character you must use the sequence%%.

Conversion specifiers determine how variables are displayed on the screen. In this case, you use ad, which is a decimal specifier that applies to integer values (whole numbers). It just means that the second argument,salary, will be interpreted and output as a decimal (base 10) number.



blog comments powered by Disqus

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials