"Paminu" <ja******@asd.com> wrote in message
news:dl**********@news.net.uni-c.dk...
I am trying to refresh my C skills and have therefore writtin this little
program:
#include <stdio.h>
int sum (int a, int b)
{
printf(a+b);
return a+b;
}
int main()
{
sum(1,2);
}
But when I compile I get :
test.c: In function `sum':
test.c:5: warning: passing arg 1 of `printf' makes pointer from integer
without a cast
me@ubuntu:~/opgaver$ ./test
Segmentation fault
What am I missing?
You forgot to read the documentation for 'printf()' or
a textbook which explains how to use it.
printf("%d\n", a + b);
"Style" note: Your function is named 'sum()'. So that's what it
should do. Nothing else. Output doesn't belong in it. It limits
the function's usability. What if I want to call 'sum()' several
times as part of other computation, and only want output of the
final result?
#include <stdio.h>
int sum(int a, int b)
{
return a + b;
}
int main(void)
{
int i = 5;
int j = sum(i, 3); /* your way would give unwanted output here */
printf("%d\n", sum(j, 2));
return 0;
}
-Mike