带有 %g 和正确的双精度值的 printf 打印出令人讨厌的数字 - 可能是什么原因?

printf with %g and a correct double value prints irritating number - What could be the cause?

您好,我希望能在这里找到帮助。我不明白以下行为。请注意,这是来自 The C Programming Language (4-4) 的练习。

关于double top(void)定义在stack.c。它应该 return 作为双精度数位于堆栈顶部。

当我尝试打印它时(使用命令 p),数字格式不正确。 如果我将 printf(...) 放在顶部函数本身中,那么格式是正确的。 我不明白这是为什么。

我正在使用带有默认 gcc 编译器的 NetBeans 8.0.2。

main.c

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

#define MAXOP 100 /* max size of operand or operator */
#define NUMBER  '0' /* signal that a number was found */

int getop(char []);
void push(double);
double pop(void);

/* reverse Polish calculator */
main() {
    int type;
    double op2;
    char s[MAXOP];

    while ((type = getop(s)) != EOF) {
        switch (type) {
            case NUMBER:
                push(atof(s));
                break;
            case '+':
                push(pop() + pop());
                break;
            case '*':
                push(pop() * pop());
                break;
            case '-':
                op2 = pop();
                push(pop() - op2);
                break;
            case '/':
                op2 = pop();
                if (op2 != 0.0)
                    push(pop() / op2);
                else
                    printf("error: zero divisor\n");
                break;
            case '%':
                op2 = pop();
                if (op2 != 0.0)
                    push((int) pop() % (int) op2);
                else
                    printf("error: zero divisor\n");
                break;
            case 'p':
                printf("\t%.8g\n", top());
                break;
            case 'd':
                push(top());
                break;
            case 's':
                printStack();
                break;
            case '\n':
                printf("\t%.8g\n", pop());
                break;
            default:
                printf("error: unknown command %s\n", s);
                break;
        }

    }
    return 0;
}

stack.c

#include <stdio.h>

#define MAXVAL  100   /* maximum depth of val stack */

int sp = 0;
/* next free stack position */
double val[MAXVAL];
/* value stack */

/* push:  push f onto value stack */
void push(double f) {
    if (sp < MAXVAL)
        val[sp++] = f;
    else
        printf("error: stack full, can′t push %g\n", f);
}

/* pop:  pop and return top value from stack */
double pop(void) {
    if (sp > 0)
        return val[--sp];
    else {
        printf("error: stack empty\n");
        return 0.0;
    }
}

/* top: return top value from stack */
double top(void) {
    if (sp > 0) {
        return val[sp - 1];
    } else {
        printf("error: stack empty\n");
        return 0.0;
    }
}

/* swap two top values */
void swap(void) {
    if (sp > 1) {
        double t1 = pop();
        double t2 = pop();
        push(t1);
        push(t2);
    } else
        printf("error: stack (almost) empty\n");
}

/* clear the stack */
void clear(void) {
    sp = 0;
}

/* print the stack */
void printStack(void) {
    int i;
    for (i = sp - 1; i >= 0; i--)
        printf("%g ", val[i]);
}

getop.c

#include <ctype.h>
#include <stdio.h>

#define NUMBER  '0' /* signal that a number was found */

int getch(void);
void ungetch(int);

/* getop:  get next operator or numeric operand */
int getop(char s[]) {
    int i, c;

    while ((s[0] = c = getch()) == ' ' || c == '\t')
        ;
    s[1] = '[=12=]';
    if (!isdigit(c) && c != '.' && c != '-')
        return c;
    if (c == '-') { // possible negative number
        if (!isdigit(s[0] = c = getch())) { // minus operator
            ungetch(c);
            s[0] = c = '-';
            return c;
        } else { // negative number
            s[0] = '-';
            s[1] = c;
            i = 1;
        }
    } else
        i = 0; // positive number
    if (isdigit(c)) /* collect integer part */
        while (isdigit(s[++i] = c = getch()))
            ;
    if (c == '.') /* collect fraction part */
        while (isdigit(s[++i] = c = getch()))
            ;
    s[i] = '[=12=]';
    if (c != EOF)
        ungetch(c);

    return NUMBER;
}

getch.c

#define BUFSIZE 100

char buf[BUFSIZE]; /* buffer for ungetch */
int bufp = 0; /* next free position in buf */

int getch(void) /* get a (possibly pushed back) character */ {
    return (bufp > 0) ? buf[--bufp] : getchar();
}

void ungetch(int c) /* push character back on input */ {
    if (bufp >= BUFSIZE)
        printf("ungetch: too many characters\n");
    else
        buf[bufp++] = c;
}

%g 打印两个表示形式中较短的一个: f 或 e 。 例子: 1) printf( "%g", 3.1415926 ); 输出:3.1515926

2) printf( "%g", 93000000.0 ); 输出:9.3e+07 科学记数法最合适的地方。

你的main.c不知道top是怎么定义的。它决定了 int 的默认 return 值,这会导致在使用为 double 设计的格式打印时出现未定义的行为。如果你启用警告,你会得到类似 "Implicit declaration of top".

的信息

你在main.c的顶部写了堆栈函数pushpop的声明,但没有top的声明。

手动添加所需的原型不是好的做法。最好在header文件中声明一个编译单元的接口:

// stack.h
#ifndef STACK_H_INCLUDED
#define STACK_H_INCLUDED

void push(double f);
double pop(void);
double top(void);
void swap(void);

#endif

在您希望使用界面的代码中包含 header 文件。您将始终拥有正确的 up-to-date 版本。

也包括stack.c中的header,以确保发布的接口和实际实现是一致的。