Pratik Ratnaparkhi
Pratik Ratnaparkhi

Reputation: 27

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

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");  
}

Upvotes: 0

Views: 126

Answers (5)

Sourav Ghosh answered to most of your questions; 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). Others compiler options (-fsanitize=address etc etc...) and other tools (the gdb debugger, the valgrind tool, strace, ...) might be useful too!

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. Be aware that with UB, your (or any other) program might seems to work while in fact it has some undefined behavior.

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).

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. 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 (e.g. 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.

Upvotes: 1

sarath chambayil
sarath chambayil

Reputation: 77

printf("the result of the %d and %d is %d, a, b, result"); it has to be printf("the result of the %d and %d is %d", a, b, result); it will read and assign the %d value from left to write. printf returns number of characters

Upvotes: 0

Dilip Kumar
Dilip Kumar

Reputation: 1746

printf statement is the reason for undesired output,

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.

Upvotes: 1

Sourav Ghosh
Sourav Ghosh

Reputation: 134286

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()

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

and

The functions in the printf() family produce output according to a format ....

So basically your code should be

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

The supplied variables are not part of the 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

[...] 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(). So, always enable warnings.

Upvotes: 5

sharon
sharon

Reputation: 734

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.

You will get the warning message that %d expects a matching int

It will have the garbage value in that position, So it is printing some value.

Upvotes: 2

Related Questions