Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C++ Example - Create Different Types of Variables

C++ Example - Create Different Types of Variables

C++ Example C++ Example

The following example demonstrates how to create variables of different types:

Example

include

<

iostream

include

<

string

using

namespace

std

;

int

main

(

)

{

//

Create variables

int

myNum

=

5

;

//

Integer

float

myFloatNum

=

5.99

;

//

Single-precision floating-point

double

myDoubleNum

=

9.98

;

//

Double-precision floating-point

char

myLetter

=

'

D

'

;

//

Character

bool

myBoolean

=

true

;

//

Boolean

string

myString

=

"

Runoob

"

;

//

String

//

Output variables

cout

<<

"

int:

"

<<

myNum

<<

"

\n

"

;

cout

<<

"

float:

"

<<

myFloatNum

<<

"

\n

"

;

cout

<<

"

double:

"

<<

myDoubleNum

<<

"

\n

"

;

cout

<<

"

char:

"

<<

myLetter

<<

"

\n

"

;

cout

<<

"

bool:

"

<<

myBoolean

<<

"

\n

"

;

cout

<<

"

string:

"

<<

myString

<<

"

\n

"

;

return

0

;

}

The output of the above program is:



int: 5

float: 5.99

double: 9.98

char: D

bool: 1

string: Runoob

C++ Example C++ Example