Urgent C Questions

C

Chris Hills

CBFalconer said:
If that is the H Schildt (sp?) book, you are lucky. You have now
been advised that it is useless and stuffed full of errors, because
the author doesn't understand C. See if you can get some practical
use of it as a replacement for a log in the fireplace.


For once we agree completely!
BTW Ditto for his C++ books
 
C

Chris Hills

Jack Klein said:
No, it is not. Once there is "void main()" there are no requirements
at all on the program, because it is not C. And if malloc() is called

It is this sort of pedantry that is very unhelpful to everyone.
Once a program generates undefined behavior, the C language no longer
imposes any requirements on it. Period.

This sort of pedantry reflects quite badly on you. You have not helped
anyone

Are you actually the Chris Hills of PhaedruS SystemS, Hitex, MISRA,
and the BSI/ISO C panel,
Yes.

Aren't you aware of MISRA-C 2004 12.2 (required)?
Aren't you aware of 6.3 in ISO 9899:1990 and the identical wording in
ISO 9899:1999:
"Between the previous and next sequence point an object shall have its
stored value modified at most once by the evaluation of an
expression."
Since this code modifies the value of the object 'x' twice without an
intervening sequence point, it violate a "shall" clause outside of a
constraints section, which, of course, produces undefined behavior.

That is the answer you could have given instead of the reply you gave.
Giving answers like
With no explanation is not clever. It might make you feel good/ superior
but it is not a good way to communicate with people or help the OP
I do not assume that the OP wrote the code. In fact I think it is
more likely that it was given to him as a homework assignment. And
that's the sad part of it, that he is dealing with an instructor, or
perhaps a tutorial web site, that considers these legitimate
questions, and they are not.

But instead of trying to help you were as obtuse as possible. That is
worse than the original teacher who is trying to help the students.

Oh, I'm sad, for now you have turned to personal insult.

No more than you did with your original replies. (And this one) If you
can't see that you may need to talk to someone about dealing with
people.
And
incorrect, because most of my coworkers are human, and the rest almost
so. ;)
:)

As a member of several standards bodies and a certified engineer, I am
appalled at the fact that you are condoning the teaching of blatantly
incorrect material.

I am not condoning it. Several people gave helpful and instructive
replies. Your reply to the OP was, whilst being accurate was neither
helpful nor instructive on what the problems were or how to correct
them. I was not suggesting that you do the homework. For that is clearly
what it was but be helpful to the OP in a constructive manner. .

Are you the real Chris Hills?
One of them....

A couple of weeks ago I found another one 30 miles from where I live who
is in IT and has a blog... Search on your name and you find there are
often many people with *your* name.
 
R

Richard Heathfield

Chris Hills said:
According to its author it was when he started.

It would be interesting to hear him attempting to defend that view.

Things move C++ was based on C89 since then noth C and C++ have moved
in different directions.

It is trivial to write a legal C89 program that is not a legal program in
the C++ that was based on C89.

In the beginning C++ was a superset of C otherwise they could not have
used the C++ compiler for initial testing and then a C compiler for
final testing,

Of course they could - and they did. For some C programs to be legal C++
programs does not require C++ to be a superset of C.
 
P

Philip Potter

Hans said:
I didn't write this code, but what is wrong about it?
In 'C: the complete reference' all prgs start like that.

C: the complete reference by Herbert Schildt is a book everyone knows
for all the wrong reasons. While there are many poor C textbooks, this
one is notorious. For an incomplete list of its errors, see:
http://www.seebs.net/c/c_tcr.html
Is x++ not the same as x=x+1?

As a single statement (not an expression), yes. Which is to say the
statements
x++;
and
x=x+1;
are identical in behaviour.

This is irrelevent, though, because what was written was
x = x++;
which isn't either of the above two statements. Please see the C FAQ,
question 3.3.
I can't!
It is required for an advanced class.

If this is the basic class, I shudder to think what the advanced class
contains.
I don't think that's gonna work.
He is a cousin of the principal.

