I am getting the following error while executing code on Visual Studio 2019
MSB6006"CL.exe" exited with code 2
#include<stdio.h>
#include<conio.h>
int main()
{
int a, b, c,x;
x = a / (b - c);
printf("\n Enter values of a,b and c");
scanf_s("%d%d%d", &a, &b, &c);
printf("\n The value of x is %d", x);
return 0;
}
Your order of statements is off.
First assign values to a, b, and c.
Only after use those values in calculations.
#include <stdio.h>
int main(void) {
int a, b, c, x;
// x = a / (b - c); // NOPE! a, b, and c have no valid values
printf("Enter values of a, b and c\n");
scanf("%d%d%d", &a, &b, &c);
x = a / (b - c); // calculation moved here; a, b, and c (hopefully) have valid values now
printf("The value of x is %d\n", x);
return 0;
}
Note: the return value of scanf() should be checked to be sure all of a, b, and c have valid values.
if (scanf("%d%d%d", &a, &b, &c) != 3) /* error */;
Note 2: I changed your code a little bit: removed non-standard <conio.h>, changed the placing of most '\n' to be more line-oriented, replaced the optional scanf_s (this function may not exist in all C11/C18 implementations).
Related
#include <stdio.h>
#pragma warning(disable:4996)
int math(int a, int b) {
if (a > b) {
printf("%d %d %d", a + b, a - b, a * b);
}
else if (b > a) {
printf("%d %d %d", b + a, b - a, b * a);
}
}
int main(void) {
int n1, n2;
scanf("%d %d", &n1, &n2);
printf(math(n1, n2));
return 0;
}
A program that takes two integers and produces and outputs a function that converts the results of addition, subtraction, and multiplication of two integers. However, a Segmentation Fault error occurs when the compilation is executed. I want to know the cause of the code.
An error appears even if you create and output addition, subtraction, and multiplication functions respectively.
As mentioned in the comments, the printf function is expecting a format string prior to the call to the "math" function. With that in mind here is a snippet of code with some tweaks to avoid the segmentation fault the original code runs into.
#include <stdio.h>
int math(int a, int b) {
int result = 0;
if (a > b) {
printf("%d %d %d\n", a + b, a - b, a * b);
result = (a + b) + (a - b) + (a * b);
}
else if (b > a) {
printf("%d %d %d\n", b + a, b - a, b * a);
result = (a + b) + (b - a) + (a * b);
}
return result; /* Since this function is defined to return an integer */}
int main(void) {
int n1, n2;
printf("Enter in two integers: "); /* Aids the user to know what they are supposed to enter */
scanf("%d %d", &n1, &n2);
printf("The result of calling the math function is %d\n", math(n1, n2)); /* Revised the usage of the printf function */
return 0;
}
A couple of things to point out.
Since the "math" function is defined to return an integer, an integer variable was added to the function to accept a calculated value and then be returned to the statement that called the "math" function.
The printf statement is corrected so that it contains a formatting string to define what and how data is to be printed out.
Following is a sample of a test run from the terminal.
#Dev:~/C_Programs/Console/Formulas/bin/Release$ ./Formulas
Enter in two integers: 33 45
78 12 1485
The result of calling the math function is 1575
Give those tweaks a try to see if it meets the spirit of your project.
#include<stdio.h>
int add(int, int); // function prototype
int main()
{
int a, b;
// printf("Enter 2 integer numbers\n");
// scanf("%d%d", &a, &b);
//function call add(a, b);
printf(" %d + %d = %d \n", a, b, add(2, 7));
Please focus on this line why it gives address + 0 = 9//
return 0;
}
//function definition
int add(int x, int y)
{
return x+y;
}
// produces outPut : 199164000 + 0 = 9
You commented the scanf function and also didn't given values for the a and b. So it printed garbage values. You also need to pass a and b into the add(a,b) function.
#include<stdio.h>
int add(int, int); // function prototype
int main()
{
int a, b;
// printf("Enter 2 integer numbers\n");
scanf("%d%d", &a, &b);
//function call add(a, b);
printf(" %d + %d = %d \n", a, b, add(a, b));
return 0;
}
//function definition
int add(int x, int y)
{
return x+y;
}
It is not printing any address. It is printing garbage values. You have not given any values to the variable a and b. So it will print garbage values. Why you commented scanf statement. Just stop commenting it and it will work.
This question already has answers here:
(Why) is using an uninitialized variable undefined behavior?
(7 answers)
Closed 2 years ago.
#include <stdio.h>
int main()
{
int a, b, c;
c = a * b;
printf("a는?: ");
scanf("%d", &a);
printf("b는?: ");
scanf("%d", &b);
printf("%d * %d = %d ", a, b, c);
return 0;
}
i made a calculation program . but the result is wrong.
if i put 5,10 in a,b
c should be 50(510)
but the result is 0
i used ab instead of c . then it was solved. but i want to use c variation.
what is the problem?
should i use double instead of int? please tell me the reason why the problem evoked
You're assigning c = a * b at a point when a and b doesn't have proper values. You should do this calculation after assigning values to a and b, otherwise result will be some garbage value.
Right way to do it:
#include <stdio.h>
int main()
{
int a, b, c;
printf("a는?: ");
scanf("%d", &a);
printf("b는?: ");
scanf("%d", &b);
c = a * b;
printf("%d * %d = %d ", a, b, c);
return 0;
}
I want to calculate sum of arithmetic progression in which we have to take 3 variables from user. a=first number, b= step size/increment, c=length of sequence.
If there are more than 1 test case , say three, then I have to scan a,b,c three time. How to do this?
E.g scanf (" %d %d %d", a,b,c); 3 times without affect initial values in first test case.
If you know no of test cases read it first and store it in a variable.
int calculate_ap(int a, int b, int c)
{
//Implement function to calculate Arithmetic progression and return the result
}
int main()
{
int test_cases = 0;
int a, b, c;
scanf("%d", &test_cases); //Reads no of test cases
while(test_cases--)
{
scanf("%d, %d, %d", &a, &b, &c); //read A, B, C
printf("%d\n", calculate_ap(a, b, c));
}
}
Hope this helps.
I've a problem with a function in my calculator-program. The function returns me back only 0 values. I want that:
Input:3+4
Output:7
I have worked with pointers to use the call by reference method. Please give me some tips.
The mistake is in the function readcalc. If I write the whole syntax in the main program it works.
Here is my code.
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
double readcalc(double*,char*,double*);
double addition(double,double);
double subtraction(double,double);
double multiplication(double,double);
double division(double,double);
int main()
{
double a=0, b=0;
char op='R', restart = 'Y';
while (restart != 'N')
{
readcalc(&a,&op,&b);
printf("%lf%lf", a, b);
printf("\n ---CALCULATOR--- \n\n\n");
switch (op)
{
case '+':printf("%lf + %lf = %lf\n", a, b, addition(a, b)); break;
case '-':printf("%lf - %lf = %lf\n", a, b, subtraction(a, b)); break;
case '*':printf("%lf * %lf = %lf\n", a, b, multiplication(a, b)); break;
case '/':printf("%lf / %lf = %lf\n", a, b, division(a, b)); break;
default:printf("bad operator!");
}
printf("New Calc? (Y,N) \n");
fflush(stdin);
scanf("%c", &restart);
if (restart != 'Y'&&restart != 'N')
{
printf("Bad input!");
}
}
fflush(stdin);
getchar();
return 0;
}
double readcalc(double* x,char* opp,double* y)
{
printf("\n Type your calculation!(z.B.4+7)\n");
scanf("%lf%c%lf", &x, &opp, &y);
return 0;
}
double addition(double a,double b)
{
double c = 0;
c = a + b;
return c;
}
double subtraction(double a, double b)
{
double c = 0;
c = a - b;
return c;
}
double multiplication(double a, double b)
{
double c = 0;
c = a*b;
return c;
}
double division(double a, double b)
{
double c = 0;
c = a / b;
return c;
}
What can I change?
The problem with readcalc is that you pass the pointer to the pointer as argument to scanf. The variables are already pointers, so you don't have to use the address-of operator to get a pointer, as then you get pointers to the pointers.
Also be careful with the scanf format, as the "%c" format doesn't skip leading whitespace, so if you enter e.g. 1 +2 then the scanf call will not be able to read the operator or the second number.
void readcalc(double* x,char* opp,double* y)
{
printf("\n Type your calculation!(z.B.4+7)\n");
scanf("%lf%c%lf", x, opp, y); // <== no &'s
}
Your readcalc function invokes
scanf("%lf%c%lf", &x, &opp, &y);
on pointers, int *x, etc...
When you place it inline in main, it works on the addresses of the variables.
Change it to
scanf("%lf%c%lf", x, opp, y);
At a suitable warning level, you may have seen warning.
While you are there, readcalc returns a double, which you never use. Perhaps it should simply be void?