Defined Identifier & Keywords and Data Types In Programming

Identifier

The identifiers are the names that represent variable, constants, functions and labels in the program. Identifier is an important feature of all programming languages. A good identifier name should be descriptive but short.

Identifier

An identifier in C++ may consist of 31 characters. If the name of an identifier is longer than 31 characters, the first 31 characters will be used. The remaining characters will be ignored by C++ compiler. 

What is an identifier? Give some examples of identifier?

The identifiers are the names that represent variable, constants, types, functions and labels in the program. Identifier is an important feature of all programming languages. A good identifier name should be descriptive but short. Some examples of identifiers are Student Age, Item20 and Sum etc.

Write the legal characters for an identifier?

The legal characters for an identifier include upper alphabets from A to Z, lower alphabets from a to z, digits from 0 to 9 and the underscore character.

How many types of identifiers are used in C++ language?

There are two types of identifiers in C++ language. These are standard identifiers and user defined identifiers.

Some important rules for identifier name are as follows:

  • The first character must be an alphabetic or underscore (-).
  • The identifier name must consist of only alphabetic characters, digits or underscores.
  • The reserved word cannot be used as identifier name.

Types of Identifiers

C++ provides the following types of identifiers:

  • Standard Identifiers
  • User-defined Identifiers

Standard Identifiers

A type of identifier that has special meaning in C++ is known as standard identifier. C++ cannot use a standard identifier for its original purpose if it is redefined.

Example

cout and cin are examples of standard identifiers. These are the names of input/output objects defined in standard input/output library iostream.h

User-defined Identifiers

The type of identifier that is defined by the programmer to access memory location is known as user-defined identifier. The user-defined identifiers are used to store data and program results.

Example

Some examples of user-defined identifiers are a, marks and age etc.

What is standard identifier? Give some examples?

A type of identifier that has special meaning in C++ is known as standard identifier. C++ cannot use a standard identifier for its original purpose if it is redefined cout and cin are examples of standard identifiers.

What is user-defined identifier? Give some examples.

The type of identifier that is defined by the programmer to access memory location is known as user-defined identifier. The user-defined identifiers are used to store data and program results. Some examples of user-defined identifiers are num, age, marks etc.

Keywords

Keyword is a word in C++ that has a predefined meaning and purpose. The meaning and purpose of a keyword is defined by the developer of the language. It cannot be changed or redefined by the user Keyword can be used for the same purpose for which it is defined. Keywords are also known as reserved words. There are different types of keywords in C++ The total number of keywords in C++ is 63.

What is keyword? Give some examples.

Keyword is a word in C++ that has a predefined meaning and purpose. It defined by the developer of the language. It cannot be changed or redefined by the user. Some examples of keywords are if, while, int, and const

How can you differentiate keyword from identifier?

Keywords are the words that have predefined meaning and purpose. The identifiers are defined by the user.

The list of keywords in C++ is as follows:

asm

acto

wchar

while

using

virtual

char

class

const

break

case

catch

delete

do

double

const_cast

continue

default

explicit

export

extern

dynamic_cast

else

enum

friend

goto

if

false

float

for

mutable

namespace

new

inline

int

long

public

register

reinterpret_cast

operator

private

protected

size of

static

static_cast

return

short

signed

typename

throw

true

struct

switch

template

volatile

union

unsigned

try

tupedef

typeid


Data Types

The data type defines a set of values and a set of operations on those values. The computer manipulates various types of data. The data is given to the program as input. It is processed according to the program instructions and output is returned. The data and its type are defined before designing the actual program to process the data. The type of each data value is identified at the beginning of program design.

Data Types

A C++ program may need to process different types of data. Each data type requires different amount of memory. 

C++ provides the following data types:

Data type

Purpose

int

It is used to store numeric values.

float

It is used to store real values.

double

It is used to store large real values.

char

It is used to store character values.

Integer Data Types

