What is wrong with this code?

K

Keith Thompson

Bill Reid said:
Ummmm, let's think about this...maybe because I actually compiled
and linked two separate "translation units" with "extern" declarations
and typedefs in a what's called a "header file"? Maybe I just cut and
pasted that code into what appears to be a single source code file,
and just forgot to paste in the include stdio.h define? Naaahhhh,
never happen...

And you expected us to know that, even if you didn't happen to tell
us?

[...]
Another egregious personality flaw, perhaps requiring "professional"
help...

Why do you respond to good advice with sarcasm?

[...]
Well, you didn't actually make me laugh here, but you did make
me smile...

Global variables are usually a bad idea. I fail to see the humor.

[...]
 
R

Richard Heathfield

Keith Thompson said:
Why do you respond to good advice with sarcasm?

Presumably because he isn't interested in good advice. Which suits me
just fine. Into the bozo bin with him...
 
R

Richard Tobin

Bill Reid said:
I tend to prefer nice big preferably unabbreviated English words that
I can understand immediately, but that's not really a "C" convention, now
is it?

I like unabbreviated names because they're easy to understand, but
I like short names because then expressions and statements fit on
a single line, making it easier to grasp the structure. Overall
I find the latter approach results in more readable code.

I suspect a background in mathematics also inclines one to short
variable names; mathematicians almost invariably use single-letter
names.

-- Richard
 
O

Old Wolf

Since your value is a double, why not use %lf and avail yourself of
the extra precision.

Huh? "%f" is the specifier for printing doubles. %lf causes
undefined behaviour in C90. (In C99 it has the same effect
as %f).
 
O

Old Wolf

I suspect a background in mathematics also inclines one to short
variable names; mathematicians almost invariably use single-letter
names.

A minor notational quirk. In mathematics, "bar" means b multiplied
by a multiplied by r ! In fact, the first algebra text used words for
variable names -- specifically, the Arabic word for "the thing".

In mathematics you rarely, if ever, come across a situation where
you would need to have multiple variables with descriptive names.
Most multi-variable situations call for the variables to be held
in arrays of some form or another.
 
S

Sjouke Burry

Old said:
Huh? "%f" is the specifier for printing doubles. %lf causes
undefined behaviour in C90. (In C99 it has the same effect
as %f).
Hu?? back at you.
check the help below.

l for long int or double,
f for float.

On all the systems I know, where float is 32bit
and double 64, f is for float, and lf for double.


The format for the printf family of functions is as follows:

% flags width .precision type prefix format type

.--------------------------.--------------------------------------.
| Flags | Format Type |
| - (left justify) | d,i (signed decimal) |
| + (prefix with sign) | u (unsigned decimal integer) |
| blank (prefix with blank)| o (unsigned octal integer) |
| # (modifies o,x,X, | x,X (unsigned hex integer) |
| e,E,f,g,G) | f (fixed-point float) |
.--------------------------. e,E (scientific notation) .
| Type Prefix | g,G (%e or %f; whichever is shorter) |
| F (far pointer) | c (single character) |
| N (near pointer) | s (string) |
| h (short int) | p (pointer) |
| l,L (long int or double) | n (character count) |
.--------------------------.--------------------------------------.
 
R

Richard Tobin

Huh? "%f" is the specifier for printing doubles. %lf causes
undefined behaviour in C90. (In C99 it has the same effect
as %f).
[/QUOTE]
Hu?? back at you.
check the help below.

Get better help.

The C99 standard says that the l modifier "has no effect on a following
a, A, e, E, f, F, g, or G conversion specifier".

The reason for this is that printf() is a variadic function, so the
arguments are subject to the "default argument promotions". In
particular, arguments of type float are converted to double before the
function is called. So there is no way to pass a float to printf(),
so %f always gets a double.

Of course, things could have been defined so that printf() converts
its argument from a double to a float if the format is %f, but that
would be pointless.

-- Richard
 
K

Keith Thompson

Sjouke Burry said:
Hu?? back at you.
check the help below.

l for long int or double,
f for float.

On all the systems I know, where float is 32bit
and double 64, f is for float, and lf for double.


The format for the printf family of functions is as follows:

% flags width .precision type prefix format type

.--------------------------.--------------------------------------.
| Flags | Format Type |
| - (left justify) | d,i (signed decimal) |
| + (prefix with sign) | u (unsigned decimal integer) |
| blank (prefix with blank)| o (unsigned octal integer) |
| # (modifies o,x,X, | x,X (unsigned hex integer) |
| e,E,f,g,G) | f (fixed-point float) |
.--------------------------. e,E (scientific notation) .
| Type Prefix | g,G (%e or %f; whichever is shorter) |
| F (far pointer) | c (single character) |
| N (near pointer) | s (string) |
| h (short int) | p (pointer) |
| l,L (long int or double) | n (character count) |
.--------------------------.--------------------------------------.

That table is either wrong or just ambiguously written.

"F" and "N" are non-standard, as are "far" and "near" pointers, so
I'll ignore those.

The "%f" format expects an argument of type double. It can also be
used with an argument of type float, but that's only because float
arguments are promoted to double when they match the "..." of a
variadic function.

The format for type "long double" is "%Lf". I suspect that
the entry in the table:

l,L (long int or double)

really means "l for long int, L for long double"; whoever wrote it
should be embarrassed.

In C90, "%lf" invokes undefined behavior. In C99, "%lf" means the
same thing as "%f".

So, in C99, or in an implementation that provides the C99 feature of
allowing "%lf", you *can* use "%f" for float and "%lf" for double.
(You can also, if you like, use "%lf" for float and "%f" for double,
but I wouldn't recommend it.) If you care about your code being
portable, you should use "%f" for both float and double, since that's
required to work for either C90 or C99.
 
R

Richard Heathfield

CBFalconer said:
You are really talking about 4k blocks, when you are including an
attribute byte, as in VGA.

Sorry, you're right - 80x25x2 (see above) is actually 4000, not 2000. So
much for arithmetic. Yes, 48 spare bytes looked wrong when I wrote it,
but I trusted my arithmetic rather than my memory!
 
D

David Thompson

Bill Reid said:
typedef struct {
unsigned value_1;
double value_2;
double value_3;
double value_4;
} VALUES;

typedef struct {
unsigned num_values;
unsigned tot_value_1;
double tot_value_2;
double tot_value_3;
double tot_value_4;
VALUES values[MAX_VALUES];
} VALUES_STRUCT;

To avoid replicating the fields here, you could do

typedef struct {
unsigned num_values;
VALUES total[MAX_VALUES];

Ahem. No array for this one.
VALUES values[MAX_VALUES];
} VALUES_STRUCT;

and refer to v.total.value1 instead of v.tot_value_1, etc.
Right.

<snip rest>
- formerly david.thompson1 || achar(64) || worldnet.att.net
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top