Nevertheless, if his entire class issues complaints, it will look bad.
If the complaints are backed up with citations from the C Standard or
one of its drafts, it will also look bad. There is more support
available to you at a university than you realise. Seek advice from your
student's union, or your tutor.

You have to ask yourself: Why am I doing this class? Is it just to get
letters after my name, and a shiny certificate? Or am I doing it to
actually learn something? If I want to learn something when my
instructor is teaching me falsehoods, how do I go about doing that?

Phil
 
N

Nick Keighley

it's wrong. See the clc FAQ. But in summary main() *must*
return int.


nope. Read a good book. Besides we aren't saying "x++" is exhibits
Undefined Behaviour[*] but that "x = x++" is. Essentially x is being
updated twice in the same statement (the technical term is "between
sequence points") and no ordering is defined. Read the clc FAQ.

[*] Undefined Behviour is a term from the C programming language
specification. It means no behviour has been defined for the
construct, leaving the compiler writer free to do whatever he
pleases. Including crashing. Well written programs do not
contain UB.

["For the purposes of this lecture I'm going to simplify,
to lie a little" paraphrasing Neils Bohr]
Only when x++ is defined, such as
    ((int *)a)[x++] = y;
    a[x++] = b[y++];
    x++;
But not
    x = x++;
    a[x] = a[x++];
    a[x++] = b[x++];
See comp.lang.c FAQ, Questions 3.1, 3.2, and 3.9
link:
(3.1)http://c-faq.com/expr/evalorder1.html
(3.2)http://c-faq.com/expr/evalorder2.html
(3.9)http://c-faq.com/expr/evalorder4.html

I'd read those if I were you



also you need a \n at the end. Or an fflush(stdout)
to ensure the ouput appears (on all compilers).
Mental note: QuickC is a retarded compiler.

But now you have a reason for bad code: Microsoft's C compiler said it
was fine. :p
But seriously, Visual C++ .NET onwards has full C89 support (stdint is
C99) and whines about these errors. It whines more about deprecated
functions in stdio, stdlib, string, and others.

these are depracted ***by microsoft***. You need to suppress
these warnings to get a clean compile of C89 code.

lcc-win32 prints lots of warnings.

*GOOD*

AOL. me too

lcc is standards-compliant and whines for good reasons.
Warning test.c: 3  old-style function definition for 'main'

Methinks ambiguous prototype: main(void) or main(int, char **)?
what?

I have a habit of main returning int and having int, char ** as
parameters. It's a good habit that avoids warnings.

it's not "a good habbit" it's the law!
Warning test.c: 3  'void main()' is a non-ANSI definition

Non-int return type.

effectivly the compiler provides it.
The error message could be better.
But in essence there are a limited number
of valid declaration for main and they all
specify a return type of int.

<snip>

the comments about your course are partially true.
Get a good book (eg. K&R) and study on your own.
 
C

Chris Hills

[QUOTE="Philip Potter said:
I don't think that's gonna work. He is a cousin of the principal.

Nevertheless, if his entire class issues complaints, it will look bad.
If the complaints are backed up with citations from the C Standard or
one of its drafts, it will also look bad[/QUOTE]

It depends where this university is. It may get the OP chucked out.
Lecturers don't like being name to look silly. You have to tread
carefully especially as he is related to the principal.

When training a junior rugby team they got press-ups for any
infringement in training. (As I said, "You may not be able to play rugby
but by god you will be fit!" :) 5 was the usual. However arguing
with the ref (in a training match) got you 10. Arguing with the ref, and
you were right got you 15!
This was to stop them behaving like football payers who always argue
with the ref.

The point being that in a real rugby match if you argue with the referee
as a player you tend to get sent off. The Ref is God.

I suspect that if you argue with a lecturer who is only there because
his cousin is the principal you ain't going to win (or get your
qualification as you will probably fail a crucial exam) and that isn't
in the C standard either.
. There is more support available to you at a university than you
realise. Seek advice from your student's union, or your tutor.

What Student Union? It depends where this university is. Besides as in
all gambling the House Wins
You have to ask yourself: Why am I doing this class? Is it just to get
letters after my name, and a shiny certificate?

In some cultures that is what matters. This is why they turn up at
interviews with the certificates. I have seen some students complain
that the degree certificate did not look prestigious enough to put
before an employer. This was important in the country they were going
back to.
Or am I doing it to actually learn something? If I want to learn
something when my instructor is teaching me falsehoods, how do I go
about doing that?

Without failing the course..... you have to pass the course to get the
qualification to get a job to eat to live.

You can be right later. Being right and broke means you starve.

It is a difficult problem for students in some "universities" You HAVE
to pass the course to get a job. This usually means not fighting with
the lecturers and the Establishment.

However you can learn by yourself in places where they give good helpful
advice not just one line "not standard C" as Jack did which helps no
one other than making him feel superior and probably frightening off the
OP from asking any more questions

If you want to help do as John Bode and andreyvul did. Even Keith
Thompson was helpful without doing the homework, a difficult balance.

You are not sure if the question is asked because the OP wanted the
assignment done for him because he is lazy or because he wanted real
guidance and may or may not have known it was full of holes.
 
R

Richard Bos

Hans Schneider said:
I can't!
It is required for an advanced class.


I don't think that's gonna work.
He is a cousin of the principal.

Get the hell out of there. Now. This is a _very_ bad sign, and the code
is even worse. Your diploma will be worthless.

Richard
 
B

Bart van Ingen Schenau

Heap: malloc()ed memory
Stack: local variables

This is true for most PC-based C implementations, but it is not
actually required by the language.
Data Segment: No clue (haven't done 16-bit x86 asm in a *long* time)

In a typical PC implementation, the data segment is used to hold
variables with static storage duration (i.e. variables declared as
static and global variables).
This is also not required by the language definition.
On the outermost stack frame.

Could be, but for most PC implementations it will live in the data
segment.

int *k = (void *)malloc(1);
Furthermore, I think you meant to type:
int *k = (int *)malloc(1 * sizeof(int));
Big difference, for the following reasons:
1) malloc()'s return type is void *, making (void *)malloc()
redundant.
I think you meant (int *)malloc() so that malloc() returns int *.

Actually, the cast is completely redundant and can mask the error of
not having a prototype visible.
The correct call would be:
int *k = malloc(1 * sizeof(int));
or (preferrably)
int *k = malloc(1 * sizeof(*k));

2. how to find sizeof variable w/o using sizeof command?

sizeof is a compiler (not preprocessor) macro.

sizeof is not a macro at all, but an operator, like +, -, *, etc.
The main differences with the other operators are:
- the operator sizeof is identified by a keyword instead of a symbol
- you can apply the sizeof operator to a type.

x = x++ is undefined, but judging on how ++ works, I think the program
expands it to something like this (asm in comments):
x = x; /* MOV X, X */
x++; /* INC X */

This is one possible implemenation.
It is equally possible to do the operation the other way around, or
even in parallel.
The final verdict is: Whatever the result is, it is correct, because
the behaviour is not defined.

Sounds like a homework question. I explained it to the best (read most
verbose) of my abilities.

And I corrected the misconceptions to the best of my abilities.

Bart v Ingen Schenau
 
P

Philip Potter

Chris said:
[QUOTE="Philip Potter said:
2. Complain to the school administration about the unqualified
instructor and erroneous material.
I don't think that's gonna work. He is a cousin of the principal.
Nevertheless, if his entire class issues complaints, it will look bad.
If the complaints are backed up with citations from the C Standard or
one of its drafts, it will also look bad

It depends where this university is. It may get the OP chucked out.
Lecturers don't like being name to look silly. You have to tread
carefully especially as he is related to the principal.

When training a junior rugby team they got press-ups for any
infringement in training. (As I said, "You may not be able to play rugby
but by god you will be fit!" :) 5 was the usual. However arguing
with the ref (in a training match) got you 10. Arguing with the ref, and
you were right got you 15!
This was to stop them behaving like football payers who always argue
with the ref.
>
The point being that in a real rugby match if you argue with the referee
as a player you tend to get sent off. The Ref is God.[/QUOTE]

Yes, but rugby doesn't exist outside of the stadium. C does exist
outside of the OP's university, and his lecturer sure isn't God as far
as C is concerned, even if he is as far as the university is concerned.
I suspect that if you argue with a lecturer who is only there because
his cousin is the principal you ain't going to win (or get your
qualification as you will probably fail a crucial exam) and that isn't
in the C standard either.

I wasn't suggesting arguing with the lecturer; I was suggesting seeking
advice and raising matters with the University administration. If the
principal is abusing his position to keep his incompetent relation in a
job, then perhaps the student press could get involved too. If this
really is the case, it's scandalous.
What Student Union? It depends where this university is. Besides as in
all gambling the House Wins

So you think universities are similar to casinos?
In some cultures that is what matters. This is why they turn up at
interviews with the certificates. I have seen some students complain
that the degree certificate did not look prestigious enough to put
before an employer. This was important in the country they were going
back to.


Without failing the course..... you have to pass the course to get the
qualification to get a job to eat to live.

It depends on whether the qualification is worth the paper it's written
on. Getting a degree doesn't guarantee a job, and not getting a degree
doesn't guarantee unemployment.
It is a difficult problem for students in some "universities" You HAVE
to pass the course to get a job. This usually means not fighting with
the lecturers and the Establishment.

See above.

Phil
 
N

Nick Keighley

On Jan 14, 9:41 pm, Hans Schneider <[email protected]> wrote:

I suggest you read the other posts in this thread.
Then read the clc FAQ.
Then read more clc posts.

1. in the prg bellow what vars are stored on stack, heap, data segment?

"program below"
Heap: malloc()ed memory
Stack: local variables

no. these are implementation details and vary from compiler to
compiler. This applies to all your answers to questions like this.

Data Segment: No clue (haven't done 16-bit x86 asm in a *long* time)

    void main()

A return type of void works for main();

works? In what sense does it "work"?
however, a non-int return type
for main is *not* recommended as [it] can potentially cause
segmentation faults and other problems.

you call segmentaion faults "working"? I hope
you aren't programming anything I rely on...

just curious, on which compiler? It is not recomended
because *it is non-standard*.

        int *k = (void *)malloc(1);

Furthermore, I think you meant to type:
          int *k = (int *)malloc(1 * sizeof(int));

I hope not. He *meant* to type

int *k = malloc (sizeof (int));

or
int *k = malloc (sizeof *k);
Big difference, for the following reasons:
1) malloc()'s return type is void *, making (void *)malloc()
redundant.
   I think you meant (int *)malloc() so that malloc() returns int *.

nope. (void*) is automatically converted (int*)

2) malloc()'s argument is the size of memory *in bytes* to allocate.
malloc(1) allocates 1 byte, malloc(1 * sizeof(int)) allocates memory
for 1 int; the size in bytes allocated being sizeof(int).

I don't see what the "1" buys you.
Do *not* do
sizeof(void) *at all*, as this is undefined (for good reasons).

ie. it makes no sense.

   You could also use (element type *)calloc(number of elements,
sizeof(element type)).

note: calloc() zeros the memory. This may not be what you want.
calloc() cannot be relied on zero pointers or floating point types
correctly. Generally, I use malloc().

   Or create a (preprocessor) macro like this:
        #define mymalloc(n, type) (type *)malloc(n * sizeof(type))

   and replace "int * i = (int *)malloc(n * sizeof(int))"
   with "int * i = mymalloc(n, int);"

puke.

1. by convention macros are in upper case
2. heavy use of the preprocessor makes for obscure code.
3. the cast is unnecessary
4. you need more brackets in your macro

int *i = mymalloc (NORMAL_COUNT + SPECIAL_COUNT, int);

does what?

and isn't "type" a C++ reserved word?
:)

sizeof is a compiler (not preprocessor) macro.

no, it isn't a macro.
During compilation, the
compiler replaces sizeof(foo) with a number equal to the number of
bytes 1 foo takes up in memory. This is why malloc() and realloc()
*must* have "* sizeof(type)" in the size argument for them to perform
as expected.

Example (assume sizeof(int) == 2):

    int * i = (int *)malloc(sizeof(int));

is equal to
    int * i = (int *)malloc(2);



x = x++ is undefined, but judging on how ++ works, I think the program
expands it to something like this (asm in comments):

the point is the behaviour is not defined by the standard.
So the compiler writer can do what he damn well pleases.

<snip>
 
C

Chris Hills

[QUOTE="Philip Potter said:
The point being that in a real rugby match if you argue with the
referee as a player you tend to get sent off. The Ref is God.

Yes, but rugby doesn't exist outside of the stadium. C does exist
outside of the OP's university, and his lecturer sure isn't God as far
as C is concerned, even if he is as far as the university is concerned.[/QUOTE]

Getting caned by a university is the end in some places. Some parts
of the world fighting authority will not get you any job. (With or
without the degree) Burma, China etc
I wasn't suggesting arguing with the lecturer; I was suggesting seeking
advice and raising matters with the University administration.

If he is related to the principal it may be one and the same. If the
lecturer is as bad as the source code indicates I don't think he got the
job on technical merit.... Its not quite the same as a UK university.
If the principal is abusing his position to keep his incompetent
relation in a job, then perhaps the student press could get involved
too. If this really is the case, it's scandalous.

Not everywhere works the way you might like it too. In some cultures you
don't argue with the administration and win. See Burma the whole
religious and legal professions got involved and lost.

I have argued with the lecturer in the UK but at the time I was a
mature student with a lot of relevant experience. Even so it was not as
simple as you might think. There was a lot of "compromise" to save face
for the collage.
So you think universities are similar to casinos?

Universities and life in general. Even in court it depends on the mix
of your lawyer, their lawyer and the judge. Being right does not always
have any bearing on the result.

Some people just can't hack university life, the way the course is
run/taught etc whereas they would have done very well at a different
university, course, syllabus or lecturer. Life is full of variables.

As you point out you can pass a degree without actually learning
anything if you just give the answer the lecturer wants even if it is
technically bot the best answer or actually as per the source code we
have seen not "conforming" even if it does work on the compiler used.
It depends on whether the qualification is worth the paper it's written
on.

However in some parts of the world no degree no interview. Therefore no
job. It is not as bad in the UK but you still see adverts for 2.1 and
1sts only not just "a degree" and many do ask for a degree. It is just
that a lot of places don't
Getting a degree doesn't guarantee a job,
Absolutely but it can get you to the start line. No degree you don't
even get there in some cultures and parts of the world.
and not getting a degree doesn't guarantee unemployment.

Not always true in some places, at least not in the filed in question
(anyone can flip burgers) . Some parts of the world you fail or not
complete you don't even get the interview.

The point is the students may not be in a position to argue with the
lecture that the course is hopelessly wrong and win. They may not have
the luxury of changing universities or finding work without a degree.

Besides if all the software people there are taught the same and it
works in the local developments environments no one is going to employ
a disruptive student who failed the degree... no job no money no food.
(Not everywhere has social services.)
 
C

Chris Hills

Richard Bos said:
Get the hell out of there. Now. This is a _very_ bad sign, and the code
is even worse. Your diploma will be worthless.

No.. It may get him a job where he can learn the right way. On the other
hand there may not be anywhere else that is practical to go to.

That said if there is an opportunity to move and the other place is
better (not run by a brother of the cousin :) I would say move as well.
..
 
P

Philip Potter

Chris said:
[QUOTE="Philip Potter said:
The point being that in a real rugby match if you argue with the
referee as a player you tend to get sent off. The Ref is God.
Yes, but rugby doesn't exist outside of the stadium. C does exist
outside of the OP's university, and his lecturer sure isn't God as far
as C is concerned, even if he is as far as the university is concerned.

Getting caned by a university is the end in some places. Some parts
of the world fighting authority will not get you any job. (With or
without the degree) Burma, China etc[/QUOTE]

A name like Hans Schneider does not suggest Burma or China. I fail to
see how this is relevant.
If he is related to the principal it may be one and the same. If the
lecturer is as bad as the source code indicates I don't think he got the
job on technical merit.... Its not quite the same as a UK university.

Perhaps. I don't know where the OP comes from, but neither do you.
Not everywhere works the way you might like it too. In some cultures you
don't argue with the administration and win. See Burma the whole
religious and legal professions got involved and lost.

Again, I fail to see how this is relevant.
I have argued with the lecturer in the UK but at the time I was a
mature student with a lot of relevant experience. Even so it was not as
simple as you might think. There was a lot of "compromise" to save face
for the collage.

At my undergrad university, an important mathematical theorem was proved
when a student asked a lecture "but what if..?". YMMV.
Universities and life in general. Even in court it depends on the mix
of your lawyer, their lawyer and the judge. Being right does not always
have any bearing on the result.

I never said it did. I wasn't expecting the OP to openly revolt, or take
the lecturer to court, or anything like that. I was expecting him to
consider whether the course he was on would be useful to him in general,
or if there were better options available to him. The answer is for him
to decide. Not you, and not me.
However in some parts of the world no degree no interview. Therefore no
job. It is not as bad in the UK but you still see adverts for 2.1 and
1sts only not just "a degree" and many do ask for a degree. It is just
that a lot of places don't

don't what?

And many jobs advertised as "2.i or 1st only" will still accept a 2.ii
to interview if the going is tough. Most job adverts overspecify
requirements - see all the examples of "5 years C# experience" when C#
was only a year old.

And "2.i or 1st only" would probably accept a 2.ii from an "elite"
institution such as Oxbridge, MIT or Harvard, and may well reject a 1st
from "Honest Joe's University of the Isle of Sheppy".
Absolutely but it can get you to the start line. No degree you don't
even get there in some cultures and parts of the world.

Perhaps if you are only applying for jobs through adverts. But less than
half of jobs are acquired that way. Being in the right place at the
right time, knowing the right people, counts just as much (if not more)
than qualifications. See the OP's instructor for example.
Not always true in some places, at least not in the filed in question
(anyone can flip burgers) . Some parts of the world you fail or not
complete you don't even get the interview.
>
The point is the students may not be in a position to argue with the
lecture that the course is hopelessly wrong and win. They may not have
the luxury of changing universities or finding work without a degree.

[Again, I didn't say the student should argue with the lecturer.] More
people have the luxury of changing universities than realise it.
Besides if all the software people there are taught the same and it
works in the local developments environments no one is going to employ
a disruptive student who failed the degree... no job no money no food.
(Not everywhere has social services.)

Are you still talking about Burma? I fail to see how this is relevant.
 
J

James Kuyper

Chris said:
There is no defence for that sort of behaviour from an adult.

While rudeness is generally inappropriate, I believe that there are
exceptions.

I think that a certain degree of rudeness is not only permissible, but
actually appropriate, behavior for an adult in response to certain kinds
of provocation. Rudeness is, in those circumstances, an appropriate way
of communicating to someone that they have stepped out of bounds.

For instance, staring at a stranger eye-to-eye with a disapproving
facial expression is generally considered rude. However, if they've cut
ahead of you in line, I think it's an entirely appropriate (though
possibly inadequate) way of communicating to them your disapproval.
 
B

Ben Bacarisse

andreyvul said:
On Jan 15, 9:57 pm, Hans Schneider <[email protected]> wrote:
Is x++ not the same as x=x+1?
Only when x++ is defined, such as
((int *)a)[x++] = y;
a[x++] = b[y++];
x++;

I don't know how these relate to the question ("is x++ not the same as
x=x+1?") but it seems worth noting that the first two of these are not
always well-defined. It might make an interesting (but advanced)
exercise to show a code fragment in which these first two have
undefined behaviour.

For extra credit, do the same for the third one! (This is unrelated to
the current issue).

PS. This is not really an exercise. I am just pointing out how
careful one must be about sequence points.
 
J

James Kuyper

Richard said:
Chris Hills said:



C++ has never been a superset of C. If it were, all C programs would be C++
programs, which is clearly not the case.

C++ has never been an exact superset of C, but it has always been
approximately a superset, as a matter of deliberate design. A program
that uses prototypes in every function declaration, and avoids any of
the additional keywords of C++, has a pretty good chance of compiling
under C++ to produce the same behavior as it would when compiled under
C. There are exceptions, but for the most part they're pretty obscure cases.
 
J

James Kuyper

Hans said:
I didn't write this code, but what is wrong about it?
In 'C: the complete reference' all prgs start like that.

That particular book is well known for the large number of errors it
contains. However, many other textbooks also handle this incorrectly.
That doesn't make it correct. The standard specifies two ways to declare
main(). A conforming implementation of C is required to accept any
declaration that is equivalent to one of those two ways; but it's not
required to accept any other ways. 'void main()' isn't equivalent to
either of those ways.
Is x++ not the same as x=x+1?

Basically, yes. As a result, x=x=x+1 would suffer from exactly the same
problem as x = x++. You cannot modify the value of an object twice
without an intervening sequence point, and there are no sequence points
associated with either of those expressions.
QuickC didn't complain.

When the behavior is undefined, diagnostics are not required. They are
only required for syntax errors and constraint violations.
lcc-win32 prints lots of warnings.
Warning test.c: 3 old-style function definition for 'main'
Warning test.c: 3 missing prototype for 'main'

These messages both refer to the fact that you wrote main() rather than
main(void). Without the 'void', it's an old-style definition, not a
function prototype. Old-style definitions are allowed, but not
recommended, hence the warning.
Warning test.c: 3 'void main()' is a non-ANSI definition

This is warning you about using 'void' rather than 'int', which violates
the requirements of the ANSI/ISO standard for C.
Warning test.c: 6 missing prototype for printf

You should have written

#include <stdio.h>

in order to have the correct prototype for printf(). Without that
prototype, most uses of printf() are likely to fail.
Warning test.c: 6 Missing prototype for 'printf'
0 errors, 5 warnings

What do they mean?
Why are there 2 warnings on line 6?

I have no idea. Jacob Navia participates in this newsgroup, hopefully he
can explain it.
Some of them go away with #include <stdio.h>.
But where is the prototype for main()?

You create the prototype when you define it:

int main(void)

....
With MS QuickC and GCC it prints 6.
With lcc-win32 it prints 5.
But gcc says 'operation on x may be undefined'.
I'll have to investigate why.

Several people have already given you the reason; I've repeated it above.

....
I can't!
It is required for an advanced class.

If your instructor is in fact the source for the code you're asking
about, he doesn't know C very well. If you have to take this class, and
this is the only instructor available, I recommend looking for a second
source of education to correct the mistakes he will be teaching you.
 
W

William Pursell

I didn't write this code, but what is wrong about it?
In 'C: the complete reference' all prgs start like that.


Which says a lot about the quality of 'C: the complete reference'.
 
R

Richard Heathfield

James Kuyper said:
C++ has never been an exact superset of C,

Precisely my point. I like to speak with precision. :)
but it has always been approximately a superset,

....ish. Whenever I call malloc, for example, I am writing a C program that
is not a legal C++ program. I could make it a C++ program, but only by
damaging the C code. As written, it is not C++. And quite a few of my
programs call malloc.
 

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,880
Messages
2,569,944
Members
46,246
Latest member
RosalieMar

Latest Threads

Top