Just starting out in C

R

Robert Miles

I'm just starting out learning C, but using Windows Vista instead of a
more C-oriented operating system.

So far I've found this on-line tutorial, which looks like a good brief
introduction:

http://www.physics.drexel.edu/courses/Comp_Phys/General/C_basics/c_tutorial.html

I've found that Vista does not include gcc, but Cygwin, which I've
installed for other reasons, does.

I've finished creating the source file, hello.c, with my favorite
editor, which does not run under Cygwin.

An attempt to compile it gave an error that gcc could not find the
source file.

What do I need to do to make gcc able to find that source file?
 
G

Geoff

I'm just starting out learning C, but using Windows Vista instead of a
more C-oriented operating system.

If you're going to follow a tutorial that's based on Linux and GCC you
would have been better off with a Linux operating system like Ubuntu.
So far I've found this on-line tutorial, which looks like a good brief
introduction:

http://www.physics.drexel.edu/courses/Comp_Phys/General/C_basics/c_tutorial.html

I've found that Vista does not include gcc, but Cygwin, which I've
installed for other reasons, does.

I've finished creating the source file, hello.c, with my favorite
editor, which does not run under Cygwin.

An attempt to compile it gave an error that gcc could not find the
source file.

What do I need to do to make gcc able to find that source file?

Change to the directory where you stored the source file, probably "My
Documents" and invoke the compiler from there.
 
M

Mickey Mouse

I'm just starting out learning C, but using Windows Vista instead of a
more C-oriented operating system.

So far I've found this on-line tutorial, which looks like a good brief
introduction:

http://www.physics.drexel.edu/courses/Comp_Phys/General/C_basics/c_tutorial.html

I've found that Vista does not include gcc, but Cygwin, which I've
installed for other reasons, does.

I've finished creating the source file, hello.c, with my favorite
editor, which does not run under Cygwin.

An attempt to compile it gave an error that gcc could not find the
source file.

What do I need to do to make gcc able to find that source file?

There is codeblocks IDE that intergrates with cygwin. If you intend ro
program for just Windows then mingw would probably be better for you.

Codeblocks and mingw can be downloaded as a single package from here.
http://sourceforge.net/projects/cod...10.05/Windows/codeblocks-10.05mingw-setup.exe
or here
http://prdownload.berlios.de/codeblocks/codeblocks-10.05mingw-setup.exe

Codeblocks without mingw.

http://sourceforge.net/projects/codeblocks/files/Binaries/10.05/Windows/codeblocks-10.05-setup.exe
or
http://prdownload.berlios.de/codeblocks/codeblocks-10.05-setup.exe
 
K

Keith Thompson

Geoff said:
If you're going to follow a tutorial that's based on Linux and GCC you
would have been better off with a Linux operating system like Ubuntu.

Probably, but Cygwin provides a fairly decent Linux-like environment,
and if you need MS Windows for other reasons ...

[...]
 
K

Keith Thompson

Robert Miles said:
I'm just starting out learning C, but using Windows Vista instead of a
more C-oriented operating system.

So far I've found this on-line tutorial, which looks like a good brief
introduction:

http://www.physics.drexel.edu/courses/Comp_Phys/General/C_basics/c_tutorial.html

I've found that Vista does not include gcc, but Cygwin, which I've
installed for other reasons, does.

I've finished creating the source file, hello.c, with my favorite
editor, which does not run under Cygwin.

An attempt to compile it gave an error that gcc could not find the
source file.

What do I need to do to make gcc able to find that source file?

You need to tell gcc where to put it.

