C++ tutorial, C++ programs, C++ Exercises, C++ Example Programs

Thursday, April 12, 2012

Variable Declaration in C Plus Plus

Variable declaration means to tell the C++ compiler about variable name and its data type.



The syntax of variable declaration in C++ is as follows:

General Syntax of Variable Declaration

DataType   List-of-Variables-separated-by-commas;

Examples of variable declaration

  • int RollNo, Marks;
  • In above example we have declared two integer variables. The names of variables are RollNo and Marks. Type of variables is integer. Integer data type variables can hold whole numbers like 100, 5 or -8. There will be no fractional part.
  • float percentage, average, installment;
  • In above example we have declared three variables of float data type. float data type variables can store numbers with decimal or fraction.
Share:

Naming Rules For C++ Variables

What is a Variable Name


As we have come to know in a previous post on C++ Variables, that a Variable is a named memory location to store some program data temporarily. "Named Memory Location" tells us that every variable in C++ has a variable name to refer the data stored in it.

Naming Rules To Write C++ variable Names

If we use a variable in a C++ program, we will declare this variable. To declare a C++ variable we will have to use a distinct name for each and every variable. We have to obey the following naming rules to write variable names in C++ programming.

    Start With Alphabets or Underscore

  1. The variable name will start with an alphabet or an Underscore.
  2. No Blank Space

  3. There should be no blank spaces in a variable name.
  4. No Special Characters

  5. Special characters like +, *, &, ^, % cannot be used in variable names.
  6. No Reserved / Keywords

  7. Reserved Words or Keywords cannot be used as variable names. Reserved words or keywords are the words used in C++ language for specific purpose. For example, int or for or while are reserved words in C language and C++ programming language, so programmers are not allowed to use these words as variable names in a program.
  8. Up to 31 characters long

  9. Although the maximum length of a variable name is compiler dependent in C++, several C++ compilers allow up to 31 characters long variable names. If anyone uses more than 31 characters for a variable name, extra characters are ignored by the C++ compiler.
  10. Short and Precise

  11. Variable names should be short and precise. Don't use abbreviations that may not self explanatory. Use abbreviations like RollNo in spite of RNo or RN or R or Roll. Abbreviations should be meaningful that may express the purpose of the use of a variable, clearly. 
  12. Naming Conventions

  13. Use uniform naming conventions in your program. For example: Use of RollNumb as a variable name states that the programmer will use first 4 characters for each word in a variable name. Secondly, each word in a variable name will be capital letter. If you follow uniform naming conventions for variables, writing, changing and maintaining your programs will be much easier. In addition, other programmers will find it easy to understand your programs.
  14. Unique Names

  15. Variable names must be unique in a program. A variable name can be used only for one data type. 
  16. Meaningful Names

  17. Use meaningful names that can explain the purpose of using the variable and type of data it can hold. 

Variables in C++
1 Explain
Variables in Programming C++
2 Naming
Rules For C++ Variables
Share:

Tuesday, April 10, 2012

Explain Variables in Programming C++

Variables




Variable may be defined as a named memory location to store program data temporarily during the execution of the program. So, a variable can be used to store name of a student or pay of an employee etc. in a C++ program or in any other programming language.
We can store data in a variable and can change the data or value contained in the variable time to time during program execution. We can access a variable with its name. Therefore, whenever we need value of the variable we will use the variable name for reference.


If we want to change the value of a variable, we will assign the new value to it. Now the variable will lost the old value and the new value will be placed in the memory location of that variable.

how many values a variable can have?

An atomic variable can have only one value at a time. No doubt, its value or contents can be changes time to time during program execution, but it will have only one value at a time.

Variables and Data Types

Variables can be of different data types according to the data they are going to store. For example, integer data type is used to declare such variables that can hold numeric data in whole numbers. Float data type will be used to declare variable to hold numbers with fractional parts.

Where Are Variables Created?

Variables are created in computer memory called RAM - Random Access Memory. There are different types of variables. Each data type variable can take some bytes of memory to store a particular value. For example, a variable of integer data type will take two bytes of memory in RAM.

Variables in C++
1 Explain
Variables in Programming C++
2 Naming
Rules For C++ Variables
Share:

Friday, April 6, 2012

Program Age Years To Days Convert

Today, we will discuss the logic of a simple C++ Program to input age in Years and Convert into Days. This is a simple C++ logic program. Simply we will multiply the years entered by user by 365 to convert years into  days.

a) Program Requirements: Input your age in years, convert it into days and show the result.

Let's write down an algorithm first

b) Algorithm: Convert-Years-To-Days

  1. Start 
  2. Input Your age in years 
  3. Calculate days = years X 365 
  4. Output "Your age in days=" , days 
  5. Stop




    c). Flow Chart: Convert Years Into Days





    d.) Picture of C++ Program Years to days conversion from Turbo C++ 3.0 IDE


    C++ Program Convert your Age from Years to days conversion


    e). Program coding.


    #include <iostream.h>
    #include <conio.h>
    void main()
    {
        // Declare two integer variables(for whole numbers)
        int years, days;
       // clear output screan and display message to user for input
        clrscr();
        cout<<"\n Enter Your Age in Years=";
        // get input from user
        cin>>years;
        //calculate days
        days = years * 365;
        //display output message to user showing age in days
        cout<<"\n Your age in days="<<days;
        // make output screen stay as long as a character key is pressed
        getch();
    }

    f).Program sample run output: 

    C++ Program Convert your Age from Years to days conversion 


    Enter Your Age in Years=17
    Your age in days=6205
    Share:

    EasyCPPprogramming.blogspotcom

    Labels