C Tutorial/printf scanf/printf Basics

Материал из C\C++ эксперт
Версия от 13:32, 25 мая 2010; Admin (обсуждение | вклад) (1 версия: Импорт контента...)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

A format specifier: how to print the data

%d is one of the format specifiers for integer.


<source lang="cpp">#include <stdio.h> main(){

   int i,j,k;
  
   i = 6;    
   j = 8;
   k = i + j;
  
   printf("sum of two numbers is %d \n",k);

}</source>

sum of two numbers is 14

d, i: Signed integers

<source lang="cpp">#include <stdio.h> main() {

   int i = 100;
   printf(" %d\n",i);
   printf(" %i\n",i);

}</source>

100
      100

Do calculation in printf

<source lang="cpp">#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);

}</source>

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

  1. Width specifiers
  2. Precision specifiers
  3. Input-size modifiers

The printf Function

  1. printf displays information on screen.
  2. printf returns the number of characters printed.
  3. printf displays the text you put inside the double quotes.
  4. printf requires the backslash character - an escape sequence - to display some special characters.
  5. printf can display variables by using the % conversion character.
  6. printf format: a string argument followed by any additional arguments.


<source lang="cpp">#include <stdio.h> main() {

   int i = 0;
   
   i=printf("abcde\n");   
   
   printf("total characters printed %d\n",i);

}</source>

abcde
     total characters printed 6

The printf() function redirects the output to a standard output, which is the output on screen

<source lang="cpp">#include <stdio.h> main(){

 printf("Hi \n");

}</source>

Hi

Use multiple conversion specifiers in a single printf statement

<source lang="cpp">#include <stdio.h> main(){

   char firstInitial, middleInitial, lastInitial;
   firstInitial= "M";
   middleInitial= "A";
   lastInitial= "V";
   printf("My Initials are %c.%c.%c.", firstInitial, middleInitial, lastInitial);

}</source>

My Initials are M.A.V.