C++
  Home arrow C++ arrow Page 2 - 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 - What Is a Variable?


    (Page 2 of 8 )

    A variable is a specific piece of memory in your computer that consists of one or more contiguous bytes. Every variable has a name, and you can use that name to refer to that place in memory to retrieve what it contains or store a new data value there.

    Let’s start with a program that displays your salary using the printf() function that you saw in Chapter 1. Assuming your salary is $10,000, you can already write that program very easily:

    /* Program 2.1 What is a Variable? */ #include <stdio.h>
    void main()
    {
     
    printf("My salary is $10000");
    }

    I’m sure you don’t need any more explanation about how this works; it’s almost identical to the programs you developed in Chapter 1. So how can you modify this program to allow you to customize the message depending on a value stored in memory? There are, as ever, several ways of doing this. What they all have in common, though, is that they use a variable.

    In this case, you could allocate a piece of memory that you could call, say, salary, and store the value10000 in it. When you want to display your salary, you could use the name you’ve given to the variable, which issalary, and the value that’s stored in it (10000) would be displayed. Wherever you use a variable name in a program, the computer accesses the value that’s stored there. You can access a variable however many times you need to in your program. And when your salary changes, you can simply change the value stored in the variablesalaryand the whole program will carry on working with the new value. Of course, all these values will be stored as binary numbers inside the computer.

    You can have as many variables as you like in a program. The value that each variable contains, at any point during the execution of that program, is determined by the instructions contained in your program. The value of a variable isn’t fixed, and it can change as many times as you need it to throughout a program.

    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 5 hosted by Hostway
    Stay green...Green IT