473,763 Members | 6,666 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem with functions

Hi all, forgive me if there is a simple solution for this. I am going
through the following piece of code which simply calculates factorials
out of a book, but when i run it I get the answer 0 for whatever number
I input. Can anyone help?

Thanks in advance
strictly_mk

#include "stdio.h"

unsigned long long int return_factoria l(unsigned int num); //function
prototype

int main (void) {

char input[3]; //keyboard input
int number; // number to work with

//prompt user

printf("Enter a positive integer for which the factorial will be
calculated: ");

fgets (input, sizeof(input), stdin); // read the input

sscanf (input, "%d", &number);

//check the input as conditional

if (number > 0) {

printf ("The factorial of %d is %lu.\n", number,
return_factoria l(number));
} else {
printf ("You must enter a positive integer!\n");
}

getchar(); //pause and wait for user

return 0;
}

//This function takes a number and returns its factorial

unsigned long long int return_factoria l(unsigned int num) {

unsigned long long int sum = 1;

unsigned int i; //multiplier to be used in calculating factorial

//Loop through every multiplier up to and including sum

for (sum = 1, i = 1; i <= num; ++i) {

sum *= i;
}

return sum;

} // End of return_factoria l function

May 24 '06 #1
35 1926
In article <11************ **********@g10g 2000cwb.googleg roups.com>,
<st*********@ho tmail.com> wrote:
Hi all, forgive me if there is a simple solution for this. I am going
through the following piece of code which simply calculates factorials
out of a book, but when i run it I get the answer 0 for whatever number
I input. unsigned long long int return_factoria l(unsigned int num); printf ("The factorial of %d is %lu.\n", number,
return_factori al(number));


return_factoria l returns an unsigned long long, which you then
attempt to print using a format specifier suitable for an
unsigned long. Try using a format specifier of %llu
--
Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson
May 24 '06 #2
Thanks, it worked. Thats all it was. Sorry for being a dumbass.

Strictly_mk

May 24 '06 #3
st*********@hot mail.com a écrit :
Hi all, forgive me if there is a simple solution for this. I am going
through the following piece of code which simply calculates factorials
out of a book, but when i run it I get the answer 0 for whatever number
I input. Can anyone help?

Thanks in advance
strictly_mk

#include "stdio.h"

unsigned long long int return_factoria l(unsigned int num); //function
prototype

int main (void) {

char input[3]; //keyboard input
int number; // number to work with

//prompt user

printf("Enter a positive integer for which the factorial will be
calculated: ");

fgets (input, sizeof(input), stdin); // read the input

sscanf (input, "%d", &number);

//check the input as conditional

if (number > 0) {

printf ("The factorial of %d is %lu.\n", number,
return_factoria l(number));
} else {
printf ("You must enter a positive integer!\n");
}

getchar(); //pause and wait for user

return 0;
}

//This function takes a number and returns its factorial

unsigned long long int return_factoria l(unsigned int num) {

unsigned long long int sum = 1;

unsigned int i; //multiplier to be used in calculating factorial

//Loop through every multiplier up to and including sum

for (sum = 1, i = 1; i <= num; ++i) {

sum *= i;
}

return sum;

} // End of return_factoria l function


If I compile your code with lcc-win32 it will say:

Warning tprintfw.c: 23 printf argument mismatch for format u. Expected
int got unsigned long long

use %llu
May 24 '06 #4


jacob navia wrote On 05/24/06 14:03,:
st*********@hot mail.com a écrit :

unsigned long long int return_factoria l(unsigned int num); //function
prototype

int main (void) {
[...]

printf ("The factorial of %d is %lu.\n", number,
[...]


If I compile your code with lcc-win32 it will say:

Warning tprintfw.c: 23 printf argument mismatch for format u. Expected
int got unsigned long long


That diagnostic seems very confusing to me. Yes, there
is a mismatch between the format and the argument -- but the
message describes a mismatch that isn't there ...

--
Er*********@sun .com

May 24 '06 #5
jacob navia wrote:
st*********@hot mail.com a écrit :
<snip>
unsigned long long int return_factoria l(unsigned int num);
//function
prototype

int main (void) {

char input[3]; //keyboard input
int number; // number to work with
<snip>
printf ("The factorial of %d is %lu.\n", number,
return_factoria l(number));


<snip>
If I compile your code with lcc-win32 it will say:

Warning tprintfw.c: 23 printf argument mismatch for format u. Expected
int got unsigned long long

use %llu


Then you have a rather misleading warning. I see no u format and even if
I did it would expect an unsigned int, not a plain int. I do see an lu
format, but that expects an unsigned long not an int.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
May 24 '06 #6
Eric Sosman a écrit :

jacob navia wrote On 05/24/06 14:03,:
st*********@h otmail.com a écrit :
unsigned long long int return_factoria l(unsigned int num); //function
prototype

int main (void) {
[...]

printf ("The factorial of %d is %lu.\n", number,
[...]


If I compile your code with lcc-win32 it will say:

Warning tprintfw.c: 23 printf argument mismatch for format u. Expected
int got unsigned long long

That diagnostic seems very confusing to me. Yes, there
is a mismatch between the format and the argument -- but the
message describes a mismatch that isn't there ...


I ignore the difference of long/int. If I would not do that, I would
warn when
printf("%lu",3) ;

Obviously in principle you are right, but in practice I try to emit less
warnings so that when there is a warning it doesn't get drowned
in the noise.

Basically when sizeof(effectiv e type) != sizeof(expected type)
there is a warning

May 24 '06 #7
I ignore the difference of long/int. If I would not do that, I would
warn when
printf("%lu",3) ;

Obviously in principle you are right, but in practice I try to emit less
warnings so that when there is a warning it doesn't get drowned
in the noise.

Basically when sizeof(effectiv e type) != sizeof(expected type)
there is a warning

May 24 '06 #8
jacob navia <ja***@jacob.re mcomp.fr> writes:
I ignore the difference of long/int. If I would not do that, I would
warn when
printf("%lu",3) ;
Which you should.
Obviously in principle you are right, but in practice I try to emit less
warnings so that when there is a warning it doesn't get drowned
in the noise.

Basically when sizeof(effectiv e type) != sizeof(expected type)
there is a warning


Thus you encourage non-portable code.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
May 24 '06 #9
jacob navia wrote:
Eric Sosman a écrit :

jacob navia wrote On 05/24/06 14:03,:
st*********@hot mail.com a écrit :

unsigned long long int return_factoria l(unsigned int num);
//function
prototype

int main (void) {
[...]

printf ("The factorial of %d is %lu.\n", number,
[...]

If I compile your code with lcc-win32 it will say:

Warning tprintfw.c: 23 printf argument mismatch for format u.
Expected int got unsigned long long

That diagnostic seems very confusing to me. Yes, there
is a mismatch between the format and the argument -- but the
message describes a mismatch that isn't there ...


I ignore the difference of long/int. If I would not do that, I would
warn when
printf("%lu",3) ;

Obviously in principle you are right, but in practice I try to emit less
warnings so that when there is a warning it doesn't get drowned
in the noise.

Basically when sizeof(effectiv e type) != sizeof(expected type)
there is a warning

If it is not a standard constraint, I guess there is no one saying you
should do any different, but you might consider levels of warning
controlled by switches.
May 24 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
1086
by: roger | last post by:
I'm having difficulties invoking a user defined table function, when passing to it a parameter that is the result of another user defined function. My functions are defined like so: drop function dbo.scalar_func go create function dbo.scalar_func() returns int
4
1609
by: Merlin | last post by:
Hi Imagine the following classes (A class diagram will help) BASE, A, B, C, D, E, F, G. A, B, C, D, G inherit from BASE. E, F inherit from D.
57
4302
by: Xarky | last post by:
Hi, I am writing a linked list in the following way. struct list { struct list *next; char *mybuff; };
1
1336
by: ashutosh | last post by:
hi, i am making the dll for clamav antivirus libraray so that i can make activex control for windows. i complied the library successfully and make the Libclamav.dll file using win32 Api project in debug mode. this dll file exports all the 21 function which is needed for clamscan.
2
2533
by: Fernando Barsoba | last post by:
Dear all, I have been posting about a problem trying to encrypt certain data using HMAC-SHA1 functions. I posted that my problem was solved, but unfortunately, I was being overly optimistic. I am really desperate now, because I havent' been able to locate the origin of the problem for a couple of days now.. PROBLEM: the message digest obtained differs each time I execute the code, but works perfectly when applying the "control", that...
14
2942
by: Christian Kaiser | last post by:
We have a component that has no window. Well, no window in managed code - it uses a DLL which itself uses a window, and this is our problem! When the garbage collector runs and removes our component (created dynamically by, say, a button click, and then not referenced any more), the GC runs in a different thread, which prohibits the DLL to destroy its window, resulting in a GPF when the WndProc of that window is called - the code is gone...
6
1641
by: cppnow | last post by:
Hello. I have a strange conceptual problem I'm trying to think about. I would like to build a library that allows the user to do the following: 1) User defined types: The user defines their own types as needed for their particular application and registers them with the library. class ClassA {};
4
2460
by: r_ahimsa_m | last post by:
Hello, I am learning WWW technologies in Linux. I created index.html file which I can browse with Firefox/Konqueror using URL localhost/~robert/rozgloszenia/index.html. The page looks fine but there's one strange problem: I have written some JavaScript code in functions.js file which seems to be ignored when called although I have JavaScript enabled in Firefox/Konqueror. For example in functions.js I have:
6
4552
by: pauldepstein | last post by:
Let double NR( double x, double(*)(const double&) f ) be the signature of a Newton-Raphson function NR. Here, f is a function which returns a double and accepts a const double&. The aim of the game is to find a zero of this function f (the point at which f crosses the x-axis). This zero-of-f which solves our problem is the double which NR returns. It remains to explain what the "double x" represents. This is the...
0
10002
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9938
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9823
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8822
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7368
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6643
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5270
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3528
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2794
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.