C Tutorial/printf scanf/scanf Basics
Содержание
Pass pointer argument to scanf
<source lang="cpp">#include <stdio.h> int main(void) {
int value = 0; int *pvalue = NULL; pvalue = &value; /* Set pointer to refer to value */ printf ("Input an integer: "); scanf(" %d", pvalue); /* Read into value via the pointer */ printf("\nYou entered %d\n", value); return 0;
}</source>
Input an integer: string You entered 0
Reading and discarding characters from the input stream
<source lang="cpp">#include <stdio.h> int main() {
int month1; int day1; int year1; int month2; int day2; int year2; printf( "Enter a date in the form mm-dd-yyyy: " ); scanf( "%d%*c%d%*c%d", &month1, &day1, &year1 ); printf( "month = %d day = %d year = %d\n\n", month1, day1, year1 ); printf( "Enter a date in the form mm/dd/yyyy: " ); scanf( "%d%*c%d%*c%d", &month2, &day2, &year2 ); printf( "month = %d day = %d year = %d\n", month2, day2, year2 ); return 0;
}</source>
Enter a date in the form mm-dd-yyyy: 01-01-2001 month = 1 day = 1 year = 2001 Enter a date in the form mm/dd/yyyy: 01/01/2002 month = 1 day = 1 year = 2002
scanf
The scanf function: read information from a standard input device (keyboard).
<source lang="cpp">scanf("conversion specifier", variable);</source>
The conversion specifier argument tells scanf how to convert the incoming data.
- scanf starts with a string argument and may contain additional arguments.
- Additional arguments must be pointers.
- scanf returns the number of successful inputs.
Common Conversion Specifiers Used with Scanf
Conversion Specifier Description %d Receives integer value %f Receives floating-point numbers %c Receives character
The scanf placeholders
d, i Used for signed integers; the expected argument should be a pointer to int.
<source lang="cpp">#include <stdio.h>
main()
{
int i = 0; int k; printf("input an integer:\n"); i=scanf("%d",&k); printf("total values inputted %d\n",i); printf("The input values %d\n",k);
}</source>
input an integer: 3 total values inputted 1 The input values 3
Use scanf to get input from a standard input device, such as a keyboard
In scanf, you have to specify the address of a variable, such as &i, &j, and a list of format specifiers (%d). The number of format specifiers must be the same as the number of variables.
<source lang="cpp">#include <stdio.h>
main(){
int i,j,k; printf("Input two integers and press Enter to comfirm:"); scanf("%d%d",&i,&j); k = i + j; printf("sum of two numbers is %d \n",k);
}</source>
Input two integers and press Enter to comfirm:223 2 sum of two numbers is 225
Using an inverted scan set
<source lang="cpp">#include <stdio.h> int main() {
char z[ 9 ]; printf( "Enter a string: " ); scanf( "%[^aeiou]", z ); printf( "The input was \"%s\"\n", z ); return 0;
}</source>
Enter a string: string The input was "str"
Using Scanf to Receive Input from a User
<source lang="cpp">#include <stdio.h> main(){
int iOperand1 = 0; int iOperand2 = 0; printf("\nEnter first operand: "); scanf("%d", &iOperand1); printf("Enter second operand: "); scanf("%d", &iOperand2); printf("The result is %d\n", iOperand1+iOperand2);
}</source>
Enter first operand: 1 Enter second operand: 2 The result is 3