When you save hello.c from your editor, save it to the directory
where you're going to run gcc. Either that, or give gcc the path
to the file (gcc -c /where/you/saved/hello.c). (Translating between
Windows paths and Cygwin paths is beyond the scope of this newsgroup;
if you're using Cygwin, I presume you know about it. If not,
there's documetation at cygwin.com.)
 
K

Keith Thompson

Robert Miles said:
I'm just starting out learning C, but using Windows Vista instead of a
more C-oriented operating system.

So far I've found this on-line tutorial, which looks like a good brief
introduction:

http://www.physics.drexel.edu/courses/Comp_Phys/General/C_basics/c_tutorial.html
[...]

I just took a very quick look at that tutorial. I can't tell yet
whether the tutorial as a whole is any good, but the things it gets
wrong in the first few paragraphs do not make me optimistic.

Some things it gets wrong:

ANSI issued the first C standard in 1989, not 1988. K&R's 1998 book
(see below) was based on the ANSI standard as it was being developed.

"#include < stdio.h>" is wrong; there's no space after the '<'.
That's probably an HTML markup issue. You'll note that if you
copy and paste one of the examples into a C source file, it won't
compile until you delete the extra space.

"void main()" is wrong, and I'd like to know where the &*^&
the authors got the idea that it's correct; K&R2 certainly
never suggests it. The correct declaration is "int main(void)";
"int main()" is probably acceptable. Many compilers will accept
"void main()" without complaint, but it's particularly important
in C to understand that just because something appears to work,
that doesn't mean it's correct.

printf("\nHello World\n") prints an extra blank line before the output.
Harmless, I suppose, but why?

"This creates an executable file a.out, which is then executed simply by
typing its name." No, it doesn't, unless you have '.' in your $PATH,
which you shouldn't. Type "./a.out". (That's a Unix issue, not a C
issue.)

Skipping to the end, you should be aware that the dialog box routines in
the final example are not standard; they appear to be something created
for the course.

And so on. The fact that the authors recommend K&R2 is a point in their
favor. Please consider skipping the tutorial and reading K&R2
(Kernighan & Ritchie, "The C Programming Language", 2nd edition, 1988).
 
B

BruceS

it's particularly important
in C to understand that just because something appears to work,
that doesn't mean it's correct.
<snip>

Robert, given that you're just getting started in C, the above is
(IMO) the single most important thing to keep in mind. At my first C
job, I learned the expression "works by accident", to explain why
things that are incorrect may appear to work correctly here, now.
Tomorrow, or on another platform, they may behave quite differently.
HTH.
 
S

Stefan Ram

Keith Thompson said:
printf("\nHello World\n") prints an extra blank line before the output.
Harmless, I suppose, but why?

We need to know the requirements specification to judge this.
If the requirements specification is:

»Write an expression whose evaluation will emit a
newline character, then the text "Hello World" and
finally another newline character«,

then this expression fits the requirements specification.

From what I read here in the newsgroup, I cannot say that
it prints a /blank line/, because something might already
have been written out by preceding evaluations to the line
or [when stdout is being redirected to a file] even by
other preceding processes.

One can assume that a C program always starts at the begin
of a line when stdout was not redirected and represents a
console. But I do not know whether ISO/IEC 9899:1999 (E)
guarantees this. It might be a property of the shell program
used. So, if a program wants to be sure to start at a new
line, it might emit a new-line character.
 
J

John Bode

Robert Miles said:
I'm just starting out learning C, but using Windows Vista instead of a
more C-oriented operating system.
So far I've found this on-line tutorial, which looks like a good brief
introduction:

[...]

I just took a very quick look at that tutorial.  I can't tell yet
whether the tutorial as a whole is any good, but the things it gets
wrong in the first few paragraphs do not make me optimistic.

Some things it gets wrong:

ANSI issued the first C standard in 1989, not 1988.  K&R's 1998 book
(see below) was based on the ANSI standard as it was being developed.

"#include < stdio.h>" is wrong; there's no space after the '<'.
That's probably an HTML markup issue.  You'll note that if you
copy and paste one of the examples into a C source file, it won't
compile until you delete the extra space.

"void main()" is wrong, and I'd like to know where the &*^&
the authors got the idea that it's correct; K&R2 certainly
never suggests it.  The correct declaration is "int main(void)";
"int main()" is probably acceptable.  Many compilers will accept
"void main()" without complaint, but it's particularly important
in C to understand that just because something appears to work,
that doesn't mean it's correct.

To expand on this a bit, there is a standards document that defines
the C programming language (an online version can be found at
http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf). There's a
section that defines the allowable signatures for main in a hosted
implementation (such as gcc running under Cygwin), and the choices
are:

int main(int argc, char **argv) /* or char *argv[], the syntax is
equivalent */

or

