EE 355 A: Computer Programming in C++ (E) : (Lecture-2) Topic
EE 355 A: Computer Programming in C++ (E) : (Lecture-2) Topic
(Lecture-2)
Topic:
• Variables & Data types in C++
• C++ Basic Input/output
• Type Casting in C++
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
int main()
{
int num1 = 50;
double num2 = 25.7;
char ch = 'A';
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;
return 0;
}
7
Type conversion.
C++ allows us to convert data of one type to that of another. This is known as type conversion.
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;
(data_type)expression;
For example,