[1+1=2]
OneAndOneIs2

Tue, Apr 22, 2008

[Link][Icon]Back into K&R

Right. It's been ages since I tried learning any C, so I'm scanning back through the K&R pages I already did as a refresher. I'm jotting down the things to remember as I do so in this post, so it probably won't be all that interesting, but what the Hell, it's my blog [Smiley]

Right.

The obligatory start:
#include <stdio.h> This is an instruction to the compiler to read the header file stdio.h, in which functions are declared so you don't have to redundantly declare them in every program you ever use.

int main() Every program has to have at least one function, the main() function. The 'int' tells the program that the function should return an integer. It isn't really necessary for main() to do this, but what the hell.

{ Brackets are used to define the start and finish of a function.

printf("Hello, world\n"); Printf() is a function, declared in stdio.h. "Hello, world\n" is the string you want it to print, with \n being the carriage-return character. The semi-colon at the end tells the compiler where the statement ends: Unlike, say, python, carriage returns don't signify a new statement in C

return 0; This is where we tell the function to return an integer, as we already defined main() as an integer-returning function. It doesn't have to be 0, it just has to be a number.
} And of course, we have to close brackets after we open them...

Well, that was fun, wasn't it? :o)

2 comments • Categories: Omni, My Life, Programming

Comments:

Comment from: KTC [Visitor] Email
main() in C *has* to return an int.

The return value can be any int value, but 0 denote successful termination of the program.

:)
PermalinkPermalink 22/04/08 @ 19:00
Comment from: hari [Member] Email · http://hari.literaryforums.org
Yes, KTC is quite right. The return value of main () is actually the status code of the program which tells the caller (or the OS) whether the program completed normally or abnormally.
PermalinkPermalink 25/04/08 @ 12:26

Leave a comment:

Your email address will not be displayed on this site.
Your URL will be displayed.

Allowed XHTML tags: <p, ul, ol, li, dl, dt, dd, address, blockquote, ins, del, span, bdo, br, em, strong, dfn, code, samp, kdb, var, cite, abbr, acronym, q, sub, sup, tt, i, b, big, small>
(Line breaks become <br />)
(Set cookies for name, email and url)
(Allow users to contact you through a message form (your email will NOT be displayed.))

Categories

July 2008
Mon Tue Wed Thu Fri Sat Sun
 << <   > >>
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      

Search

Misc

XML Feeds

What is this?
eXTReMe Tracker

Valid XHTML 1.0 Transitional

Valid CSS!

[Valid RSS feed]

powered by
b2evolution

blank