zoukankan      html  css  js  c++  java
  • Chapter 5 :Operators,Expressions and Statements

    1. Assume all variables are of type int. Find the value of each of the following variables:

    a.  x = (2 + 3) * 6; 

    b.  x = (12 + 6)/2*3;

    c. y = x = (2 + 3)/4;

    d. y = 3 + 2*(x = 7/2);

    a. x = 30    b. x = 27    c. y = 1, x = 1    d. x = 3, y = 9


    ​​​​​​2. Assume all variables are of type int. Find the value of each of the following variables:

    a. x = (int) 3.8 + 3.3;

    b. x = (2 + 3) * 10.5;

    c.   x = 3 / 5 * 22.0;

    d.   x = 22.0 * 3 / 5;

    a. x = 6    b. x = 52    c. x = 0    d. x = 13


    3. Evaluate each of the following expressions:

    a. 30.0 / 4.0 * 5.0;

    b. 30.0 / (4.0 * 5.0);

    c.   30 / 4 * 5;

    d.   30 * 5 / 4;

    e.   30 / 4.0 * 5;

    f.   30 / 4 * 5.0;

    a. 37.5    b. 1.5    c. 35    d. 37    e. 37.5    f. 35.0


    4. What will this program print?

    #include <stdio.h>
    
    #define FORMAT "%s! C is cool
    "
    
    int main(void) {
        int num = 10;
    
        printf(FORMAT, FORMAT);
        printf("%d
    ", num);
        printf("%d
    ", ++num);
        printf("%d
    ", num++);
        printf("%d
    ", num--);
        printf("%d
    ", num);
    
        return 0;
    }
    %s! C is cool
    ! C is cool
    10
    11
    11
    12
    11
    

    5. What will this program print?

    #include <stdio.h>
    
    #define TEN 10
    
    int main(void) {
        int n = 0;
    
        while (n++ < TEN)
            printf("%5d", n);
        printf("
    ");
    
        return 0;
    }

        1    2    3    4    5    6    7    8    9   10


    6. Modify the last program so that it prints the letters a through g instead.

    #include <stdio.h>
    
    #define terminatingLetter 'g'
    
    int main(void) {
        char startingLetter = 'a';
        while (startingLetter <= terminatingLetter)
            printf("%5c", startingLetter++);
        printf("
    ");
        return 0;
    }

    7. If the following fragments were part of a complete program, what would they print?

    a. 

    int x = 0;
    
    while (++x<3)
        printf("%4d", x);

    b. 

    int x = 100;
    
    while (x++ < 103)
        printf("%4d
    ",x);
        printf("%4d
    ",x);
    

    c.

    char c = 's';
    
    while(ch<'w'){
        printf("%c", ch);
        ch++;
    }
    printf("%c
    ", ch);

    a. 

       1   2

    b. 

     101

     102

     103

     104

    c. 

    stuvw


    8. ​​​​​​​What will the following program print?

    #define MESG "COMPUTER BYTES DOG"
    
    #include <stdio.h>
    
    int main(void) {
        int n = 0;
    
        while (n < 5) 
            printf("%s
    ", MESG);
            n++;
        printf("That's all.
    ");
        
        return 0;
    }
    

    This is an ill-constructed program. Because the while statement doesn't use braces, only the printf() statement is part of the loop, so the program prints the message COMPUTER BYTES DOG indefinitely until you can kill the program.

    苟利国家生死以, 岂因祸福避趋之
  • 相关阅读:
    MyBatis基础面试题
    MyBatis面试题
    Spring MVC @RequestBody自动转JSON HTTP415错误解决方法
    【GDKOI 2016】地图 map 类插头DP
    【Codechef FRBSUM】【FJOI2016】【BZOJ4299】【BZOJ 4408】 可持久化线段树
    【NOI2005】聪聪和可可 概率与期望 记忆化搜索
    【bzoj 1076】【SCOI2008】奖励关
    BZOJ 1009 HNOI 2008 GT考试 递推+矩乘
    BZOJ 3809 Gty的二逼妹子序列
    BZOJ 2821作诗(Poetize) 分块
  • 原文地址:https://www.cnblogs.com/chintsai/p/11829256.html
Copyright © 2011-2022 走看看