KEMBAR78
Input Output function in c programing language.pptx
Click to edit Master title style
1
Title: "Exploring
Input/output Function in C
Programming"
Presented By
Amit Kumar, Asma Shamshad
A nd N andini Singh
Click to edit Master title style
2
Different types of I/O
functions
• Standard Input/Output
functions
• Command line arguments
Functions
2
Click to edit Master title style
3 3
Click to edit Master title style
4
Formatted I/O
Functions
4
 Formatted I/O functions are used to take
various inputs from the user and display
multiple outputs to the user.
 These types of I/O functions can help to display
the output to the user in different formats using
the format specifiers.
 These I/O supports all data types like int, float,
char, and many more.
Click to edit Master title style
5
List of some format specifiers
5
S NO. Format
Specifier
Type Description
1. %d int/signed int used for I/O signed integer value
2. %c char Used for I/O character value
3. %f float Used for I/O decimal floating-point value
4. %s string
Used for I/O string/group of characters
5. %lf double Used for I/O fractional or floating data
6. %n double prints nothing
Click to edit Master title style
6
The following formatted I/O functions will be discussed
in this section-
printf()
scanf()
sprintf()
sscanf()
6
Click to edit Master title style
7
Printf()
7
 printf() function is used in a C program to display
any value like float, integer, character, string, etc
on the console screen.
 It is a pre-defined function that is already declared
in the stdio.h(header file).
 printf(“Format Specifier”, var1, var2, …., varn);