Integer data is numeric value with no decimal point or fraction. It includes both positive and negative values. The minus sign is used to indicate the negative value The value is positive by default if no sign is used.

Integer Data Types

Examples

Some examples of integer values are 10, 520 and-20 etc.

Types of Integers

C++ provides different types of integer data. These are as follows:

Data Types

Size in Bytes

Description

int

2

Ranges from -32,768 to 32,767

short

2

Ranges from -32,768 to 32,767

unsigned int

2

Ranges from 0 to 65,535

long

4

Ranges from -2,147,483,648 to 2,147,483,647

unsigned long

4

Ranges from 0 to 4,294,967,295

Note:

The above table shows the size of data types according to Turbo C++ compiler. The int and unsigned int take 4 bytes in some compilers.

int Data Type

int data type is used to store integer values. It takes 2 or 4 bytes in memory depending on the computer and compiler being used. It takes 2 bytes in Turbo C++ and its range is from -32768 to 32767.

INT _MIN represents the smallest value that can be stored in int data type. INT_MAX represents the largest value that can be stored in int data type. Both INT_ MIN and INT_MAX are defined in limits.h header file.

short int Data Type

short int data type is used to store integer values. It takes 2 bytes in memory. Its range is from -32768 to 32767.

SHRT_MIN represents the smallest value that can be stored in short int data type. SHRT _MAX represents the largest value that can be stored in short int data type. Both SHRT_MIN and SHRT_MAX are defined in limits.h header file.

unsigned int Data Type

unsigned int data type is used to store only positive integer values. It takes 2 bytes in memory. Its range is from 0 to 65.535. 

UINT_MAX represents the largest value that can be stored in unsigned int data type.

It is defined in limits.h header file

long int Data Type

long int data type is used to store large integer values. It takes 4 bytes in mamury. Its range is from-2,147,483,648 to 2,147,483,647.

LONG_MIN represents the smallest value that can be stored in long int data type. LONG_MAX represents the largest value that can be stored in long int data type. Both LONG_MIN and LONG_MAX are defined in limits.h header file

unsigned long int Data Type

unsigned long int data type is used to store large positive integer values. It takes 4 bytes in memory Its range is from 0 to 4,294,967,295.

ULONG _MAX represents the largest value that can be stored in unsigned long int data type. It is defined in limits.h header file.

Real Data Types

Real data is a numeric value with decimal point or fraction. It is also called floating point number. It includes both positive and negative values. The minus sign is used to indicate negative value. The value is positive by default if no sign is used.

Examples:

Some examples of real values are 10.5, 5.3 and-10.91 etc

Types of Real

C++ provides different types of real data as follows:

Data type

Size in Bytes

Description

float

4

3.4 * 10^-38 to 3.4 * 10^+308

double

8

1.7 * 10^-308 to 1.7 * 10^+308

long double

10

1.7 * 10^-4932 to 1.7 * 10^+4932


Float Data Types

float data type is used to store real values. It takes 4 bytes in memory. Its range is from 3.4x10^-38 to 3.4x10^+38. It provides accuracy of 6 decimal places.

FLT_MIN represents the smallest value that can be stored in float data type. FLT_MAX represents the largest value that can be stored in float data type. Both FLT_MIN and FLT_MAX are defined in float.h header file.

double Data Type

double data type is used to store large real values. It takes 8 bytes in memory. Its range is from 1.7x10^-308 to 1.7x10^+308. It provides accuracy of 15 decimal places.

DBL_MIN represents the smallest value that can be stored in double data type. DBL_MAX represents the largest value that can be stored in double data type. Both DBL_MIN and DBL_MAX are defined in float.h header file.

long double Data Type

long double data type is used to store very large real values it takes 10 bytes in memory. Its range is from 1.7x10^-4932 to 1.7x10^+4932  It provides accuracy of 19 decimal places.

LDBL_MIN represents the smallest value that can be stored in long double data type LDBL_MAX represents the largest value that can be stored in long double data type Both LDBL_MIN and LDBL_MAX are defined in float.h header file.