int main(void) /* the "void" indicates that main takes no
arguments */

or *in some other implementation-defined manner*, meaning that the
implementation has to document it as a legal signature. Unless the
gcc documentation *explicitly* says that "void main()" is kosher (and
I'm pretty sure it doesn't), you shouldn't use it.

To be safe, every time you see "void main()" in the tutorial (at least
until they get up to command line arguments), replace it with "int
main(void)".

Why does this matter? On most modern desktop systems, a program with
main() typed void may run to completion with no *apparent* ill effects
(which is why people still use it), but there are some oddball systems
where the program will crash on exit, or may not even load at all. It
all comes down to a mismatch between what the runtime environment
expects main() to look like, and what the compiler produces.

Skimming through the rest of the tutorial, I don't see any *obvious*
howlers. Their description of array declaration syntax is simplistic
to the point of being wrong, but a proper description of declarators
would take up a chapter by itself. Likewise, their description of the
correspondence between arrays and pointers is similarly simplistic.

Some words about gcc:

Always add the -Wall and -Werror command line options when you invoke
gcc: -Wall enables all warnings, and -Werror treats all warnings as
errors, so you basically have to address all the problems that crop
up.

gcc provides some extensions to the language that aren't supported by
other compilers, and some of these extensions may conflict with a
particular version of the language standard. Use -ansi or -std=c89 to
force gcc to recognize only code that's legal under the 1989 standard,
or -std=c99 to use the additions from the 1999 standard. You don't
want to get into the habit of using compiler-specific extensions when
you don't really *have* to.

For example, to compile the Hello, World program, use the command line

gcc -o hello hello.c -ansi -pedantic -Wall -Werror

The "-o hello" tells gcc to write the executable to a file named
"hello" instead of "a.out".
 
K

Keith Thompson

John Bode said:
"void main()" is wrong, and I'd like to know where the &*^&
the authors got the idea that it's correct; K&R2 certainly
never suggests it.  The correct declaration is "int main(void)";
"int main()" is probably acceptable.  Many compilers will accept
"void main()" without complaint, but it's particularly important
in C to understand that just because something appears to work,
that doesn't mean it's correct.

To expand on this a bit, there is a standards document that defines
the C programming language (an online version can be found at
http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf). There's a
section that defines the allowable signatures for main in a hosted
implementation (such as gcc running under Cygwin), and the choices
are:

int main(int argc, char **argv) /* or char *argv[], the syntax is
equivalent */

or

int main(void) /* the "void" indicates that main takes no
arguments */

or *in some other implementation-defined manner*, meaning that the
implementation has to document it as a legal signature. Unless the
gcc documentation *explicitly* says that "void main()" is kosher (and
I'm pretty sure it doesn't), you shouldn't use it.

There appears to be no occurrence of "void main" in the gcc
documentation.

But even if a compiler does document "void main()" (or "void
main(void)") as a valid declaration for main, there's still no good
reason to use it, and some very good reasons not to. In such an
implementation, it's likely that this:
void main(void) { }
is equivalent to this:
int main(void) { return 0; }
(the return statement can be omitted in C99). So just write it
that way! Using the implementation-defined form gains you exactly
nothing, and costs you portability to implementations that don't
support it.

Note that even if your program appears to work, it might fail in
some subtle way. And even if it always works (on an implementation
that supports void main() but doesn't document it), you'll always
have to wonder, every time your program malfunctions, whether the
non-portable declaration of main might be the culprit.

Note that all of this applies *only* to hosted implementations.
Freestanding implementations (basically implementations where
the program doesn't necessarily run under an operating system)
can place their own requirements on your program's entry point.
On some such systems, "void main(void)" might be the *only* valid
way to declare main -- or the entry point might have a different
name altogether. But if you're just learning C, you're probably
not using a freestanding implementation.
To be safe, every time you see "void main()" in the tutorial (at least
until they get up to command line arguments), replace it with "int
main(void)".

To be even safer, every time you see "void main()" in a tutorial,
find yourself a different tutorial. It's not necessarily a fatal
error by itself, but it tends to be indicative of a general ignorance
about C, particularly about the distinction between C the language
and particular implementations of it.

[...]
 

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,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top