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;
}
Related
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).
This question already has answers here:
Scanning with %c or %s
(2 answers)
Why does C's printf format string have both %c and %s?
(11 answers)
Closed 4 years ago.
Writing a simple code that's suppose to scan an integer and a character and then write them out.
my input is 1a and the output should be 1a but i'm getting 0 on the integer spot. have a pretty basic understanding of c so may have missed something that's pretty obvious thanks in advance.
#include <stdio.h>
int main()
{
int a;
char b;
scanf("%d", &a);
scanf(" %s", &b);
printf("%d", a);
printf("%s", &b);
}
b is a character so replace %s with %c, moreover
scanf() takes & before the variable as it want to store the variable refering to that address.
2.printf() Just outputs the value to the console present in that varaible.
Thereby no need to use & inside it
CORRECTED CODE:
#include <stdio.h>
int main()
{
int a;
char b;
scanf("%d", &a);
scanf(" %c", &b);
printf("%d", a);
printf("%c", b);
}
b is a char, %s is for string input so adds trailing 0 after b and you can get a crash. Use %c to input char.
You basically want this:
#include <stdio.h>
int main()
{
int a;
char b[100]; // array of 100 chars
scanf("%d", &a);
scanf("%s", b);
printf("%d", a);
printf("%s", b);
}
To fully understand this, you need to read the chapters dealing with scanf and the one dealing with strings in your C text book.
you can try it:
#include <stdio.h>
int main()
{
int a;
char b;
scanf("%d", &a);
scanf(" %c", &b);
printf("%d", a);
printf("%c", b);
}
This question already has answers here:
Why does scanf() need & operator (address-of) in some cases, and not others? [duplicate]
(5 answers)
Ampersand(&) and scanf in C?
(5 answers)
errors and warning message when using pointer
(3 answers)
Closed 5 years ago.
It's an extremely simple code, when I run it, it lets me enter the int, but when I enter it, the program crashes. I tried putting a printf right after the scanf to check if the program reads the int correctly, it seems it doesn't. I can't output the integer that I input.
#include <stdio.h>
#include <math.h>
void coolfunction(int x, int *y)
{
*y = exp(x);
}
int main()
{
int n;
double result = 0;
printf("Enter an integer: ");
scanf("%d", n);
coolfunction(n, &result);
printf("\n e^%d = %lf \n", n, result);
return 0;
}
Here's a version using a double parameter:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void coolfunction(int x, double *y)
{
*y = exp(x);
}
int main()
{
int n = 0, scanned = 0;
double result = 0;
printf("Enter an integer: ");
scanned = scanf("%d", &n);
if (scanned < 1) {
fprintf(stderr, "value entered not a valid integer\n");
exit(EXIT_FAILURE);
}
coolfunction(n, &result);
printf("\n e^%d = %g \n", n, result);
return 0;
}
You also missed the & in your scanf call: the function needs to know the address of your variable, not its value.
When I use:
delay(XXX);
and/or
modulo(XXX);
I get an error saying implicit declaration of function 'delay' or 'modulo' is invalid in C99.
I read that I need to declare it before I call it, so I tried many variations of:
void delay(int);
and
void modulo(int);
at the top of the program. It then doesn't build or still has the error..
This program is very very simple and was wondering if someone could tell me or teach me how to get it to work. Thanks.
#include <stdio.h>
#include <math.h>
int main() {
float a, b;
float s, d, p, q;
printf("Please enter a number: ");
scanf("%f", &a);
printf("\nPlease enter another number: ");
scanf("%f", &b);
/* Calculate Sum, Difference, Product and Quotient */
s = (a + b);
d = modulo(a - b); // error occurs here
p = (a * b);
q = (a / b);
printf("\nSum: %.2f\nDifference: %.2f\nProduct: %.2f\nQuotient: %.2f", s, d, p, q);
return(0);
}
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
Hi I have written a c program that reads in 2 values then swaps them and prints the new values except the second value keeps showing 0. For example it you enter 10 for 'a' and 8 tor 'b', then a will be 8 but b will be 0. Does anyone know the solution to fix this? Here is the code:
#include <stdio.h>
int getData()
{
int a, b;
printf("Enter first number: ");
scanf("%d", &a);
printf("Enter second number: ");
scanf("%d", &b);
return(a, b);
}
void swapValues(int a, int b)
{
printf("The value of a is: %d", b);
printf("\nThe value of b is: %d", a);
return;
}
int main()
{
int a, b = getData();
swapValues(a, b);
return(0);
}
return (a, b);
doesn't do what you think it does, it's a misapplication of the comma operator.
The expression op1, op2 evaluates both op1 and op2 but gives you the value of op2. So it's not passing back a couple of values (although some languages like Python can do this sort of thing).
Similarly,
int a, b = getData();
won't grab the mythical two values returned from getData(). Rather it will set a to an indeterminate value and set b based on the single value returned from the function.
I would be looking at something like this:
#include <stdio.h>
int getData (char *which) {
int val;
printf ("Enter value for %s: ", which);
scanf("%d", &val);
return val;
}
void swapValues (int a, int b) {
printf("The swapped value of a is: %d\n", b);
printf("The swapped value of b is: %d\n", a);
}
int main (void) {
int a = getData ("a");
int b = getData ("b");
swapValues(a, b);
return 0;
}
You should also keep in mind that, if you actually want to swap the variables a and b and have that reflected back to main(rather than just print them as if they've been swapped), you'll need to pass pointers to them and manipulate them via the pointers.
C is a pass-by-value language meaning that changes to function parameters aren't normally reflected back to the caller. That would go something like this:
void swapValues (int *pa, int *pb) {
int tmp = *pa;
*pa = *pb;
*pb = tmp;
}
:
swapValues (&a, &b);
// a and b are now swapped.
You have unnecessarily complicated the whole thing.For one, something like return(a,b) is absurd in C.Further, if you intend to swap, why are you passing b as argument for the printf() meant to print 'a' and passing a to the printf() of 'b'?Anyways,here's a modified code that keeps it simple and gets the job done.
#include <stdio.h>
void swapValues()
{
int a, b,tem;
printf("Enter first number: ");
scanf("%d", &a);
printf("\nEnter second number: ");
scanf("%d", &b);
tem=a;
a=b;
b=tem;
printf("\nThe value of a is: %d", a);
printf("\nThe value of b is: %d", b);
}
int main()
{
swapValues();
return(0);
}
First of all you can't return more than one value in C. The way around that is to return a struct or pass the values address.
void getData(int *a,int* b)
{
//int a, b;
printf("Enter first number: ");
scanf("%d", a); // look here you passed the address of a to scanf
// by doing that scanf can write to a
printf("Enter second number: ");
scanf("%d", b);
//return(a, b);
}
The old main:
int main()
{
int a, b = getData(); // b gets the return value from getData()
// but a is still uninitialized
//to call the new function you have to do the following
int a,b;
getData(&a,&b);
swapValues(a, b);
return(0);
}
You cannot return multiple values from a C function. I'm not even sure why the statement return(a, b) compiles.
If you want to return more than value from a function you should either put them into an array or a structure. I'm going to use a structure to demonstrate one way to do this correctly. There are many ways to do this, but this one modifies you code the least.
struct TwoNums{
int a;
int b;
};
TwoNums getData()
{
/* This creates a new struct of type struct TwoNums */
struct TwoNums nums;
printf("Enter first number: ");
scanf("%d", &(nums.a));
printf("Enter second number: ");
scanf("%d", &(nums.b));
return(a, b);
}
void swapValues(int a, int b)
{
printf("The value of a is: %d", b);
printf("\nThe value of b is: %d", a);
return;
}
int main()
{
/* Get the whole structure in one call */
struct TwoNums nums = getData();
/* Call the swap function using fields of the structure */
swapValues(nums.a, nums.b);
return 0;
}
The first:
getData() function is written incorrectly.
You can not return more than one parameter from the function in C. So you can to separate data reading, or use pointers as below:
void getData(int* a, int* b) {
printf("Enter first number: ");
scanf("%d", a);
printf("Enter second number: ");
scanf("%d", b);
}
In main()
int a, b;
getData(&a, &b);
The second:
swapValues(int a, int b) does not swap the data.
More correct:
void swapValues(int* a, int* b) {
int tmp = *a;
*a = *b;
*b = tmp;
}
without using temporary variable.
So try this code
#include <stdio.h>
int swape()
{
int a,b;
printf("Enter first number: ");
scanf("%d", &a);
printf("Enter second number: ");
scanf("%d", &b);
a=a+b;
b=a-b;
a=a-b;
printf("The value of a is: %d", a);
printf("\nThe value of b is: %d", b);
}
int main()
{
swape();
return(0);
}