C++
  Home arrow C++ arrow Page 4 - 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  
Mobile Linux 
App Generation ROI 
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 / 6
    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 - Try It Out: Using More Variables


    (Page 4 of 8 )

    Let’s try a slightly larger example:

    /* Program 2.3 Using more variables */ #include <stdio.h>
    void main()
    {
      int brothers;     /* Declare a variable called brothers */
     
    int brides;       /* and a variable called brides          */
      brothers = 7;     /* Store 7 in the variable brothers   */
      brides = 7;       /* Store 7 in the variable brides     */
      /* Display some output */
     
    printf("%d brides for %d brothers", brides, brothers);
    }

    If you run this program you should get the following output:

    --------------------------------------------
    7 brides for 7 brothers
    --------------------------------------------

    HOW IT WORKS

    This program works in a very similar way to the previous example. You first declare two variables,brothersandbrides, with the following statements:

    int brothers;   /* Declare a variable called brothers */
    int brides; /* and a variable called brides */

    Both of these variables are declared as typeintso they both store integer values. Notice that they’ve been declared in separate statements. Because they’re both of the same type, you could have saved a line of code and declared them together like this:

    int brothers, brides;

    When you declare several variables in one statement, the variable names following the data type are separated by commas, and the whole line ends with a semicolon. This can be a convenient format, although there’s a downside in that it isn’t so obvious what each variable is for, because if they appear on a single line you can’t add individual comments to describe each variable. However, you could write this single statement spread over two lines:

    int brothers,      /* Declare a variable called brothers */
        brides;        /* and a variable called brides          */

    By spreading the statement out over two lines, you’re able to put the comments back in. The comments will be ignored by the compiler, so it’s still the exact equivalent of the original statement without the comments. Of course, you might as well write it as two statements.

    Note that the declarations appear at the beginning of the executable code for the function. You should put all the declarations for variables that you intend to use at the beginning.

    The next two statements initialize each of the variables with the same value, 7:

    brothers = 7;        /* Store 7 in the variable brothers   */
    brides = 7;          /* Store 7 in the variable brides     */

    Note that the statements that declared these variables precede these statements. If one or the other of the declarations were missing or appeared later in the code, the program wouldn’t compile.

    In the next statement is a control string that will display a line of text. Within this text, the%dconversion specifiers will be replaced by the values currently stored in the variables that appear as the second and third arguments to theprintf()function call—in this case,bridesandbrothers:

    printf("%d brides for %d brothers", brides, brothers);

    The conversion specifiers are replaced in order by the values of the variables that appear as the second and subsequent arguments to theprintf()function, so the value ofbridescorresponds to the first specifier, and the value ofbrotherscorresponds to the second. This would be clearer if you changed the statements that set the values of the variables as follows:

    brothers = 8;     /* Store 8 in the variable brothers  */
    brides = 4;       /* Store 4 in the variable brides    */

    In this somewhat dubious scenario, theprintf()statement would show clearly which variable corresponded to which conversion specifier, because the output would be

    --------------------------------------------
    4 brides for 8 brothers
    --------------------------------------------

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

    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

    - Paths and Files
    - Directories in C++
    - Focusing on C++ Files
    - Const Correctness in C++
    - Manipulating Streams and Files with C++
    - Streams and Files
    - 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






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