Lexical Elements - Chapter 2


Example: Scan and Print

/* Read in two integers and print their sum. */

#include <stdio.h>

int main(void)
{
   int   a, b, sum;

   printf("Input two integers:  ");
   scanf("%d%d", &a, &b);
   sum = a + b;
   printf("%d + %d = %d\n", a, b, sum);
   return 0;
}
sum.c

Example: Scan and Print - a comment

/* Read in two integers and print their sum. */

#include <stdio.h>

int main(void)
{
  int a, b, sum;

  printf("Input two integers:  ");
  scanf("%d%d", &a, &b);
  sum = a + b;
  printf("%d + %d = %d\n", a, b, sum);
  return 0;
}

Example: Scan and Print - a preprocessor directive

/* Read in two integers and print their sum. */

#include <stdio.h>

int main(void)
{
  int a, b, sum;

  printf("Input two integers:  ");
  scanf("%d%d", &a, &b);
  sum = a + b;
  printf("%d + %d = %d\n", a, b, sum);
  return 0;
}

Example: Scan and Print - a function

/* Read in two integers and print their sum. */

#include <stdio.h>

int main(void)
{
  int a, b, sum;

  printf("Input two integers:  ");
  scanf("%d%d", &a, &b);
  sum = a + b;
  printf("%d + %d = %d\n", a, b, sum);
  return 0;
}

Example: Scan and Print - variables

/* Read in two integers and print their sum. */

#include <stdio.h>

int main(void)
{
  int a, b, sum;

  printf("Input two integers:  ");
  scanf("%d%d", &a, &b);
  sum = a + b;
  printf("%d + %d = %d\n", a, b, sum);
  return 0;
}

Example: Scan and Print - a string constant

/* Read in two integers and print their sum. */

#include <stdio.h>

int main(void)
{
  int a, b, sum;

  printf("Input two integers:  ");
  scanf("%d%d", &a, &b);
  sum = a + b;
  printf("%d + %d = %d\n", a, b, sum);
  return 0;
}

Comments


Keywords


Identifiers


Constants

0
0123
123456789000    /* too large? */
0123            /* an octal integer */
-49             /* a constant expression */
123.0           /* a floating constant */

String Constants

"a string of text"
""              /* the null string */
"a = b + c"     /* nothing is executed */
"a string with double quotes \" within"
"a string backslash \\ is in this string"

Operators and Punctuators

a+b             /* the expression a plus b */
a_b             /* a 3-character identifier */

Increment and Decrement Operators

int a, b, c = 0;
a = ++c;
b = c++;
printf("%d %d %d\n", a, b, c++);        /* 1 1 2 is printed */

Assignment Operators

b = 2;
c = 3;
a = b + c;
<=>
a = (b = 2) + (c = 3);

Example: Computing Powers of 2

/* Some powers of 2 are printed. */

#include <stdio.h>

int main(void)
{
   int   i = 0, power = 1;

   while (++i <= 10)
      printf("%6d", power *= 2);
   printf("\n");
   return 0;
}
power_of_2.c

Output

     2     4     8    16    32    64   128   256   512  1024

The C System


Randomly Distributed Integers

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
   int   i, n;

   printf("\n%s\n%s",
      "Some randomly distributed integers will be printed.",
      "How many do you want to see?  ");
   scanf("%d", &n);
   for (i = 0; i < n; ++i) {
      if (i % 10 == 0)
         putchar('\n');
      printf("%7d", rand());
   }
   printf("\n\n");
   return 0;
}
prn_rand.c

Output

Some randomly distributed integers will be printed.
How many do you want to see?  19

  16838   5758  10113  17515  31051   5627  23010   7419  16212   4086
   2749  12767   9084  12060  32225  17543  25089  21183  25137

Statistics

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define   NCALLS    1000000   /* number of fct calls */
#define   NCOLS     10        /* number of columns   */
#define   NLINES    5         /* number of lines     */

int main(void)
{
   int    i, val;
   long   begin, diff, end;

   begin = time(NULL);
   srand(time(NULL));
   printf("\nTIMING TEST: %d calls to rand()\n\n", NCALLS);
   for (i = 0; i < NCALLS; ++i) {
      val = rand();
      if (i < NCOLS * NLINES) {
         if (i % NCOLS == 0)
               putchar('\n');
         printf("%7d", val);
      }
      if (i == NCOLS * NLINES)
         printf("\n.......\n");
   }
   end = time(NULL);
   diff = end - begin;
   printf("%s%ld\n%s%ld\n%s%ld\n%s%.10f\n\n",
      "          end time: ", end,
      "        begin time: ", begin,
      "      elapsed time: ", diff,
      "time for each call: ", (double) diff / NCALLS);
   return 0;
}
stat.c

Output

TIMING TEST: 1000000 calls to rand()


  14290  14842  22261  26349   1071    676   8285  21259  11015  15521
  27374  23015  23493  22998  16951  19599  10507   9277   8695  19685
  25234   2196  10584  26157  12484  24519  17900   8792  19995  25374
   4712  10254  31287  14118  25891  11645    706  14689  19020  19428
  32260  31917  26094  32734   4933   8309  31141  25199  27878  28313
.......
          end time: 763870220
        begin time: 763870218
      elapsed time: 2
time for each call: 0.0000020000

top prev


Last modified: July 23 2004.