HomePortalFAQSearchMemberlistUsergroupsRegisterLog in

Share | 
 

 Step-by-step first C program

Go down 
AuthorMessage
Excimer Sun Software

avatar

Posts : 152
Join date : 2010-05-07
Age : 25
Location : USA

PostSubject: Step-by-step first C program   Sun Aug 15, 2010 8:00 pm

Hello all! Excimer Sun here! Today I've decided to write about how to complete your first program in C! I will be teaching you the classic first program, the hello world. First, to get started, I'm going to show you the entire program Shocked, don't be frightened by this. I will go through it and explain it line by line. Your first C program will look something like this:

Code:
#include <stdio.h>
int main() {
    printf("Hello World!");
    scanf();
}

There! Not too bad, eh? Here's the line-by-line:

1.)
Code:
#include <stdio.h>
What this does is tell the program to treat the functions and variables declared in a particular file as though they were part of itself. This must always start with # and can only include either a .c file which has no main (we'll get to that in a sec) or a .h file, aka a header, which is a file that defines functions and variables and does certain work for the program that includes it. The file name has to be enclosed in either angle brackets or double quotes. For standard .h files, you will typically use angle brackets and for non-standard .h files and for .c files, you will typically use quotes. With this one, we tell it to include a standard .h, stdio.h, which contains the function that is going to allow us to print text to the screen, and another function that we'll use at the end to pause the program, so that the printed text can be read.

2.)
Code:
int main() {
This line declares the beginning of your program's main, which is where the main body of your code will go. This can start with either void or int. For our purposes, it will typically be int. The word 'main' must always be followed by an opening parenthesis which can, optionally, be followed by a list of arguments (don't worry about this for now) and then must have a matching end parenthesis. Then follows either a space or a newline and an opening curly brace. This curly brace tells the computer that everything that comes after it, untill the closing curly, belongs to the main(). This is very important!

3.)
Code:
    printf("Hello World!");
This is the meat of your program. This function tells the computer to print whatever is between the parenthesis of the function to the computer screen. This is one of the most basic C functions. The 'printf' must be followed by an opening parenthesis that has a matching closing parenthesis. Between the parenthesis goes the stuff to be output to the screen, usually a string of text or a variable. Here we want to tell the world hello, so we place, "Hello World!", between the parenthesis. This is a string. A string is a series of text characters, usually printed on the screen. The string, Hello World!, is placed within quotes to tell the computer that this is a string, otherwise it will think that you are trying to print a variable and it will complain. A string must be enclosed in quotes, single or double, to be properly interpreted. Also, at the end of this line you will notice, and probably be wondering about, the semicolon. Almost all lines of code in C must be ended with a semicolon to denote the end of a line. The only major exceptions are lines that begin with '#' and commands such as 'if' that use curly brackets to denote membership (this includes main declarations) and following closing curly braces.

4.)
Code:
    scanf();
This command is an input function, which tells the program to pause and wait untill the 'Enter' key is pressed. This is so that the program displays your text long enough for it to be read and untill you are ready for it to be done. Once Enter is pressed, the program will move on. This function can also accept keyboard text input, but this is for another tutorial.

5.)
Code:
}
Finally! The end! Here the extra line is unnecessary, the curly could follow right after the printf statement, it's just a separate line for clarity. This curly tells the computer that this is the end of the main. This also happens to be the end of our little program. This curly, while small, is absolutely essential. If you were to leave the main open, your program would not function properly. In fact, it wouldn't compile so that you could try to run it. If you don't include this one, lonely character, your computer/compiler will whine and complain about it, and that's never pleasant. Smile Just add it. It's only one character but it will save you a little grief.

There! That wasn't that bad, was it? I apologize for the long-windedness, but I wouldn't have felt good about just giving you the code and not teaching you what it meant. And trust me, it's much better to know why you're doing it and not just be doing it 'just because ESS said so'.Smile If anyone has any questions, comments, constructive criticism, or discrepancies in the tutorial to point out, feel free to post them below.

Regards,
ESS


Last edited by Excimer Sun Software on Wed Oct 13, 2010 9:36 pm; edited 1 time in total
Back to top Go down
View user profile
Compu-Teck Inc.



Posts : 80
Join date : 2010-04-29
Age : 25

PostSubject: Re: Step-by-step first C program   Mon Aug 23, 2010 1:39 pm

You did get a little long-winded there, but it was all for the sake of making programming easier. I think I will do this with my future Python tutorials. xD
Back to top Go down
View user profile
Excimer Sun Software

avatar

Posts : 152
Join date : 2010-05-07
Age : 25
Location : USA

PostSubject: Re: Step-by-step first C program   Mon Aug 23, 2010 6:41 pm

Yeah, it can be a long-winded way of doing tutorials, but I feel that, with such things, it is important to thouroghly explain the content in a clear, concise, easily-understandable format.Smile How'd I do on that, Compu-Teck?
Back to top Go down
View user profile
Compu-Teck Inc.



Posts : 80
Join date : 2010-04-29
Age : 25

PostSubject: Re: Step-by-step first C program   Tue Aug 24, 2010 8:57 am

You did alright, ESS. I did not do that with my first Python program. Maybe I can remedy that, you know for kinda the same reason. I don't want people asking how this works to one of our members, and having them reply: "... because CTI said so...", ya know...
Back to top Go down
View user profile
Excimer Sun Software

avatar

Posts : 152
Join date : 2010-05-07
Age : 25
Location : USA

PostSubject: Re: Step-by-step first C program   Tue Aug 24, 2010 10:15 pm

Oh yeah, I know.Smile "because so-and-so said so" is one of the most common, and awful, reasons/excuses in all walks of life! As Admins, it is our responsibility (everyone's, really) to help keep this awful phrase at bay!Very Happy
Back to top Go down
View user profile
Compu-Teck Inc.



Posts : 80
Join date : 2010-04-29
Age : 25

PostSubject: Re: Step-by-step first C program   Wed Aug 25, 2010 8:53 am

Amen to that, 'mano!
Back to top Go down
View user profile
Sponsored content




PostSubject: Re: Step-by-step first C program   

Back to top Go down
 
Step-by-step first C program
Back to top 
Page 1 of 1

Permissions in this forum:You cannot reply to topics in this forum
Compile Crazy :: Programming :: C Family :: C :: For Beginners-
Jump to: