Lecture No. 03 [Basic Input and Output Statements]
Lecture No. 03 [Basic Input and Output Statements]
Prepared by: ARSHAD IQBAL, Lecturer (CS), Computer Science Department, CECOS University of IT & Emerging Sciences, Peshawar.
Topics:
Basic Input and Output Statements in C/C++.
scanf ()/cin
printf ()/cout
How to take input and output of basic data types in C/C++?
How to take input and output of advanced data type in C/C++?
Prepared by: ARSHAD IQBAL, Lecturer (CS), Computer Science Department, CECOS University of IT & Emerging Sciences, Peshawar.
Basic Input and Output Statements in C/C++:
C/C++ language has standard libraries that allow input and output in a program.
The stdio.h (standard input output library) in C that has methods for input and output while
the iostream.h (input output stream library) in C++ that has methods for input and output.
scanf ()/cin:
In C programming language, scanf is a function that stands for Scan Formatted String while in C++,
cin is used which means "character input". The cin object is used along with the extraction operator
>> in order to receive a stream of characters as it is defined in the iostream header file.
Scanf function is used to read data from stdin (standard input stream i.e. usually keyboard).
Scanf accepts character, string, and numeric data from the user using standard input.
Scanf also uses format specifiers like printf.
The scanf () method, in C, reads the value from the console as per the data type specified and store it
in the given address.
Syntax:
scanf ("%X", &variableOfXType);
Why &?
While scanning the input, scanf needs to store that input data somewhere.
To store this input data, scanf needs to know the memory location of a variable.
Cin:
Syntax:
Cin>>var_name; (Syntax in C++)
Example:
Cin>>var;
The cin will write the value input by the user into the integer variable var.
Prepared by: ARSHAD IQBAL, Lecturer (CS), Computer Science Department, CECOS University of IT & Emerging Sciences, Peshawar.
Example of scanf/cin:
// Driver code
int main ()
{
int a, b;
printf ("Enter first number: "); //cout<< “enter first number”;
scanf ("%d", &a); //cin>>a;
printf ("Enter second number: "); //cout<< “enter second number”;
scanf ("%d", &b); //cin>>b;
printf ("A: %d \t B: %d", a, b); //cout<< “A: ”<<a<< “ B: ”<<b;
return 0;
}
Output
Enter first number: 5
Enter second number: 6
A: 5 B: 6
printf ()/cout:
In C language, printf () function is used to print formatted output to the standard output stdout
(which is generally the console screen) while cout in C++ is used to display output to the screen.
The 'c' in cout stands for character, and 'out' stands for output. Thus, cout refers to character output.
The printf function is a part of the C standard library <stdio.h> and it can allow formatting the output
in numerous (many) ways while the cout function is a part of the C++ standard library <iostream.h>.
The printf () method, in C, prints the value (passed as the parameter to it), on the console screen.
Syntax:
printf ("%X", variableOfXType);
Where %X is the format specifier in C. It is a way to tell the compiler what type of data is in a variable
and variableOfXType is the variable to be printed.
Example format specifiers recognized by printf:
It is the character that denotes the type of data. Some commonly used specifiers are:
“%d”: for printing integers
“%f”: for printing floating-point numbers
“%c”: for printing characters
“%s”: for printing strings
“%p”: for printing memory addresses
“%x”: for printing hexadecimal values
Example:
printf ("%c", char_variable);
Prepared by: ARSHAD IQBAL, Lecturer (CS), Computer Science Department, CECOS University of IT & Emerging Sciences, Peshawar.
Cout:
Syntax:
Cout<<sample_output;
Here << is the insertion operator used in C++ to print the output. It is followed by the output that
is to be printed on the screen. This output might be some variable, constant, array element.
Example of printf/cout:
In this example, we are printing the string “Hello Geek!” in the output using printf () function.
In printf () function what we will write inside the double quotes (“ ”) is printed in the output.
int main ()
{
// using printf to print "Hello Geek!"
printf ("Hello Geek!"); //cout<< “Hello Geek!”; in C++
return 0;
}
The C/C++ syntax for input and output for these are:
Integer: C C++
Input: scanf ("%d", &intVariable); int variable_name cin>>variable_name;
Output: printf ("%d", intVariable); cout<<sample_output;
Float:
Input: scanf ("%f", &floatVariable); float variable_name cin>>variable_name;
Output: printf ("%f", floatVariable); cout<<sample_output;
Character:
Input: scanf ("%c", &charVariable); char variable_name cin>>variable_name;
Output: printf ("%c", charVariable); cout<<sample_output;
Prepared by: ARSHAD IQBAL, Lecturer (CS), Computer Science Department, CECOS University of IT & Emerging Sciences, Peshawar.
Example:
// C/C++ program to show input and output.
C++
#include <stdio.h> #include<iostream.h>
int main ()
{
// Declare the variables
int num;
char ch;
float f;
return 0;
}
Output
Enter the integer: 10
Entered integer is: 10
Prepared by: ARSHAD IQBAL, Lecturer (CS), Computer Science Department, CECOS University of IT & Emerging Sciences, Peshawar.
How to take input and output of advanced data type in C/C++?
The advanced data type in C/C++ includes: String.
In order to input or output the string data type, the X in the above syntax is changed with
the %s format specifier.
C C++
Input: scanf ("%s", stringVariable); cin >> name_of_the_string;
Output: printf ("%s", stringVariable); cout<<name_of_the_string;
Example:
return 0;
}
Output
Enter the Word: GeeksForGeeks
Entered Word is: GeeksForGeeks
Prepared by: ARSHAD IQBAL, Lecturer (CS), Computer Science Department, CECOS University of IT & Emerging Sciences, Peshawar.