OUTPUT HANDLING in C++ - Text Book Exercise and Question Answers - Computer Science For Class XSSC Part 1 and 2
OUTPUT HANDLING in C++ - Text Book Exercise and Question Answers - Computer Science For Class XSSC Part 1 and 2
Home ▼
GO TO INDEX
Computer Science For Class X
Unit 03: Input / Output Handling In C++
Question Answers
By Mrs. Ayesha Arif
Vice Principal
DETAILED NOTES
A C++ program is structured in a specific and particular manner. In C++, a program is divided into the following three
sections:
1. Preprocessor Directives
2. Main Function Header
3. Body of Program /Function
1. PREPROCESSOR DIRECTIVES:
a. #include:
The # symbol is called preprocessor directive. #include is a specific preprocessor command that effectively copies and
pastes the entire text of the file. It links the external header files / libraries which may be required in program.
b. <iostream>:
The file <iostream>, which is a standard file that should come with the C++ compiler, is short for input-output streams.
This command contains code for displaying and getting an input from the user.
c. namespace std:
It is a prefix that is applied to all the names in a certain set. This instruction tells the compiler to use standard
namespace. It is a collection of identifiers . It is used for variables, functions, class and objects . All these elements of
the Standard library of C++ are declared within standard “std”.
a. int main( ):
The starting point of all C++ programs is the int main( ) function. This function is called by the operating system when
your program is executed by the computer .int main ( ) return value zero.
(int main(void) is used for execution of C++ program. void main(void) nothing to return.)
b. { :
It signifies the start of a block of code or indicates the beginning of the main function. It is also known as opening curly
braces.
c. } :
This signifies the end of a block of code or indicates the ending of the main function. It is also known as closing curly
braces.
The body of the function is enclosed between curly braces. All instructions are executed within opening "{" and closing "}"
curly braces.
a. Statements:
Instructions that perform a particular task are called statements . Statement terminator (;) is used to end a statement.
The symbol is called semi-colon.
b. cout :
The name cout is short for character output and displays whatever is between the << brackets.
c. << :
Symbols such as << can also behave like functions and are used with the keyword cout.
For example:
d. The return:
The return keyword tells the program to return a value to the function int main.
e. After the return statement, execution control returns to the operating system component that launched this program.
(The return value; is the exit code of our program. By defualt, main( ) in C++ return an int data type value to the operating
system.)
A comment statement is where the programmer place a remark in the source code. The content of the comment is
ignored by the compiler.These statements do not execute. Through comments, the programmers give special remarks to
the statements for their convenience.
It is used for a single-line explanation with the help of a double slash (//) symbol. If the programmer wants to use a single
line comment on more than one line, this may need to put a double slash on each line at the start. These comments are
ignored by the compiler, which means comments are not executable.
For example:
#include<iostream>
int main( )
Statements…….;
return 0;
It is used for multiple-line explanations. Symbols (/* and */) are needed at the start and end of the statements. These
comments are ignored by the compiler, which means comments are not executable.
For Example:
#include<iostream>
int main( )
Statements…...
return 0;
Q No.3: Write a note on I/O streams or input/output handling in C++. List all the input and output functions of I/O
streams with descriptions.
In C++ input and output (I/O) are performed in the form of a sequence of bytes, commonly known as streams. These I/O
streams perform operation and are stored in header file. Such as <iostream>. These header files must be mentioned at
the beginning of the program.
Input Stream:
If the direction of flow of bytes is from the device (for example, Keyboard) to the main memory then this process is called
input.
Output Stream:
If the direction of flow of bytes is opposite, i.e. from main memory to device ( display screen ) then this process is called
output.
a. cout statement:
cout stands for "Character Output". Here 'C' menas "character" and 'Out' means "output".
Cout is a predefined object in C++. It is used to produce output on the standard output device which is
usually the display screen or monitor.
The data needed to be displayed on the screen is inserted in the standard output stream (cout) using the
insertion operator (<<).
Syntax: cout << variable or cout <<exp./string << variable
For Example:
b. puts():
For Example:
#include<iostream>
int main(void)
return 0;
OUTPUT:
MY FIRST PROGRAM
INPUT FUNCTIONS:
a. cin statement:
cin is a pre defined object that reads data from the keyboard with the extraction operator(>>).
It allows to accept data from standard input devices.
Syntax: cin >> varaible;
For Example:
#include<iostream>
int main(void)
int a;
return 0;
b. getch ():
It is a predefined function.
It is defined in conio.h.(Console input and output header file).
It stands for get character.
It is used to get a single character input from user (keyboard) during execution of program.
The entered character it is not displayed or printed on the screen.
It is used to hold the output screen until the user press any key from the keyboard.
For Example:
#include<iostream>
int main(void)
char ch = getch();
return 0;
c. getche ():
For Example:
#include<iostream>
#include<conio.h>
int main(void)
char ch0;
int a=10,b=10;
ch=getche();
>>a+b;
return 0;
OUTPUT:
d. getchar ():
It is function in C++ that reads the charcter from standard input stream . It is defined in <stdio.h> header
file.
It needs to press enter key after entering the character.
For Example:
#include<iostream.h>
#include<stdio.h>
int main(void)
char ch;
ch=getchar();
return 0;
OUTPUT:
e. gets():
It is a predefined function in C++ and it reads characters from stdin and stores them until a newline
character found.
It is defined in <cstdio> header file.
Program shows how to apply this function in C++.
Syntax: gets(variable);
For Example:
#include<iostream>
#include<stdio.h>
int main(void)
char ch[20];
get(ch);
return 0;
OUTPUT
Ans:STATEMENT TERMINATORS:
Statement terminators are semicolon (;) that indicates the end of the statement. Every statement in C++ must be
terminated with semi colon (;). If the terminator is missing an error message will occur.
The escape sequences are special non-printing characters that can be used with the “cout” in C++. It starts with a
backslash (\) and a code character.
Ans: OPERATORS:
Operators are special symbols that perform operations on variables and values. They are used for specific purposes.
They perform mathematical operations on operands.
For example: x + y.
Here
TYPES OF OPERATORS:
1. Arithmetic Operators
2. Increment Operators
3. Decrement Operators
4. Relational Operators
5. Logical Operators
6. Assignment Operators
7. Arithmetic assignment operators
1. Arithmetic Operators:
Arithmetic operators are used to perform arithmetic operations on variables and data. There are five different arithmetic
operators. They are:
i. Addition (+):
It is used to perform arithmetic addition.
Example: a + b;
Example: a - b;
Example: a * b;
Example: a/ b.
It is used to find remainder of a division. It returns the remainder of an integer value. Remainder operator is also
known as Modulus operator. This operator is used only with integral data types.
All arithmetic operators except Remainder or Modulus operator can be used in integer and float data type.
2. Increment Operators:
Increment operators are used to add 1 to the value of a variable. They can be used with any type of variable and are
represented by ++ (double plus ) sign. It can be applied to a single variable.
i.e. x=++a;
In prefix increment operation value of 'a' is printed as 11 because 1 is added in 'a' before printing.
In Postfix increment operation value of 'a' is printen as 10 because 1 is added in 'a' after printing. So the value of 'a'
will change to 11 after printing.
3. Decrement Operators:
Decrement operators are same as increment operators but they are used to subtract 1 from the value of a variable. They
can be used with any variable and are represented by -- (double minus ) sign.
Example --a
Example a—
4.Relational Operators:
A relational operator is used to test the relationship between two values (operands). All relational operators are binary
operators. They are also called comparison operators.
These operators must require two operands. The result of the comparison is True (1) or False(0).
5. Logical Operator:
Logical Operators are used to determine two relational expression. They operators can be used in many conditional and
relational expressions.
There are three logical operators that are used in C++ programming:
6. Assignment Operators:
The assignment operator (=) is used for assigning a variable to a value. This operator assigns the value of right side
expression to left side variable.
Such as x=10;
In arithmetic assignment operators , the arithmetic operator is combined with the assignment operator. The assignment
operator comes to the right of an arithmetic operator. This is also called compound assignment operator.
It adds the right operand to the left operand and assigns result to
1. += (Addition–Assignment) the left operand
It subtracts the right operand from the left operand and assigns
2. -= (Subtraction-assignment) result to the left operand.
It multiplies the right operand with the left operand and assigns
3. *= (Multiplication–Assignment) result to the left operand.
It divides the left operand with the right operand and assigns
4. /= (Division–Assignment) result to the left operand.
The assignment operator (=) is used for assigning a The equal to (= =) operator is used to check the
1.
variable to a value.
equality of two operands values.
Such as x=10
Ans: \a:
\r:
Carriage return "r". It is used to position the cursor to the beginning of the cursor line.
Ans: Ans. There are two types of comment statements used in C++.
It is used for a single-line explanation with the help of a double slash (//) symbol. If the programmer wants to use a single
line comment on more than one line, this may need to put a double slash on each line at the start. These comments are
ignored by the compiler, which means comments are not executable.
For Example:
#include<iostream>
int main( )
Statements…….;
return 0;
It is used for multiple-line explanations. Symbols (/* and */) are needed at the start and end of the statements. These
comments are ignored by the compiler, which means comments are not executable.
For Example:
#include<iostream>
int main( )
Statements…...
return 0;
The assignment operator (=) is used for assigning a The equal to (= =) operator is used to check the
1.
variable to a value.
equality of two operands values.
Such as x=10
S.NO. \n \t
1.
"t" stands for Horizontal tab. "n" stands for New line or line feed.
2. It is used to shift the cursor to a couple of spaces to It insert a new line and cursor moves to the
the right in the same line. beginning of the next line.
Ans: Output:
on
December 13, 2021
Share
No comments:
Post a Comment
‹ Home ›
View web version
Powered by Blogger.