By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,805 Members | 1,270 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,805 IT Pros & Developers. It's quick & easy.

Very simple question

P: n/a
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?
Nov 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Paminu said:
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);


printf("The sum is %d\n", a + b);

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Nov 21 '05 #2

P: n/a
Paminu <ja******@asd.com> wrote:

In addition to Mr. Heathfield's comments...
int main()
int main( void ) /* better */
{
sum(1,2); return 0; /* return is required in C89 */ }


--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 21 '05 #3

P: n/a
Paminu wrote
(in article <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);


$ man 3 printf

<or look it up in whatever documentation is appropriate for your
compiler and library configuration>

--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw

Nov 21 '05 #4

P: n/a

"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
Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.