ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

Variable Types in C++ Bools, Floats, Integers, Arrays, Characters and Strings

Updated on September 13, 2011

Introduction

C++ comes with many variable types. These variables store different types of information that allows you to perform different operations to yield desired results. The variable types I will be introducing are the integer or int, the float, characters, bools, arrays and strings.

Integers

Integers, int in c++, are variable types that store numbers such as 20, 15 or 100. Numbers that contain decimals such as 15.5 are not integers because they contain decimal points. There is a limit to what an integer variable type can store. Typical machines use 32 bits, or 4 bytes, to store an integer while others have 64 bit integers. The range for a 32 bit integer is 2,147,483,647 or (231-1) to -2,147,483,648 or (-231) for signed integers. For unsigned integers the value is 4,294,967,295 or (232-1). 64 bit versions are -9,223,372,036,854,775,808 or (-263) to 9,223,372,036,854,775,807, or (263-1)

Generally an integer is declared like so:

int myInt;
myInt = 75;

Floating Point Numbers

Foating point numbers, or in c++, float, are numbers that have a decimal point contained within them. Numbers such as 20.0 are floating point numbers while numbers such as 20 are not because they do not contain decimal points. Floating point numbers can be expressed expenentionally for instance 1.2e19 is 1.2x1019. There is a limit on the range that is able to be stored in a float variable and varies widely depending on the machine. Here is an excellent page on floats

Floats can be declared like such:

float myFloat;
myFloat = 33.9;

Boolean Types

Boolean types are variables that simply hold a true or a false and can be declared as such:

bool skyIsBlue;
skyIsBlue = true;

Characters

Characters are used to store single characters such as a, b, or c. Characters are declared with single quotations such as

char myChar = 'a';

There are special characters called escape characters that begin with a \ and followed by a character. These special characters perform special actions. A table of escape characters can be found here.

Characters were designed to hold only the basic character set for american characters. For foreign languages wide characters were created. Wide characters use the pefix L to indicate it is a wide character. Characters can be initialized in the following manner:

char variableName;
variableName = 'a';

Wide characters can be initilaized in the following way:

wchar_t wide;
wide = L 'é';

The symbol represents a character in another language.

Arrays

Arrays are special storage containers in c++ that allow you to store data in consecutive memory locations. Items in arrays are called elements and the number of elements in the array is called the dimension of the array.

Arrays can be declared like this:

int myArray[10];

This declares the array to be an array of 10 elements, myArray[0], myArray[1], myArray[2].... and so on until you get to myArray[9]. Notice how the array starts at zero instead of 1 and goes to 9. The number inside of the bracket is called a subscript. When accessing a value inside of an array you use the subscript to get the data stored in that particular container. If you were to try to access anything in the array past 9 i.e. myArray[10] you will get an error and possibly crash your program because 10 will be the 11th element in the array since arrays start at 0.

Arrays can be declared like in the following manner:

int myArray[2];
myArray[0] = 1;
myArray[1] = 2;


The elements of the array are initialized to the values given. The first element of myArray is equal to 1 while the second element is equal to 2.

Strings

Strings are just like an array of characters meaning they are characters stored in consecutive memory locations. You can use strings when you want to store words in a variable. Strings are not built into the c++ core language so in order to use them you have to first use an include statement so they can be included form the c++ library.

#include <string>

Then you can declare a string in this way:

string myString;
myString = "cupcake";

As you can see strings have many different characters contained in succession, hence the name string. Since a string is an array of characters an element in a string can be accessed in the following manner:

cout << myString[3] << endl;

This will output the character c to the sceen since you are accessing the 4th element in the array of characters or string. Remember 3 is the 4th element since arrays start at 0. Just like characters there are also wide strings for use with foreign languages. Wide strings can be declared this way:

wstring myName;
name = L "Ãïò";


Wide strings begin with the letter L just as with wide characters. The symbols represent the characters in another language. There are many other operations that can be performed on strings but that is for another hub.

Conclusion

In this hub I have went over a few of the variable types in C++ and how to declare and initialize them. There are integers and floats that store numeric values, there are characters and strings used to store single letters and chains of letters respectively. Arrays are like containers that store variable consecutivley and bools hold true or false values. These are just a few types of variables in c++ but hopefully this helps anyone reading this get started on their way to becoming a programmer.

Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.

    working

    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, hubpages.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://hubpages.com/privacy-policy#gdpr

    Show Details
    Necessary
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
    Features
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Marketing
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Statistics
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)