saving . . . saved Regarding the program has been deleted. Regarding the program has been hidden .
Regarding the program
Title
Question
While writing the following code for general arthematic operators, I ran the code with an error (which I later found & also marked below).
Surprisingly, without showing an error, the compiler gave an output, which is very odd and absurd.
FYI, I ran the code on repl.it. How did it even take the value of 'b'? On what basis it produced the outputs?

#include <stdio.h>

int main()
{
 int a,b; 

float sum,diff,prod,quo;
int rem;

<span style="background-color: rgb(250, 250, 250);"> printf("Enter the value of 'a': ");</span>
 scanf("%d",&a);
 printf("Enter the value of 'b': ");
scanf("%d",&a);    -->Here, I had an error. Instead of 'b', I typed 'a'.

sum= a + b;
diff= a-b;
prod= a * b;
quo= (float) a/b;
rem= a % b;
 printf("\n");
printf("The sum of a & b is %.2f\n",sum);
 printf("\n");
 printf("The difference of a & b is %.2f\n",diff);
 printf("\n");
printf("The product of a & b is %.2f\n",prod);
printf("\n");
 printf("The quotient when a is divided by b is %.2f\n",quo);
printf("\n");
printf("The remainder when a is divided by b is %d\n",rem);

return 0;

C-and-Cpp Arithmetic-Operators 06-07 min 40-50 sec 21-10-20, 8:46 p.m. bsscharan

Answers:

Dear bsscharan,
   All memory addresses have some arbitrary values initialised (garbage value). If your program does not initialise values for that variable, the compiler uses the garbage value, and proceeds ahead with execution. Hope to clear your doubt.

Spoken Tutorial Group
13-07-21, 4:05 p.m. nags


#include <stdio.h>

int main()
{
 int a,b; 

float sum,diff,prod,quo;
int rem;

<span style="background-color: rgb(250, 250, 250);"> printf("Enter the value of 'a': ");</span>
 scanf("%d",&a);
 printf("Enter the value of 'b': ");
scanf("%d",&a);    -->Here, I had an error. Instead of 'b', I typed 'a'.

sum= a + b;
diff= a-b;
prod= a * b;
quo= (float) a/b;
rem= a % b;
 printf("\n");
printf("The sum of a & b is %.2f\n",sum);
 printf("\n");
 printf("The difference of a & b is %.2f\n",diff);
 printf("\n");
printf("The product of a & b is %.2f\n",prod);
printf("\n");
 printf("The quotient when a is divided by b is %.2f\n",quo);
printf("\n");
printf("The remainder when a is divided by b is %d\n",rem);

return 0;
12-08-24, 2:49 p.m. tanmayi.P


#include <stdio.h>

int main()
{
 int a,b; 

float sum,diff,prod,quo;
int rem;

<span style="background-color: rgb(250, 250, 250);"> printf("Enter the value of 'a': ");</span>
 scanf("%d",&a);
 printf("Enter the value of 'b': ");
scanf("%d",&a);    -->Here, I had an error. Instead of 'b', I typed 'a'.

sum= a + b;
diff= a-b;
prod= a * b;
quo= (float) a/b;
rem= a % b;
 printf("\n");
printf("The sum of a & b is %.2f\n",sum);
 printf("\n");
 printf("The difference of a & b is %.2f\n",diff);
 printf("\n");
printf("The product of a & b is %.2f\n",prod);
printf("\n");
 printf("The quotient when a is divided by b is %.2f\n",quo);
printf("\n");
printf("The remainder when a is divided by b is %d\n",rem);

return 0;
12-08-24, 3:10 p.m. 238T1A05D3


#include <stdio.h>

int main()
{
 int a,b; 

float sum,diff,prod,quo;
int rem;

printf("Enter the value of 'a': ");
scanf("%d",&a);
printf("Enter the value of 'b': ");
scanf("%d",&b);

sum= a + b;
diff= a-b;
prod= a * b;
quo= (float) a/b;
rem= a % b;
 printf("\n");
printf("The sum of a & b is %.2f\n",sum);
 printf("\n");
 printf("The difference of a & b is %.2f\n",diff);
 printf("\n");
printf("The product of a & b is %.2f\n",prod);
printf("\n");
 printf("The quotient when a is divided by b is %.2f\n",quo);
printf("\n");
printf("The remainder when a is divided by b is %d\n",rem);

return 0;
12-08-24, 3:34 p.m. 238T1A05E1


#include <stdio.h>

int main()
{
 int a,b; 

float sum,diff,prod,quo;
int rem;

printf("Enter the value of 'a': ");
scanf("%d",&a);
printf("Enter the value of 'b': ");
scanf("%d",&b);

sum= a + b;
diff= a-b;
prod= a * b;
quo= (float) a/b;
rem= a % b;
 printf("\n");
printf("The sum of a & b is %.2f\n",sum);
 printf("\n");
 printf("The difference of a & b is %.2f\n",diff);
 printf("\n");
printf("The product of a & b is %.2f\n",prod);
printf("\n");
 printf("The quotient when a is divided by b is %.2f\n",quo);
printf("\n");
printf("The remainder when a is divided by b is %d\n",rem);

return 0;
}
16-08-24, 4:56 p.m. 238T1A05F5


#include <stdio.h>

int main()
{
 int a,b; 

float sum,diff,prod,quo;
int rem;

printf("Enter the value of 'a': ");
scanf("%d",&a);
printf("Enter the value of 'b': ");
scanf("%d",&b);

sum= a + b;
diff= a-b;
prod= a * b;
quo= (float) a/b;
rem= a % b;
 printf("\n");
printf("The sum of a & b is %.2f\n",sum);
 printf("\n");
 printf("The difference of a & b is %.2f\n",diff);
 printf("\n");
printf("The product of a & b is %.2f\n",prod);
printf("\n");
 printf("The quotient when a is divided by b is %.2f\n",quo);
printf("\n");
printf("The remainder when a is divided by b is %d\n",rem);

return 0;
}
19-08-24, 3:12 p.m. nasreensk2005@gmail.com


Log-in to answer to this question.