0% found this document useful (0 votes)
6 views25 pages

C++ Data Types and Identifiers Guide

The document outlines the primary data types in C++, including int, float, char, double, bool, wchar_t, and string, along with their memory usage and characteristics. It also discusses the rules for naming identifiers and variables in C++, emphasizing the importance of avoiding keywords, special characters, and whitespace. Additionally, it introduces the use of cin for user input in C++ programming.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views25 pages

C++ Data Types and Identifiers Guide

The document outlines the primary data types in C++, including int, float, char, double, bool, wchar_t, and string, along with their memory usage and characteristics. It also discusses the rules for naming identifiers and variables in C++, emphasizing the importance of avoiding keywords, special characters, and whitespace. Additionally, it introduces the use of cin for user input in C++ programming.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

C++

Primary Data Types in C++


• Primary (Fundamental) data types in C programming includes the 4 most basic data types, that
is:
• int: It is responsible for storing integers. The memory it occupies depends on the compiler (32
or 64 bit). In general, int data type occupies 4 bytes of memory when working with a 32-bit
compiler.
• float: It is responsible for storing fractions or digits up to 7 decimal places. It is usually referred
to as a single-precision floating-point type. It occupies 4 bytes of memory
• char: It can be used to store a set of all characters which may include alphabets, numbers and
special characters. It occupies 1 byte of memory being the smallest addressable unit of a
machine containing a fundamental character set.
• double: It is responsible for storing fractions or digits up to 15-16 decimal places. It is usually
referred to as a double-precision floating-point type.
• void (Null) data type: It indicates zero or no return value. It is generally used to assign the null
value while declaring a function.
• In C++, in addition to the primary data types available in C, there are few more data types
available in the C++ programming language.
• bool: It refers to a boolean/logical value. It can either be true or false.
• wchar_t: It refers to a wide character whose size is either 2 or 4 bytes. It is similar
to the char data type but the only difference is the space occupied in the computer
memory.
• string: Instead of declaring an array of characters to enter a string data type, C++
gives you the provision to declare the “string” data type.
• Along with data types, comes modifiers. Modifiers basically alter the function of a
data type and make it more specific by the inclusion of additional provisions. These
include:

• Signed: It is used to store zero, positive or negative values.


• Unsigned: It can store only zero or positive values.
• Long: It is used to store large integer numbers. The size of the long type is 8 bytes.
• Short: It is used to store small integer numbers. Its size is of 2 Bytes.
• 421
• 23
• 44
• 62
• 65
• 77
• 331 312 present
In C++, tokens are the smallest individual units of a
program that are meaningful to the compiler. They act as
the fundamental building blocks, enabling the compiler to
understand and process the source code during
compilation
Tokens in C++ are referred to as the smallest individual
units in a program.
1. Keywords in C++ refer to the pre-existing, reserved words, each holding its
own position and power and has a specific function associated with it.
2. Keywords in C++ help the user in framing statements and commands in a
language.

3. Each keyword conveys a unique connotation to the compiler to perform a


specific task.

4. Just like the combination of words helps us in framing sentences, the


combination of keywords helps us in framing statements to perform logical
operations in a programming language.
C++ Identifiers
• C++ allows the programmer to assign names of his own choice to variables, arrays, functions, structures, classes, and various
other data structures called identifiers. The programmer may use the mixture of different types of character sets available in
C++ to name an identifier.

• Rules for C++ Identifiers


• There are certain rules to be followed by the user while naming identifiers, otherwise, you would get a compilation error.
These rules are:

• First character: The first character of the identifier in C++ should positively begin with either an alphabet or an underscore. It
means that it strictly cannot begin with a number.
• No special characters: C++ does not encourage the use of special characters while naming an identifier. It is evident that we
cannot use special characters like the exclamatory mark or the “@” symbol.
• No keywords: Using keywords as identifiers in C++ is strictly forbidden, as they are reserved words that hold a special
meaning to the C++ compiler. If used purposely, you would get a compilation error.
• No white spaces: Leaving a gap between identifiers is discouraged. White spaces incorporate blank spaces, newline, carriage
return, and horizontal tab.
• Word limit: The use of an arbitrarily long sequence of identifier names is restrained. The name of the identifier must not
exceed 31 characters, otherwise, it would be insignificant.
• Case sensitive: In C++, uppercase and lowercase characters connote different meanings.
Rules for C++ Identifiers
There are certain rules to be followed by the user while naming identifiers, otherwise, you
would get a compilation error. These rules are:
• First character: The first character of the identifier in C++ should positively begin with either
an alphabet or an underscore. It means that it strictly cannot begin with a number.
• No special characters: C++ does not encourage the use of special characters while naming an
identifier. It is evident that we cannot use special characters like the exclamatory mark or the
“@” symbol.
• No keywords: Using keywords as identifiers in C++ is strictly forbidden, as they are reserved
words that hold a special meaning to the C++ compiler. If used purposely, you would get a
compilation error.
• No white spaces: Leaving a gap between identifiers is discouraged. White spaces incorporate
blank spaces, newline, carriage return, and horizontal tab.
• Word limit: The use of an arbitrarily long sequence of identifier names is restrained. The
name of the identifier must not exceed 31 characters, otherwise, it would be insignificant.
• Case sensitive: In C++, uppercase and lowercase characters connote different meanings.
DECLARATION OF VARIABLE
The general rules for naming variables are:

• Names can contain letters, digits and underscores


• Names must begin with a letter or an underscore (_)
• Names are case-sensitive (myVar and myvar are different variables)
• Names cannot contain whitespaces or special characters like !, #, %,
etc.
• Reserved words (like C++ keywords, such as int) cannot be used as
names
• C++ User Input
• You have already learned that cout is used to output (print) values.
Now we will use cin to get user input.

• cin is a predefined variable that reads data from the keyboard with
the extraction operator (>>).

You might also like