HOW TO PROGRAM IN C++

17
Introduction to C- Language Lecture 3

Transcript of HOW TO PROGRAM IN C++

Page 1: HOW TO PROGRAM IN C++

Introduction to C-Language

Lecture 3

Page 2: HOW TO PROGRAM IN C++

First Program in C++

Page 3: HOW TO PROGRAM IN C++

Preprocessor Directive # is called Preprocessor Directive. The # line tells the compiler to use a file

<iostream.h> or <stdio.h> or whatever written in <angle brackets>.

Files having .h extension in C/C++ are called header files. They are also sometimes called include files.

The iostream.h file is included in the program as it contains the information about the “cout” identifier and the << operator. Normally all the header files of C/C++ are present in the INCLUDE directory

Page 4: HOW TO PROGRAM IN C++

Main Function A C/C++ program may consist of many

functions, classes and other program elements, but on startup, control always goes to main() function.

The first statement executed by the C/C++ compiler will be the one that is the first statement in function

void main(void) or

void(main)or

main()

Page 5: HOW TO PROGRAM IN C++

Using Comments in the Program

// It is a C++ Program#include <iostream.h>void main(void) //main function{

/* These linesare thepart of comments andwill not execute*/

cout<<"We are studying C++";}

Page 6: HOW TO PROGRAM IN C++

Using Comments in the Program

There are two ways of specifying comments in C++.

i) Using // ii) /* and */

Page 7: HOW TO PROGRAM IN C++

Defining and using Integer Variables

#include <iostream.h>void main(void){

int a;int b;a = 10;b = a + 5;cout<<"A is "<<a <<endl;cout<<"B is "<<b <<endl;

}

OutputA is 10B is 15

Page 8: HOW TO PROGRAM IN C++

Defining and using Integer Variables

#include <iostream.h>#include <conio.h>void main(void){ int a,b;

a = 10; b = a + 5;clrscr();cout<<"A is "<<a <<" and B is "<<b<<endl;cout<<"Press any key to finish";getch();

}

OutputA is 10 and B is 15Press any key to finish

Page 9: HOW TO PROGRAM IN C++

Defining and using Integer Variables

#include <iostream.h>#include <conio.h>void main(void){ clrscr();

char first=65;int second=964;float third=5.543;cout<<first<<endl<<second<<endl<<third;getch();

}

OutputA9645.543

Page 10: HOW TO PROGRAM IN C++

Using Escape Sequences in the Program

#include <iostream.h>#include <conio.h>void main(void){ clrscr();cout<<"Hello\nHow\nAre\nYou\n";cout<<“Hello\n\tHow\n\t\tAre\n\t\t\tYou\n”;getch();}

Page 11: HOW TO PROGRAM IN C++

Using Escape Sequences in the ProgramOutput

Page 12: HOW TO PROGRAM IN C++

Arithmetic operators

Plus +Minus -Multiply *

Divide / Modulus %

Page 13: HOW TO PROGRAM IN C++

Arithmetic operators

i + jx * ya / ba % b

Page 14: HOW TO PROGRAM IN C++

% = Remainder

5 % 2 = 12 % 2 = 0

Page 15: HOW TO PROGRAM IN C++

4 / 2 = 25 / 2 = ?

Page 16: HOW TO PROGRAM IN C++

Precedence Highest: ( ) Next: * , / , % Lowest: + , -

Page 17: HOW TO PROGRAM IN C++

Using Escape Sequences in the Program

Assignment # 1 What is escape sequence, write all escape

sequence characters along with their usage in C/C++.