Re: Showing problem

From: Mike Wahler (mkwahler_at_mkwahler.net)
Date: 01/03/05


Date: Mon, 03 Jan 2005 17:29:32 GMT


"beginner10" <petri_leskinen@yahoo.com> wrote in message
news:88e220258f36d428965e650c4e512c01@localhost.talkaboutprogramming.com...
> I'm still doesn't understood this? Sorry.

Please don't drop attributions and context. See below.

"Neo" <timeless_illusion@yahoo.com> wrote in message
news:33srquF40paiiU1@individual.net...
>
> "beginner10" <petri_leskinen@yahoo.com> wrote in message
>
news:d9e0472019dc0fd37decf5be82d50d86@localhost.talkaboutprogramming.com...
> >
> > How can i changhe this code showing how many persons i saved to the
file?
> > And it prints pretty much rubbish. Where is the problem?
> >
> > #include <stdio.h>
> > int main()
> > {
> > int i;
> > FILE *data_file;
> > char list[] = "list.txt";
> >
> > struct person {
> > char firstname[20];
> > char lastname[25];
> > int phonenumber;
>
> better to use char array for phonenumber field too....
> lets say user input something like : 9899325025
> then it will overflow and store wrong information.
> by using char array you have more precise control
> over how many digits you want to store in phonenumber
> field.

What he means is that you're only guaranteed (by the
language standard) a range of values for type 'int' from
-32767 to 32767 inclusive. Attempts to store a value outside
the range of a signed integer type will produce undefined
behavior.

Of course many implementations provide a type 'int' with
a much larger range, but you should still provide protection
against overflow/underflow. If you want your code to be
maximally portable, don't assume 'int' can store less than
-32767 or greater than 32767.

I agree with Neo that an array of char would be better and
more flexible. But you still need to remember to protect
against overflowing your array.

-Mike



Relevant Pages

  • Re: Marshalling Message??
    ... > I need to marshall all the messages in order to send them thru a socket. ... > So I have a big char array, what is the easiest way to store an 8-byte ... int main{ ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Adding large numbers in C
    ... one of the numbers - or perhaps the result - is too big to store in an int. ... you have bitstrings longer than 8 bits, simply use an array of unsigned ... Incidentally, the subtraction routine does similar juggling, so if M and N ...
    (comp.lang.c)
  • Re: read keyboard input and storing in an array?
    ... > I'm trying to store user input in an array, ... You have the beginnings of that logic already since your prompt tells the ... 201st value since the array only has room for 200 values. ... This will force you to store the int values as Integer ('Integer' ...
    (comp.lang.java.help)
  • Re: attempting an actual game...
    ... > be used to store a colour definition rather than an index? ... if I were you and wanted to get to a finished game I ... ugly bitmasking teqniques and similar can sometimes save effort over more ... >>> (I once started with typedef int Cell, with a view to making a Cell ...
    (comp.games.development.programming.misc)
  • Re: Basic questions
    ... The compiler is allowed to insert padding bits wherever it likes ... > As 16 is an int, what you wrote is equivalent to ... if the OP uses 'unsigned short' only to store 16-bit numbers (writing ... values, so they're not a panacea for portability, and they might trick ...
    (alt.comp.lang.learn.c-cpp)