Explain different input and output functions in c with example (scanf,printf,gets,puts).
Question
Explain different input and output functions in C with example (scanf, printf, gets, puts).
Solution
Sure, here are some of the most commonly used input and output functions in C:
scanf
: This is an input function used to read data from the keyboard. It reads the value, converts it into the specified data type and then stores it in the memory.
Example:
int num;
printf("Enter a number: ");
scanf("%d", &num);
In this example, scanf
reads an integer value from the user and stores it in the variable num
.
printf
: This is an output function used to print data onto the screen. It can print data of different data types including int, char, float, double, string, etc.
Example:
int num = 10;
printf("The number is %d", num);
In this example, printf
prints the value of the variable num
onto the screen.
gets
: This is an input function used to read a string from the user. It reads a line from stdin into the buffer pointed to by s until either a terminating newline or EOF.
Example:
char str[50];
printf("Enter a string: ");
gets(str);
In this example, gets
reads a string from the user and stores it in the character array str
.
puts
: This is an output function used to print a string onto the screen. It writes a string to stdout up to but not including the null character. A newline character is appended to the output.
Example:
char str[50] = "Hello, World!";
puts(str);
In this example, puts
prints the string str
onto the screen.
Please note that gets
is deprecated due to the risk of buffer overflow. It's recommended to use fgets
instead.
Similar Questions
scanf() is a predefined function in______header file.Answer areaa) stdlib. hb) ctype. hc) stdio. hd) stdarg. H
Which of the following function used as reading data ?Options :main()print()scanf()printf()
What is the output of the following code?char str1[] = "C programming";char str2[20];strcpy(str2, str1);printf("%s\n", str2);
What will be the output of the following C code?#include <stdio.h>int main() { int i; for (i = 0; i < 5; i++); printf("%d\n", i); return 0;}
What is the output of the following code?#include <stdio.h>int main() { int arr[5] = {1, 2, 3, 4, 5}; printf("%d\n", arr[3]); return 0;}
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.