Click here to Skip to main content
15,899,475 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#include<stdio.h>
main()
{
	int a,b,c=a+b;
	printf("Enter the value for a\n");
	scanf("%d, &a");
	printf("Enter the value for b\n");
	scanf("%d, &b");
	printf("a+b=%d", c);
	system ("pause");
}


What I have tried:

I tried with different inputs but the output is showing 1 always for different inputs
Posted
Updated 10-Sep-18 11:24am

The code works as you wrote it.

What is happening is that you defined the variables a, b, and c. The variables a and b have no values, but should be 0. You told the compiler to add a and b together and assign the result to c.

Wellllllll, that code only works once. You're NOT telling the compiler to use this as a formula that say "this is how you always determine the value of c". There is no such thing as defining a formula like this in C, or any other language that I know of.

You have to run code that makes that calculation AFTER you get the values of a and b from the user.
C
int a, b, c;
// Get the values for a and b...
c = a + b;
// Show the result...
 
Share this answer
 
Comments
gschahal 10-Sep-18 12:25pm    
#include<stdio.h>
main()
{
int a,b,c;
printf("Enter the value for a\n");
scanf("%d,&a");
printf("Enter the value for b\n");
scanf("%d,&b");
c = a + b;
printf("a+b=%d\n",c);
system ("pause");
}

I changed the program as you said but still it is showing me the output as 1.
Dave Kreskowiak 10-Sep-18 12:36pm    
Like Richard said, you also made the same mistake as you made in the other question you posted. You have the double quotes in all of your scanf statements in the wrong place.
Exactly the same error on your part as The output for float variable is 0.0000[^]
 
Share this answer
 
Good grief, but you are a slow learner...
Same as your last question, the double quotes are in the wrong place. Change this:
scanf("%d, &b");
To this:
scanf("%d", &b);
And try to remember this time! :laugh:
 
Share this answer
 
Quote:
The addition output is not correct

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your cpde is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900