小编use*_*996的帖子

srand函数返回相同的值

嘿伙计们来看看这个节目.

/* The craps game, KN king page 218 */

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

int roll_dice(void);
bool play_game(void);

int roll_dice(void)
{
    int roll;

    getchar();
    srand((unsigned) time(NULL));

    roll = rand() % 13;

    if(roll == 0)
    roll = roll + 1;

    return roll;
}

bool play_game()
{
    int sum = 0, wins = 0, loss = 0, point;

    sum = roll_dice();

    printf("You rolled: %d", sum);

    if(sum == 7 || sum == 11)
    {
        printf("\nYou won!\n");
        return true;
    } …
Run Code Online (Sandbox Code Playgroud)

c coding-style srand

0
推荐指数
1
解决办法
1445
查看次数

使用switch语句时出现MACRO错误

嘿看看代码:

#define SUFFIX(n)  (switch(n)                                               \
                   {                                                        \
                        case 1: printf("st\n");                             \
                        break;                                              \
                                                                            \
                        case 2: printf("nd\n");                             \
                        break;                                              \
                                                                            \
                        case 3: printf("rd\n");                             \
                        break;                                              \
                   }                                                        \
                   )                                                        
Run Code Online (Sandbox Code Playgroud)

在main中调用上面的宏:

int main()
{
    printf("%s", suffix(1));
}
Run Code Online (Sandbox Code Playgroud)

但是当我调用它时,我收到一条错误消息:

expected expression before switch
Run Code Online (Sandbox Code Playgroud)

但我错过了什么表达方式?

c macros switch-statement

0
推荐指数
1
解决办法
6013
查看次数

标签 统计

c ×2

coding-style ×1

macros ×1

srand ×1

switch-statement ×1