#include<iostream>using namespace std; void main() { cout<<“Hello World”;}
If your compiler gives error then
#include<iostream.h>void main() { cout<<“Hello World”; cout<<endl; // for going to new line cout<<“ good bye”;}
cout is an object for printing out some thing on the screen.
#include<iostream.h>void main() { cout<<“Hello World \n”; cout<<“ good bye”;}
we can use \n instead of cout<<endl;
How to run your program
• Using Linux or unix:g++ myFile.cpp
./a.out
g++ myFile.cpp myFile.out
./myFile.out
Declare Statement & Variable• Each variable must be declare before use• Each variable has a type:
For example int , char, float. int for Integer type; char for character like ‘A’; float for real number;
Identifiers
• Identifier: name of a variable, function, or class • Rules for identifiers in C++:
1 Can be made up of letters, digits, and the underscore (_) character
2 Cannot start with a digit 3 Cannot use other symbols such as ? or % 4 Spaces are not permitted inside identifiers 5 You cannot use reserved words 6 They are case sensitive
Self Check
1. What is the type of the values 0 and ‘0’?
2. Which of the following are legal identifiers?
Greeting1gvoid101dalmatiansHello, World<greeting>
Syntax : Variable Definition
• typeName variableName = value;or typeName variableName;
Example :int number = 12;
Purpose:To define a new variable of a particular type and optionally
supply an initial value
The Assignment Operator
• Assignment operator: =
• Not used as a statement about equality
• Used to change the value of a variable
int number1; int number2, number3;
number1=number2=number3=88;
How to read a variable
#include<iostream>using namespace std; void main() { int number; cout<<“please enter a number \n”; cin>>number; number=number+1; cout<<“the number is “<<number;}
Integer Types
• The short, int and long Integer Types
A short integer is at least 16 bits
An int integer is at least as big as short
A long integer is at least 32 bits and at
least as big as int .
• E.g. A 16-bit int might run from -32768 to 32767
• The sizeof operator returns the size (in bytes)
#include<iostream.h>int main() { int n_int = INT_MAX; short n_short = SHRT_MAX; long n_long = LONG_MAX; cout << “int is “ << sizeof (int) << “ bytes” << endl; cout << “short: “ << n_short << endl; cout << “long: “ << n_long << endl; return 0;}
Characters and small integers#include<iostream.h>int main() { char ch = ‘M’; // assign ASCII code int a = ch; cout << “ASCII code: “ << ch << “ is “ << a << endl; ch = ch + 1; a = ch; cout << “ASCII code: “ << ch << “ is “ << a << endl;
return 0;}
Boolean type
• bool isReady = true;
• int ans = true; // ans assigned 1
• int promise = false; // promise assigned 0
• bool start = -100; // true
• bool stop = 0; // false