Online Programming Server

Login

Login with facebook [?]

Facebook

Tutorial 97: Basic Input/Output of C

You are here: Tutorials >> Basic >> C >> Basic Input/Output of C

Tutorial ID97
TitleBasic Input/Output of C
In C, two of the most popular input and output function is printf() and scanf(). As in Tutorial1 : Your first C Program, the example output "Hello World" by the function printf(). This tutorial will talk more about their properties.

Here's an example code:
#include <stdio.h>

int main(void){
char name[10];
printf("What is your name?\n");
scanf("%s", name);
printf("Hi, %s!\n", name);
return 0;
}

Sample output
What is your name?
Leo (input by user)
Hi, Leo!

Printf()
The format of printf is:
printf("PUT YOUR TEXT HERE!");
or
printf("TEXT AND %d %s %c......", variable1, variable2, variable3, etc........);

"%" tells the compiler you want to output value of a variable, and the character after "%" indicates what type of variable you want to print. Here's some basic types you may know.

%c : char
%s : string(array of char)
%d : integer
%f : float

"\" is for printing some special characters, like line break "\n".

Scanf()
The format of scanf is similar:
scanf("%d %s%f", &integer1, string1, &float1);

The text inside two quotation marks are exactly the same with printf. However, you need to add "&" to the list of variables, because the compiler needs the "reference" of the variables. "Reference" should be talked in some later tutorials. But all array variables hold their reference, thus do not need "&" before them.

Related Problems

problem_id title description submit accepted difficulty
1003 a + b  A + B Problem Given 2 integers a and b, calculate a + b. 620 565 231
1115 Cashier Bob is working at a cashier counter, he needs to make hundreds of changes everyday. But he is very laze and does not want to count how ma... 3 0 537

Post Your Comment

Title
Message