A question

B

bob

I've read (almost) all the C tutorials online. I understand everything
in the tutorials, but I have no clue how to do anything practical. For
instance, I can't understand any code anyone else has written. Help,
anyone?
 
R

Richard Heathfield

bob said:
I've read (almost) all the C tutorials online. I understand everything
in the tutorials, but I have no clue how to do anything practical. For
instance, I can't understand any code anyone else has written. Help,
anyone?

Unfortunately, most people don't write very readable code. I suggest you do
the following:

(a) swear a solemn swear /never/ to write unreadable code yourself;
(b) have a go at solving the following exercise:

Exercise BOB1: Write a C program to display the words "Hello, world!" on the
standard output device.

(c) post your solution here, inviting comments.

The exercise may or may not sound trivial, but I invite you to have a go
anyway. You may be surprised at just how much juice can be squeezed from
such a simple program (depending largely on how good your tutorials were).

Once that's all done, we'll know better how to advise you.
 
N

Nick Vatamaniuc

Bob,

Only reading the tutorials is not very much help. You need to start
coding yourself. The first code that you write will probably be code
from the tutorials. The only way to go about using a computer language
tutorial is to write and execute the code as you go along. The phrase
'no pain, no gain' comes to mind. Find yourself a good C book (look at
reviews online to find the best ones) that has exercises after each
chapter then try to solve those exercises as you go along.

Hope this helps,
Nick Vatamaniuc
 
J

John Bode

bob said:
I've read (almost) all the C tutorials online. I understand everything
in the tutorials, but I have no clue how to do anything practical. For
instance, I can't understand any code anyone else has written. Help,
anyone?

Programming is a skill, and like all skills it requires a non-trivial
amount of practice to master. The only way to understand other
people's code is to write a lot of code yourself; then you begin to
understand why other people did what they did.
 
A

Allan M. Bruce

John Bode said:
Programming is a skill, and like all skills it requires a non-trivial
amount of practice to master. The only way to understand other
people's code is to write a lot of code yourself; then you begin to
understand why other people did what they did.

You could try writing a small calculator offering certain mathematical
functions - this will get you used to using functions, getting user input
and displaying some output.

You could try writing a linked list to see well you cope with pointers, this
list can then be used to create a queue and stack which have different
behaviours.

Once you are happy with this, I suggest you write something you are
interested in, for example I wrote a small game, HexMine, then a fractal
generator. Find something you like thats doable and get your hands dirty
but remember to keep your code neat and well documented.

Allan
 
A

Allan M. Bruce

bob said:
I've read (almost) all the C tutorials online. I understand everything
in the tutorials, but I have no clue how to do anything practical. For
instance, I can't understand any code anyone else has written. Help,
anyone?

You could try writing a small calculator offering certain mathematical
functions - this will get you used to using functions, getting user input
and displaying some output.

You could try writing a linked list to see well you cope with pointers, this
list can then be used to create a queue and stack which have different
behaviours.

Once you are happy with this, I suggest you write something you are
interested in, for example I wrote a small game, HexMine, then a fractal
generator. Find something you like thats doable and get your hands dirty
but remember to keep your code neat and well documented.

Allan
 
C

Christopher Benson-Manica

bob said:
I've read (almost) all the C tutorials online.

That actually may have been a bad idea, as the quality of such
tutorials varies. An excellent online resource that you should avail
yourself of, if you have not yet done so, is the FAQ for this
newsgroup:

http://c-faq.com

Also consider buying, borrowing, or otherwise procuring "The C
Programming Language" by Kernighan and Ritchie. It is full of sample
problems to test and improve your skills, not to mention authoritative
information concerning the language.
 
M

Matt Junx

bob said:
I've read (almost) all the C tutorials online. I understand everything
in the tutorials, but I have no clue how to do anything practical. For
instance, I can't understand any code anyone else has written. Help,
anyone?
I'd recommend continuing by learning more about general computer science
concepts first. You should read more about data structures, algorithms,
and if it fancies you, compression and cryptology.
 
B

bob

Richard said:
bob said:


Unfortunately, most people don't write very readable code. I suggest you do
the following:

(a) swear a solemn swear /never/ to write unreadable code yourself;
(b) have a go at solving the following exercise:

Exercise BOB1: Write a C program to display the words "Hello, world!" on the
standard output device.

(c) post your solution here, inviting comments.

The exercise may or may not sound trivial, but I invite you to have a go
anyway. You may be surprised at just how much juice can be squeezed from
such a simple program (depending largely on how good your tutorials were).

Once that's all done, we'll know better how to advise you.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)

Thanks for all the replies. Here's the Hello World program you asked
for:

#include <stdio.h>
main()
{
printf("Hello, world!");
return 0;
}
 
T

Tom St Denis

bob said:
#include <stdio.h>
main()

int main(void)

You must specify a return type, it must be int.

