- 1
My C code is generating error,please i need correction>>Thank you all-:
3 Respostas
+ 2
#include <stdio.h>
int main() {
int a,b;
int sum;
// sum=a+b; // a and b uninitialized here
printf("Enter the value of a:");
scanf ("%d",&a); // you must pass pointer, not value
printf("Enter the value of b:");
scanf ("%d",&b); // same here
sum = a+b; // now a and b initialized
printf("sum = %d",sum); // print sum value
// scanf("%d ,&sum); // scanf is for getting value from user but sum is computed and already displayed to user
return 0;
}
// few shorter method by avoiding sum temporary variable:
int main() {
int a,b;
// int sum; // not needed
printf("Enter the value of a:");
scanf("%d",&a);
printf("Enter the value of b:");
scanf("%d",&b);
// sum = a+b; // not needed
printf("sum = %d",a+b);
return 0;
}
+ 1
missing semicolon on line 7.
you also did not scan for the two digits you will be adding. After the prompt to enter a number is printed, scanf() should be called to fill the int variable with the value entered.
+ 1
So, as I may understand you want a program that takes two numbers as input and outputs their sum, am I right?
In that case its basic structure should be:
Input -> a
Input -> b
(sum = a+b)
Output <- a+b (sum)
While you did something really strange:
sum = a+b (which are uninitialized, they carry nothing)
Input -> sum
Also be careful with the syntax, you missed a ; and a "