C Programming - Formatted I/O Functions (PDF)
Document Details
Uploaded by LavishEinsteinium7936
Presidency College
Swetha C V
Tags
Summary
This document provides an overview of formatted input/output functions in C programming. It describes the various functions such as printf, scanf, sprintf, and sscanf, and their uses. The document also covers unformatted input/output functions and their functionalities.
Full Transcript
C Programming Unit II: Control Structures Formatted IO Functions Formatted I/O functions are essential for handling user inputs and displaying outputs in a user- friendly way. They enable programmers to: Receive User Inputs: Formatted input fu...
C Programming Unit II: Control Structures Formatted IO Functions Formatted I/O functions are essential for handling user inputs and displaying outputs in a user- friendly way. They enable programmers to: Receive User Inputs: Formatted input functions help programs collect user input in a structured manner. They use format specifiers to interpret and extract specific data types from user input. Present Data to Users: Formatted output functions allow programs to present data to users in various formats. Format specifiers are used to control how data is displayed, making it more readable and visually appealing. Support Multiple Data Types: These I/O functions are versatile and support a wide range of data types, including integers, floating-point numbers, characters, and more. Each data type has corresponding format specifiers for precise formatting. Formatting Control: Programmers can use format specifiers to control the alignment, width, precision, and other formatting aspects of displayed data, ensuring it’s presented as intended. These functions are called formatted I/O functions as they can use format specifiers in these functions. Moreover, we can format these functions according to our needs. Here are the list of some specifiers The following formatted I/O functions will be discussed in this section- 1. printf() 2. scanf() 3. sprintf() 4. sscanf() printf(): In C, printf() is a built-in function used to display values like numbers, characters, and strings on the console screen. It’s pre-defined in the stdio.h header, allowing easy output formatting in C programs. scanf(): scanf(): In C, scanf() is a built-in function for reading user input from the keyboard. It can read values of various data types like integers, floats, characters, and strings. scanf() is a pre-defined function declared in the stdio.h header file. It uses the & (address-of operator) to store user input in the memory location of a variable. sprintf():sprintf(): Short for “string print,” sprintf() is similar to printf() but it stores the formatted string into a character array instead of displaying it on the console screen. Swetha C V, DCA, Presidency College (Autonomous) C Programming Unit II: Control Structures sscanf(): sscanf(): Abbreviated for “string scanf,” sscanf() resembles scanf() but reads data from a string or character array rather than from the console screen Unformatted Input/Output functions Unformatted I/O Functions: These functions are used exclusively for character data types or character arrays/strings. They are designed for reading single inputs from the user at the console and for displaying values on the console. Why “Unformatted”?: They are referred to as “unformatted” I/O functions because they do not support format specifiers. Unlike formatted I/O functions like printf() and scanf(), you cannot use format specifiers to control the formatting of the data. They display or read data as- is without formatting options. The following are unformatted I/O functions 1. getch() 2. getche() 3. getchar() 4. putchar() 5. gets() 6. puts() getch(): In C, getch() reads a single character from the keyboard without displaying it on the console screen. It immediately returns without requiring the user to press the Enter key. This function is declared in the conio.h header file and is often used for controlling screen display. getche():In C, getche() reads a single character from the keyboard, displays it on the console screen, and immediately returns without requiring the user to press the Enter key. This function is declared in the conio.h header file. getchar(): In C, getchar() reads a single character from the keyboard and waits until the Enter key is pressed. It processes one character at a time. This function is declared in the stdio.h header file. putchar():In C, putchar() is used to display a single character at a time, either by passing the character directly or by using a variable that stores the character. This function is declared in the stdio.h header file. gets():In C, gets() reads a group of characters or strings from the keyboard, and these characters are stored in a character array. It allows you to input space-separated texts or strings. This function is declared in the stdio.h header file. However, please note that gets() is considered Swetha C V, DCA, Presidency College (Autonomous) C Programming Unit II: Control Structures unsafe due to the risk of buffer overflow and is generally discouraged in favor of safer alternatives like fgets(). puts(): In C programming, puts() is used to display a group of characters or strings that are already stored in a character array. This function is declared in the stdio.h header file. 'if' Statement in C What is Decision Making Statement? In the C programming language, the program execution flow is line by line from top to bottom. That means the c program is executed line by line from the main method. But this type of execution flow may not be suitable for all the program solutions. Sometimes, we make some decisions or we may skip the execution of one or more lines of code. Consider a situation, where we write a program to check whether a student has passed or failed in a particular subject. Here, we need to check whether the marks are greater than the pass marks or not. If marks are greater, then we decide that the student has passed otherwise failed. To solve such kind of problems in c we use the statements called decision making statements. Decision-making statements are the statements that are used to verify a given condition and decide whether a block of statements gets executed or not based on the condition result. In the c programming language, there are two decision-making statements they are as follows. 1. if statement 2. switch statement if statement in c In c, if statement is used to make decisions based on a condition. The if statement verifies the given condition and decides whether a block of statements are executed or not based on the condition result. In c, if statement is classified into four types as follows... 1. Simple if statement 2. if-else statement 3. Nested if statement 4. if-else-if statement (if-else ladder) Simple if statement Simple if statement is used to verify the given condition and executes the block of statements based on the condition result. The simple if statement evaluates specified condition. If it is TRUE, it executes the next statement or block of statements. If the condition is FALSE, it skips Swetha C V, DCA, Presidency College (Autonomous) C Programming Unit II: Control Structures the execution of the next statement or block of statements. The general syntax and execution flow of the simple if statement is as follows. Simple if statement is used when we have only one option that is executed or skipped based on a condition. Example Program | Test whether given number is divisible by 5. #include #include void main(){ int n ; clrscr() ; printf("Enter any integer number: ") ; scanf("%d", &n) ; if ( n%5 == 0 ) printf("Given number is divisible by 5\n") ; printf("statement does not belong to if!!!") ; } Output 1: Swetha C V, DCA, Presidency College (Autonomous) C Programming Unit II: Control Structures Output 2: if-else statement The if-else statement is used to verify the given condition and executes only one out of the two blocks of statements based on the condition result. The if-else statement evaluates the specified condition. If it is TRUE, it executes a block of statements (True block). If the condition is FALSE, it executes another block of statements (False block). The general syntax and execution flow of the if-else statement is as follows. The if-else statement is used when we have two options and only one option has to be executed based on a condition result (TRUE or FALSE). Example Program | Test whether given number is even or odd. #include #include void main(){ int n ; clrscr() ; printf("Enter any integer number: ") ; scanf("%d", &n) ; if ( n%2 == 0 ) printf("Given number is EVEN\n") ; Swetha C V, DCA, Presidency College (Autonomous) C Programming Unit II: Control Structures else printf("Given number is ODD\n") ; } Output 1: Output 2: Nested if statement Writing a if statement inside another if statement is called nested if statement. The general syntax of the nested if statement is as follows... The nested if statement can be defined using any combination of simple if & if-else statements. Example Program | Test whether given number is even or odd if it is below 100. #include Swetha C V, DCA, Presidency College (Autonomous) C Programming Unit II: Control Structures #include void main(){ int n ; clrscr() ; printf("Enter any integer number: ") ; scanf("%d", &n) ; if ( n < 100 ) { printf("Given number is below 100\n") ; if( n%2 == 0) printf("And it is EVEN") ; else printf("And it is ODD") ; } else printf("Given number is not below 100") ; } Output 1: Output 2: if-else-if statement (if-else ladder) Writing a if statement inside else of an if statement is called if-else-if statement. The general syntax of the if-else-if statement is as follows... Swetha C V, DCA, Presidency College (Autonomous) C Programming Unit II: Control Structures The if-else-if statement can be defined using any combination of simple if & if-else statements. Example Program | Find the largest of three numbers. #include #include void main(){ int a, b, c ; clrscr() ; printf("Enter any three integer numbers: ") ; scanf("%d%d%d", &a, &b, &c) ; if( a>=b && a>=c) printf("%d is the largest number", a) ; else if (b>=a && b>=c) printf("%d is the largest number", b) ; else printf("%d is the largest number", c) ; } Output: Swetha C V, DCA, Presidency College (Autonomous) C Programming Unit II: Control Structures 'switch' statement in C Consider a situation in which we have many options out of which we need to select only one option that is to be executed. Such kind of problems can be solved using nested if statement. But as the number of options increases, the complexity of the program also gets increased. This type of problem can be solved very easily using a switch statement. Using the switch statement, one can select only one option from more number of options very easily. In the switch statement, we provide a value that is to be compared with a value associated with each option. Whenever the given value matches the value associated with an option, the execution starts from that option. In the switch statement, every option is defined as a case. The switch statement has the following syntax and execution flow diagram. The switch statement contains one or more cases and each case has a value associated with it. At first switch statement compares the first case value with the switchValue, if it gets matched the execution starts from the first case. If it doesn't match the switch statement compares the second case value with the switchValue and if it is matched the execution starts from the second case. This process continues until it finds a match. If no case value matches with the switchValue specified in the switch statement, then a special case called default is executed. Swetha C V, DCA, Presidency College (Autonomous) C Programming Unit II: Control Structures When a case value matches with the switch Value, the execution starts from that particular case. This execution flow continues with the next case statements. To avoid this, we use the "break" statement at the end of each case. That means the break statement is used to terminate the switch statement. However, it is optional. Example Program | Display pressed digit in words. #include #include void main(){ int n ; clrscr() ; printf("Enter any digit: ") ; scanf("%d", &n) ; switch( n ) { case 0: printf("ZERO") ; break ; case 1: printf("ONE") ; break ; case 2: printf("TWO") ; break ; case 3: printf("THREE") ; break ; case 4: printf("FOUR") ; break ; case 5: printf("FIVE") ; break ; case 6: printf("SIX") ; break ; case 7: printf("SEVEN") ; break ; case 8: printf("EIGHT") ; Swetha C V, DCA, Presidency College (Autonomous) C Programming Unit II: Control Structures break ; case 9: printf("NINE") ; break ; default: printf("Not a Digit") ; } getch() ; } Output 1: Output 2: 'while' statement in C Consider a situation in which we execute a single statement or block of statements repeatedly for the required number of times. Such kind of problems can be solved using looping statements in C. For example, assume a situation where we print a message 100 times. If we want to perform that task without using looping statements, we have to either write 100 printf statements or we have to write the same message 100 times in a single printf statement. Both are complex methods. The same task can be performed very easily using looping statements. The looping statements are used to execute a single statement or block of statements repeatedly until the given condition is FALSE. C language provides three looping statements... while statement do-while statement for statement Swetha C V, DCA, Presidency College (Autonomous) C Programming Unit II: Control Structures while Statement The while statement is used to execute a single statement or block of statements repeatedly as long as the given condition is TRUE. The while statement is also known as Entry control looping statement. The while statement has the following syntax... The while statement has the following execution flow diagram... At first, the given condition is evaluated. If the condition is TRUE, the single statement or block of statements gets executed. Once the execution gets completed the condition is evaluated again. If it is TRUE, again the same statements get executed. The same process is repeated until the condition is evaluated to FALSE. Whenever the condition is evaluated to FALSE, the execution control moves out of the while block. Example Program | Program to display even numbers upto 10. #include #include void main(){ int n = 0; clrscr() ; printf("Even numbers upto 10\n"); Swetha C V, DCA, Presidency College (Autonomous) C Programming Unit II: Control Structures while( n