Problem with simple function in C - c

I am new in C and I have problem with compiling this code.
#include <stdio.h>
void suma( int a, int b, int wynik)
{
wynik=0;
printf("a=\n");
scanf("%d",&a);
printf("b=\n");
scanf("%d",&b);
wynik=a+b;
printf("wynik = %d",&wynik);
}
int main()
{
suma(int a, int b, int wynik);
}
I don't know why but compiler tells me that 2 argument has type int * insted of int. I dont' know what does it mean and where I made mistake.

Change
printf("wynik = %d",&wynik);
to
printf("wynik = %d",wynik);
Otherwise you'll be printing the address of wynik as an integer.
Also the way you call suma makes no sense.

change printf("wynik = %d",&wynik); to printf("wynik = %d",wynik);
and also you don't seem to need the arguments of suma.
Try this:
void suma()
{
int a,b,wynik;
wynik=0;
printf("a=\n");
scanf("%d",&a);
printf("b=\n");
scanf("%d",&b);
wynik=a+b;
printf("wynik = %d",wynik);
}
int main()
{
suma();
}

Related

How to modify a local variable of one function from other function?

int main ()
{
int a, b;
call(&b);
printf("%d, %d",a , b);
}
void call(int *ptr)
{
}
Desired output:
50, 100
How to write the call function so as to modify both the variables to get the desired output??
Not sure where the values 50 and 100 are coming from or exactly what you are asking but maybe this will help with your question.
Since C is pass by value you need to send pointers to actually change the value inside another function.
Since the call function will have pointer values you need to dereference the pointers before changing the value.
Here is an example:
void call(int *a, int *b)
{
*a = 50;
*b = 100;
}
int main()
{
int a, b;
call(&a, &b);
printf("%d, %d\n", a, b);
}
While we are exploring the many ways this output could be achieved, consider that the function could store state in a static variable:
#include <stdio.h>
void call(int *ptr);
int main(void)
{
int a, b;
call(&a);
call(&b);
printf("%d, %d\n",a , b);
}
void call(int *ptr)
{
static int store = 0;
store += 50;
*ptr = store;
}
Program output:
50, 100
Note that you may also be able to do this as follows, without any modifications to main(). But be warned that this method invokes undefined behavior! It is undefined behavior to write to a location past the end of an array object, and in the case of a and b, these are considered to be array objects of size 1. Here we are assuming that this write will work, and that a and b are stored next to each other in memory. We further assume that a has the higher address in memory.
I would say that you should never do this, but I can see no other way to modify a from the function call() without knowing the address of a. You have been warned.
void call(int *ptr)
{
*ptr = 100;
*(ptr + 1) = 50;
}
Try something like this:
void call(int *ptr)
{
*ptr = 100;
}
int main ()
{
int a, b;
a = 50;
call(&b);
printf("%d, %d",a , b);
}
See demo
Maybe you want this:
int main ()
{
int a, b;
call(&a, &b);
printf("%d, %d",a , b);
}
void call(int *ptr1, int *ptr2)
{
*a = 50;
*b = 100;
}
To change a local variable in function a by calling function b you have two options.
1) Let function b return a value that you assign to the variable in function a. Like:
int b() {return 42;}
void a()
{
int x = b();
printf("%d\n", x);
}
This does, however, not seem to be what you are looking for.
2) Pass a pointer to the variable to function b and change the variable through that pointer
void b(int* p) // Notice the * which means the function takes a pointer
// to integer as argument
{
*p = 42; // Notice the * which means that 42 is assigned to the variable
// that p points to
}
void a()
{
int x;
b(&x); // Notice the & which means "address of x" and thereby
// becomes a pointer to the integer x
printf("%d\n", x);
}
int main()
{
int a,b;
call(&b);
printf("%d, %d\n", a,b);
}
int call(int *ptr)
{
int *m;
m = ptr++;
*ptr = 50;
*m = 100;
}

Initialization makes pointer from integer without a cast - C

