Logo

Navigation
  • Home
  • Services
    • ERP Solutions
    • Implementation Solutions
    • Support and Maintenance Solutions
    • Custom Solutions
    • Upgrade Solutions
    • Training and Mentoring
    • Web Solutions
    • Production Support
    • Architecture Designing
    • Independent Validation and Testing Services
    • Infrastructure Management
  • Expertise
    • Microsoft Development Expertise
    • Mobile Development
    • SQL Server Database and BI
    • SAP BI, SAP Hana, SAP BO
    • Oracle and BI
    • Oracle RAC
  • Technical Training
    • Learn Data Management
      • Business Intelligence
      • Data Mining
      • Data Modeling
      • Data Warehousing
      • Disaster Recovery
    • Learn Concepts
      • Application Development
      • Client Server
      • Cloud Computing Tutorials
      • Cluster Computing
      • CRM Tutorial
      • EDI Tutorials
      • ERP Tutorials
      • NLP
      • OOPS
      • Concepts
      • SOA Tutorial
      • Supply Chain
      • Technology Trends
      • UML
      • Virtualization
      • Web 2.0
    • Learn Java
      • JavaScript Tutorial
      • JSP Tutorials
      • J2EE
    • Learn Microsoft
      • MSAS
      • ASP.NET
      • ASP.NET 2.0
      • C Sharp
      • MS Project Training
      • Silverlight
      • SQL Server 2005
      • VB.NET 2005
    • Learn Networking
      • Networking
      • Wireless
    • Learn Oracle
      • Oracle 10g
      • PL/SQL
      • Oracle 11g Tutorials
      • Oracle 9i
      • Oracle Apps
    • Learn Programming
      • Ajax Tutorial
      • C Language
      • C++ Tutorials
      • CSS Tutorial
      • CSS3 Tutorial
      • JavaScript Tutorial
      • jQuery Tutorial
      • MainFrame
      • PHP Tutorial
      • VBScript Tutorial
      • XML Tutorial
    • Learn Software Testing
      • Software Testing Types
      • SQA
      • Testing
  • Career Training
    • Career Improvement
      • Career Articles
      • Certification Articles
      • Conflict Management
      • Core Skills
      • Decision Making
      • Entrepreneurship
      • Goal Setting
      • Life Skills
      • Performance Development
      • Personal Excellence
      • Personality Development
      • Problem Solving
      • Relationship Management
      • Self Confidence
      • Self Supervision
      • Social Networking
      • Strategic Planning
      • Time Management
    • Education Help
      • Career Tracks
      • Essay Writing
      • Internship Tips
      • Online Education
      • Scholarships
      • Student Loans
    • Managerial Skills
      • Business Communication
      • Business Networking
      • Facilitator Skills
      • Managing Change
      • Marketing Management
      • Meeting Management
      • Process Management
      • Project Management
      • Project Management Life Cycle
      • Project Management Process
      • Project Risk Management
      • Relationship Management
      • Task Management
      • Team Building
      • Virtual Team Management
    • Essential Life Skills
      • Anger Management
      • Anxiety Management
      • Attitude Development
      • Coaching and Mentoring
      • Emotional Intelligence
      • Stress Management
      • Positive Thinking
    • Communication Skills
      • Conversation Skills
      • Cross Culture Competence
      • English Vocabulary
      • Listening Skills
      • Public Speaking Skills
      • Questioning Skills
    • Soft Skills
      • Assertive Skills
      • Influence Skills
      • Leadership Skills
      • Memory Skills
      • People Skills
      • Presentation Skills
    • Finding a Job
      • Etiquette Tips
      • Group Discussions
      • HR Interviews
      • Interview Notes
      • Job Search Tips
      • Resume Tips
      • Sample Resumes
 

C++ Variables and Data types

By Sripriya R | on April 26, 2007 |
C++ Tutorials

In this C++ tutorial, you will learn about variables, constants and data types in C++, rules for defining variable names, short int, int, long int, float, double, long double, char, bool, declaring variables and constants.

Variables

A variable is the content of a memory location that stores a certain value. A variable is identified or denoted by a variable name. The variable name is a sequence of one or more letters, digits or underscore, for example: character_

Rules for defining variable name:

  • A variable name can have one or more letters or digits or underscore for example character_.
  • White space, punctuation symbols or other characters are not permitted to denote variable name.
  • A variable name must begin with a letter.
  • Variable names cannot be keywords or any reserved words of the C++ programming language.
  • Data C++ is a case-sensitive language. Variable names written in capital letters differ from variable names with the same name but written in small letters. For example, the variable name EXFORSYS differs from the variable name exforsys.

The amount of memory reserved or occupied by each variable differs as per the data stored. The amount of memory used to store a single character is different from that of storing a single integer. A variable must be declared for the specific data type.

Data Types:

