Chapter 2 Basics of Programming
Chapter 2 Basics of Programming
Chapter Two
Basics of C++ Programming Language
float and double are also types for variables with large and floating point values
Cont’d….
Example
#include <iostream>
using namespace std;
int main()
{
int x=10; // Initialized once
int y=20; // Initialized again
cout <<“x = "<< x<<endl;
cout <<“y= "<< y<<endl;
cout <<“First value of x = "<< x;
cout <<“The value of y= "<< y;
return 0;
}
#Question: What will be the out put?
Basics of Data Types
Data types are sets (ranges) of values that have similar
characteristics.
Several data types are built into C++.
Basic (fundamental) data types in C++ can be
conveniently divided into numeric and character types.
Numeric variables can further be divided into integer
variables and floating-point variables.
The numeric data type can be short, long, signed and
unsigned.
Signed integers are either negative or positive.
Unsigned integers are always positive.
Cont’d.....
Data Type Size Range of Values
unsigned short int 2 bytes 0 to 65,535
short int(signed short int) 2 bytes -32,768 to 32,767
unsigned long int 4 bytes 0 to 4,294,967,295
long int(signed long int) 4 bytes -2,147,483,648 to 2,147,483,647
int 2 bytes -32,768 to 32,767
unsigned int 2 bytes 0 to 65,535
signed int 2 bytes -32,768 to 32,767
Char 1 byte 256 character values
Float 4 bytes 3.4e-38 to 3.4e38
Double 8 bytes 1.7e-308 to 1.7e308
long double 10 bytes 1.2e-4932 to 1.2e4932
Example to display correct size of various data types
// correct size of various data types
#include<iostream>
using namespace std;
int main()
{
cout <<"Size of char : "<<sizeof(char)<< endl;
cout <<"Size of int : "<<sizeof(int)<<endl;
cout <<"Size of short int : "<<sizeof(short int)<< endl;
cout <<"Size of long int : "<<sizeof(long int)<< endl;
cout <<"Size of float : "<<sizeof(float)<< endl;
cout <<"Size of double : "<<sizeof(double)<< endl;
cout <<"Size of wchar_t : "<<sizeof(wchar_t)<< endl;
return 0;
}
Operators
C++ provides operators for different expressions.
2. Assignment operators
3. Relational operators
4. Logical operators
5. Increment/decrement Operators
6. Precedence operators.
Arithmetic (operand) operators
== Equality 5 == 5 // gives 1
!= Inequality 5 != 5 // gives 0