0% found this document useful (0 votes)
11 views

Lecture No. 03 [Basic Input and Output Statements]

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Lecture No. 03 [Basic Input and Output Statements]

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Discipline: BS (CS) 1st Semester

Subject: Programming Fundamentals


Week No.: 03 [Basic Input/Output Statements]
Prepared by: ARSHAD IQBAL, Lecturer (Computer Science),
Computer Science Department, CECOS University of IT & Emerging Sciences,
Peshawar

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);

 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 & is the address operator in
C, scanf store the input data at this address in the memory.

 Format Specifiers recognized by scanf:


 %d or %i to accept input of integers.
 %ld to accept input of long decimal integers.
 %lld to accept input of long long decimal integers.
 %f to accept input of real/float number.
 %lf to accept input of double number.
 %Lf to accept input of long double number.
 %c to accept input of character types.
 %s to accept input of a string.
 Example:
Int var;
scanf (“%d”, &var);
 The scanf will write the value input by the user into the integer variable var.

 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:

// C/C++ program to implement


// scanf
#include <stdio.h>
//#include<iostream.h> for 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.

// C/C++ program to illustrate the use of printf function


#include <stdio.h> //#include<iostream.h> for C++

int main ()
{
// using printf to print "Hello Geek!"
printf ("Hello Geek!"); //cout<< “Hello Geek!”; in C++

return 0;
}

How to take input and output of basic data types in C/C++?


 The basic data type in C/C++ includes: int, float, char, etc.
 In order to input or output the specific data type, the X in the above syntax is changed with the specific
format specifier of that data type.

 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;

// Input the integer


printf ("Enter the integer: "); cout<< “Enter the integer: ”;
scanf ("%d", &num); cin>>num;

// Output the integer


printf ("\nEntered integer is: %d", num); cout<< endl<< “Entered integer is: ”<<num;

// Input the float


printf ("\n\nEnter the float: "); cout<<endl<<endl<< “Enter the float: ”;
scanf ("%f", &f); cin>>f;

// Output the float


printf ("\nEntered float is: %f", f); cout<<endl<< “Entered float is: ”<<f;

// Input the Character


printf ("\n\nEnter the Character: "); cout<<endl<<endl<< “Enter the Character: ”;
scanf ("%c", &ch); cin>>ch;

// Output the Character


printf ("\nEntered character is: %c", ch); cout<<endl<< “Entered character is: ”<<ch;

return 0;
}

Output
Enter the integer: 10
Entered integer is: 10

Enter the float: 2.5


Entered float is: 2.500000

Enter the Character: A


Entered Character is: A

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.

 The Syntax for input and output for String is:

C C++
Input: scanf ("%s", stringVariable); cin >> name_of_the_string;
Output: printf ("%s", stringVariable); cout<<name_of_the_string;

Example:

// C/C++ program to show input and output

#include <stdio.h> #include<iostream.h>


int main ()
{
// Declare string variable
// as character array
char str [50];

// --- String ---

// Input the Word


printf ("Enter the Word: "); cout<< “Enter the Word: ”<<endl;
scanf ("%s\n", str); cin>>str;

// Output the Word


printf ("\nEntered Word is: %s", str); cout<<endl<< “Entered Word is: ”<<str;

// --- String ---


// Input the Sentence
printf ("\n\nEnter the Sentence: "); cout<< “Enter the sentence: ”<<endl;
scanf ("%[^\n]s", str); >>str;

// Output the String


printf ("\nEntered Sentence is: %s", str); cout<<endl<< “Entered Sentence is: ”<<str;

return 0;
}

Output
Enter the Word: GeeksForGeeks
Entered Word is: GeeksForGeeks

Enter the Sentence: Geeks For Geeks


Entered Sentence is: Geeks For Geeks

Prepared by: ARSHAD IQBAL, Lecturer (CS), Computer Science Department, CECOS University of IT & Emerging Sciences, Peshawar.

You might also like