Introduction
The c++ programming language was developed at AT&T Bell Laboratories in the early 1980 by Bjarne Stroustrup.
Where C++ term was coined by Rick Mascitti.
Why C++ ?
C++ is generally preferred over other programming language because it is more close to the hardware i.e. it gives more command over the hardware control.
Now lets see how C++ program looks like –
#include<iostream.h>
#include<conio.h>
void main(){
int num ;
cout<<"Enter a number - " ;
cin>>num ;
cout<<"The number given by user is = "<<num ;
getch();
}
Some terms related to the C++ programs are defined below .
Tokens
The smallest individual unit in a program is known as Token or a Lexical unit.
The various tokens are-
Keywords, Identifiers, Literals, Punctuators, Operators
- Keywords
Keyword is a word having special meaning reserved by programming language or for the compiler.
Ex –return, void, int, cout, cin, etc.
- Identifiers
Identifiers are the name given by user for a unit of the program.
Example from above code is – num.
An identifier can contain letters and digits. The first character must be a letter, the underscore. Upper and lower case letters are treated differently. All characters are significant.
- Literals
Literals are the data items that never changes their value during a program runs.
The various literals are – bool literal, integer-constant, character-constants, float-constants, string-literals.
- Bool Literal :- The literals used to represents true or false.
- Integer-Constants :- They are the whole numbers without any fractional part , like – 2,5,99,33etc.
- Character-Constants :- A character constant is one character enclosed in single quotes, like – ‘a’, ‘x’ , etc.
- Float-constants :- They are also called Real constants, or the number having fractional parts, like7.5,8,44,etc.
- String-constants :- Multiple character constants are treated as string-literals, like- “abcdef”.
Every string by default ends with null character i.e. “\0”.
- Punctuators
The symbols used as separators are called as punctuators.
Like- [ ] { } ( ) , ; : * = # etc.
Brackets [ ] indicate single and multidimensional array subscripts.
Parentheses ( ) indicate function calls and function parameters.
Braces { } indicates the start and end of a compound statement or body of function.
Comma , used as separator in function.
Semicolon ; used as sentence terminator.
- Operators
They are the tokens that triggers some computation when applied to variable and other objects in an expression.
The various operators are – Unary and Binary Operator.
- Unary Operators Those operators that require one operand to operate upon. Like - &, *, +, -, ++, --, !, etc.
- Binary Operators Those operators that require two operands to operate upon.The various binary operators are -
Shift operators:- >> , <<
Bitwise operators:- &, ^, |
Logical operator:- &&, || (i.e. and , or)
Relational operator:- >, <, <=, >=, ==, !=
Basic Example of Programming :-
#include<iostream.h>
#include<conio.h>
/* Above defined are Header files where all the reserved word used in program are defined*/
void main () {
/* main body where main program have to be written */
int numb ;
/* declaration of variable */
cout<<"Please enter an integer value = " ;
/* To display on output screen */
cin>>numb ;
cout<<"The Number you entered is = "<<numb ;
getch();
}
Comments :-
Comments are non-executable part of program , used to make the program more understandable.
For single line comments, use //
For multiple line comments, use /* */
What is #include<iostream.h> ?
Statements that begin with # sign are directives for the preprocessor. That means, these statements are processed before compilation of other program takes place.
The #include<iostream.h> statement tells the compiler to include the header file iostream in the program.
Similarly, #include<conio.h> statement tells the compiler to include the header file conio in the program.
What is cout<<” ”; ?
It is used for standard output purpose. It is predefined in iostream.h header file.
What is cin>> ; ?
It is used for standard input purpose. It is also predefined in iostream.h header file.
What is getch( ) ; ?
It is a predefined function in conio.h header file and is used to hold the output screen. The Output screen in some programming platform changes so quickly that it is difficult for the user to notice the output , so to hold the output screen until any key is pressed, getch() function is used.
Variables :-
A variable refers to a storage area whose contents can vary during processing. Variable are used to storage the data or information which is going to be used or to be processed in the program.
Example – int a=9, b=12;
Here , a and b are variables which store 9 and 12.
Data Type :-
Data type are means to identify the type of data and associated operations of handling it.
The various data types are – Fundamental datatype , Derived datatype and User defined datatype.
- Fundamental datatype :- Fundamental datatype are those that are not composed of other data types. Like – int , char, float, double .
int are integers or whole number such as 98 , -7, 0 etc, which does not have any fractional part. It generally takes 2 bytes of memory.
char is character enclosed in single quotes, like – ‘a’, ‘x’ , etc. It generally takes 1 byte of memory.
float is number with fractional part like – 89.4334, -8.232, 5567.53 etc. It generally takes 4 byte of memory.
double is number with fractional part but for more precision floating-point number. It generally takes 8 byte of memory.
- Derived datatype :- These datatypes are derived from fundamental datatype by using the declaration operators. Like – Array, Pointers, References, etc.
- User defined datatype :- These are some derived datatype that are defined by the user. Like – Class, Structure, Union, Enumeration.
Program to Print " Welcome to c++ programming ".
#include<iostream.h>
#include<conio.h>
void main(){
clrscr() ;
/* this is used to clear print screen */
cout<<"Welcome to c++ programming " ;
/* this will print the message */
getch() ;
}
Program to input a number from user and print it .
#include<iostream.h>
#include<conio.h>
void main(){
clrscr() ;
int number ;
cout<<"Enter the number please : " ;
cin>>number ;
cout<<"\n The number entered by you is = "<<number ;
getch() ;
}
Program to take 2 numbers input from user and add them and print their sum.
#include<iostream.h>
#include<conio.h>
void main(){
clrscr() ;
/* this is used to clear print screen */
int variable1, variable2, sum ;
/* the variable are declared to store the entered value and for the added value */
cout<<" Enter the first number = ";
cin>>variable1 ;
cout<<" Enter the second number = ";
cin>>variable2 ;
sum = variable1 + variable2 ;
cout<<"The summation is = "<<sum ;
getch() ;
}
Program to find the average marks of 5 subjects and marks should be entered by user.
#include<iostream.h>
#include<conio.h>
void main(){
clrscr() ;
int mark1, mark2, mark3, mark4, mark5 ;
float average ;
/* float is used for storing decimal value since average can have decimal part */
cout<<" Enter the 1st mark = " ;
cin>>mark1 ;
cout<<" Enter the 2nd mark = " ;
cin>>mark2 ;
cout<<" Enter the 3rd mark = " ;
cin>>mark3 ;
cout<<" Enter the 4th mark = " ;
cin>>mark4 ;
cout<<" Enter the 5th mark = " ;
cin>>mark5 ;
average = ( mark1 + mark2 + mark3 + mark4 + mark5 ) / 5 ;
cout<<" The average mark is = "<<average ;
getch() ;
}
Program to find the remainder by dividing a number by 7.
#include<iostream.h>
#include<conio.h>
void main(){
clrscr() ;
int numb, remainder ;
cout<<" Enter the number to be divided by 7 = " ;
cin>>numb ;
remainder = numb % 7 ;
cout<<" The remainder is = "<<remainder ;
getch() ;
}
_____________________________________________________________
0 comments:
Post a Comment
Please give your valuable suggestions