Chapter 2. Lexical elements, Operators, and the C system

Home

ÄÚµå¿Í ÇѱÛ

procedence and associativity of operators

Operators

Associativity

() ++(postfix) --(postfix)

+(unary) -(unary) ++(prefix) --(prefix)

* / %

+ -

= += -= *= /= etc

left to right

right to left

left to right

left to right

right to left

¡¡

PROGRAM 20 : ascii.c


#include <stdio.h>

main()
{
  int i;

  for(i=0; i<256; i++) {
    printf("%3d=%c:", i, i);
    if((i+1)%10==0) printf(" \n");
  }
}

¡¡

PROGRAM 21 : sum.c


/* 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;
}

PROGRAM 22 : power.c


/* 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;
}

PROGRAM 23 : rand.c 


#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;
}

PROGRAM 24 : exer_18.c


#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;
}


[Last Update: 2001.2.20] Dongseo University Cyber Campus