You must specify arguments (or void).
{
printf("Hello, world!");

You need a newline if you plan to print out text and not have the shell
prompt at the end of that string.
return 0;

Technically you should #include stdlib.h and return EXIT_SUCCESS which
is the official "all clear" return value.

Isn't C fun :)

Tom
 
M

Michael Mair

bob said:
Thanks for all the replies. Here's the Hello World program you asked
for:

#include <stdio.h>
main()

main() returns int and takes in this case an empty argument list.
int main (void)

The necessity of the "void" is open to debate but says exactly
what you want.
Relying on "implicit int" for the function return type is a bad
habit and takes away the chance of easy transition to C99 (where
implicit int no longer applies).
Your compiler should warn you about that. If it didn't, then turn
up the warning level.
{
printf("Hello, world!");

You forgot the '\n' at the end of the format string.
This can lead to your not actually seeing the output or only
part of it.
printf("Hello, world!\n");
or
puts("Hello, world!");
are better.
return 0;
}


Cheers
Michael
 
K

Keith Thompson

Tom St Denis said:
int main(void)

You must specify a return type, it must be int.

You certainly should, but it isn't actually required in C90.
You must specify arguments (or void).

In a function *definition* (as opposed to a declaration), empty
parentheses mean that the function takes no arguments (though there's
been some debate about this). Specifying void is certainly better
style.

To summarize: "main()" is at best of questionable style and legality,
whereas "int main(void)" is definitely correct. If you use
"int main(void)", you won't have to put up with silly debates like
this one.
You need a newline if you plan to print out text and not have the shell
prompt at the end of that string.

Yes. Leaving off the newline raises some more questions for us
standards geeks; be sure to include it, and you won't have to worry
about it. (Tom: see C99 7.19.2p2; IMHO, leaving off the newline
invokes undefined behavior, though in practice I'd be very surprised
by any consequence worse than losing the last (partial) line of
output.)
Technically you should #include stdlib.h and return EXIT_SUCCESS which
is the official "all clear" return value.

No, "return 0;" is perfectly valid. Returning either EXIT_SUCCESS or
0 indicates successful termination of the program. Returning
EXIT_FAILURE indicates failure. Any other values are
implementation-defined. (In particular, you may see "return 1;" or
"exit(1);" in some programs; this is intended to indicate an error,
but it doesn't do so portably.)
Isn't C fun :)

Indeed.

bob: If this stuff is over your head, don't worry about it. I'll post
a more reasonable response directly to you original aricle
(geek-to-newbie rather than geek-to-geek).

bob and Tom: Just to be clear, I'm using the term "geek" in a jocular
sense; no offense is intended.
 
K

Keith Thompson

bob said:
Thanks for all the replies. Here's the Hello World program you asked
for:

#include <stdio.h>
main()
{
printf("Hello, world!");
return 0;
}

As promised, here's my newbie-level critique.

"main()" should be "int main(void)". "main()" is likely to work, but
it's better to state explicitly that your main function takes no
arguments and returns a result of type int.

You need a newline at the end of your output:

printf("Hello, world!\n");

And proper indentation is important (the compiler doesn't care, but
anyone reading your code will). There's a lot of debate about
indentation style and placement of braces; consistency is more
important than picking the "right" style. (You can't go too far wrong
copying the style in K&R2, but if you're working on existing code you
should follow its style.)

The "return 0;" is fine. (If you wanted to terminate the program from
within some function other than main, you'd need to call exit(), but
you needn't worry about that for now.) (There are circumstances in
which the "return 0;" cam be omitted, but even then it's harmless, so
it's a good idea to always use it.)

Applying these suggestions, we get:

#include <stdio.h>
int main(void)
{
printf("Hello, world!\n");
return 0;
}
 
B

boa

* Keith Thompson wrote, On 24.07.2006 21:10:
Tom St Denis said:
bob wrote:
[snip]
You need a newline if you plan to print out text and not have the shell
prompt at the end of that string.

Yes. Leaving off the newline raises some more questions for us
standards geeks; be sure to include it, and you won't have to worry
about it. (Tom: see C99 7.19.2p2; IMHO, leaving off the newline
invokes undefined behavior,

ITYM IB, not UB. From C99 7.19.2p2
2 A text stream is an ordered sequence of characters composed into lines, each line
consisting of zero or more characters plus a terminating new-line character. Whether the
last line requires a terminating new-line character is implementation-defined.


boa

[snip]
 
K

Keith Thompson

boa said:
* Keith Thompson wrote, On 24.07.2006 21:10:
Tom St Denis said:
bob wrote:
[snip]
{
printf("Hello, world!");
You need a newline if you plan to print out text and not have the shell
prompt at the end of that string.
Yes. Leaving off the newline raises some more questions for us
standards geeks; be sure to include it, and you won't have to worry
about it. (Tom: see C99 7.19.2p2; IMHO, leaving off the newline
invokes undefined behavior,

ITYM IB, not UB. From C99 7.19.2p2
2 A text stream is an ordered sequence of characters composed into
lines, each line consisting of zero or more characters plus a
terminating new-line character. Whether the last line requires a
terminating new-line character is implementation-defined.

N, IM UB, not IB.

(*ahem*)

No, I mean undefined behavior, not implementation-defined behavior.

The implementation has to define whether a terminating new-line
character is required. If the implementation says it's required, I
see nothing in the standard that says the implementation has to define
its behavior when the new-line is missing.

Possibly this should be tightened up a bit; I think there are
reasonable limits to what can actually go wrong if the new-line is
missing. But the standard currently doesn't define the behavior,
which means the behavior is undefined.
 
R

Richard Heathfield

bob said:

Thanks for all the replies. Here's the Hello World program you asked
for:

#include <stdio.h>
main()
{
printf("Hello, world!");
return 0;
}

Well, lots of people have bombarded you with comments about this already, so
I won't add to their number. (See how much juice they squeezed from it?)

I hope the exercise gave you some idea of the sorts of problems that can
arise from an informal approach to the language (which is not to say there
aren't problems with the formal approach too). I must admit you surprised
me by remembering to return 0 from main - well done. :)

Your next task, should you choose to accept it, is to think up your own
project. Let's not make this too complicated - I recommend some kind of
text-filtering program, as these are very easy to do and can be useful
additions to your toolbox. Here are some possibilities:

Exercise BOB2: write a correct C program to display the first n lines of
input. (Easy)

Exercise BOB3: write a correct C program to display the leftmost n
characters of each line of input. (Easy)

Exercise BOB4: write a correct C program to display the *last* n lines of
input. (Harder.)

Exercise BOB5: write a correct C program to display the *rightmost* n
characters of each line of input. (Harder.)

(In all the above exercises, make n configurable via the command line.)

Exercise BOB6: write a correct C program to replace all occurrences in the
input of the string argv[1] with the string argv[2]. (Pretty hard.) Make
sure your user supplies a sufficient number of arguments, and complain if
he doesn't.

I'm not suggesting you write all the above, but try at least some of them.

Look for commonality between your solutions. Are there any parts you could
usefully abstract into a separate library, so that they only need be
written once?
 
J

Jack Klein

boa said:
* Keith Thompson wrote, On 24.07.2006 21:10:
bob wrote: [snip]

{
printf("Hello, world!");
You need a newline if you plan to print out text and not have the shell
prompt at the end of that string.
Yes. Leaving off the newline raises some more questions for us
standards geeks; be sure to include it, and you won't have to worry
about it. (Tom: see C99 7.19.2p2; IMHO, leaving off the newline
invokes undefined behavior,

ITYM IB, not UB. From C99 7.19.2p2
2 A text stream is an ordered sequence of characters composed into
lines, each line consisting of zero or more characters plus a
terminating new-line character. Whether the last line requires a
terminating new-line character is implementation-defined.

N, IM UB, not IB.

(*ahem*)

No, I mean undefined behavior, not implementation-defined behavior.

Then what you mean is incorrect. Undefined behavior, implementation
defined behavior, and unspecified behavior, as used in the C standard,
are mutually exclusive. Something that the standard describes as
"implementation-defined" or "unspecified" may not ever produce
undefined behavior in a conforming implementation.
 
K

Keith Thompson

Jack Klein said:
boa said:
* Keith Thompson wrote, On 24.07.2006 21:10:
bob wrote:
[snip]

{
printf("Hello, world!");
You need a newline if you plan to print out text and not have the shell
prompt at the end of that string.
Yes. Leaving off the newline raises some more questions for us
standards geeks; be sure to include it, and you won't have to worry
about it. (Tom: see C99 7.19.2p2; IMHO, leaving off the newline
invokes undefined behavior,

ITYM IB, not UB. From C99 7.19.2p2

2 A text stream is an ordered sequence of characters composed into
lines, each line consisting of zero or more characters plus a
terminating new-line character. Whether the last line requires a
terminating new-line character is implementation-defined.

N, IM UB, not IB.

(*ahem*)

No, I mean undefined behavior, not implementation-defined behavior.

Then what you mean is incorrect. Undefined behavior, implementation
defined behavior, and unspecified behavior, as used in the C standard,
are mutually exclusive.
Agreed.

Something that the standard describes as
"implementation-defined" or "unspecified" may not ever produce
undefined behavior in a conforming implementation.

I don't think that's correct. C99 7.19.2p2 doesn't use the magic
phrase "implementation-defined behavior", just
"implementation-defined". It requires the implementation to document
whether the new-line is required or not. I don't see a requirement to
document the *behavior* in the case where it's required.

The choice, not the behavior, is implementation-defined. The
behavior, lacking any definition, is undefined.
 
S

Scott W

Christopher Benson-Manica said:
That actually may have been a bad idea, as the quality of such
tutorials varies. An excellent online resource that you should avail
yourself of, if you have not yet done so, is the FAQ for this
newsgroup:

http://c-faq.com

Also consider buying, borrowing, or otherwise procuring "The C
Programming Language" by Kernighan and Ritchie. It is full of sample
problems to test and improve your skills, not to mention authoritative
information concerning the language.

or get C Programming: A Modern Approach by K.N. King. K&R seems to be
geared towards people who can already program, have CS degrees, probably
a good maths understanding etc. but then, if you already have those
backgrounds then you may be ok with K&R ;)
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top