Character Data Type

char data type is used to store character value. It takes 1 byte in memory. It is used to represent a letter, number or punctuation mark and a few other symbols.

CHAR_MIN represents the smallest value that can be stored in char data type CHAR_MAX represents the largest value that can be stored in char data type. Both CHAR_MIN and CHAR_MAX are defined in limits.h header file.

The character values are normally given in single quotes. It can represent individual characters such as 'a', 'x', '5', and '#'. The character '5' is manipulated differently than integer 5. It is possible to perform mathematical operation on character values. The characters can be added, subtracted and compared like numbers because they are stored in computer as numeric codes. C++ language uses ASCII code to store characters. For example, the ASCII code 65 is used for A, 66 for B and so on.

#include <iostream.h>

#include <conio.h>

void main()

{

clrscr();

char ch1, ch2;

ch1 = 'A';

ch2 = 'B';

cout<<"Characters are: "<<ch1<<ch2;

getch();

}

Integer Overflow and Underflow

Integer overflow occurs when the value assigned to an integer variable is more than the maximum possible value. Integer underflow occurs when the value assigned to an integer variable is less than the possible minimum value.

An integer variable can store the values from -32768 to 32767. An integer overflow will occur if the assigned value is more than 32767. An integer underflow will occur if the assigned value is less than -32768. 

Integer overflow and underflow is not detected by the compiler but it may produce wrong result. It is the responsibility of the user to ensure that the values assigned to integer. Variables are within the allowed range.

Program that explains the concept of overflow and underflow:

#include <conio.h>

#include <iostream.h>

void main()

{

clrscr();

short testVar = 32767;

cout << testVar << endl;

testVar testVar + 1; 

cout << testVar << endl;

testVar = testVar – 1;

cout << testVar << endl;

getch();

}

Note:

The function clrscr() is used to clear the output screen. The function getch() is commonly used at the end of the program to hold the output on the screen. C++ compiler displays the output for a short time and returns to the editing window The user may not see the output.

The getch() function allows the user to see the output and then press any key to return to editing window. The header file conio.h must be included to use these functions.

Scientific or Exponential Notation

The real numbers can also be written in exponential or scientific notation. This notation represents the large real numbers in a short way.

The exponential notation consists of two parts:

  • Mantissa
  • Exponent

The general form of writing real values in exponential notation is as follows:

+-m e +-n  OR  +-m E +-n

The value before e is known as mantissa and the value after e is known as exponent

Some important points about exponential notation are as follows:

  • Mantissa and exponent may be positive or negative value.
  • Exponent is always an integer value It cannot be a real number.
  • The absolute value of mantissa must be greater or equal to 1 and less than 10.

For example: a real number 10000 can be represented in scientific notation as 1.0 * 10^3. It can also be written in exponential form as 1.0e3. In this example, 1.0 is mantissa and 3 is the value of exponent. Similarly, 0.004694=4.694 * 10^-3. It can also be written as 4.694e-3.

Some examples of real numbers with scientific or exponential notations are as follows.

Real number

Scientific Notation

Exponential Notation

1.98

1.98 * 10^0

198e0

4,679,000,0

4.679 * 10^6

4.679e6

0.00053

5.3 * 10^-4

5.3e^-4


Range and Precision

The possible values that a floating type variable can store are described in terms of precision and range.

Precision

It is the number of digits after the decimal point.

Range

It is the exponential power of 10.

A float variable has a precision of 6 digits and a range of 10 to 10 Suppose we have a real value 0.1234567 x 10^3 The precision of this value is 7 and range is 3. Similarly a value 0.123456 x 10^-3 has a precision of 6 digits and a range of -3.

A double variable has a precision of 15 digits and a range of 10-00 to 10-30 A large precision provides more accuracy The double data type is more suitable than float if the value to be stored requires more precision.

Post a Comment

0 Comments