This question already has answers here:
How to use a function containing scanf() in the main function in C
(3 answers)
Closed 3 years ago.
The function is not mapping the user input to the variable
#include <stdio.h>
void input(int x);
int main (void)
{
int h = 0;
input(h);
printf("%d\n", h);
}
void input(int x)
{
printf("Enter a number: ");
scanf("%d", &x);
}
When entering something like 5, I expect 5 to be printed
x was passing by value, scanf changed the local copy of x. You need to return user input instead:
int input();
int main (void)
{
int h;
h = input();
printf("%d\n", h);
}
int input()
{
int x;
printf("Enter a number: ");
scanf("%d", &x);
return x;
}
This is because parameters in C are passed by copy. So the x that you modify is just a copy of what you passed, and at the end of input, that copy is discarded. Instead, you could pass a pointer. Since scanf already operates on a pointer, the fix is simple:
void input(int *x); // parameter is now a pointer to an int
int main(void)
{
int h = 0;
input(&h); // pass the address instead of the value
printf("%d\n", h);
}
void input(int *x)
{
printf("Enter a number: ");
scanf("%d", x); // no need to take the address again
}
Any decent book, tutorial or class should have mentioned that arguments in C are passed by value. That means the value is copied into the local argument variable, and any modifications to that local variable won't reflect on the original variable.
There are two possible solutions:
The simple one: Return the value from the function
int input(void)
{
int x;
printf("Enter a number: ");
scanf("%d", &x);
return x;
}
...
int h = input();
printf("%d\n", h);
The complicated solution: Emulate pass by reference
void input(int *x)
{
printf("Enter a number: ");
scanf("%d", x);
}
...
int h;
input(&h); // Pass a pointer to the variable
printf("%d\n", h);
You call input(h); and then void input(int x) { ... }. The value of h is copied into x
If you want to get back the changed value, you can either :
return a value from the function input() :
// notice the int before function name
int input();
int main (void)
{
int h = 0;
// notice h = ...
h = input(h);
printf("%d\n", h);
}
// notice the int before function name
int input()
{
int x;
printf("Enter a number: ");
scanf("%d", &x);
// notice the return
return x;
}
Or, pass the variable as reference (pointer) :
// notice int *
void input(int *x);
int main (void)
{
int h = 0;
// notice the &h this is used to pass addresses of variables
input(&h);
printf("%d\n", h);
}
// notice int *
void input(int *x)
{
printf("Enter a number: ");
// notice the & removed
scanf("%d", x);
}
The problem is that you didn't understand the basic workflow of functions. Maybe you should once again revise your basics and theory.
This is how your code should have been written (I have tried my best to make minimal changes to your code):
#include <stdio.h>
int input();
int main (void)
{
int y;
y = input();
printf("%d\n", y);
}
int input()
{
printf("Enter a number: ");
scanf("%d", &x);
return x;
}
You should have set the return type of function input() as int. Then, a variable (y in this case) could get the value returned by input() and use it in the main() function to display the actual input that the user had given.
Related
the following is given
#include <stdio.h>
void func2 (int num, int *result);
int main()
{
int num, result;
printf("Enter a number: \n");
scanf("%d", &num);
func2(num, &result);
printf("func2(): %d\n", result);
return 0;
}
void func2(int num, int *result)
{
//key in code
}
in the void func2 i wrote
int i=0;
result=&i;
while (num!=0)
{
i+=((num%10)*(num%10));
num=num/10;
}
but the programming is not returning the value of variable i properly. what's wrong with my variable assignment?
expected output:
Enter a number:
24 (user enter)
func2(): 20
actual output:
Enter a number:
24 (user enter)
func2(): 32767
You need to assign indirectly through result, not set result to the address of another variable.
int i=0;
while (num!=0)
{
i+=((num%10)*(num%10));
num=num/10;
}
*result = i;
Hello there I am learning pointers so I created a calculator.
I managed to return the value and the pointers from a function but by declaring them globally. How can I declare them locally?
#include <stdio.h>
#include <stdlib.h>
Declaration of all functions
int Addition();
int Subtraction();
int Devision();
int Multiplication();
Declaration of Global Variables, which I would like to declare them locally
int p;
int n;
int *r=&n;
int *b=&p;
Start of main Function
int main()
{
int g,s;
while (1)
{
printf("Please choose the Arithmetic operation: \n");
printf("Addition-> 1 \nSubtraction-> 2 \nDevision-> 3 \nMultiplication-> 4 \nExit-> 0\n ");
scanf("%d",&g);
The user chooses an Arithmetic operation (function) by inputting one number(1,2,3,4 or 0 for Exit)
if (g==1)
{
s=Addition(r,b);
printf("The addition result is %d+%d=%d",*r, *b, s);
}
else if (g==2)
{
s=Subtraction(r,b);
printf("The Subtraction result is %d-%d=%d",*r, *b, s);
}
else if (g==3)
{
s=Devision(r,b);
printf("The Devision result is %d/%d=%d",*r, *b, s);
}
else if (g==4)
{
Multiplication(r,b);
printf("The Multiplication result is %d/%d=%d",*r, *b, s);
}
else
{
break;
}
return 0;
}
}
End of main function.
Below are all the other functions
Addition()
{
int x;
printf("Ennter first nr: ");
scanf("%d",&n);
printf("Ennter second nr: ");
scanf("%d",&p);
x=n+p;
return x;
}
Subtraction()
{
int x;
printf("Ennter first nr: ");
scanf("%d",&n);
printf("Ennter second nr: ");
scanf("%d",&p);
x=n-p;
return x;
}
Devision()
{
int x;
printf("Ennter first nr: ");
scanf("%d",&n);
printf("Ennter second nr: ");
scanf("%d",&p);
x=n / p;
return x;
}
Multiplication()
{
int x;
printf("Ennter first nr: ");
scanf("%d",&n);
printf("Ennter second nr: ");
scanf("%d",&p);
x=n * p;
return x;
}
1) Refresh the knowledge about pointers and pass parameters via pointers if you want to.
The indirection or dereference operator * gives the contents of an object pointed to by a pointer.
The unary or monadic operator & gives the address of a variable.
2) Avoid global variables declare them locally after main.
3) Use switch instead of if-else chains
#include <stdio.h>
#include <stdlib.h>
int Addition(int* n, int* p)
{
int x;
printf("Ennter first nr: ");
scanf("%d",n);
printf("Ennter second nr: ");
scanf("%d",p);
x = *n + *p;
return x;
}
int Subtraction(int* n, int* p)
{
int x;
printf("Ennter first nr: ");
scanf("%d",n);
printf("Ennter second nr: ");
scanf("%d",p);
x = *n - *p;
return x;
}
int main(void)
{
int n1 = 0;
int n2 = 0;
int *r = &n1;
int *b = &n2;
int g,s;
while (1)
{
printf("Please choose the Arithmetic operation: \n");
printf("Addition-> 1 \nSubtraction-> 2 \nDevision-> 3 \nMultiplication-> 4 \nExit-> 0\n ");
scanf("%d",&g);
switch(g)
{
case 0:
printf("END\n");
return 0;
break;
case 1:
s = Addition(r, b);
printf("The addition result is: %d+%d=%d\n\n",n1, n2, s);
break;
case 2:
s = Subtraction(r ,b);
printf("The Subtraction result is: %d-%d=%d\n\n",*r, *b, s);
break;
}
}
return 0;
}
Test:
Please choose the Arithmetic operation:
Addition-> 1
Subtraction-> 2
Devision-> 3
Multiplication-> 4
Exit-> 0
2
Ennter first nr: 5
Ennter second nr: 3
The Subtraction result is: 5-3=2
Please choose the Arithmetic operation:
Addition-> 1
Subtraction-> 2
Devision-> 3
Multiplication-> 4
Exit-> 0
0
END
I used them Globally because it was easier for me to call and read them. That is why I want to learn how can I declare them locally.
Very simply, just declare them locally, as shown: (without your user input code to focus on your primary question)
int main(void)
{ // locally declared
double a = 4.5;
double b = 10.8;
double result_1, result_2, result_3, result_4;
result_1 = add(a, b);
result_2 = sub(a, b);
result_3 = mul(a, b);
result_4 = div(a, b);
return 0;
}
// example function (the others will be of similar form,
// a single return line with the appropriate math operator.
double add(double a, double b)//no pointers necessary
{
return a + b;
}
If you really want to use a pointer, then you can implement the function to return the result via an argument:
void add(double a, double b, double *result)
{
*result = a + b;
}
Usage: (call from main for example)
add(a, b, &result_1);// passing the address of result_1
// to allow the value at that address
// to be modified.
Well, not sure if this is what you are asking for, but let me suggest you this code:
#include <stdio.h>
#include <stdlib.h>
int Addition(int* n, int* p)
{
int x;
printf("Ennter first nr: ");
scanf("%d",n);
printf("Ennter second nr: ");
scanf("%d",p);
x=*n+*p;
return x;
}
int main()
{
int n1=0;
int n2=0;
int *r=&n1;
int *b=&n2;
int g,s;
while (1)
{
printf("Please choose the Arithmetic operation: \n");
printf("Addition-> 1 \nSubtraction-> 2 \nDevision-> 3 \nMultiplication-> 4 \nExit-> 0\n ");
scanf("%d",&g);
if (g==1)
{
s=Addition(r,b);
printf("The addition result is %d+%d=%d",*r, *b, s);
}
return 0;
}
}
As you can see, we are using pointers to store the value of the number written by the user, so you can keep them after calling the function.
This is my assignment I am supposed to do however whenever I run my code I get the error stated in my question.
Create a function,
return type: char
parameters: int *, int *
Inside the function, ask for two integers. Set the user responses to the int * parameters.
Prompt the user for a character.
Get their response as a single character and return that from the function.
In main:
Define two variables and call your function.
Print the result of your function and the values of your variables.
Here's my code:
#include<stdio.h>
#include<stdlib.h>
char load(int *x, int *y);
void main()
{
int integer1 = 0;
int integer2 = 0;
int *pointer1;
int *pointer2;
pointer1 = &integer1;
pointer2 = &integer2;
char returnValue;
returnValue = load(*pointer1, *pointer2);
printf("%c", returnValue);
system("pause");
}
char load(int *x, int *y)
{
char input;
printf("Please enter two integers: ");
scanf_s("%d %d", x, y);
printf("Please enter a character:");
scanf_s(" %c", &input);
return input;
}
The code need to send to the function an int must to send just int and show the changes after the function. so I tried this:
int main(void)
{
int number = 0;
printf("Please enter some number: \n");
scanf("%d", &number);
printf("the number that you entered: %d\n", number);
inc(number);
printf("After the 'inc' function, your number is: %d\n", number);
system("PAUSE");
return 0;
}
/*
*/
void inc(int x)
{
int* px = &x;
*px= *px+1;
}
it's prints just the same number and doesn't change at all. help?
you must pass the parameter as a pointer
void inc(int *px)
{
*px= *px+1;
}
in your code you are just modifying the functions local copy of x
A hacktacularly terrible hackaround:
inc((int)&number);
// ...
void inc(int n)
{
++*(int*)n;
}
Don't do this. It won't work on all but certain archaic architectures.
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);
}