C Tutorial/printf scanf/printf Basics
Содержание
- 1 A format specifier: how to print the data
- 2 d, i: Signed integers
- 3 Do calculation in printf
- 4 Placeholders
- 5 printf() Escape Sequences
- 6 The printf() Conversion Characters and flags
- 7 The printf Function
- 8 The printf() function redirects the output to a standard output, which is the output on screen
- 9 Use multiple conversion specifiers in a single printf statement
A format specifier: how to print the data
%d is one of the format specifiers for integer.
#include <stdio.h>
main(){
int i,j,k;
i = 6;
j = 8;
k = i + j;
printf("sum of two numbers is %d \n",k);
}
sum of two numbers is 14
d, i: Signed integers
#include <stdio.h>
main()
{
int i = 100;
printf(" %d\n",i);
printf(" %i\n",i);
}
100 100
Do calculation in printf
#include <stdio.h>
main (){
float fRevenue, fCost;
fRevenue = 0;
fCost = 0;
printf("\nEnter total revenue: ");
scanf("%f", &fRevenue);
printf("\nEnter total cost: ");
scanf ("%f", &fCost);
printf("\nYour profit is $%.2f\n", fRevenue - fCost);
}
Enter total revenue: 1 Enter total cost: 2 Your profit is $-1.00
Placeholders
The general form of a placeholder is: % flags field-width precision prefix type-identifier.
4.1.printf Basics 4.1.1. <A href="/Tutorial/C/0080__printf-scanf/TheprintfFunction.htm">The printf Function</a> 4.1.2. <A href="/Tutorial/C/0080__printf-scanf/TheprintfConversionCharactersandflags.htm">The printf() Conversion Characters and flags</a> 4.1.3. Placeholders 4.1.4. <A href="/Tutorial/C/0080__printf-scanf/diSignedintegers.htm">d, i: Signed integers</a> 4.1.5. <A href="/Tutorial/C/0080__printf-scanf/printfEscapeSequences.htm">printf() Escape Sequences</a> 4.1.6. <A href="/Tutorial/C/0080__printf-scanf/Theprintffunctionredirectstheoutputtoastandardoutputwhichistheoutputonscreen.htm">The printf() function redirects the output to a standard output, which is the output on screen</a> 4.1.7. <A href="/Tutorial/C/0080__printf-scanf/Aformatspecifierhowtoprintthedata.htm">A format specifier: how to print the data</a> 4.1.8. <A href="/Tutorial/C/0080__printf-scanf/Usemultipleconversionspecifiersinasingleprintfstatement.htm">Use multiple conversion specifiers in a single printf statement</a> 4.1.9. <A href="/Tutorial/C/0080__printf-scanf/Docalculationinprintf.htm">Do calculation in printf</a>
printf() Escape Sequences
Sequence Meaning \a Beeps the speaker \b Backspace (moves the cursor back, no erase) \f Form feed (ejects printer page; may clear the screen on some computers) \n Newline, like pressing the Enter key \r Carriage return (moves the cursor to the beginning of the line) \t Tab \v Vertical tab (moves the cursor down a line) \\ The backslash character \" The apostrophe \" The double-quote character \? The question mark \0 The "null" byte (that"s 0, not the letter O) \Onn A character value in octal (base 8) \xnnn A character value in hexadecimal (base 16)
The printf() Conversion Characters and flags
Conversion Character Displays Argument (Variable"s Contents) As %c Single character %d Signed decimal integer (int) %e Signed floating-point value in E notation %f Signed floating-point value (float) %g Signed value in %e or %f format, whichever is shorter %i Signed decimal integer (int) %o Unsigned octal (base 8) integer (int) %s String of text %u Unsigned decimal integer (int) %x Unsigned hexadecimal (base 16) integer (int)
Flags for printf
- Width specifiers
- Precision specifiers
- Input-size modifiers
The printf Function
- printf displays information on screen.
- printf returns the number of characters printed.
- printf displays the text you put inside the double quotes.
- printf requires the backslash character - an escape sequence - to display some special characters.
- printf can display variables by using the % conversion character.
- printf format: a string argument followed by any additional arguments.
#include <stdio.h>
main()
{
int i = 0;
i=printf("abcde\n");
printf("total characters printed %d\n",i);
}
abcde total characters printed 6
The printf() function redirects the output to a standard output, which is the output on screen
#include <stdio.h>
main(){
printf("Hi \n");
}
Hi
Use multiple conversion specifiers in a single printf statement
#include <stdio.h>
main(){
char firstInitial, middleInitial, lastInitial;
firstInitial= "M";
middleInitial= "A";
lastInitial= "V";
printf("My Initials are %c.%c.%c.", firstInitial, middleInitial, lastInitial);
}
My Initials are M.A.V.