473,546 Members | 2,289 Online
Bytes | Software Development & Data Engineering Community
+ 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 1383
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.c om> 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)cybers pace.org | don't, I need to know. Flames welcome.
Nov 21 '05 #3
Paminu wrote
(in article <dl**********@n ews.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.c om> 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
1385
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 blah</content> </newspage> How can I use XLink to make an HTML hyperlink out of the "click here" text?
2
1209
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 button to save the changes. Within that button, I refer to dataGrid1.EditItemIndex and it's returning -1. Isn't this property set when I click on...
16
1646
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 two kinds of people can make such a comment on C programming. One is C expert with rich experiences of some years on real projects; The other is...
50
5666
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
4198
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 file (which I can provide if necessary). I use 'Add new Item' and pick 'DataSet'. I believe this creates a TypedDataSet, CORRECT? I take all the...
27
2814
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
1266
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 frames. Left frame (LFRAME) Right frame (RFRAME) Top frame (HFRAME) (and an iframe, called progressbar)
1
1210
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 ID (Done before Auction) Unit Description (Done before Auction) Unit Donor (Done before Auction) Unit Value (Done before Auction)
4
3603
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 (¿) as their first character. What creates it? How do we avoid it? Thanks! maria
3
1842
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 Gurtovoy. I’m not doing this because I want to be a Meta Programming guru (because a lot of that stuff looks too crazy for use in the real world). Rather...
0
7504
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7435
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7694
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7947
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7461
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7792
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6026
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3470
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1921
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 we have to send another system

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.