Input/output functions


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 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.

Some commonly used output functions include:

  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.

    For instance:

    printf("Hello, World!\n");
  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.ย 

    For instance:

    puts("Hello, World!");
  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.

    For instance:

    putchar('H');
    putchar('i');
    putchar('\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.ย 

    For instance:

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

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.

ย 

For instance: The use of printf().

#include <stdio.h>   
int main()
{ 
    // Displays the string inside quotations
    printf("C Programming");
    return 0;
}

Output

C Programming 

ย 

How does this program work?

  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 return 0; 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.

Some commonly used input functions include:

  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 scanf(const char *format, ...);
  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.

    For instance:

    int getchar(void);
  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.

    For instance:

    char *gets(char *str);
  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.

    For instance:

    char *fgets(char *str, int n, FILE *stream);
  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.

    For instance:

    int getch(void);

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.

For instance:: The use of scanf().


#include <stdio.h>
int main()
{
    int number;
    printf("Enter the number: ");
    scanf("%d", &number);
    // taking  an  the number as input from the user  
    printf("Number = %d",number);
    return 0;
}

Output

Enter an integer : 4 Number = 4

How does this program work?

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 return 0; 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.