问题:

我正在将代码从Linux C移植到Visual C ++ for windows。

Visual C ++不知道#include <stdint.h>所以我评论了它。

后来,我发现了很多'uint32_t': identifier not found错误。 怎么解决?

I’m porting code from Linux C to Visual C++ for windows.Visual C++ doesn’t know #include <stdint.h> so I commented it out.Later, I found a lot of those 'uint32_t': identifier not found errors.How can it be solved?

问题:

而不是2*i ,我不小心写了2i :

int foo(int i)
{
    2i;
    return 2i;
}

我希望编译器能够捕获错误。 但事实并非如此。 那么2i是C中的有效陈述吗? 如果是这样,它做什么? 困惑!

我使用gcc版本5.3.0编译,这里是程序集输出:

    .file   "strange.c"
    .text
    .globl  foo
    .type   foo, @function
foo:
.LFB0:
    .cfi_startproc
    pushq   %rbp
    .cfi_def_cfa_offset 16
    .cfi_offset 6, -16
    movq    %rsp, %rbp
    .cfi_def_cfa_register 6
    movl    %edi, -4(%rbp)
    nop
    popq    %rbp
    .cfi_def_cfa 7, 8
    ret
    .cfi_endproc
.LFE0:
    .size   foo, .-foo
    .ident  "GCC: (GNU) 5.3.0"
    .section    .note.GNU-stack,"",@progbits

Instead of 2*i , I carelessly wrote 2i :I expected the compiler to catch the error.But it did not.So is 2i a valid statement in C?If so what does it do?Puzzled!I compiled using gcc version 5.3.0 and here is the assembly output:

问题:

这个问题实际上是不久前在programming.reddit.com上进行有趣讨论的结果。 它基本归结为以下代码:

int foo(int bar)
{
    int return_value = 0;
    if (!do_something( bar )) {
        goto error_1;
    }
    if (!init_stuff( bar )) {
        goto error_2;
    }
    if (!prepare_stuff( bar )) {
        goto error_3;
    }
    return_value = do_the_thing( bar );
error_3:
    cleanup_3();
error_2:
    cleanup_2();
error_1:
    cleanup_1();
    return return_value;
}

在这里使用goto似乎是最好的方法,导致所有可能性中最干净,最有效的代码,或者至少在我看来。 Code Complete中引用Steve McConnell:

goto在分配资源,对这些资源执行操作,然后释放资源的例程中很有用。 使用goto,您可以清理代码的一部分。 goto可以降低忘记在检测到错误的每个位置释放资源的可能性。

此方法的另一个支持来自本节中的“ Linux设备驱动程序”一书。

你怎么看? 这种情况是否适用于C中的goto ? 您是否更喜欢其他方法,这些方法产生更复杂和/或效率更低的代码,但是避免使用goto ?

This question is actually a result of an interesting discussion at programming.reddit.com a while ago.It basically boils down to the following code:The usage of goto here appears to be the best way to go, resulting in the cleanest and most efficient code of all possibilities, or at least so it seems to me.Quoting Steve McConnell in Code Complete :The goto is useful in a routine that allocates resources, performs operations on those resources, and then deallocates the resources.With a goto, you can clean up in one section of the code.The goto reduces the likelihood of your forgetting to deallocate the resources in each place you detect an error.Another support for this approach comes from the Linux Device Drivers book, in this section .What do you think?Is this case a valid use for goto in C?Would you prefer other methods, which produce more convoluted and/or less efficient code, but avoid goto ?

问题:

这个问题已经在这里有了答案:

我是C语言的新手,我有以下代码:

#include <stdio.h>
#include <math.h>
int main(void)
{
  double x = 0.5;
  double result = sqrt(x);
  printf("The square root of %lf is %lf\n", x, result);
  return 0;
}

但是当我用以下命令编译时:

gcc test.c -o test

我收到这样的错误:

/tmp/cc58XvyX.o: In function `main':
test.c:(.text+0x2f): undefined reference to `sqrt'
collect2: ld returned 1 exit status

为什么会这样? sqrt()不在math.h头文件中? 我在cosh和其他三角函数中遇到了相同的错误。 为什么?

This question already has an answer here:undefined reference to sqrt (or other mathematical functions) 5 answersI’m very new to C and I have this code:But when I compile this with:I get an error like this:Why does this happen?Is sqrt() not in the math.h header file?I get the same error with cosh and other trigonometric functions.Why?