[C] String Input/Output
scanf
#include <stdio.h>
int main(void)
{
char food[20];
puts("--------------------------");
puts(" ** String input/output ** ");
puts("--------------------------");
printf(" What is your favorite food : ");
scanf("%s", food);
printf(" the Food : %s \n", food);
puts("--------------------------");
puts(" scanf cannot use for space ");
puts("--------------------------");
return 0;
/*
--------------------------
** String input/output **
--------------------------
What is your favorite food :Hawaiian pizza
the Food : Hawaiian
--------------------------
scanf cannot use for space
--------------------------
*/
}
fgets
- Read String from file
#include <stdio.h>
int main(void)
{
char food[20];
puts("--------------------------");
puts(" ** String input/output ** ");
puts("--------------------------");
printf(" What is your favorite food : ");
fgets(food, sizeof(food), stdin);
printf(" the Food : %s", food);
puts("--------------------------");
return 0;
/*
--------------------------
** String input/output **
--------------------------
What is your favorite food : Hawaiian pizza
the Food : Hawaiian pizza
--------------------------
*/
}
integer and string input/output
#include <stdio.h>
int main(void)
{
char sports[10];
int win;
puts("-------------------------------");
puts(" ** integer and string ** ");
puts("-------------------------------");
printf(" What sports : ");
scanf("%s", sports);
printf(" How many win : ");
scanf("%d", &win);
printf(" The %s team win %d time(s). \n", sports, win);
puts("-------------------------------");
return 0;
/*
-------------------------------
** integer and string **
-------------------------------
What sports : baseball
How many win : 10
The baseball team win 10 time(s).
-------------------------------
*/
}
mix input/outpu
#include <stdio.h>
int main(void)
{
char name[10];
int year;
char btype;
float vision;
puts("-------------------------");
puts(" ** mix input/output ** ");
puts("-------------------------");
printf(" Name : ");
scanf("%s", name);
printf(" Born year : ");
scanf("%d", &year);
printf(" blood type : ");
while(getchar() != '\n');
btype = getchar();
printf(" Vision : ");
scanf("%f", &vision);
printf("%s's born in %d, blood type : %c and vision : %.1f \n", name, year, btype, vision);
return 0;
/*
-------------------------
** mix input/output **
-------------------------
Name : Ian
Born year : 2000
blood type : B
Vision : 2.0
Ian's born in 2000, blood type : B and vision : 2.0
*/
}
Reference
- func.kr
- C Programming: A Modern Approach by K.N.King