Formatted IO


printf

Formally:
    int printf(char *format, ...)
...that is...
    printf( format string, argument list );


Example

    printf("The littlest=%d the largest=%d\n", little, large);
thus
    The littlest=3 the largest=10


printf Conversions

argument type; printed as
%d, %i int; decimal number
%c int; single character
%s char *; characters from string till \0 or number of characters given by precision
%f double; m.dddddd where number of ds given by precision (not float)
%e, %E double; scientific notation, m.ddddddexx (ds given by precision)


Example


scanf

  1. Formally:
            int scanf(char *format, ...)
        
  2. Informally:
            scanf( format string, argument list );
        
    for example
            scanf("%d%f", &first, &second);
        
  3. The arguments to scanf must be pointers. Compare
            scanf("%d", n);       /* WRONG */
            scanf("%d", &n);      /* right */
        
    Such errors are not usually detected at compile time.


Example

Suppose we want to scan dates of the form "25/09/1965" and "25 Sep 1965" then we use, respectively,
   int day, month, year;
   
   scanf("%d/%d/%d", &day, &month, &year);
and
    int day, year;
    char monthname[20];

    scanf("%d %s %d", &day, monthname, &year);
N.B. no & needed for the array.


scanf Conversions

input data; argument type
%d decimal integer; int
%e, %f floating point; float (not double)
%c characters; char *
%s character string (not quoted); char * pointing to an array of characters large enough for the string and terminating with \0



...previousup (conts)next...



About this document:

Produced from the SGML: /home/isd/public_html/_course_crash_in_c/_reml_grp/index.reml
On: 3/3/2003 at 17:43:41
Options: reml2 -i noindex -l long -o html -p multiple