Wednesday, June 6, 2012

The Formatted Output of a C Program

printf() function

   The printf() function is one of the most widely used functions in C. The purpose is to output its argument
list to the standard output device. All the characters within a pair of double quotes will be treated as a
string constant except for escape sequences and conversion specifications.

Escape Sequence

\ tells the compiler to treat the following characters in a different way.


Format Control Codes

In the printf() function the control string contains format commands that tells printf() how to display the
remaining arguments and how many arguments are there.

Eg: 1) printf(“%d\n”,2); => 2                     2) Printf(“%f\n,2.01) => 2.010000

My First C Program
              #include<stdio.h>
              int main()
              {
                      printf("Hello World");
                      return(0);
               }

puts() function

puts() is a simpler version of printf() taking only a single string argument and performing no formatting.
puts() automatically appends a new line after displaying the string.

           /* Put a String */
           #include <stdio.h>
           #define mychoice “IT Faculty – Moratuwa University”
           main()
          {
                 puts(mychoice);
                 return(0);
           }


           /* Escape Sequences */
           #include <stdio.h>
           main()
          {
                printf(“C\t U\t Later \n”);
                return(0);
           }






No comments:

Post a Comment