Exercises : Answer the following Question

Submitted by Anonymous (not verified) on Tue, 02/26/2013 - 00:00

Workshop

To help solidify your understanding of this hour's lesson, you are encouraged to answer the quiz questions and finish the exercises provided in the Workshop before you move to the next lesson. The answers and hints to the questions and exercises are given in Appendix E, "Answers to Quiz Questions and Exercises."

Quiz

    What is the difference between the = operator and the == operator?
    In the x + - y - - z expression, which operator is the subtraction operator, and which one is the unary minus operator?
    Given x = 15 and y = 4, what do the x / y and (float)x / y expressions return, respectively?
    Is the y *= x + 5 expression equivalent to the y = y * x + 5 expression?

Exercises

   1. Given x = 1 and y = 3, write a program to print out the results of these expressions: x += y, x += -y, x -= y, x -= -y, x *= y, and x *= -y.
    Given x = 3 and y = 6, what is the value of z after the expression  z = x * y == 18 is executed?

   2. Write a program that initializes the integer variable x with 1 and outputs results with the following two statements:

    printf("x++ produces:   %d\n", x++);
    printf("Now x contains: %d\n", x);

    Rewrite the program you wrote in exercise

 

3. This time, include the following two statements:

    printf("x = x++ produces: %d\n", x = x++);
    printf("Now x contains:   %d\n", x);

    What do you get after running the executable of the program? Can you explain why you get such a result?
    The following program is supposed to compare the two variables, x and y, for equality. What's wrong with the program? (Hint: Run the program to see what it prints out.)

    #include <stdio.h>

    main()
    {
       int x, y;

       x = y = 0;
       printf("The comparison result is: %d\n",  x = y);
       return 0;
    }

 

Related Items

The if-else Statement

The if-else Statement

The if statement

The if statement

If life were a straight line, it would be very boring. The same thing is true for programming. It would be too dull if the statements in your program could only be executed in the order in which they appear.

Mathematical Functions in C

Mathematical Functions in C

Basically, the math functions provided by the C language can be classified into three groups:

    Trigonometric and hyperbolic functions, such as acos(), cos(), and cosh().

Changing Data Sizes

Changing Data Sizes

Enabling or Disabling the Sign Bit

Enabling or Disabling the Sign Bit

As you know, it's very easy to express a negative number in decimal. All you need to do is put a minus sign in front of the absolute value of the number. But how does the computer represent a negative number in the binary format?