Understanding main() function Input/Output Streams

Similar documents
4. Structure of a C++ program

Probably the best way to start learning a programming language is with a program. So here is our first program:

Computer Programming : C++

3. Except for strings, double quotes, identifiers, and keywords, C++ ignores all white space.

Lab # 02. Basic Elements of C++ _ Part1

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

Programming. Computer. Program. Programming Language. Execute sequence of simple (primitive) instructions What instructions should be provided?

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.1

CSI33 Data Structures

UEE1302 (1102) F10: Introduction to Computers and Programming

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Chapter 2: Introduction to C++

Your First C++ Program. September 1, 2010

2 nd Week Lecture Notes

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

Starting Out with C++: Early Objects, 9 th ed. (Gaddis, Walters & Muganda) Chapter 2 Introduction to C++ Chapter 2 Test 1 Key

UNIT- 3 Introduction to C++

Programming with C++ as a Second Language

Chapter 1 INTRODUCTION

File I/O Christian Schumacher, Info1 D-MAVT 2013

CMPE110 - EXPERIMENT 1 * MICROSOFT VISUAL STUDIO AND C++ PROGRAMMING

Programming with C++ Language

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

Introduction to C++ IT 1033: Fundamentals of Programming

CHAPTER 3 BASIC INSTRUCTION OF C++

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 5: Control Structures II (Repetition)

Introduction to C++ (Extensions to C)

The sequence of steps to be performed in order to solve a problem by the computer is known as an algorithm.

Computer Programming

Why Is Repetition Needed?

LECTURE 02 INTRODUCTION TO C++

CSCI 1061U Programming Workshop 2. C++ Basics

Dr. Md. Humayun Kabir CSE Department, BUET

C++ character set Letters:- A-Z, a-z Digits:- 0 to 9 Special Symbols:- space + - / ( ) [ ] =! = < >, $ # ; :? & White Spaces:- Blank Space, Horizontal

A A B U n i v e r s i t y

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

I/O Streams and Standard I/O Devices (cont d.)

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

C++ Input/Output: Streams

Chapter 2. Outline. Simple C++ Programs

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

C++ Support Classes (Data and Variables)

CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++

UNIT-2 Introduction to C++

Chapter 1 - What s in a program?

Engineering Problem Solving with C++, Etter/Ingber

Getting started with C++ (Part 2)

Chapter 1 Introduction to Computers and C++ Programming

BITG 1233: Introduction to C++

Computer Science II Lecture 1 Introduction and Background

Your first C++ program

Unit-V File operations

LOGO BASIC ELEMENTS OF A COMPUTER PROGRAM

Chapter 1. C++ Basics. Copyright 2010 Pearson Addison-Wesley. All rights reserved

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

Streams. Ali Malik

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

CHAPTER-6 GETTING STARTED WITH C++

This watermark does not appear in the registered version - Slide 1

File Operations. Lecture 16 COP 3014 Spring April 18, 2018

Maciej Sobieraj. Lecture 1

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

DHA Suffa University CS 103 Object Oriented Programming Fall 2015 Lab #01: Introduction to C++

Scientific Computing

Creating a C++ Program

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

Introduction to Programming

C++ Programming: From Problem Analysis to Program Design, Third Edition

Chapter 1 Introduction to Computers and Programming

Lecture 3 Tao Wang 1

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

Introduction to C++ Chapter

Programming I Laboratory - lesson 01

Introduction to C++ Programming Pearson Education, Inc. All rights reserved.

CS 241 Computer Programming. Introduction. Teacher Assistant. Hadeel Al-Ateeq

The C++ Language. Arizona State University 1

Tutorial 2: Compiling and Running C++ Source Code

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

Program Organization and Comments

Chapter 2: Basic Elements of C++

CSc Introduction to Computing

Programming Language. Functions. Eng. Anis Nazer First Semester

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

A SHORT COURSE ON C++

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

OBJECT ORIENTED PROGRAMMING USING C++

