prev

Constants/Printf Example

next
Source code
     #include <stdio.h>
     
     /* Example program demonstrating the use of numeric, string, and enumeration
      * constants.
      */
     
     int main (int argc, char** argv) {
     
         int some_kind_of_integer;
     
         enum boolean { NO, YES };
         enum months { JAN = 1, FEB, MAR };
         enum instructors { JIM = 5, MATT = 10 };
         
         some_kind_of_integer = 12;
     
         printf ("integer               : %d\n", 16);
         printf ("octal                 : %d\n", 020);
         printf ("hexadecimal           : %d\n", 0x10);
         printf ("long integer          : %ld\n", -0x10L);
         printf ("signed long integer   : %ld\n", -123L);
         printf ("double                : %f\n", 12.34);
         printf ("double (exp notation) : %f\n", 1234E-2);
     
         printf ("integer from variable : %d\n", some_kind_of_integer );
     
         printf ("Character code        : %d\n", '0');
         printf ("constant string       : %s\n", "foobar");
     
         printf ("Enumerations\n");
         printf ("    NO = %d,  YES = %d\n", NO, YES);
         printf ("    JAN = %d, FEB = %d, MAR = %d\n", JAN, FEB, MAR);
         printf ("    JIM = %d, MATT = %d\n", JIM, MATT);
     
         return 0;
     }

Produces the output
integer               : 16
octal                 : 16
hexadecimal           : 16
long integer          : -16
signed long integer   : -123
double                : 12.340000
double (exp notation) : 12.340000
integer from variable : 12
Character code        : 48
constant string       : foobar
Enumerations
    NO = 0,  YES = 1
    JAN = 1, FEB = 2, MAR = 3
    JIM = 5, MATT = 10