Show List

Input/Output functions

C has several functions for input and output operations. Some of the most commonly used input/output functions are:

  • printf(): The printf() function is used to print data to the screen. The general syntax for printf() is:
bash
Copy code
printf("format_string", list_of_variables);

For example, the following code prints the string "Hello, World!" to the screen:

c
Copy code
#include <stdio.h> int main() { printf("Hello, World!"); return 0; }
  • scanf(): The scanf() function is used to read data from the keyboard. The general syntax for scanf() is:
c
Copy code
scanf("format_string", &variable_name);

For example, the following code reads an integer from the keyboard and stores it in the variable age:

c
Copy code
#include <stdio.h> int main() { int age; printf("Enter your age: "); scanf("%d", &age); printf("Your age is: %d", age); return 0; }
  • gets(): The gets() function is used to read a string from the keyboard. The general syntax for gets() is:
scss
Copy code
gets(variable_name);

For example, the following code reads a string from the keyboard and stores it in the character array name:

c
Copy code
#include <stdio.h> int main() { char name[100]; printf("Enter your name: "); gets(name); printf("Your name is: %s", name); return 0; }
  • putchar(): The putchar() function is used to print a single character to the screen. The general syntax for putchar() is:
scss
Copy code
putchar(variable_name);

For example, the following code prints the character 'A' to the screen:

c
Copy code
#include <stdio.h> int main() { char ch = 'A'; putchar(ch); return 0; }
  • getchar(): The getchar() function is used to read a single character from the keyboard. The general syntax for getchar() is:
scss
Copy code
getchar();

For example, the following code reads a single character from the keyboard and stores it in the variable ch:

c
Copy code
#include <stdio.h> int main() { char ch; printf("Enter a character: "); ch = getchar(); printf("You entered: %c", ch); return 0; }


    Leave a Comment


  • captcha text