Home  Interview Questions  Certifications  Aptitude Questions  Tutorials  Placement Papers  Search  Resume  Soft Skills  Video  Forum  Blog

  C++ Programming Tutorials
Basics of C++

Structure of a program
Variables Data types
Basic Input/output

Control Structures
Control Structures
Functions (I)
Functions (II)

Compound Data Types
Character Sequences
Dynamic Memory
Data Structures
Other Data Types

Object Oriented Programming
Classes [I]
Classes [II]
Friendship & Inheritance

Advanced Concepts
Type Casting
Preprocessor Directives

C++ Standard Library
Input/output with Files

Soft Skills
Communication Skills
Leadership Skills




C++ Programming Tutorials

Constants are expressions with a fixed value.

Literals are used to express particular values within the source code of a program. We have already used these previously to give concrete values to variables or to express messages we wanted our programs to print out, for example, when we wrote:

a = 5;

the 5 in this piece of code was a literal constant.

Literal constants can be divided in Integer Numerals, Floating-Point Numerals, Characters, Strings and Boolean Values.

Integer Numerals


They are numerical constants that identify integer decimal values. Notice that to express a numerical constant we do not have to write quotes (") nor any special character. There is no doubt that it is a constant: whenever we write 1776 in a program, we will be referring to the value 1776.

In addition to decimal numbers (those that all of us are used to use every day) C++ allows the use as literal constants of octal numbers (base 8) and hexadecimal numbers (base 16). If we want to express an octal number we have to precede it with a 0 (zero character). And in order to express a hexadecimal number we have to precede it with the characters 0x (zero, x). For example, the following literal constants are all equivalent to each other:

75           // decimal
0113       // octal
0x4b       // hexadecimal

All of these represent the same number: 75 (seventy-five) expressed as a base-10 numeral, octal numeral and hexadecimal numeral, respectively.

Literal constants, like variables, are considered to have a specific data type. By default, integer literals are of type int. However, we can force them to either be unsigned by appending the u character to it, or long by appending l:

75        // int
75u      // unsigned int
75l       // long
75ul     // unsigned long

In both cases, the suffix can be specified using either upper or lowercase letters.

Floating Point Numbers
They express numbers with decimals and/or exponents. They can include either a decimal point, an e character (that expresses "by ten at the Xth height", where X is an integer value that follows the e character), or both a decimal point and an e character:

3.14159        // 3.14159
6.02e23        // 6.02 x 10^23
1.6e-19         // 1.6 x 10-19
3.0                // 3.0

These are four valid numbers with decimals expressed in C++. The first number is PI, the second one is the number of Avogadro, the third is the electric charge of an electron (an extremely small number) -all of them approximated- and the last one is the number three expressed as a floating-point numeric literal.

The default type for floating point literals is double. If you explicitly want to express a float or long double numerical literal, you can use the f or l suffixes respectively:

3.14159L         // long double
6.02e23f          // float

Any of the letters than can be part of a floating-point numerical constant (e, f, l) can be written using either lower or uppercase letters without any difference in their meanings.

NEXT >> Character and String Literals

Have a Question ? post your questions here. It will be answered as soon as possible.

Check C Aptitude Questions for more C Aptitude Interview Questions with Answers

Check C Interview Questions for more C Interview Questions with Answers.