Click to edit Master title style
8
Example:
8
#include <stdio.h>
int main()
{
// Declaring an int type variable
int a;
// Assigning a value in a variable
a = 20;
// Printing the value of a variable
printf("%d", a);
//Printing the string value
printf("This is a string");
return 0;
}
Output :-
20
This is a string
Click to edit Master title style
9
scanf():
9
• scanf() function is used to take any value from the keyboard
by the user.
• Datatype:- integer, float, character, string, and many more.
• This function is declared in stdio.h(header file).
• In scanf() function we use &(address-of operator) which is
used to store the variable value on the memory location of
that variable.
• scanf(“Format Specifier”, &var1, &var2, …., &varn);
Click to edit Master title style
10
Example:
10
#include <stdio.h>
int main() {
int num1;
// Printing a message on the output screen
printf("Enter a integer number: ");
// Taking an integer value from keyboard
scanf("%d", &num1);
// Displaying the entered value
printf("You have entered %d", num1);
return 0;
}
Output:- Enter a integer number: 56
You have entered 56
Click to edit Master title style
11
Sprintf()
11
•sprintf stands for “string print”. This
function is similar to printf() function
but this function prints the string into
a character array instead of printing it
on the console screen.
•sprintf(array_name, “format
specifier”, variable_name);
Click to edit Master title style
12
#include <stdio.h>
int main()
{
char str[50];
int a = 2, b = 8;
// The string "2 and 8 are even number" is now stored into str
sprintf(str, "%d and %d are even number", a, b);
// Displays the string
printf("%s", str);
return 0;
}
Output:- 2 and 8 are even number
12
Example:
Click to edit Master title style
13
Sscanf()
13
sscanf stands for “string scanf”. This function is similar
to scanf() function but this function reads data from
the string or character array instead of the console
screen.
sscanf(array_name, “format specifier”,
&variable_name);
Click to edit Master title style
14
#include <stdio.h>
int main() {
char str[50];
int a = 2, b = 8, c, d;
// The string "a = 2 and b = 8" is now stored into str
// character array
sprintf(str, "a = %d and b = %d", a, b);
// The value of a and b is now in c and d
sscanf(str, "a = %d and b = %d", &c, &d);
// Displays the value of c and d
printf("c = %d and d = %d", c, d);
return 0;
}
Output
c = 2 and d = 8
14
Example:
Click to edit Master title style
15
Unformatted Input/Output functions
15
Unformatted I/O functions are used only for character data
type or character array/string and cannot be used for any
other datatype.
These functions are used to read single input from the user at
the console and it allows to display the value at the console.
Click to edit Master title style
16
Why they are called unformatted I/O?
16
These functions are called unformatted I/O
functions because we cannot use format specifiers
in these functions and hence, cannot format these
functions according to our needs.
Click to edit Master title style
17
The following unformatted I/O functions will be discussed
in this section-
17
• getch()
• getche()
• getchar()
• putchar()
• gets()
• puts()
• putch()
Click to edit Master title style
18
getch()
18
getch() function reads a single character from the
keyboard by the user but doesn’t display that
character on the console screen and immediately
returned without pressing enter key. This function
is declared in conio.h(header file). getch() is also
used for hold the screen.
Syntax:
getch();
or
variable-name = getch();
Click to edit Master title style
19
#include <conio.h>
#include <stdio.h>
int main() {
printf("Enter any character: ");
// Reads a character but not displays
getch();
return 0;
}
Output:- Enter any character:
19
Example:
Click to edit Master title style
20
getchar()
20
The getchar() function is used to read only a
first single character from the keyboard
whether multiple characters is typed by the
user and this function reads one character at
one time until and unless the enter key is
pressed. This function is declared in
stdio.h(header file)
Syntax:
Variable-name = getchar();
Click to edit Master title style
21
#include <conio.h>
#include <stdio.h>
int main() {
// Declaring a char type variable
char ch;
printf("Enter the character: ");
// Taking a character from keyboard
ch = getchar();
// Displays the value of ch
printf("%c", ch);
return 0;
}
Output:
Enter the character: a
a 21
Example:
Click to edit Master title style
22
putchar()
22
The putchar() function is used to display a
single character at a time by passing that
character directly to it or by passing a
variable that has already stored a
character. This function is declared in
stdio.h(header file)
Syntax:
putchar(variable_name);
Click to edit Master title style
23
#include <conio.h>
#include <stdio.h>
int main() {
char ch;
printf("Enter any character: ");
// Reads a character
ch = getchar();
// Displays that character
putchar(ch);
return 0;
}
Output: Enter any character: Z
Z 23
Example:
Click to edit Master title style
24
gets()
24
gets() function reads a group of characters or strings from
the keyboard by the user and these characters get stored in
a character array. This function allows us to write space-
separated texts or strings. This function is declared in
stdio.h(header file).
Syntax:
char str[length of string in number]; //Declare a char type
variable of any length
gets(str);
Click to edit Master title style
25
#include <conio.h>
#include <stdio.h>
int main() {
// Declaring a char type array of length 50
characters
char name[50];
printf("Please enter some texts: ");
// Reading a line of character or a string
gets(name);
// Displaying this line of character or a string
printf("You have entered: %s", name);
return 0;
}
Output: Please enter some texts: the gets() function
You have entered: the gets() function
25
Example:
Click to edit Master title style
26
puts():
26
• In C programming puts() function is
used to display a group of characters
or strings which is already stored in a
character array. This function is
declared in stdio.h(header file)
• Syntax:
• puts(identifier_name );
Click to edit Master title style
27
#include <stdio.h>
int main() {
char name[50];
printf("Enter your text: ");
// Reads string from user
gets(name);
printf("Your text is: ");
// Displays string
puts(name);
return 0;
}
Output: Enter your text: the puts() function
Your text is: the puts() function 27
Example:
Click to edit Master title style
28
Command Line Arguments
28
• Command-line arguments are the values given
after the name of the program in the command-
line shell of Operating Systems.
• Command-line arguments are handled by the
main() function of a C program.
• To pass command-line arguments, we typically
define main() with two arguments: the first
argument is the number of command-line
arguments and the second is a list of
command-line arguments.
Click to edit Master title style
29
#include <stdio.h>
int main(int argc, char* argv[])
{
printf("Program name is: %s", argv[0]);
if (argc == 1)
printf("nNo Extra Command Line Argument Passed Other Than
Program Name");
if (argc >= 2) {
printf("nNumber OfArguments Passed: %d", argc);
printf("n----Following Are The Command Line Arguments Passed----
");
for (int i = 0; i < argc; i++)
printf("nargv[%d]: %s", i, argv[i]);
}
return 0;
}
Output:- Program Name Is: ./a.out
Number OfArguments Passed: 4
----Following Are The Command Line Arguments Passed----
argv[0]: ./a.out
argv[1]: First
argv[2]: Second
29
Example:
Click to edit Master title style
30
Thank You

Input Output function in c programing language.pptx

  • 1.
    Click to editMaster title style 1 Title: "Exploring Input/output Function in C Programming" Presented By Amit Kumar, Asma Shamshad A nd N andini Singh
  • 2.
    Click to editMaster title style 2 Different types of I/O functions • Standard Input/Output functions • Command line arguments Functions 2
  • 3.
    Click to editMaster title style 3 3
  • 4.
    Click to editMaster title style 4 Formatted I/O Functions 4  Formatted I/O functions are used to take various inputs from the user and display multiple outputs to the user.  These types of I/O functions can help to display the output to the user in different formats using the format specifiers.  These I/O supports all data types like int, float, char, and many more.
  • 5.
    Click to editMaster title style 5 List of some format specifiers 5 S NO. Format Specifier Type Description 1. %d int/signed int used for I/O signed integer value 2. %c char Used for I/O character value 3. %f float Used for I/O decimal floating-point value 4. %s string Used for I/O string/group of characters 5. %lf double Used for I/O fractional or floating data 6. %n double prints nothing
  • 6.
    Click to editMaster title style 6 The following formatted I/O functions will be discussed in this section- printf() scanf() sprintf() sscanf() 6
  • 7.
    Click to editMaster title style 7 Printf() 7  printf() function is used in a C program to display any value like float, integer, character, string, etc on the console screen.  It is a pre-defined function that is already declared in the stdio.h(header file).  printf(“Format Specifier”, var1, var2, …., varn);
  • 8.
    Click to editMaster title style 8 Example: 8 #include <stdio.h> int main() { // Declaring an int type variable int a; // Assigning a value in a variable a = 20; // Printing the value of a variable printf("%d", a); //Printing the string value printf("This is a string"); return 0; } Output :- 20 This is a string
  • 9.
    Click to editMaster title style 9 scanf(): 9 • scanf() function is used to take any value from the keyboard by the user. • Datatype:- integer, float, character, string, and many more. • This function is declared in stdio.h(header file). • In scanf() function we use &(address-of operator) which is used to store the variable value on the memory location of that variable. • scanf(“Format Specifier”, &var1, &var2, …., &varn);
  • 10.
    Click to editMaster title style 10 Example: 10 #include <stdio.h> int main() { int num1; // Printing a message on the output screen printf("Enter a integer number: "); // Taking an integer value from keyboard scanf("%d", &num1); // Displaying the entered value printf("You have entered %d", num1); return 0; } Output:- Enter a integer number: 56 You have entered 56
  • 11.
    Click to editMaster title style 11 Sprintf() 11 •sprintf stands for “string print”. This function is similar to printf() function but this function prints the string into a character array instead of printing it on the console screen. •sprintf(array_name, “format specifier”, variable_name);
  • 12.
    Click to editMaster title style 12 #include <stdio.h> int main() { char str[50]; int a = 2, b = 8; // The string "2 and 8 are even number" is now stored into str sprintf(str, "%d and %d are even number", a, b); // Displays the string printf("%s", str); return 0; } Output:- 2 and 8 are even number 12 Example:
  • 13.
    Click to editMaster title style 13 Sscanf() 13 sscanf stands for “string scanf”. This function is similar to scanf() function but this function reads data from the string or character array instead of the console screen. sscanf(array_name, “format specifier”, &variable_name);
  • 14.
    Click to editMaster title style 14 #include <stdio.h> int main() { char str[50]; int a = 2, b = 8, c, d; // The string "a = 2 and b = 8" is now stored into str // character array sprintf(str, "a = %d and b = %d", a, b); // The value of a and b is now in c and d sscanf(str, "a = %d and b = %d", &c, &d); // Displays the value of c and d printf("c = %d and d = %d", c, d); return 0; } Output c = 2 and d = 8 14 Example:
  • 15.
    Click to editMaster title style 15 Unformatted Input/Output functions 15 Unformatted I/O functions are used only for character data type or character array/string and cannot be used for any other datatype. These functions are used to read single input from the user at the console and it allows to display the value at the console.
  • 16.
    Click to editMaster title style 16 Why they are called unformatted I/O? 16 These functions are called unformatted I/O functions because we cannot use format specifiers in these functions and hence, cannot format these functions according to our needs.
  • 17.
    Click to editMaster title style 17 The following unformatted I/O functions will be discussed in this section- 17 • getch() • getche() • getchar() • putchar() • gets() • puts() • putch()
  • 18.
    Click to editMaster title style 18 getch() 18 getch() function reads a single character from the keyboard by the user but doesn’t display that character on the console screen and immediately returned without pressing enter key. This function is declared in conio.h(header file). getch() is also used for hold the screen. Syntax: getch(); or variable-name = getch();
  • 19.
    Click to editMaster title style 19 #include <conio.h> #include <stdio.h> int main() { printf("Enter any character: "); // Reads a character but not displays getch(); return 0; } Output:- Enter any character: 19 Example:
  • 20.
    Click to editMaster title style 20 getchar() 20 The getchar() function is used to read only a first single character from the keyboard whether multiple characters is typed by the user and this function reads one character at one time until and unless the enter key is pressed. This function is declared in stdio.h(header file) Syntax: Variable-name = getchar();
  • 21.
    Click to editMaster title style 21 #include <conio.h> #include <stdio.h> int main() { // Declaring a char type variable char ch; printf("Enter the character: "); // Taking a character from keyboard ch = getchar(); // Displays the value of ch printf("%c", ch); return 0; } Output: Enter the character: a a 21 Example:
  • 22.
    Click to editMaster title style 22 putchar() 22 The putchar() function is used to display a single character at a time by passing that character directly to it or by passing a variable that has already stored a character. This function is declared in stdio.h(header file) Syntax: putchar(variable_name);
  • 23.
    Click to editMaster title style 23 #include <conio.h> #include <stdio.h> int main() { char ch; printf("Enter any character: "); // Reads a character ch = getchar(); // Displays that character putchar(ch); return 0; } Output: Enter any character: Z Z 23 Example:
  • 24.
    Click to editMaster title style 24 gets() 24 gets() function reads a group of characters or strings from the keyboard by the user and these characters get stored in a character array. This function allows us to write space- separated texts or strings. This function is declared in stdio.h(header file). Syntax: char str[length of string in number]; //Declare a char type variable of any length gets(str);
  • 25.
    Click to editMaster title style 25 #include <conio.h> #include <stdio.h> int main() { // Declaring a char type array of length 50 characters char name[50]; printf("Please enter some texts: "); // Reading a line of character or a string gets(name); // Displaying this line of character or a string printf("You have entered: %s", name); return 0; } Output: Please enter some texts: the gets() function You have entered: the gets() function 25 Example:
  • 26.
    Click to editMaster title style 26 puts(): 26 • In C programming puts() function is used to display a group of characters or strings which is already stored in a character array. This function is declared in stdio.h(header file) • Syntax: • puts(identifier_name );
  • 27.
    Click to editMaster title style 27 #include <stdio.h> int main() { char name[50]; printf("Enter your text: "); // Reads string from user gets(name); printf("Your text is: "); // Displays string puts(name); return 0; } Output: Enter your text: the puts() function Your text is: the puts() function 27 Example:
  • 28.
    Click to editMaster title style 28 Command Line Arguments 28 • Command-line arguments are the values given after the name of the program in the command- line shell of Operating Systems. • Command-line arguments are handled by the main() function of a C program. • To pass command-line arguments, we typically define main() with two arguments: the first argument is the number of command-line arguments and the second is a list of command-line arguments.
  • 29.
    Click to editMaster title style 29 #include <stdio.h> int main(int argc, char* argv[]) { printf("Program name is: %s", argv[0]); if (argc == 1) printf("nNo Extra Command Line Argument Passed Other Than Program Name"); if (argc >= 2) { printf("nNumber OfArguments Passed: %d", argc); printf("n----Following Are The Command Line Arguments Passed---- "); for (int i = 0; i < argc; i++) printf("nargv[%d]: %s", i, argv[i]); } return 0; } Output:- Program Name Is: ./a.out Number OfArguments Passed: 4 ----Following Are The Command Line Arguments Passed---- argv[0]: ./a.out argv[1]: First argv[2]: Second 29 Example:
  • 30.
    Click to editMaster title style 30 Thank You