Re: [SLUG] Wanting to learn C - book suggestions?

From: perthie (perthie@yahoo.com)
Date: Fri Mar 11 2005 - 11:02:10 EST


> > main() {prinf("doh!\n")}
>
> Nitpick: Technically that's not correct ANSI C.
>

which standard are you referring to? technically, its correct in c89
and i believe also in c99 (though ive never read it so i dont know).
explainations follow.

> * The return type (int) should be specified.

the return type if unspecified is assumed to be int. its good
programming practice to specify the return type, but its not required
or pedantically incorrect according to ANSI/ISO.

> * printf is not declared.

correct. however it should be noted some compilers will include stdio.h
automagically, namly gcc.

> * printf really shouldn't be used for this, puts should be. Some
> (stupid)
> implementations of varargs don't handle vararg functions with only
> on argument.

this is an implementation issue, not a standards issue. the standard
allows for examples such as these. if you're using a non-compliant
compiler, then you're going to have problems no matter how pedantically
correct you write the code.

> * the semicolon after printf is missing.

yup.

> * There is no return statement. main must return an integer value.
>

also not true according to the standard, however your compiler should
issue a warning (unless of course you treat warnings as errors).

> Though, at least you didn't write "void main".
> That's so completely incorrect, and I can't say how many times I've
> seen people type that on slashdot...
>

and the reason its so popular is because most people dont care what the
standard says, and in many cases, rightfully so. programs are supposed
to solve problems. likewise, if a program solves a problem, then it is
correct for all practical purposes. additionally, few programs are 100%
ansi compliant these days due the the need for implementation specific
extensions and libraries.

code such as the above is only a problem in environments where the code
must be *guarenteed* to work or must be maintainable 10 years after its
written. i sincerly hope the above program needs no maintenance in
2015. :)

> A correct implementation would be:
> #include <stdio.h>
> int main() {
> puts("doh!");
> return 0;
> }
>

or

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

int main(void) {
    puts("doh!");
    return EXIT_SUCCESS;
}

> Sorry for being pedantic and nit-picky, I had to deal with some very
> ugly non-standard C code today. :)

                
__________________________________
Do you Yahoo!?
Yahoo! Small Business - Try our new resources site!
http://smallbusiness.yahoo.com/resources/
-----------------------------------------------------------------------
This list is provided as an unmoderated internet service by Networked
Knowledge Systems (NKS). Views and opinions expressed in messages
posted are those of the author and do not necessarily reflect the
official policy or position of NKS or any of its employees.



This archive was generated by hypermail 2.1.3 : Fri Aug 01 2014 - 20:31:40 EDT