[go: up one dir, main page]

0% found this document useful (0 votes)
60 views10 pages

EE 355 A: Computer Programming in C++ (E) : (Lecture-2) Topic

This document provides a summary of variables and data types in C++ including: 1) Variables are used to store and manipulate data in C++. Variables must follow naming conventions and cannot be keywords. 2) C++ supports basic data types like int, char, float, and boolean. Each data type occupies a different amount of memory. 3) The document demonstrates how to declare variables, take input, display output, and perform type casting in C++. Both implicit and explicit type conversion methods like cast operators are covered.

Uploaded by

lucky
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views10 pages

EE 355 A: Computer Programming in C++ (E) : (Lecture-2) Topic

This document provides a summary of variables and data types in C++ including: 1) Variables are used to store and manipulate data in C++. Variables must follow naming conventions and cannot be keywords. 2) C++ supports basic data types like int, char, float, and boolean. Each data type occupies a different amount of memory. 3) The document demonstrates how to declare variables, take input, display output, and perform type casting in C++. Both implicit and explicit type conversion methods like cast operators are covered.

Uploaded by

lucky
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

EE 355 A: Computer Programming in C++ (E)

(Lecture-2)

Topic:
• Variables & Data types in C++
• C++ Basic Input/output
• Type Casting in C++

Department of Electrical engineering


MBM Engineering College, Jodhpur
2
C++ Variables
a variable is a container (storage area) to hold data.
example, // The int data type suggests that the variable can only hold integers.
int b = 7;
The value of a variable can be changed, hence the name variable.
• A variable name can only have alphabets, numbers, and the underscore _.
int b = 7; // b is 7
• A variable name cannot begin with a number.
b = 10; // b is 10 • Variable names should not begin with an uppercase character.
• A variable name cannot be a keyword.

C++ Constants
In C++, we can create variables whose value cannot be changed. For that, we use the const keyword.

Example:
const int xyz = 10;
xyz = 25 // Error! xyz is a constant.

3
Data Type in C++
Data of different types take a different amounts of memory to store. In C++, data types are
declarations for variables. This determines the type and size of data associated with variables. Here
are the built-in datatypes we will use most often:

Type Keyword
Boolean bool
Character char
Integer int
Floating point float
Double floating point double
Valueless void

4
C++ Type Modifiers
Type Typical Bit Width Typical Range
char 1byte -127 to 127 or 0 to 255
unsigned char 1byte 0 to 255
signed char 1byte -127 to 127
int 4bytes -2147483648 to 2147483647

unsigned int 4bytes 0 to 4294967295


signed int 4bytes -2147483648 to 2147483647

short int 2bytes -32768 to 32767


unsigned short int 2bytes 0 to 65,535
signed short int 2bytes -32768 to 32767
long int 8bytes -2,147,483,648 to 2,147,483,647

signed long int 8bytes same as long int


unsigned long int 8bytes 0 to 4,294,967,295
long long int 8bytes -(2^63) to (2^63)-1 5
C++ Basic Input/output
include <iostream>
using namespace std;

int main()
{
int num1 = 50;
double num2 = 25.7;
char ch = 'A';

cout << num1 << endl; // print integer


cout << num2 << endl; // print double
cout << "character: " << ch << endl; // print char
return 0;
}

Output
50
25.7
character: A
6
C++ Taking Multiple Inputs/outputs

#include <iostream>
using namespace std;
Output
int main( )
{ Enter a character and an integer: A
char a; 25
int num; Character: A
Number: 25
cout << "Enter a character and an integer: ";
cin >> a >> num;

cout << "Character: " << a << endl;


cout << "Number: " << num;

return 0;
}

7
Type conversion.
C++ allows us to convert data of one type to that of another. This is known as type conversion.

There are two types of type conversion in C++.


• Implicit Conversion
• Explicit Conversion (also known as Type Casting)

Implicit Conversion (Automatic Conversion)


The type conversion that is done automatically done by the compiler is known as implicit type conversion.
// Working of implicit type-conversion
#include <iostream>
using namespace std;

int main()
{ // assigning an int value to num_int cout << "num_int = " << num_int << endl;
int num_int = 9; cout << "num_double = " << num_double << endl;

// declaring a double type variable return 0;


double num_double; }

// implicit conversion Output


num_double = num_int; num_int = 9 8
num_double = 9
C++ Explicit Conversion
Changing data from one type to another, this is known as explicit conversion.
This type of conversion is also known as type casting.
• C-style type casting (also known as cast notation)
• Function notation (also known as old C++ style type casting)
• Type conversion operators

C-style Type Casting


It is also known as cast notation.

The syntax for this style is:

(data_type)expression;
For example,

// initializing int variable // declaring double variable


int num_int = 26; double num_double;

// converting from int to double


num_double = (double)num_int;
9
Type Casting
Output
#include <iostream>
num_double = 3.56
using namespace std; num_int1 = 3
num_int2 = 3
int main()
{
// initializing a double variable Type casting using Cast operator
double num_double = 3.56;
cout << "num_double = " << num_double << endl; #include <iostream>
using namespace std;
// C-style conversion from double to int int main()
int num_int1 = (int)num_double; {
cout << "num_int1 = " << num_int1 << endl; float f = 3.5;

// function-style conversion from double to int // using cast operator


int num_int2 = int(num_double); int b = static_cast<int>(f);
cout << "num_int2 = " << num_int2 << endl;
cout << b;
return 0; } 10
}

You might also like