C and C++ I. Spring 2014 Carola Wenk

Computer Skills (2) for Science and Engineering Students

Chapter 2 Basic Elements of C++

Introduction to C++ CS 1: Problem Solving & Program Design Using C++

1 Unit 8 'for' Loops

Structured Programming Using C++ Lecture 2 : Introduction to the C++ Language. Dr. Amal Khalifa. Lecture Contents:

CSCE 206: Structured Programming in C++

CSCE 206: Structured Programming in C++

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Manual. Subject Code: CS593. Computer Science and Engineering

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Chapter 5: Control Structures II (Repetition) Objectives (cont d.) Objectives. while Looping (Repetition) Structure. Why Is Repetition Needed?

Transcription:

Understanding main() function Input/Output Streams

Structure of a program // my first program in C++ #include <iostream.h> int main () { cout << "Hello World!"; return 0; } Output: Hello World!

// my first program in C++ This is a comment line. All lines beginning with two slash signs (//) are considered comments and do not have any effect on the behavior of the program. The programmer can use them to include short explanations or observations within the source code itself. In this case, the line is a brief description of what our program is. #include <iostream> Lines beginning with a hash sign (#) are directives for the preprocessor. They are not regular code lines with expressions but indications for the compiler's preprocessor. In this case the directive #include <iostream> tells the preprocessor to include the iostream standard file. This specific file (iostream) includes the declarations of the basic standard input-output library in C++, and it is included because its functionality is going to be used later in the program.

int main () This line corresponds to the beginning of the definition of the main function. The main function is the point by where all C++ programs start their execution, independently of its location within the source code. It does not matter whether there are other functions with other names defined before or after it - the instructions contained within this function's definition will always be the first ones to be executed in any C++ program. For that same reason, it is essential that all C++ programs have a main function. The word main is followed in the code by a pair of parentheses (()). That is because it is a function declaration: In C++, what differentiates a function declaration from other types of expressions are these parentheses that follow its name. Optionally, these parentheses may enclose a list of parameters within them. Right after these parentheses we can find the body of the main function enclosed in braces ({}). What is contained within these braces is what the function does when it is executed.

cout << "Hello World!"; This line is a C++ statement. A statement is a simple or compound expression that can actually produce some effect. In fact, this statement performs the only action that generates a visible effect in our first program. cout is the name of the standard output stream in C++, and the meaning of the entire statement is to insert a sequence of characters (in this case the Hello World sequence of characters) into the standard output stream (cout, which usually corresponds to the screen). cout is declared in the iostream standard file within the std namespace, so that's why we needed to include that specific file and to declare that we were going to use this specific namespace earlier in our code. Notice that the statement ends with a semicolon character (;). This character is used to mark the end of the statement and in fact it must be included at the end of all expression statements in all C++ programs (one of the most common syntax errors is indeed to forget to include some semicolon after a statement).

return 0; The return statement causes the main function to finish. return may be followed by a return code (in our example is followed by the return code with a value of zero). A return code of 0 for the main function is generally interpreted as the program worked as expected without any errors during its execution. This is the most usual way to end a C++ console program.

Input/Output - Streams The most basic stream types are the standard input/output streams: Istream cin built-in input stream variable; by default hooked to keyboard Ostream cout built-in output stream variable; by default hooked to console Header file: <iostream.h> The input and output streams, cin and cout are actually c++ objects. Istream is actually a type name for a class. Cin is the name of a variable of type istream. So, we would say that cin is an instance or an object of the class istream.

In order to use the standard I/O streams, we must have in out program the pre-compiler directive: #include<iostream.h> In order to do output to the screen, we merely use a statement like: Cout << x= << x; // Hint : the insertion operator (<<) points in the //direction the data is flowing. Where x is the name of some variable or constant that we want to write to the screen. In order to do Input from the keyboard, Statement is. Cin >> x; Where x is the name of some variable that we want to store the value that will be read from the keyboard.