Sorry if this post comes off as ignorant, but I'm still very new to C, so I don't have a great understanding of it. Right now I'm trying to figure out pointers.
I made this bit of code to test if I can change the value of b in the change function, and have that carry over back into the main function(without returning) by passing in the pointer.
However, I get an error that says.
Initialization makes pointer from integer without a cast
int *b = 6
From what I understand,
#include <stdio.h>
int change(int * b){
* b = 4;
return 0;
}
int main(){
int * b = 6;
change(b);
printf("%d", b);
return 0;
}
Ill I'm really worried about is fixing this error, but if my understanding of pointers is completely wrong, I wouldn't be opposed to criticism.
To make it work rewrite the code as follows -
#include <stdio.h>
int change(int * b){
* b = 4;
return 0;
}
int main(){
int b = 6; //variable type of b is 'int' not 'int *'
change(&b);//Instead of b the address of b is passed
printf("%d", b);
return 0;
}
The code above will work.
In C, when you wish to change the value of a variable in a function, you "pass the Variable into the function by Reference". You can read more about this here - Pass by Reference
Now the error means that you are trying to store an integer into a variable that is a pointer, without typecasting. You can make this error go away by changing that line as follows (But the program won't work because the logic will still be wrong )
int * b = (int *)6; //This is typecasting int into type (int *)
Maybe you wanted to do this:
#include <stdio.h>
int change( int *b )
{
*b = 4;
return 0;
}
int main( void )
{
int *b;
int myint = 6;
b = &myint;
change( &b );
printf( "%d", b );
return 0;
}
#include <stdio.h>
int change(int * b){
* b = 4;
return 0;
}
int main(){
int b = 6; // <- just int not a pointer to int
change(&b); // address of the int
printf("%d", b);
return 0;
}
Maybe too late, but as a complement to the rest of the answers, just my 2 cents:
void change(int *b, int c)
{
*b = c;
}
int main()
{
int a = 25;
change(&a, 20); --> with an added parameter
printf("%d", a);
return 0;
}
In pointer declarations, you should only assign the address of other variables e.g "&a"..

Function not returning value at all - not a void

I have this function that is not returning a function value. I've added some random testers to try and debug but no luck. Thanks!
#include <stdio.h>
#include <math.h>
#include <time.h>
#define N 100
float error(int a, int b);
int main(){
printf("START\n");
srand(time(NULL));
int a, b, j, m;
float plot[N+1];
printf("Lower bound for x: ");
scanf("%d", &a);
printf("Upper bound for x: ");
scanf("%d", &b);
printf("okay\n");
for(j = 0; j < N; j++)
plot[j] = 0;
printf("okay1\n");
m = error(a,b);
printf("%f\n",m);
return 0;
}
float error(int a, int b){
float product = a*b;
printf("%f\n",product);
return product;
}
so the m = error(a,b) always gives 0 no matter what!
Please help. I apologise for not cleaning this up...
This is because you declared m as int. Declare it as float and cast a*b to float because a and b are also ints. Another way is change the return type of your function to int;
int error(int a, int b){
int product = a*b;
printf("%f\n",product);
return product;
}
and print m and product with %d specifier. Also do not forgot to change you function prototype in latter case.
int m;
m = error(a,b);
printf("%f\n",m);
You are trying to display m as float while it is int.
This shoud be:
m = error(a,b);
printf("%d\n",m);
My advice: stick to ints for now.
I think we have misunderstanding of how types work in C. It's not like in PHP that a variable can hold an int, then a float and some time later some string. In C variables are like steel buckets. They can change content, but never their shape. If you assign a float to an int, the floating point value will be converted into a integer value. Once you declare m as int, it will remain int to the end of it's life, capable of holding only int values.

Using variables

I have a problem with this simple program because it doesnt give me the true outcome. I just want to sum two arguments in the first function and then use the outcome in the second one. It will be nice to have overall outcome in the main function. Also I would like to ask the same question with arrays.
#include <stdio.h>
#include <stdlib.h>
int sum()
{
int a=2;
int b=3;
int s=a+b;
printf("sum=%d\n",s);
return s;
}
int sum2(int s)
{
int c=5;
int d=c+s;
}
int main(int s,int d)
{
sum();
printf("sum=%d\n",s);
printf("sum2=%d\n",d);
getchar();
return 0;
}
There are many problems with this code:
int main(int s, int d) won't do what you think. Command-line arguments to your program come in string format. So you would need to use int main(int argc, char *argv[]).
The variables s and d in main() are completely independent to the variables in sum() and sum2(). So changing their values in those functions will not affect the original variables.
You're not even calling the second function!
You can do things like this:
int sum(int a, int b)
{
return a+b;
}
int sum2(int c)
{
return c+5;
}
int main(void)
{
int x = 2;
int y = 3;
int z = sum(x,y);
int w = sum2(z);
printf("z = %d\n", z);
printf("w = %d\n", w);
}
First of all, s is a local variable inside sum( ). Hence it cannot be available outside the function.
int sum() {
// ..
int s = a+b; // local variable, hence local scope
// ..
}
Also, secondly, int main(int s,int d) won't work since in command line arguments come in String format. So can't use a int there
I won't tell you the answer (lol but others have) but I'll give you these clues to figuring it out.
Ask your self which functions are returning data and which ones aren't.
Clue: the function needs a return to return some data.
Then ask yourself which functions' returns are actually being used.
Clue: to collect the data returned from a function you need to assign the result to a variable like so
int i;
i = somefunct();
You can't access the value of variable 's' outside the function sum() since it is out of scope. You'll have to return the value to your main() function. Also your main function parameters are incorrect. You need something more like this:
#include <stdio.h>
#include <stdlib.h>
int sum(int a, int b)
{
int s=a+b;
printf("sum=%d\n",s);
return s;
}
int sum2(int c, int sum)
{
return c+sum;
}
int main(int argc, char *argv[])
{
int val1 = sum(2, 3);
printf("sum=%d\n",val1);
int val2 = sum2(5, val1);
printf("sum2=%d\n", val2);
getchar();
return 0;
}

What does "implicit declaration of function" mean?

#include <stdio.h>
int main()
{
int a = 4;
int b = 3;
addNumbers(a, b);
}
int addNumbers(int a, int b)
{
return a + b;
}
Why does this not compile, I get a message saying implicit declaration of function addNumbers()?
Either define the function before main() or provide its prototype before main().
So either do this:
#include <stdio.h>
int addNumbers(int a, int b)
{ //definition
}
int main()
{ //Code in main
addNumbers(a, b);
}
or this:
#include <stdio.h>
int addNumbers(int, int);
int main()
{ //Code in main
addNumbers(a, b);
}
int addNumbers(int a, int b)
{ // definition
}
You need to declare the function before you call it in main(). Either move it before main or at least declare it there.
Also, you should prob add return 0 at the end of the main function since it's supposed to return int.
#include <stdio.h>
int addNumbers(int a, int b)
{
return a + b;
}
int main()
{
int a = 4;
int b = 3;
addNumbers(a, b);
return 0;
}
You have to either move the entire addNumber() function above main or provide a prototype. The latter is done the following way:
int addNumbers(int a, int b);
int main()
{
// code of main() here
}
int addNumbers(int a, int b)
{
//code of addNumbers() here
}
Put addNumbers before main
int addNumbers(int a, int b)
{
return a + b;
}
int main()
{
int a = 4;
int b = 3;
addNumbers(a, b);
}
UPDATE:
To print it, printf("%i",addNumbers(a, b)); will display 7 in above case.
You can move the whole function above the point where it is called, or use a function prototype, like this:
#include <stdio.h>
int addNumbers(int a, int b); // function prototype
int main()
{
int a = 4;
int b = 3;
addNumbers(a, b);
}
int addNumbers(int a, int b)
{
return a + b;
}
You'll need a forward declaration of the addNumbers function or its definition moved up before the first usage:
// 2161304
#include <stdio.h>
// forward declaration
int addNumbers(int a, int b);
int main()
{
int a = 4;
int b = 3;
addNumbers(a, b);
}
// alternatively move this up before main ...
int addNumbers(int a, int b)
{
return a + b;
}
Regarding main and return:
Programs will compile without. The signatures of main defined by the standard are:
int main(void)
int main(int argc, char **argv)
There are three portable return values:
0, EXIT_SUCCESS, EXIT_FAILURE
which are defined in stdlib.h.
Declare the function before using it by either adding a prototype before main():
int addNumbers(int a, int b);
or move the whole addNumbers function before main().
I agree with declaration and definition thing but i am not getting any compilation errors with the above code.My gcc version is "4.4.1-4ubuntu9".Any ideas.
I have done a little modification to test the code.
#include <stdio.h>
int main()
{
int a = 4;
int b = 3;
printf("%d", addNumbers(a, b)); // Printf added.
}
int addNumbers(int a, int b)
{
return a + b;
}
You must declare the function before using.
Remember these 4 basic parts while dealing with functions.
Declaration
Call
Definition
Return
if your compiler is C99 standard it throws and error "implicit declaration", since since default promotion is obsolete in C99 standard.
if you try to compile with C89 standard this would be allowable.
In C99 standard function prototype is necessary
Since the compiler executes one line after another,by the time it sees the function call,it has to have the information about that function which the main function is calling.so my friend u need to tell the compiler about the function before you can ever use it.

Resources