Define Type Casting In Programming & The Comments And "Sizeof" Operator In Cpp

Type Casting

The process of converting the data type of a value during execution is known as type casting.

Type casting can be performed in two ways:

  • Implicit type casting
  • Explicit type casting
Type Casting

Implicit Type Casting

Implicit type casting is performed automatically by the C++ compiler. The operands in arithmetic operation must be of similar types. An expression in which operands are of different data types is called mixed-type expression. In this case, the result of an expression is evaluated to larger data type in the expression. Suppose an expression contains an integer and double as operands. The result will be evaluated to a double data type.

Different types of variables are as follows:

6. Long double (Highest datatype)

5. Double

4. Float

3. Long

2. Int

1. char (lowest data types)


Some example of implicitly conversions are as follows:

Expression

Intermediate Types

Char + float

Float

Int – loop

Long

Int * double

double

Float / long double

Long double


Example
Suppose x is an integer and y is a long variable and following expression is evaluated:

x + y

In the above expression, data type of x is lower than data type of y. The value of x will be converted into long during the evaluation of expression. The data type of x is not changed. Only the data type of the value of x is changed during the evaluation of expression.

Define type casting and its types?

The process of converting the data type of a value during execution is known as type casting Two types of casting are implicit type casting and explicit type casting.

Explicit Casting

Explicit casting is performed by the programmer using the cast operator. The cast operator tells the computer to convert the data type of a value.

Syntax

The syntax of using cast operator is as follows:

(type) expression;

type

It indicates the data type to which operand is to be converted. 

expression

It indicates the constant, variable or expression whose data type is to be converted.

Example:

Suppose x and y are two float variables, x contains 10.3 and y contains 5.2 and the following expression is evaluated.

x %  y

The above expression will generate an error because data type of x and y is float. The modulus operator cannot work with float variables. 

It only works with integers. The above expression can be written as follows:

(int) x % (int) y

The above statement converts the values of x and y into integers and evaluates the expression. The value of x will be converted to 10 and the value of y will be converted to 5. The result of the above expression will be 0.

C++ Program that divides two float variables and finds the remainder by using explicit casting.

#include<iostream>

#include<conio.h>

void main( )

{

Clrscr( )

{

float a,b;

int c;

a=10.3;

b=5.3;

c=(int)a % (int)b;

cout<<”Result is”<<c;

getch( );

}


How above Program Works?

The above program finds the remainder of two float variables using modulus operator. The modulus operator cannot work with float variables so the type casting is used to convert these variables into integers in the expression. The result of the expression will be 0.

The "sizeof" Operator

The sizeof operator is used to find the size of any data value. It gives the number of bytes occupied by that value.
"sizeof" Operator

Syntax:

The syntax of using this operator is:
The operand can be a variable or a constant:

Example:

Following are some examples of using sizeof operator:
sizeof(n);
sizeof(10);
sizeof("Pakistan");

#include <iostream.h>

#include <conio.h>

void main()

{

clrscr();

cout<<"size of char = "<<sizeof(char)<<endl;

cout<<"size of int = "<<sizeof(int)<<endl;

cout<<"size of float = "<<sizeof(float)<<endl;

cout<<"size of long = "<<sizeof(long)<<endl;

cout<<"size of double = "<<sizeof(double)<<endl;

cout<<"size of long double = "<<sizeof(long double)<<endl;

getch();

}

Note: 

The output of the above program is according to Turbo C++ Different compilers may show different output.

Comments

Comments are the lines of program that are not executed. The compiler ignores comments and does not include them in the executable program. That is why the comments do not affect the size of executable program.

Comments are used to increase the readability of the program. They are notes about different lines of the code to explain the purpose of the code. It helps in debugging and modifying the program later Comments can be added anywhere in programs.

What is the purpose of comment? Write its syntax.

Comments are the explanatory lines of a program that help user to understand the source code They are inserted to explain the purpose of the code. The syntax of single line comments is // The syntax of multiline comments is//.

Types of Comments

Comments can be added in programs in two ways:

Sing-line Comments

The comments on single line are added by using double slash "//". Anything written on the right side of double slash is considered as comments and is ignored during execution.

Multi-line Comments

The multi-line comments are written in the program by placing /* at the beginning of the comments. The symbol */ is used to end the multi-line comments The /* and */ symbols can also be used to enter comments on a single line.

Give three reasons why it's important to comment the code?

It is important to comment the code because it makes easier for other people to understand the program. It helps the programmer to organize and write the code. It also helps the programmer to track down and debug errors.

Where can you add the comments in programs?

Comments can be added anywhere in the program. Comments can be added in programs in two ways. The user can write comments on single line or on multiple lines.

How are comments added on single line?

Comments on single line are added by using double slash"//" Anything written on the right side of double slash is considered as comments and is ignored during execution.

How are comments added on multiple lines?

Comments on multiple lines are added by using "/* and */ symbols. Anything written between two symbols is considered as comments and is ignored during execution.

Why comments do not affect the size of program?

The compiler ignores comments and does not include them in the executable program. That is the comments do not affect the size of executable program.

Post a Comment

0 Comments