简体   繁体   English

函数和参数给出错误

[英]Functions and arguments giving error

The result of this code down below should be 30, however when compiled and run this is giving me the following result 下面的代码的结果应该是30,但是在编译和运行时,这给了我以下结果

the result of 2358968 and 0 is 4200271, a , b , result

I don't understand when I have no variables of that value how could the result be like that? 我不明白当我没有该值的变量时,结果将如何?

#include<stdio.h>
#include<conio.h>

void add( int , int );   

int main(){
    add(10,20);
    getch();
    return 0 ;
}


void add(int a, int b){
    int result = a + b;
    printf("the result of the %d and %d is %d, a, b, result");  
}

In your code, 在您的代码中

printf("the result of the %d and %d is %d, a, b, result");

should be 应该

 printf("the result of the %d and %d is %d\n", a, b, result);
                                           ^
                                           |
                                     //format string ends here
    // arguments for format specifiers are meant to be 
   // supplied as different arguments to `printf()`, not as a part of <format> string itself

From the man page of printf() printf()手册页

int printf(const char *format, ...); int printf(const char * format,...);

and

The functions in the printf() family produce output according to a format .... printf()系列中的printf()根据format ...产生输出。

So basically your code should be 所以基本上你的代码应该是

 printf("<format>", var1, var2,....);

The supplied variables are not part of the format . 提供的变量不是 format一部分。

The problem in your code were missing variables (arguments) for supplied format specifiers. 您的代码中的问题是缺少提供的格式说明符的变量(参数)。 As per the C11 standard , chapter 7.21.6.1, paragraph 2 根据C11标准第7.21.6.1章第2段

[...] If there are insufficient arguments for the format, the behavior is undefined. [...]如果格式的参数不足,则行为未定义。 [...] [...]

So, your code produces undefined behaviour printning out some junk value. 因此,您的代码会产生未定义的行为,并打印出一些垃圾值。

Moral of the Story : If you had enabled compiler warnings, you compiler should warn you about missing (mismatch) agruments in printf() . 故事寓意 :如果启用了编译器警告,则编译器应警告您有关printf()缺少(不匹配)错误的信息。 So, always enable warnings. 因此,请始终启用警告。

Sourav Ghosh answered to most of your questions; Sourav Ghosh回答了您的大多数问题; he rightly suggested to enable all warnings when compiling. 他正确地建议在编译时启用所有警告。 If your compiler is (notably on Linux) some recent GCC or Clang/LLVM pass -Wall -Wextra -g to the gcc or clang command (to get nearly all warnings, some extra ones, and debug information). 如果您的编译器是(特别是在Linux上)编译器,则一些最新的GCCClang / LLVM-Wall -Wextra -g传递给gccclang命令(以获取几乎所有警告,一些其他警告和调试信息)。 Others compiler options ( -fsanitize=address etc etc...) and other tools (the gdb debugger, the valgrind tool, strace , ...) might be useful too! 其他编译器选项( -fsanitize=address等...)和其他工具( gdb调试器, valgrind工具, strace等)也可能有用!

I dont understand when I have no variables of that value how could the result be like that? 我不明白当我没有该值的变量时,结果将如何?

Because you have encountered undefined behavior . 因为您遇到未定义的行为 Read much more about it (in particular Lattner's blog on What Every C Programmer should know about Undefined Behavior ), and work hard to always avoid it in all cases. 阅读有关它的更多信息(特别是Lattner在每个C程序员应该了解的未定义行为方面的博客),并努力在所有情况下始终避免它。 Be aware that with UB, your (or any other) program might seems to work while in fact it has some undefined behavior. 请注意,使用UB,您的(或任何其他)程序似乎可以运行,而实际上它具有一些未定义的行为。

The particular garbage (or apparently random) values that are printed : might not be reproducible, and can be explained only by diving into every implementation details, including looking into the machine code generated by your particular compiler and inspecting some machine state (register values); 打印的特定垃圾(或显然是随机的)值:可能无法重现,并且只能通过深入研究每个实现细节来进行解释,包括查看由特定编译器生成的机器代码并检查某些机器状态(寄存器值) ; concretely the printf implementation on your system is probably using some stdarg(3) things and fetching some values from uninitialized memory or register which happens to have some previous "garbage" value (at the machine level). 具体而言,您系统上的printf 实现可能正在使用一些stdarg(3)东西,并从未初始化的内存或寄存器中获取某些值,而这些值恰好具有一些先前的“垃圾”值(在计算机级别)。

See also this answer (and many others) about undefined behavior. 另请参阅有关未定义行为的此答案 (以及许多其他答案 )。 Work hard to always avoid it. 努力工作,始终避免这种情况。 Remember that when coding (even in C) you are ultimately coding for some virtual or abstract machine which is not the real computer. 请记住,在进行编码(甚至使用C语言)时,您最终还是在为不是真正计算机的虚拟机或抽象机进行编码。 Even if writing all the software levels from the operating system kernel included - that would take you many dozens of years at least -, you are coding for some specified abstraction and you have to ignore some misbehavior of your computer (eg if it breaks, get some cosmic rays, etc etc...) outside of these abstractions. 即使从所包含的操作系统内核编写所有软件级别(至少需要花费数十年的时间),您也正在为某些指定的抽象进行编码,并且您必须忽略计算机的某些异常行为(例如,如果计算机坏了,这些抽象之外的一些宇宙射线等)。 The C99 or C11 programming language standard specification (a technical document of several hundred pages in English), with the POSIX 2008 operating system specification, are useful abstractions to code software for. 带有POSIX 2008操作系统规范的C99C11编程语言标准规范(英语的数百页技术文档)是编码软件的有用抽象。

You have closed the " at the end of the line. and in the printf function you have specified the %d So the %d will not have the variable to point and fetch the value. 您已经关闭了"在该行的末尾。而在printf功能已指定%d于是%d不会有变量指向和获取价值。

You will get the warning message that %d expects a matching int 您将收到警告消息,指出%d expects a matching int

It will have the garbage value in that position, So it is printing some value. 该位置将具有垃圾值,因此它正在打印一些值。

printf statement is the reason for undesired output, printf语句是不希望输出的原因,

printf("the result of the %d and %d is %d, a, b, result");

There is no arguments specified for format specifier, the position of " has to be changed. 没有为格式说明符指定参数, "的位置必须更改。

the statement should be like this 声明应该像这样

printf("the result of the %d and %d is %d", a, b, result);

In your printf statement it was taking garbage value since no arguments were specified there for %d . 在您的printf语句中,由于没有为%d指定参数,因此它采用了垃圾值。

printf("the result of the %d and %d is %d, a, b, result"); printf(“%d和%d的结果为%d,a,b,结果”); it has to be printf("the result of the %d and %d is %d", a, b, result); 它必须是printf(“%d和%d的结果为%d”,a,b,结果); it will read and assign the %d value from left to write. 它将读取并从左开始分配%d值进行写入。 printf returns number of characters printf返回字符数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM