473,480 Members | 2,269 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Very simple question

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
4 1380
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
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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1379
by: Gene Ellis | last post by:
Very simple question. If I have the XML file below: <?xml version="1.0" encoding="ISO-8859-1"?> <?xml-stylesheet type="text/xsl" href="newspage.xsl"?> <newspage> <content>Click here for blah...
2
1199
by: James | last post by:
I have a very simple datagrid, with an edit button that I added in PropertyBuilder. This fires the EditCommand event just fine and I show a panel where the user can edit certain fields. I have a...
16
1636
by: lovecreatesbeauty | last post by:
`Writing C code is very simple', one guy related to my work said. I'm not sure whether he is an expert or not. What he said about C programming like this can't convince me. I think there should be...
50
5642
by: diffuser78 | last post by:
I have just started to learn python. Some said that its slow. Can somebody pin point the issue. Thans
2
4188
by: dave | last post by:
Hi, I have searched for the answer for this error message without success. I have seen the question many times though:) I create an ASP.NET project (VS 2005, C#), and use a very simple .mdf...
27
2806
by: Paulo da Silva | last post by:
Hi! I was told in this NG that string is obsolet. I should use str methods. So, how do I join a list of strings delimited by a given char, let's say ','? Old way:
1
1260
by: Simon | last post by:
A very simple question that is very hard to google..(so please be so kind to answer it, if you have the answer.) How can I reach an iframe, located in another frame. Situation: frameset of three...
1
1203
by: Raycaster | last post by:
I'm 3/4 finished 2 simple VB apps for a upcoming silent auction being held at my children's school. Very Simple - This is what it does: 1st app allows input and builds a text database Unit...
4
3594
by: maria | last post by:
I only use C++ with Visual Studio 6.0 for string manipulations in thousands of HTML pages on my website. Many times, the output files of many of my C++ programs contain a spanish question mark (¿)...
3
1835
by: stdlib99 | last post by:
Hi, I have a simple question regarding templates and meta programming. I am going to try and work my way through the C++ Template Metaprogramming, a book by David Abrahams and Aleksey...
0
7051
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7054
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7097
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
6993
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5353
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4794
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3003
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1307
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.