Tuesday 15 March 2011

Back to basics

So I've been getting some comments and messages about my posts being too advanced, So I've decided that it would probably make more sense to start C from the beginning. In this post, I'll show you how to write a basic "Hello World!" program and explain the ins and outs of it. Some things will just be touched on right now and more fully explained in later posts.

#include<stdio.h>


int main()
{
    printf("Hello World!");
    return 0;
}

To fully understand how the program works, I'll explain its components.


#include<stdio.h>

#include is a macro. This particular macro goes and finds a file on your computer, in this case stdio.h. This file is what we call a header file. Basically, this file contains a bunch of function definitions (again, more on this later). The #include macro tells the program to go look in the folder on your computer containing your standard C library header files and to find stdio.h. When you hit the "build" button, the compiler will take your source code and compile it into machine code, but even before that, something called the preprocessor (we'll mention this again much later) will physically copy all the text from stdio.h into your program. Now when your program gets compiled, the compiler sees all this code in your program, so it actually becomes a part of your program.

int main()
{


}

Functions are very important in any programming language. Every program in C must have a main function. Everything contained within the curly brackets will be code executed during runtime.

printf("Hello World");

This is a call the function which prints to the screen. This printf function is defined within the stdio.h file. Having included that file, we are able to call this function. Here, we can see that the text we are printing to the screen is "Hello World".

return 0;

To indicate to the operating system that the program has terminated successfully (ie. No runtime errors occurred/The program did not crash/etc) we return 0 at the end of out main. This signifies the end of our program.

I hope this has been useful to those not yet fully comfortable with C.

20 comments:

  1. I wish i would have taken programmin this year.

    ReplyDelete
  2. Can program good enough in C, cant do squat in C++ :(

    ReplyDelete
  3. iT ALL LOOKS LIKE GREEK TO ME. nICE TO SEE SOMEONE SO PASSONITE ABOUT IT THOUGH. GREAT WORK KEEP IT UP. (CAP LOCK SORRY)

    ReplyDelete
  4. good info, someday i may start programing

    ReplyDelete
  5. Man, I'm so dang glad I was shown this blog! I wanted to get into C AGES ago, but never did. Maybe you'll inspire me to get back into it.

    ReplyDelete
  6. Have you considered making indie games for the xbox?

    ReplyDelete
  7. Im gonna be starting programming courses for my major this fall! This kinda information is exactly what ill need.

    ReplyDelete
  8. awesome love programing so this could be handy since i just know c# and java.

    ReplyDelete
  9. How similar is C to C++, as i kinda know it, and is there any specific uses for C over C++ or C#?

    ReplyDelete
  10. I've been looking to get into C++ for a long while now, maybe I can use your blog to get off the ground :). Great post!

    ReplyDelete
  11. Thx man I use programing a lot too ! :)

    ReplyDelete
  12. you should post some more complex stuff, those are far too easy.

    ReplyDelete
  13. Yeah, even for basic beginners, these are a bit easy, I agree with PenDRaGoN.

    You explained it very well though =]

    ReplyDelete
  14. I think stewenewe said it best, "I don't know anything about this" ready to learn.

    ReplyDelete
  15. Haha,C? I hardly know HTML. xD

    ReplyDelete
  16. oh god. still difficult but it helped like crazy. im not a big with the whole mathamatics (bio major) BUT i got this wayyyy more than your other posts. much obliged

    ReplyDelete