Input and Output Functions in C

Page 1

GetStarted 

FlowControl 

LearnCProgramming Input/outputfunctions

TUTORIALS  EXAMPLES  PROJECTS  ONLINECOMPILER

StructureAndUnion

In this tutorial, you will learn to use how to utilize functions for user input and output functions for displaying results to users.

Input and output functions are fundamental aspects of C programming, enabling communication between the program and the user or external devices. Understanding how to use input and output functions is essential for building interactive and user-friendly programs. Output

In C programming, “output” refers to the process of displaying or presenting data or information generated by a program. You can display output in various ways, such as printing to the console or terminal, writing to

Functions  Arrays
Pointers  Strings
FileHandling  Enumeration 
Search Home Tutorials Examples Blog
Preprocessor 

files, or sending data to external devices like printers or displays. The standard library in C provides functions to handle different types of output.

In C programming, there are several output functions available to display or output data to the user or external devices.

Somecommonlyusedoutputfunctionsinclude:

1. printf: The printf function is the go-to function for displaying formatted output to the console or terminal in C programming. It allows you to print data in a specific format based on format specifiers. Forinstance:

printf printf(("Hello, World!\n" "Hello, World!\n")); ; puts puts(("Hello, World!" "Hello, World!")); ;

2. puts: It outputs a string to the console, followed by a newline character.It is simpler to use compared to printf, but it doesn’t support format specifiers. Forinstance:

3. putchar: This function commonly outputs a single character to the console, making it useful in loops or when printing characters one at a time.

Forinstance:

putchar putchar(('H''H')); ; putchar putchar(('i''i')); ; putchar putchar(('\n''\n')); ;

4. fprintf: It prints formatted output to a file instead of the console. It allows you to specify the file stream as a parameter, along with the format specifiers. Forinstance:

FILE FILE * *file file = = fopen fopen(("output.txt" "output.txt", , "w" "w")); ; fprintf fprintf((filefile, , "This is some text: %d\n" "This is some text: %d\n", , 42 42)); ; fclose fclose((filefile)); ;

These are just a few examples of the output functions available in C. Each function has its own specific use cases, and the choice of output function depends on the requirements of your program and the desired output format.

In C programming, developers widely use the printf function to generate output.It allows you to display text and values on the console or in a designated output stream. By using format specifiers, you can format the output to include variables, strings, numbers, and other data types. The printf function is part of the standard input/output library (stdio.h) and provides a versatile way to generate output in C programs.

#include #include < <stdiostdio.hh> > int int main main(() )

Forinstance:Theuseofprintf(). Output

// Displays the string inside quotations printf printf(("C Programming" "C Programming")); ; return return 0 0; ; } }

// Displays the string inside quotations

C Programming C Programming

Howdoesthisprogramwork?

1. The #include<stdio.h> directive at the beginning of the code includes the standard input/output library, which contains the definition of the printf function.

2. The main() function serves as the entry point of the program. It is where the program execution starts.

3. Within the main() function, the printf function is called. The string “C Programming” is passed as an argument to the printf function.

4. The printf function takes the provided string and outputs it to the console. In this case, “C Programming” will be displayed as the output.

5. The return0; statement is used to indicate the successful execution of the program. The value 0 is returned to the operating system, indicating that the program has finished without any errors.

{ {

When you run the program, it will display the string “C Programming” on the console as the output.

Input

In C programming, “input” refers to the process of providing data or information to a program during its execution. It allows users to interact with the program and supply values or data that the program can use for processing.

In C programming, there are several input functions available for handling different types of input.

Somecommonlyusedinputfunctionsinclude:

1. scanf: This function allows the user to read formatted input. It allows you to specify the format of the input you expect to receive and stores the input in variables. It is commonly used for reading input from the keyboard.

For instance:

int int scanf scanf((const const char char * *formatformat, , ... ...)); ;

2. getchar: This function reads a single character from the user, typically from the keyboard. It reads and returns the next character from the input stream.

Forinstance:

int int getchar getchar((voidvoid)); ;

3. gets (deprecated): This function, typically used for reading a line of text from the user (usually from the keyboard) and storing it in a string, is considered unsafe due to the potential for buffer overflow vulnerabilities. As a result, it has been deprecated in newer versions of the C standard. Forinstance:

char char * *getsgets((char char * *strstr)); ;

4. fgets: This function reads a line of text from a file or the user, and stores it in a string. It allows you to specify the maximum number of characters to read and handles newline characters.

Forinstance:

char char * *fgetsfgets((char char * *strstr, , int n int n, , FILE FILE * *streamstream)); ;

5. getch (non-standard): This function reads a single character from the user, typically from the keyboard, without waiting for the user to press the Enter key. However, note that getch is not a standard C function and may not be available on all platforms.

Forinstance:

int int getch getch((voidvoid)); ;

These are just a few examples of input functions available in C. The choice of input function depends on the specific requirements of your program, the type of input you want to handle, and the desired behavior for handling user input.

One of the often used functions in C programming to receive user input is scanf(). Using common input devices like keyboards, the scanf() function reads formatted input.

Forinstance::Theuseofscanf().

#include #include < <stdio stdio h h> > int int main main(() )

int number int number; ; printf printf(("Enter the number: " "Enter the number: ")); ; scanf scanf(("%d""%d", , & &numbernumber)); ; // taking an the number as input from the user // taking an the number as input from the user printf printf(("Number = %d" "Number = %d",,numbernumber)); ; return return 0 0; ;

Output

{ {
} }

Howdoesthisprogramwork?

This C program prompts the user to enter an integer, reads the input from the user, and then displays the entered number.

1. The program starts with the inclusion of the <stdio.h> header file, which provides input and output functions like printf and scanf.

2. The main() function is the entry point of the program.

3. Inside the main() function, the program declares an integer variable named number to store the user’s input.

4. The program displays the message “Enter an number: ” using the printf function, prompting the user to enter an integer.

5. The program utilizes the scanf function to read an integer value from the user, indicating that it expects an integer with the %d format specifier. The & operator retrieves the memory address of the number variable and passes it as an argument to scanf, enabling scanf to store the user’s input in that variable.

6. After the user enters the integer, the program proceeds to the next line.

7. The printf function once again displays the message “Number = ” along with the value stored in the number variable, using the %d format specifier to print the integer value.

8. Finally, the statement return0; indicates that the program has successfully executed and is ready to terminate. Typically, a successful execution indicates the value 0.

When you run this program, it will ask you to enter an integer. After you input the integer and press Enter, the program will display the message “Number = ” followed by the entered integer value.

Enter an integer : 4 Number = 4 Enter an integer : 4 Number = 4
10/10OVERALLRATING      Tutorials Practice Company Ourlatestnews,articles,andresources,wewill senttoyourinboxweekly Enter your email Subscribe CProgramming CExamples CProjects CQuiz AboutUs PrivacyPolicy Contact WebStories Blog
PDFmyURL.com - convert URLs, web pages or even full websites to PDF online. Easy API for developers! ©2023Ccodelearner.Poweredby Esos Technologies    

Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.