473,770 Members | 4,029 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
35 1931
Keith Thompson a écrit :
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.


Well then, there would be so many such warnings that they would have
no longer any importance and the really important ones would get lost.

This is a matter of opinion of course. There is no requirement at all
for the compiler to emit a warning here, and the original poster's
compiler did not emit any, what led to the problem.
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.


I do not encourage "non portable code", I just do not want to bother my
users you see?
But maybe I should have a level of warning that would apply here. I have
warning levels but did not use it in this situation.
May 24 '06 #11
jacob navia <ja***@jacob.re mcomp.fr> writes:
Keith Thompson a écrit :
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.


Well then, there would be so many such warnings that they would have
no longer any importance and the really important ones would get lost.

This is a matter of opinion of course. There is no requirement at all
for the compiler to emit a warning here, and the original poster's
compiler did not emit any, what led to the problem.


No, a warning isn't required, but issuing a warning for some format
string mismatches but not for this one is misleading.

printf("%lu", 3) invokes undefined behavior. It may happen to work in
your implementation. It's still undefined behavior, and you'd be
doing your users a favor by pointing it out.

[...]
I do not encourage "non portable code", I just do not want to bother my
users you see?


You don't want to bother your users by telling them their code is
non-portable.

--
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 25 '06 #12
jacob navia wrote:
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


So, no warning for

printf ("%d = %s\n", "The Answer", 42);

.... on machines where sizeof(char*)== sizeof(int), right? Ugh.
One wonders whether half a seat belt is better or worse than
no seat belt at all.

gcc does a *much* better job of this; you may want to
study how it's done.

--
Eric Sosman
es*****@acm-dot-org.invalid
May 25 '06 #13
Eric Sosman a écrit :
jacob navia wrote:
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

So, no warning for

printf ("%d = %s\n", "The Answer", 42);

... on machines where sizeof(char*)== sizeof(int), right? Ugh.
One wonders whether half a seat belt is better or worse than
no seat belt at all.

gcc does a *much* better job of this; you may want to
study how it's done.


I was speaking about %d, %u. In this case we have:

D:\lcc\mc63\tes t>type twp.c
#include <stdio.h>
int main(void)
{
printf ("%d = %s\n", "The Answer", 42);
}

D:\lcc\mc63\tes t>lcc twp.c
Warning twp.c: 4 printf argument mismatch for format s. Expected char
pointer got int
0 errors, 1 warning

D:\lcc\mc63\tes t>

Note that printing a pointer with %d is still allowed. %s needs a char
pointer however, it is not symmetric.
May 25 '06 #14
jacob navia <ja***@jacob.re mcomp.fr> writes:
[...]
I was speaking about %d, %u. In this case we have:

D:\lcc\mc63\tes t>type twp.c
#include <stdio.h>
int main(void)
{
printf ("%d = %s\n", "The Answer", 42);
}

D:\lcc\mc63\tes t>lcc twp.c
Warning twp.c: 4 printf argument mismatch for format s. Expected char
pointer got int
0 errors, 1 warning

D:\lcc\mc63\tes t>

Note that printing a pointer with %d is still allowed. %s needs a char
pointer however, it is not symmetric.


So you don't issue a warning for an attempt to print a pointer with "%d"?

Printing a pointer with "%d" may happen to work on your particular
implementation, but it's wrong. That's what "%p" is for.

gcc gets this right. lcc-win32 should do so as well.

--
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 25 '06 #15
jacob navia said:
Keith Thompson a écrit :
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.


Well then, there would be so many such warnings that they would have
no longer any importance and the really important ones would get lost.


Not true. I use gcc, which produces a diagnostic message for a mismatch
between printf format specifier and argument type, and yet there are not
"many such warnings" when I use it to compile my code, and so the really
important messages do not in fact get lost, despite your claim to the
contrary.
This is a matter of opinion of course. There is no requirement at all
for the compiler to emit a warning here,


That is unfortunately true, yes. I consider it a quality of implementation
issue. Good compilers give good diagnostics.

--
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)
May 25 '06 #16
Richard Heathfield a écrit :

Not true. I use gcc, which produces a diagnostic message for a mismatch
between printf format specifier and argument type, and yet there are not
"many such warnings" when I use it to compile my code, and so the really
important messages do not in fact get lost, despite your claim to the
contrary.


Genius programmers like you should not use lcc-win32.
May 25 '06 #17
Keith Thompson a écrit :
jacob navia <ja***@jacob.re mcomp.fr> writes:
[...]
I was speaking about %d, %u. In this case we have:

D:\lcc\mc63\t est>type twp.c
#include <stdio.h>
int main(void)
{
printf ("%d = %s\n", "The Answer", 42);
}

D:\lcc\mc63\t est>lcc twp.c
Warning twp.c: 4 printf argument mismatch for format s. Expected char
pointer got int
0 errors, 1 warning

D:\lcc\mc63\t est>

Note that printing a pointer with %d is still allowed. %s needs a char
pointer however, it is not symmetric.

So you don't issue a warning for an attempt to print a pointer with "%d"?

Printing a pointer with "%d" may happen to work on your particular
implementation, but it's wrong. That's what "%p" is for.

gcc gets this right. lcc-win32 should do so as well.


OK OK you are right. I have added warnings for mismatch with long/int,
and with mismatches pointer/int. This will appear only if you increase
the warning level or if you ask for the "code check" option in the IDE

May 25 '06 #18
jacob navia said:
Richard Heathfield a écrit :

Not true. I use gcc, which produces a diagnostic message for a mismatch
between printf format specifier and argument type, and yet there are not
"many such warnings" when I use it to compile my code, and so the really
important messages do not in fact get lost, despite your claim to the
contrary.


Genius programmers like you should not use lcc-win32.


It's kind of you to think of me as a genius programmer, but I'm afraid I
can't agree with you. I'm merely a careful programmer.

But you seem to be claiming that lcc-win32 is not for bright people. There,
I find it impossible to disagree.

--
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)
May 25 '06 #19
jacob navia wrote:
Richard Heathfield a écrit :

Not true. I use gcc, which produces a diagnostic message for a
mismatch between printf format specifier and argument type, and yet
there are not "many such warnings" when I use it to compile my code,
and so the really important messages do not in fact get lost, despite
your claim to the contrary.


Genius programmers like you should not use lcc-win32.


The better the programmer the less important the quality of the warning
since they are less likely to make the mistake and more likely to be ale
to work out why there was a diagnostic. So it is actually
inexperienced/poor programmers who should avoid lcc-win32.

As has been stated, the warning is a QOI issue, it is just that
lcc-win32 is showing a low quality of implementation here.
--
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

Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
May 25 '06 #20

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
4303
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
1646
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
9602
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10237
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9882
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...
1
7431
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
6690
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
5326
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...
0
5467
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3987
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
3
2832
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.