Input And Output In C

On this page, you will learn about input and output in C. What are the input functions in C? What are the output functions in C?


What are the input functions in C?

In C programming language, there are several input functions that can be used to read input values from the user. Some of the commonly used input functions in C are:


  • scanf(): This function is used to read input from the user and store it in a variable. The syntax of scanf() is:

    
      scanf("format specifier", &variable_name);
    
    

    Example: To read an integer value from the user and store it in the variable 'num', we can use:

    
      int num;
      scanf("%d", &num);
    
    
  • getchar(): This function is used to read a single character input from the user. The syntax of getchar() is:

    
      char ch;
      ch = getchar();
    
    
  • gets(): This function is used to read a string input from the user. The syntax of gets() is:

    
      char str[50];
      gets(str);
    
    

    [NB:The gets() function is not recommended to be used as it has potential security vulnerabilities.]
  • fgets(): This function is used to read a string input from the user, but it is safer than gets() as it limits the number of characters to be read. The syntax of fgets() is:

    
      char str[50];
      fgets(str, 50, stdin);
    
    

    [NB: The fgets() function reads the newline character '\n' and stores it along with the input string, so it needs to be removed separately.]
  • scanf_s(): This function is a secure version of scanf() and is recommended for safer input reading. The syntax of scanf_s() is:

    
      int num;
      scanf_s("%d", &num);
    
    

    [NB: scanf_s() is a Microsoft specific function and may not be available in all compilers.]

These are some of the input functions in C programming language that can be used to read input values from the user. It is important to note that proper input validation should be performed before using the input values to avoid potential errors and vulnerabilities.

What are the output functions in C?

Output functions in C are used to display the output of a program on the screen or other output devices. There are several output functions in C, each with its own unique purpose and usage.

  • printf(): The printf() function is the most commonly used output function in C. It is used to print formatted output to the screen. The function takes a format string as its first argument, which contains placeholders for the data to be printed. Here's an example:

    
      int num = 10;
      printf("The value of num is %d", num);
    
    
  • puts(): The puts() function is used to print a string to the screen followed by a newline character. It takes a single argument, which is the string to be printed. Here's an example:

    
      char str[] = "Hello, world!";
      puts(str);
    
    
  • putchar(): The putchar() function is used to print a single character to the screen. It takes a single argument, which is the character to be printed. Here's an example:

    
      char ch = 'A';
      putchar(ch);
    
    
  • fprintf(): The fprintf() function is used to print formatted output to a file instead of the screen. It works similar to the printf() function but takes an additional argument for the file to which the output should be directed. Here's an example:

    
      FILE *fp;
      fp = fopen("output.txt", "w");
      fprintf(fp, "This is some output to a file");
      fclose(fp);
    
    
  • sprintf(): The sprintf() function is used to format a string and store it in a character array instead of printing it to the screen. It takes the same arguments as printf() but stores the formatted string in a buffer instead of printing it. Here's an example:

    
      char buffer[50];
      int num = 10;
      sprintf(buffer, "The value of num is %d", num);  
    
    

These are the most commonly used output functions in C. Understanding their usage and differences can help you write more efficient and effective programs.