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

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
TABLE OF CONTENTS:
  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
SEARCH DEVARTICLES

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

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


blog comments powered by Disqus
C++ ARTICLES

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

Developer Shed Affiliates

 




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