C++
  Home arrow C++ arrow Page 3 - First Steps in (C) Programming, introducti...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C++

First Steps in (C) Programming, introduction
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 5
    2005-11-17

    Table of Contents:
  • First Steps in (C) Programming, introduction
  • What Is a Variable?
  • Variables That Store Numbers
  • Try It Out: Using More Variables
  • Naming Variables
  • Initializing Variables
  • Basic Arithmetic Operations
  • Try It Out: Division and the Modulus Operator

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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:

    1
    10,999,000,000
    –1

    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:

    1.234
    999.9
    2.0
    –0.0005

    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.
    --------------------------------------------

    HOW IT WORKS

    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.


    CAUTION 
    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.


    NOTE 
    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.


    NOTE 
    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.

    ..........................................................................................

    ..........................................................................................

    More C++ Articles
    More By Apress Publishing


       · This article is an excerpt from the book "Beginning C," published by Apress. We hope...
       · Worthwhile reading for the beginner. BUT... I'm torn between praising the publishing...
       · Lol! Yes, I understand your ambivalence about it being a marketing ploy. On the...
       · That's a good idea. We tend to forget that C is still very capable and popular....
       · Thank you Pantaz! I'm glad you liked the article, and we'll keep your suggestion in...
     

    Buy this book now. This article is excerpted from chapter two of the book Beginning C, written by Ivor Horton (Apress, 2004; ISBN: 1590592530). Check it out today at your favorite bookstore. Buy this book now.

    C++ ARTICLES

    - Multiplying Large Numbers with Karatsuba`s A...
    - Large Numbers
    - Dijkstra`s Shunting Algorithm with STL and C...
    - Brief Introduction to the STL Containers
    - The Standard Template Library
    - Templates in C++
    - C++ Programmer Alerts
    - C++ Programming Tips
    - First Steps in (C) Programming, conclusion
    - First Steps in (C) Programming, continued
    - First Steps in (C) Programming, introduction
    - C++ Preprocessor: Always Assert Your Code Is...
    - C++ Preprocessor: The Code in the Middle
    - Programming in C
    - Temporary Variables: Runtime rvalue Detection






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT