Skip to main content

Data Types in C++

In C++, variables are essential for storing information, and their data types determine the kind of information they can hold as well as the memory they require. Let's explore the basic data types, type modifiers, and how to use them in Dev-C++ with examples.

 

Primitive Built-in Types

C++ offers a rich set of built-in data types. Here are the basic ones:

  1. Boolean (bool): Used to store true or false values.
  2. Character (char): Used to store single characters.
  3. Wide Character (wchar_t): Used to store wide characters, often for Unicode.
  4. Integer (int): Used to store whole numbers.
  5. Floating Point (float): Used to store single-precision floating-point numbers.
  6. Double Floating Point (double): Used to store double-precision floating-point numbers.
  7. Void (void): Represents the absence of type.

These types can be modified using type modifiers such as signed, unsigned, short, and long.

 

Type Modifiers

Type modifiers expand the range of data types:

  • signed: Can store negative and positive values.
  • unsigned: Can only store positive values.
  • short: Uses less memory.
  • long: Uses more memory.

 

Memory and Range

Different data types consume different amounts of memory and have different ranges. Here’s a table summarizing this:

Type

Typical Bit Width

Typical Range

char

1 byte

-128 to 127 or 0 to 255

unsigned char

1 byte

0 to 255

signed char

1 byte

-128 to 127

int

4 bytes

-2147483648 to 2147483647

unsigned int

4 bytes

0 to 4294967295

short int

2 bytes

-32768 to 32767

unsigned short int

2 bytes

0 to 65535

long int

8 bytes

-9223372036854775808 to 9223372036854775807

unsigned long int

8 bytes

0 to 18446744073709551615

long long int

8 bytes

-(2^63) to (2^63)-1

unsigned long long int

8 bytes

0 to 18446744073709551615

float

4 bytes

Varies

double

8 bytes

Varies

long double

12 bytes

Varies

wchar_t

2 or 4 bytes

1 wide character

 

Example in Dev-C++

To find out the size of various data types on your system using Dev-C++, you can use the following program:

#include <iostream>

using namespace std;

 

int main() {

    cout << "Size of bool : " << sizeof(bool) << " byte(s)" << endl;

    cout << "Size of char : " << sizeof(char) << " byte(s)" << endl;

    cout << "Size of int : " << sizeof(int) << " byte(s)" << endl;

    cout << "Size of short int : " << sizeof(short int) << " byte(s)" << endl;

    cout << "Size of long int : " << sizeof(long int) << " byte(s)" << endl;

    cout << "Size of float : " << sizeof(float) << " byte(s)" << endl;

    cout << "Size of double : " << sizeof(double) << " byte(s)" << endl;

    cout << "Size of wchar_t : " << sizeof(wchar_t) << " byte(s)" << endl;

 

    return 0;

}

C++_Data_Type_Size


  

Checking Value Limits

To find the minimum and maximum values of various data types, you can use the numeric_limits library:

#include <iostream>

#include <limits>

using namespace std;

 

int main() {

    cout << "Int Min: " << numeric_limits<int>::min() << endl;

    cout << "Int Max: " << numeric_limits<int>::max() << endl;

    cout << "Unsigned Int Min: " << numeric_limits<unsigned int>::min() << endl;

    cout << "Unsigned Int Max: " << numeric_limits<unsigned int>::max() << endl;

    cout << "Long Int Min: " << numeric_limits<long int>::min() << endl;

    cout << "Long Int Max: " << numeric_limits<long int>::max() << endl;

    cout << "Unsigned Long Int Min: " << numeric_limits<unsigned long int>::min() << endl;

    cout << "Unsigned Long Int Max: " << numeric_limits<unsigned long int>::max() << endl;

 

    return 0;

}

 

C++_Data_Type_limit



Typedef Declarations

You can use typedef to create new names for existing types:

typedef int feet;

feet distance;

 

Enumerated Types

Enumerations allow you to define a type with a set of named values:

#include <iostream>

using namespace std;

 

enum Color { red, green, blue };

Color c = blue;

 

int main() {

    cout << "Color value: " << c << endl;  // Outputs: 2 (default value for blue)

    return 0;

}


In summary, understanding variables and data types in C++ is crucial for effective programming. Dev-C++ can be used to experiment with these concepts, providing a practical way to see how different types and their modifiers behave on your system.

 





Comments

Popular posts from this blog

How to Make Automatic Room Light Controller Without Microcontroller

You must have noticed in some offices or hotels, when nobody is in gallery or washroom, the light remains OFF but when somebody enters the place, light switches ON automatically. In this post I am going to teach you how to make this circuit. Before going ahead I would like to tell you that this is VERY EASY circuit. For this circuit the material we need is… PIR Motion sensor General Purpose PCB - 5x5 cm. Transistor 2222N – 1 No. Relay 5V – 1 No. 1K/0.250W – 2 Nos. 10K/0.250W – 1 No. IN4007 – 2 Nos. LED 3mm – 1 No. Connector – 4 Nos. Few wires. Relay Circuit Concept : We can use any relay of 12V, 24V, 5V etc. but we have to consider power supply or battery we will use. Since 5V power supply is easily available and 9V battery can also be used for 5V output (after using 7805 regulator if needed). So I am using 5V relay. PIR sensor has three terminals, One for 5Vdc Second for Gnd (0V). Third for

How to drive high voltage/current load by small voltage signal from a microcontroller?

Sometimes we need to control or drive a high voltage and heavy current load by a small voltage signal. For example, if you want to control water motor with your microcontroller output. We know that microcontroller gives only 5v output which is not sufficient to drive a heavy motor. This circuit, about which this post is, is very-very useful for electronics engineer and hobbyist. So pay attention! For this circuit the material we need is… General Purpose PCB - 5x5 cm. Transistor KN 2222A (TO-92) - 1 No. Relay 5V – 1 No. 1K/0.250W – 2 Nos. 10K/0.250W – 1 No. IN4007 – 2 Nos. LED 3mm – 1 No. Connector – 4 Nos. Few wires. Tools. Concept: Weak signal triggers the transistor and transistor acts as a switch for the relay. You can use any relay of 12V, 24V, 5V etc. but we have to consider power supply or battery we will use. Since 5V power supply is easily available and 9V battery can also be used for 5V output (after using 7805 regulator if needed).

How to Read Analog Input & Use PWM pin as Analog Output

  Analog Input: An analog signal can take on any number of values. To measure the value of analog signals, Arduino has a built-in analog-to-digital converter (ADC). The ADC turns the analog voltage into a digital value. There is an inbuilt function to read Analog value; analogRead(pin_number). This function converts the value of the voltage on the analog input pin and returns a digital value ranges from 0 to 1023, relative to the reference value. The default reference voltage is 5 V (for 5 V Arduino boards) or 3.3 V (for 3.3 V Arduino boards). This function has only one parameter, which is the pin number.     Analog Output: The Arduino does not have any built-in digital-to-analog converter (DAC), but it can do pulse-width modulation (PWM); a digital signal to achieve some of the functions of an analog output. The function analogWrite(pin, value) is used to output a PWM signal. In the function ‘pin’ is the pin number used for the PWM output. ‘value’ is a number proportiona