Below is a list of the most commonly used Data Types in C++ programming language:

  • short int – This data type is used to represent short integer.
  • int – This data type is used to represent integer.
  • long int – This data type is used to represent long integer.
  • float – This data type is used to represent floating point number.
  • double – This data type is used to represent double precision floating point number.
  • long double – This data type is used to represent double precision floating point number.
  • char – This data type is used to represent a single character.
  • bool – This data type is used to represent boolean value. It can take one of two values: True or False.

Using variable names and data type, we shall now learn how to declare variables.

Declaring Variables:

In order for a variable to be used in C++ programming language, the variable must first be declared. The syntax for declaring variable names is:

data_type variable_name;

The date type can be int or float or any of the data types listed above. A variable name is given based on the rules for defining variable name (refer above rules).

Example:

int a;

This declares a variable name a of type int.

If there exists more than one variable of the same type, such variables can be represented by separating variable names using comma.

For instance

int x,y,z

This declares 3 variables x, y and z all of data type int.

The data type using integers (int, short int, long int) are further assigned a value of signed or unsigned. Signed integers signify positive and negative number value. Unsigned integers signify only positive numbers or zero.

For example it is declared as:

data_type variable_name;

  • unsigned short int a;
  • signed int z;

By default, unspecified integers signify a signed integer.

For example:

int a;

is declared a signed integer.

It is possible to initialize values to variables:

data_type variable_name = value;

Example:

  • int a=0;
  • int b=5;

Constants

Constants have fixed value. Constants, like variables, contain data type. Integer constants are represented as decimal notation, octal notation, and hexadecimal notation. Decimal notation is represented with a number. Octal notation is represented with the number preceded by a zero character. A hexadecimal number is preceded with the characters 0x.

Example:

  • 80 represent decimal
  • 0115 represent octal
  • 0x167 represent hexadecimal

By default, the integer constant is represented with a number.

The unsigned integer constant is represented with an appended character u. The long integer constant is represented with character l.

Example:

  • 78 represent int
  • 85u present unsigned int
  • 78l represent long

Floating point constants are numbers with decimal point and/or exponent.

Example:

  • 2.1567
  • 4.02e24

These examples are valid floating point constants.

Floating point constants can be represented with f for floating and l for double precision floating point numbers.

Character constants have single character presented between single quotes.

Example:

  • ‘c‘
  • ‘a‘

are all character constants.

Strings are sequences of characters signifying string constants. These sequence of characters are represented between double quotes.

Example:

“Exforsys Training” – is an example of string constant.

« « Advantages and Disadvantages of Web 2.0
How Web 2.0 Will Change The Internet » »

Author Description

Avatar

Free Training

RSSSubscribe 385 Followers
  • Popular
  • Recent
  • C++ Static Functions

    February 12, 2007 - 0 Comment
  • C++ Operators Part I

    August 24, 2007 - 0 Comment
  • C++ Dereference Operator

    October 14, 2007 - 0 Comment
  • C++ Structure Part II

    August 19, 2007 - 0 Comment
  • C++ Abstraction

    April 26, 2007 - 0 Comment
  • C++ Operators Part II

    August 27, 2007 - 0 Comment
  • C++ Operator Overloading Part 1

    September 7, 2006 - 0 Comment
  • C++ Encapsulation

    August 16, 2007 - 0 Comment
  • C++ Manipulators

    August 27, 2007 - 0 Comment
  • C++ Operator Overloading Part II

    September 9, 2007 - 0 Comment
  • C++ Dereference Operator

    October 14, 2007 - 0 Comment
  • C++ Memory Management Operators

    October 12, 2007 - 0 Comment
  • C++ Void Pointer and Null Pointer

    October 12, 2007 - 0 Comment
  • C++ Pointers

    October 1, 2007 - 0 Comment
  • C++ Static Functions

    October 1, 2007 - 0 Comment
  • C++ Friend Functions

    September 28, 2007 - 0 Comment
  • C++ Pure Virtual Function and Base Class

    September 28, 2007 - 0 Comment
  • C++ Virtual Functions

    September 25, 2007 - 0 Comment
  • C++ Inline Functions

    September 25, 2007 - 0 Comment
  • C++ Function Passing Types

    September 12, 2007 - 0 Comment

Exforsys e-Newsletter

ebook
 

Related Articles

  • C++ Dereference Operator
  • C++ Memory Management Operators
  • C++ Void Pointer and Null Pointer
  • C++ Pointers
  • C++ Static Functions

Latest Articles

  • Project Management Techniques
  • Product Development Best Practices
  • Importance of Quality Data Management
  • How to Maximize Quality Assurance
  • Utilizing Effective Quality Assurance Strategies
  • Sitemap
  • Privacy Policy
  • DMCA
  • Trademark Information
  • Contact Us
© 2023. All Rights Reserved.IT Training and Consulting
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish.AcceptReject Read More
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT