0% found this document useful (0 votes)
82 views12 pages

02 Fundamental Objects

The document discusses constants, variables, data types and comments in C++. It defines constants as fixed values that don't change during program execution, while variables can take different values. It describes the different data types in C++ including integer, floating point, character and void. It also explains how to declare variables and use comments.

Uploaded by

api-3769732
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views12 pages

02 Fundamental Objects

The document discusses constants, variables, data types and comments in C++. It defines constants as fixed values that don't change during program execution, while variables can take different values. It describes the different data types in C++ including integer, floating point, character and void. It also explains how to declare variables and use comments.

Uploaded by

api-3769732
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
You are on page 1/ 12

CONSTANTS, VARIABLES AND DATA TYPES

Introduction
A programming language is designed to help process certain kinds of
data consisting of numbers, characters and strings and to provide
useful output known as information. The task of processing of data is
accomplished by executing a sequence of precise instructions called
a program. These instructions are formed using certain symbols and
words according to some rigid rules known as syntax rules or
grammar. Every program instruction must confirm precisely to the
syntax rules of the language.

Character set
The characters in C++ are grouped into the following categories:
1. Letters (A-Z, a-z)
2. Digits (0 – 9)
3. Special character (, . ; & * < > ] [ # )
4. White spaces (Blank space, Horizontal tab, Carriage return, NL)
Every C++ word is classified as either a keyword or an identifier. All
keywords have fixed meanings and these meanings cannot be
changed. Keywords serve as a basic building blocks for programming
statements.
The following are reserved words in C++ and may not be otherwise
used.

asm auto break case catch char class const continue default delete
do double else enum extern float for friend goto if inline int long
new operator private protected public register return short signed
sizeof static struct switch template this throw try typedef union
unsigned virtual void volatile while
Rules for identifiers
2. First character must be an alphabet (or underscore)
3. Must consist of only letters, digits or underscore
4. Only first 31 characters are significant
5. Cannot use a keyword
6. Must not contain white space
Constants
Constants in C++ refer to fixed values that do not change during the
change during the execution of a program.

CONSTANTS

NUMERIC CONSTANTS CHARACTER CONSTANTS

STRING
REAL
CONSTANTS
CONSTANTS

INTEGER SINGLE CHAR


CONSTANTS CONSTANTS
An integer constant refers to a sequence of digits. There are three
types of integers, namely, decimal integer, octal integer and
hexadecimal integer.
Examples : +255, -65, 9854
Real constants represent floating point numbers.
Examples : 65.352, 698.241574
Single character constants contains simply a single character
enclosed within a pair of single quote marks.
Examples : ‘a’ , ‘5’, ‘/’ , ‘#’
A string constant is a sequence of characters enclosed within double
quotes.
Examples : “Amit”, “Hello BCA!!!”, “Taxi9211”
Variables
Variables are most fundamental part of any language. A variable has
a symbolic name and can be given a variety of values. Variables are
located in particular places in the computer’s memory. When a
variable is given a value, that value is actually placed in the memory
space assigned to the variable.
A variable is a data name that may be used to store a data value.
Unlike constants that remain unchanged during the execution of a
program, a variable may take different values at different times
during execution. A variable name can be chosen by a programmer in
a meaningful way so as to reflect its function or nature in the
program. Some examples are:
height Total Average Basic_Salary Student1
myVaRiAbLe
A variable may consist of letters, digits and the underscore( _ )
character. The following rules may be taken into consideration:
• They must begin with a letter.
• ANSI standard recognizes a length of 31 characters.
• Uppercase and lowercase are significant. That is, the variable
Total is different from TOTAL or toTal or total.
• It should not be a keyword.
• White space is not allowed.

Invalid examples : 123, %count, [hello]


Data Types
C++ is rich in data types. There are three different classes of data
types:
1 Primary (or fundamental or basic) data types
2 Derived data types
3 User-defined data types
Let us see primary data types first.
Primary Data Types

Integral Types

Integer character
Signed Unsigned char
int unsigned int signed char
short int unsigned short int unsigned char
long int unsigned long int

Floating point Type


void
float double long double
Declaration of integer variables
Remember that declaration of any variable does two things:
1. It tells the compiler what the variable name is.
2. It specifies what type of data the variable will hold.
Integer variables can hold numbers range from -2,147,483,648 to
2,147,483,647. They are declared as follows:
int i;
int variable1;
int sci,maths,phy;
int i=0;
Observe that we used int before the variable name to declare a
variable of integer type.
Example 1
Output using cout object
As you have seen, the statement
cout<<"Hello, how are you ";
causes the above phrase to be displayed on the screen.
Here, the object cout is responsible for this. It is predefined in
C++ to correspond to the standard output stream. A stream is
defined as a flow of data. The standard output stream normally flows
to the screen display.
The operator << is called the insertion or put operator. It is used
with cout to display data on the screen.
Comments
Comments are an important part of any program. They help the
person writing a program or anyone else who must read the source
file, understand the program. The compiler ignores comments, so
they do not add to the size of the file or execution time of the
executable program.
Comments are used in two ways:
Single line comments:
// this is an example of a single line comment
Multiline comments: /* this is an
example of a multiline
comment */
Example 2

You might also like