On Jul 11, 10:36 am, "Bill Cunningham" <nos...@nspam.c omwrote:
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.
In article <7Wxdk.858$713. 149@trnddc03>,
Bill Cunningham <nospam@nspam.c omwrote:
Is this valid C syntax ?
>double=doubl e/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
On Jul 11, 11:06 am, rober...@ibd.nr c-cnrc.gc.ca (Walter Roberson)
wrote:
In article <7Wxdk.858$713. 149@trnddc03>,
>
Bill Cunningham <nos...@nspam.c omwrote:
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.
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.
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_FAILU RE) (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_Keit h) 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"
"Keith Thompson" <kst-u@mib.orgwrote in message
news:ln63rd40do .fsf@nuthaus.mi b.org...
[snip]
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.
On Jul 11, 12:04 pm, "Bill Cunningham" <nos...@nspam.c omwrote:
<snip>
>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);
<snip>
Why do I get the feeling that argc != 2 wouldn't work?
I don't know. It seems to be one of more reasonable part of this
program.
You can't write y = x when the comp doesn't know the values stored in
y and x cos they're UNINITIALIZED.
That is overstating the problem. There is not problem with using y
uninitialised in this context.
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.
Try to limit yourself (especially on Usenet) to hating what people do
("I hate it when people write y=x") or the thing itself ("I hate y=x")
rather than hating people.
try to avoid starting identifiers with '_'. Only
The Implementor (the guy who writes the compiler and the
standard library) should use such identifiers.
Hiding things in macros makes your code obscure.
int main (int argc,char *argv[]) {
if (argc!=2) {
puts("usage error");
_ex;
}
int count=1;
double x,y,z;
you are mixing ststements with definitions which is not legal C89
(the most widely available standard).
y=x;z=y/count; <-----
please use whitespace
y = x;
z = y / count;
x is an uninitialised variable. Hence x, y, and z
and indetereminate values. Why do you divide
by count which is equal to 1 (one)?
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.
Comment