Re: Showing problem
From: Mike Wahler (mkwahler_at_mkwahler.net)
Date: 01/03/05
- Next message: Robert B. Clark: "Re: Showing problem"
- Previous message: Jonathan Bartlett: "Re: which loop is faster"
- In reply to: beginner10: "Re: Showing problem"
- Next in thread: Richard Bos: "Re: Showing problem"
- Reply: Richard Bos: "Re: Showing problem"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Robert B. Clark: "Re: Showing problem"
- Previous message: Jonathan Bartlett: "Re: which loop is faster"
- In reply to: beginner10: "Re: Showing problem"
- Next in thread: Richard Bos: "Re: Showing problem"
- Reply: Richard Bos: "Re: Showing problem"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|