How to Use Data Types in C++

C++ offers seven fundamental data types, which can symbolize any type of information and are built into the compiler. Five of these types are derived from C: void, int, float, double and char. C++ defines the other two: bool and wchar_t. Data types are essential to master because a program without data types has no value. Read on to learn how to use data types in C++.

Things You'll Need

  • Beginner's guide in C++
  • Microsoft Visual C++ or other IDE
Show More

Instructions

    • 1

      Learn the significance of each data type. A void type means "no data" or represents a generic pointer. The int type stores positive or negative whole numbers and is 16 or 32 bits long. "Float" (32 bits) represents decimal numbers with a moveable (floating) decimal point, and double is an extra precise float that's 64 bits long. "Char" (8 bits) represents printable characters, and wchar_t can store a wide character literal. "Bool" (8 bits) denotes true or false.

    • 2

      Use the following syntax to declare a data type in your program as the following example shows: data type keyword, space, variable name, comma, space, other variable names, semicolon. Precede the data type keyword by a data type modifer, if necessary. See Step 3 for explanations on type modifiers.
      int num1, num2, num3;

    • 3

      Learn the type modifiers: signed, unsigned, short and long. If you put one by itself, the compiler assumes you mean int. The short modifier makes an int 2 bytes while long makes it 4 bytes.

    • 4

      Initialize a variable by putting an equals sign after it and giving it a value. You can do this during the declaration or after it.

      int x = 14, y; // during declaration

      y = 34; // after declaration

    • 5

      Form an arithmetic expression using data types as follows:

      unsigned int seconds = 60, minutes = 60, hours = 24, total;

      total = hours * minutes * seconds;

    • 6

      Create a more complex data type by using char. An array is a data structure that can hold a row of elements of the same data type. An array of char functions as a string.

      char word[6] = {'H', 'e', 'l', 'l', 'o', '\0'};

Tips & Warnings

  • Use data qualifiers such as const, static, volatile, register or the star operator to give the compiler additional information about how to store the data.

  • Pointers are special data types that store a memory address and have the same type as the variable they point to in memory.

  • C++ lets programmers define custom-made data types; these are called classes.

Related Searches:

Resources

Comments

You May Also Like

  • How to Use Char Broil Smokers

    A char broil smoker can be used to slow cook meat, giving the meat a smoky or hickory flavor. his is done...

  • C Tutorial: Data Types

    Dennis Ritchie developed a structured, general purpose programming language for Bell Telephone Laboratories that came to be known as C. C has...

  • How to Use Callback Function in C

    Callback functions are advanced features in C programming language. A callback function helps in isolating the implementation of a library function from...

  • How to Use Excel's Char Function

    Excel's Char function determines the character specified by the number of a character set. This will be the Macintosh character set for...

  • ANSI C Data Types

    ANSI C Data Types. The American National Standards Institute (ANSI) standard for the C computer programming language remains true to the minimalist...

  • How to Use a Char Broil Electric Smoker

    Nothing beats the taste of meat that has been cooked slowly in a smoker and our love of smoking meat in the...

  • Data Types for Turbo C

    Data Types for Turbo C. Turbo C is a compiler and development environment for the programming language C. It came out in...

  • RAM Types for the Dell Latitude C PPX

    RAM Types for the Dell Latitude C PPX. Adding more random access memory (RAM) modules or chips to your Dell computer can...

  • How to Transfer DOS Random Type Data From the AS400 to a PC

    The IBM Application System 400 (AS/400) is a family of business computers introduced in 1988. There are several different models within this...

  • How to Use the Strcpy Function in C++

    The C++ strcpy function copies a string from a source location to a destination location and provides a null character to terminate...

  • How to Use a Char Griller

    Char-Griller brand charcoal grills incorporate the barrel style of barbecuing and often have offset side fireboxes for indirect grilling and smoking. Proper...

  • How to Use the Strcat Function in C++

    The C++ strcat function is short for "string concatenate." Strcat appends a copy of a source string to a destination string. The...

  • Common Errors in Turbo C

    Common Errors in Turbo C. The C programming language is popular, but it is also a strict language in the sense that...

  • How to Use Char-Griller Smokers

    Char-Griller manufactures a line of wood and charcoal smokers for slow-cooking thick cuts of meat at controlled temperatures. Char-Griller smokers are available...

  • What Are the Different Types of Turbo Shells?

    What Are the Different Types of Turbo Shells?. Collectors value turbo shells as decorative pieces for their homes. Typically, they have silver,...

  • How to Use the Strstr Function in C++

    The C++ strstr function locates a specified substring within a source string. The scan does not include terminating null-characters. Strstr returns a...

  • Different Data Types in Visual Basic

    Different Data Types in Visual Basic. Visual Basic has 17 basic data types that are used to construct all the data stored...

Related Ads

Featured