doubles and ints

B

Bill Cunningham

Is this valid C syntax ?

double=double/int;

I seem to be having trouble here.

Bill
 
B

Bert

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.
 
W

Walter Roberson

Bill Cunningham said:
Is this valid C syntax ?

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.
 
B

Bert

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.

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.
 
B

Barry Schwarz

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
 
B

Bill Cunningham

Barry Schwarz said:
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;
}
 
B

Bert

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.
 
K

Keith Thompson

Bill Cunningham said:
Barry Schwarz said:
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.
 
B

badc0de4

Bill said:
#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; <-----

What value were you expecting to go into `z`?
x=strtod(argv[1],NULL);

Ah!
The computer can't guess you wanted this line before the one
you marked. Computers are very literal machines. They do
exactly what we tell them to, even if it makes no sense: the trick
is to tell them to do stuff in the right order.

[snip]
 
B

Bill Cunningham

[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.


21.00 21.00 21.00 1


Then I am going to probably add a do while loop.

Bill
 
B

Ben Bacarisse

Bert said:
On Jul 11, 12:04 pm, "Bill Cunningham" <[email protected]> wrote:
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);
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.
 
N

Nick Keighley

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)?
    x=strtod(argv[1],NULL);

no error checking
    FILE *fp;
    fp=fopen("s","a");

no error checking
    char *string="%.2f\t%.2f\t%.2f\t%i\n";
    fprintf(fp,string,x,y,z,count);

x is equal to whatever strtod() returned
y and z are indeterminate.
 
N

Nick Keighley

[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.

21.00    21.00    21.00 1

Then I am going to probably add a do while loop.

why?
 
K

Kenny McCormack

Keith Thompson said:
I won't ask this again.

Sure you will. Again and again. Until you die (or stop posting to clc,
which will probably occur that same day).
 
B

Barry Schwarz

[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.


21.00 21.00 21.00 1


Then I am going to probably add a do while loop.

The only place you need to add a while loop is in your internal mental
process of haphazardly throwing code at a problem. Why a while loop?
Is there anything in your code you want to do more than once? If you
are just going to guess, you might as well compile your code with a
FORTRAN compiler. It will do the same amount of good.

You need to look at the code. When do you extract the argument from
the command line? When do you try to use this value? There should be
a very obvious chronological relationship between these two. One must
come before the other. Does your code comply with that relationship?


Remove del for email
 
B

Bill Cunningham

Perhaps you meant something like this:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
int count = 1;
double x = 1,
y,
z;
FILE *fp;

if (argc != 2) {
puts("usage error");
exit(EXIT_FAILURE);
}
y = x;
z = y / count;

x = strtod(argv[1], NULL);
fp = fopen("s", "a");
if (fp) {
fprintf(fp, "%.2f\t%.2f\t%.2f\t%i\n", x, y, z, count);
fclose(fp);
} else {
puts("failed to open file.");
}
return 0;
}
Yes but I haven't added the error checking yet as you have yet. I'm not
finshed yet.

Bill
 
B

Bill Cunningham

36:19 GMT, "Bill Cunningham"]
wrote:
[/QUOTE]


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)?
x=strtod(argv[1],NULL);

no error checking
FILE *fp;
fp=fopen("s","a");

no error checking
char *string="%.2f\t%.2f\t%.2f\t%i\n";
fprintf(fp,string,x,y,z,count);

x is equal to whatever strtod() returned
y and z are indeterminate.
fclose(fp);
return 0;


OK. here's the output I want and what I want to do.

10.00 10.00 10.00 1
10.50 20.50 10.50 2
10.00 30.50 10.67 3

And so on.

Bill
 
B

Bill Cunningham

Neither was I. The above is not even close to a useful program yet, but
it did what I *guessed* that you wanted it to do without violation of the
rules of the C language. Your program was not a proper C program.

The big thing I have missed out on is initializing the variables right?
I have only declared them accept for count.

Bill
 
R

Richard

Bill Cunningham said:
Perhaps you meant something like this:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
int count = 1;
double x = 1,
y,
z;
FILE *fp;

if (argc != 2) {
puts("usage error");
exit(EXIT_FAILURE);
}
y = x;
z = y / count;

x = strtod(argv[1], NULL);
fp = fopen("s", "a");
if (fp) {
fprintf(fp, "%.2f\t%.2f\t%.2f\t%i\n", x, y, z, count);
fclose(fp);
} else {
puts("failed to open file.");
}
return 0;
}
Yes but I haven't added the error checking yet as you have yet. I'm not
finshed yet.

Bill

LOL. Nice one Bill. The Regs will keep at it. You're their way out!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top