|  | doubles and ints |  | |
| | | Bill Cunningham |  |
| Posted: Thu Jul 10, 2008 10:36 pm Post subject: doubles and ints |  |
Is this valid C syntax ?
double=double/int;
I seem to be having trouble here.
Bill |
| |
| | | Barry Schwarz |  |
| Posted: Thu Jul 10, 2008 11:32 pm Post subject: Re: doubles and ints |  |
On Fri, 11 Jul 2008 00:36:19 GMT, "Bill Cunningham" <nospam@nspam.com> wrote:
| Quote: | Is this valid C syntax ?
double=double/int;
I seem to be having trouble here.
|
Post your compilable code so we can all see what the problem is.
Remove del for email |
| |
| | | Bill Cunningham |  |
| Posted: Fri Jul 11, 2008 12:04 am Post subject: Re: doubles and ints |  |
"Barry Schwarz" <schwarzb@dqel.com> wrote in message news:rvdd749dd48336l2kigqpqjdspo77uheuv@4ax.com...
| Quote: | On Fri, 11 Jul 2008 00:36:19 GMT, "Bill Cunningham" <nospam@nspam.com wrote:
Is this valid C syntax ?
double=double/int;
I seem to be having trouble here.
Post your compilable code so we can all see what the problem is.
Remove del for email ------ |
#include <stdio.h> #include <stdlib.h> #define _ex exit(EXIT_FAILURE)
int main (int argc,char *argv[]) { if (argc!=2) { puts("usage error"); _ex; } int count=1; double x,y,z;
y=x;z=y/count; <-----
x=strtod(argv[1],NULL); FILE *fp; fp=fopen("s","a"); char *string="%.2f\t%.2f\t%.2f\t%i\n"; fprintf(fp,string,x,y,z,count); fclose(fp); return 0; } |
| |
| | | Bill Cunningham |  |
| Posted: Fri Jul 11, 2008 12:14 am Post subject: Re: doubles and ints |  |
"Walter Roberson" <roberson@ibd.nrc-cnrc.gc.ca> wrote in message news:g56bmq$9j3$1@canopus.cc.umanitoba.ca...
| Quote: | In article <7Wxdk.858$713.149@trnddc03>, Bill Cunningham <nospam@nspam.com> wrote:
Is this valid C syntax ?
double=double/int;
I seem to be having trouble here.
No it is not. [snip] |
double a,b; int c; a=b/c;
That's what I mean.
Bill |
| |
| | | Keith Thompson |  |
| Posted: Fri Jul 11, 2008 12:31 am Post subject: Re: doubles and ints |  |
| |  | |
"Bill Cunningham" <nospam@nspam.com> writes:
| Quote: | "Barry Schwarz" <schwarzb@dqel.com> wrote in message news:rvdd749dd48336l2kigqpqjdspo77uheuv@4ax.com... On Fri, 11 Jul 2008 00:36:19 GMT, "Bill Cunningham" <nospam@nspam.com wrote:
Is this valid C syntax ?
double=double/int;
I seem to be having trouble here.
Post your compilable code so we can all see what the problem is.
Remove del for email ------ #include <stdio.h #include <stdlib.h #define _ex exit(EXIT_FAILURE)
int main (int argc,char *argv[]) { if (argc!=2) { puts("usage error"); _ex; } int count=1; double x,y,z;
y=x;z=y/count; <-----
x=strtod(argv[1],NULL); FILE *fp; fp=fopen("s","a"); char *string="%.2f\t%.2f\t%.2f\t%i\n"; fprintf(fp,string,x,y,z,count); fclose(fp); return 0; }
|
When I compile the above, I get:
c.c:13: error: parse error before '<' token
When I remove the "<-----", it compiles without error. When I enable more warnings, gcc complains about mixing declarations and statements (allowed only in C99), and says:
c.c:11: warning: 'x' might be used uninitialized in this function
Looking at the code, x is not given an initial value. Whatever garbage value it has is assigned to y, and then z is assigned y/1.
Style suggestions:
Add more whitespace, particularly around binary operators and after commas. Drop the "_ex' macro and replace the single invocation of it with a call to exit(EXIT_FAILURE) (identifiers starting with underscores should be avoided, and the macro does nothing but make your code more obscure anyway).
You initially posted a line of something that bore only an indirect resemblance to your actual code, and told us only that you "seem to be having some trouble". After considerable coaxing, you finally posted some real code -- but you *still* haven't told us what the actual problem is. I see no syntax errors in the code you posted (other than the arrow, which I presume you added later). If there had been a syntax error, your compiler would have reported it.
What is the actual problem you're having? Are you getting an error or warning message from your compiler? If so, what *exactly* did it print? Is the program misbehaving? If so, what *exactly* does it do, and how does this differ from what you expected?
I won't ask this again.
-- Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst> Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" |
| |
| | | Bert |  |
| Posted: Fri Jul 11, 2008 1:06 am Post subject: Re: doubles and ints |  |
On Jul 11, 10:36 am, "Bill Cunningham" <nos...@nspam.com> wrote:
| Quote: | Is this valid C syntax ?
double=double/int;
I seem to be having trouble here.
Bill
|
So if there's a double d and an int i, you're asking whether d = d / i is valid C syntax? I reckon that's right but if it doesn't get what you want, go change it. Experiment till the stupid computer gives you what you want. Convert i into a double and see what happens. Try
d = d / (double)i
Hope that works. Can't be screwed testing the code. |
| |
| | | Walter Roberson |  |
| Posted: Fri Jul 11, 2008 1:06 am Post subject: Re: doubles and ints |  |
| |  | |
In article <7Wxdk.858$713.149@trnddc03>, Bill Cunningham <nospam@nspam.com> wrote:
| Quote: | Is this valid C syntax ?
double=double/int;
I seem to be having trouble here.
|
No it is not.
$ cat foof.c int main(void) { double=double/int; return 0; } $ cc -fullwarn foof.c cc-1040 cc: ERROR File = foof.c, Line = 2 An identifier is expected.
double=double/int; ^
cc-1029 cc: ERROR File = foof.c, Line = 2 An expression is expected at this point.
double=double/int; ^
2 errors detected in the compilation of "foof.c".
'double' is a type name; when it appears at the beginning of a statement, it is understood as being part of a variable declarator. As you failed to follow the 'double' with a variable name (or mix of qualifiers) before the '=', the syntax is invalid.
If you mean more generally is it valid to divide a variable or value of type double by a variable or value of type int, and assign the result to a variable of type double, then the answer is Yes, that is syntactically and semantically permitted; the int would get promoted to double and the expression would proceed from there. -- "I will not approve any plan which is based on the old principle of build now and repair later." -- Walter Hickle |
| |
| | | Bill Cunningham |  |
| Posted: Fri Jul 11, 2008 1:07 am Post subject: Re: doubles and ints |  |
"Keith Thompson" <kst-u@mib.org> wrote in message news:ln63rd40do.fsf@nuthaus.mib.org...
[snip]
| Quote: | What is the actual problem you're having? Are you getting an error or warning message from your compiler? If so, what *exactly* did it print? Is the program misbehaving? If so, what *exactly* does it do, and how does this differ from what you expected?
I won't ask this again.
Everything compiles file. But when I input a number like 21.00 I get |
this printed to a file.
21.00 0.00 0.00 1
That's not what I envisioned this to print but this at first.
21.00 21.00 21.00 1
Then I am going to probably add a do while loop.
Bill |
| |
| | | Bert |  |
| Posted: Fri Jul 11, 2008 1:12 am Post subject: Re: doubles and ints |  |
| |  | |
On Jul 11, 11:06 am, rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote:
| Quote: | In article <7Wxdk.858$713.149@trnddc03>,
Bill Cunningham <nos...@nspam.com> wrote: Is this valid C syntax ? double=double/int; I seem to be having trouble here.
No it is not.
$ cat foof.c int main(void) { double=double/int; return 0;}
$ cc -fullwarn foof.c cc-1040 cc: ERROR File = foof.c, Line = 2 An identifier is expected.
double=double/int; ^
cc-1029 cc: ERROR File = foof.c, Line = 2 An expression is expected at this point.
double=double/int; ^
2 errors detected in the compilation of "foof.c".
'double' is a type name; when it appears at the beginning of a statement, it is understood as being part of a variable declarator. As you failed to follow the 'double' with a variable name (or mix of qualifiers) before the '=', the syntax is invalid.
If you mean more generally is it valid to divide a variable or value of type double by a variable or value of type int, and assign the result to a variable of type double, then the answer is Yes, that is syntactically and semantically permitted; the int would get promoted to double and the expression would proceed from there. -- "I will not approve any plan which is based on the old principle of build now and repair later." -- Walter Hickle
|
Well then I don't see why he seems to be having trouble. The int is implicitly being converted to a double and he doesn't like what he's getting. |
| |
| | | Bert |  |
| Posted: Fri Jul 11, 2008 2:18 am Post subject: Re: doubles and ints |  |
| |  | |
On Jul 11, 12:04 pm, "Bill Cunningham" <nos...@nspam.com> wrote:
| Quote: | "Barry Schwarz" <schwa...@dqel.com> wrote in message
news:rvdd749dd48336l2kigqpqjdspo77uheuv@4ax.com...> On Fri, 11 Jul 2008 00:36:19 GMT, "Bill Cunningham" <nos...@nspam.com wrote:
Is this valid C syntax ?
double=double/int;
I seem to be having trouble here.
Post your compilable code so we can all see what the problem is.
Remove del for email
------ #include <stdio.h #include <stdlib.h #define _ex exit(EXIT_FAILURE)
int main (int argc,char *argv[]) { if (argc!=2) { puts("usage error"); _ex; } int count=1; double x,y,z;
y=x;z=y/count; <-----
x=strtod(argv[1],NULL); FILE *fp; fp=fopen("s","a"); char *string="%.2f\t%.2f\t%.2f\t%i\n"; fprintf(fp,string,x,y,z,count); fclose(fp); return 0;
}
|
Why do I get the feeling that argc != 2 wouldn't work? You can't write y = x when the comp doesn't know the values stored in y and x cos they're UNINITIALIZED. Ya gotta give them something before you use them elsewhere. Which means that z=y/count doesn't work either. And I hate people who write y=x instead of y = x. |
| |
| Page 1 of 5 .:. Goto page 1, 2, 3, 4, 5 Next | |
|
|