+ Reply to Thread
Page 1 of 4
1 2 3 ... LastLast
Results 1 to 10 of 33

Thread: Getting Started (C Programming)

  1. #1
    Programmer LogicKills has a spectacular aura about LogicKills has a spectacular aura about LogicKills has a spectacular aura about LogicKills's Avatar
    Join Date
    May 2008
    Location
    US
    Posts
    137

    Getting Started (C Programming)

    This tutorial will bring light to the common question:
    "How Do I Get Started?"
    I will outline the basic concepts and what you need to start programming in the C language.

    To start programming in C you need a tool called a compiler. What a compiler does is convert the source code written by the programmer (you) into something the computer can read, in this case an executable file (.exe). Depending on the platform you are going to program on their are various compilers. Another thing you want is an environment to program in, most Windows compilers are placed within an IDE so you don't have to worry. With Linux however you have choices, I use VI or Gedit with syntax highlighting, works fine for me.

    Windows:
    Dev-C++
    Borland
    DJGPP

    Linux
    gcc
    g++

    I will make the code in this tutorial work on Linux and Windows so no need to worry about compatibility issues.

    Your First C Program
    I know it is a tradition to start with "Hello World", however I have a better starter program. Don't worry if you don't know what the code means, I will dissect it later.

    ( I will be using Dev-C++ and Gedit as an example for opening new files in )


    Step One:
    Open a new source file.
    (In Dev-C++: File ---> New ---> Source File || or CTRL + N)
    (Same thing in Gedit)


    Code:
    //FirstProgram.c
    #include <stdio.h>
    
    int main(void)
    {
        
        printf("Hello CodeCall");
        getchar();
        return(0);
    
    }
    Make sure you typed the code, and did not just copy and paste.
    After typing the code save the file as "FirstProgram.c".
    Next we need to compile the program I will show how to do this with Dev and then with gcc.

    Dev-C++
    Save: File ----> Save As || or CTRL + F12
    Compile: Execute ----> Compile || or CTRL + F9
    Run: Execute ----> Run || or CTRL + F10

    -----
    Gedit w/ GCC
    Save: File ----> Save || or CTRL + S
    Compile: gcc FirstProgram.c -o FirstProgram (note: In terminal in correct dir)
    Run: ./ FirstProgram (note: In terminal in correct dir)

    ------

    If your program compiled and ran, congratulations! You are an official C programmer! This make look too simple now, however rest assured all programmers started like this. Soon you will be on your way to writing low-level system drivers, and maybe some other fun system software :]
    If your program did not compile and(or) run make sure everything was copied and exactly right. On linux make sure you were in the correct directory, to make sure try: the command "ls" and look for your program. Next lets tear apart this program and start learning some basic concepts of C.

    ------

    The Dissect

    We will dissect the program line by line.

    //FirstProgram.c
    This line is a comment, a comment is used for describing an aspect of your program, listing the license you used and(or) contact info, version info, or maybe just some quick notes to remember what a specific line does so you don't forget down the line. In this example, I used a comment to tell the the file name was. Remember, don't overdue the comments too many will do the exact opposite of what they are intended. Two types of comments are available.
    The multi-line C style comment: /* Comment Goes Between These */
    Or the C++ single line style comment: //This is a single line comment


    #include <stdio.h>
    This is an include file. This line basically tells your compiler to add the contents of it to your file when it is compiling. The include file (or more popularly named "Header File" ) is just one of many that are included with your compiler. All of these files normally have a .h extension and are stored in a different file away from your source code. stdio.h is one of the most basic of header files and is almost always needed. It stands for Standard Input Output. Many types of header files exist and they support many different functions. When you get good enough you can even make your own.


    int main(void)
    This is the main function, it is requirement in EVERY C program. The main functions always has to be called. Everything between the { and } make up the main body of the program.


    { and }
    These are code braces, these make up code blocks. All you really need to know is that code that is between { and } executes together.


    printf("Hello CodeCall");
    printf() is the standard output function. It can display simple text messages, or even variables.In this example it outputs the text "Hello Codecall". The "\n" is a newline tab this is really for aesthetics. Notice that this function ends with a semicolon ";". Every statement in in a C program ends with a semicolon.

    getchar();
    getchar(); is a function that waits for user input. A common problem with console applications on the windows platform is they do exactly what they are told to, except they do it too fast. If you are on windows take this line out, compile and run and see what happens. Their are other uses for this function, however I just used it here to stop the program from executing to fast.

    return(0);
    This is a return statement. In this program it returns a value of 0. This means the main function ran as expected with out any errors. Return values are important in other areas. The best example would be user defined function, more on that in the next tutorial.

    ------

    Well that is the the dissection of the program line by line. This tutorial is aimed at complete beginners, remember it was made just to get your feet wet, and give you a small taste of the C language. If I would have outlined fundamentals in this tutorial it would have been way too long. Instead I will be making tutorials on every concept of the C language. I hope this tutorial helped some, and again it was meant to be basic. Expect more tutorials soon :]

    /*LogicKills*/
    http://logickills.org
    Science - Math - Hacking - Tech

  2. #2
    Retired v0id is a glorious beacon of light v0id is a glorious beacon of light v0id is a glorious beacon of light v0id is a glorious beacon of light v0id is a glorious beacon of light v0id is a glorious beacon of light
    Join Date
    Apr 2007
    Posts
    2,978
    Blog Entries
    3

    Re: Getting Started (C Programming)

    I've some few things I find necessary to comment on...

    Quote Originally Posted by LogicKills View Post
    Windows:
    Dev-C++
    Borland
    DJGPP
    I wouldn't recommend Borland C++. Its development has stopped, and its latest release was in 1997 (or 1998?), so I think we can consider it as outdated. DJGPP is probably a nice tool to use, but IMO is MingW a better alternative, which is also a port of GCC (like DJGPP is). I do agree on Dev-C++, and that it's a great tool, but the beginner has to keep in mind, that it doesn't have its own compiler, and that it's an IDE. The compiler used by Dev-C++ is GCC (via MingW). Dev-C++ is also starting to getting outdated as the project has been discontinued, so the user should probably think about moving to a newer, more up-to-date IDE, like Code::Blocks.

    Quote Originally Posted by LogicKills View Post
    Linux
    gcc
    g++
    Note that the tools (the executables) gcc and g++ both are parts of the GNU Compiler Collection (GCC).

    Quote Originally Posted by LogicKills View Post
    return(0);
    Note that the parentheses aren't required, because it's a built-in statement, and not a real function.

    -----

    Besides all this; Nice tutorial!
    It's very informative, and I like that you're guiding the user through the steps of using and compiling with an IDE (in Windows), and how to use the text-editor and how to compile using the terminal (in Linux). It's often the most difficult part for a beginner (at least it was for me, when I started back then), so it's nice with a guide to follow.

  3. #3
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,556
    Blog Entries
    97

    Re: Getting Started (C Programming)

    Very nice tutorial - quick and informative. +Rep given.

  4. #4
    Xav
    Xav is offline
    Code Slinger Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav's Avatar
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,210
    Blog Entries
    13

    Re: Getting Started (C Programming)

    It's not much longer than mine, and remember, he -repped me for writing a short tutorial...

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  5. #5
    Co-Administrator John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John's Avatar
    Join Date
    Jul 2006
    Age
    21
    Posts
    5,882
    Blog Entries
    25

    Re: Getting Started (C Programming)

    When using the include directive, I often see brackets and quotes used. Is there a difference between
    Code:
    #include "foo.h"
    and
    Code:
    #include <foo.h>
    ?

  6. #6
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,628
    Blog Entries
    57

    Re: Getting Started (C Programming)

    Yes, one is a standard header include, the other is a custom header include.
    CodeCall Blog | CodeCall Wiki | Shareware
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  7. #7
    Retired v0id is a glorious beacon of light v0id is a glorious beacon of light v0id is a glorious beacon of light v0id is a glorious beacon of light v0id is a glorious beacon of light v0id is a glorious beacon of light
    Join Date
    Apr 2007
    Posts
    2,978
    Blog Entries
    3

    Re: Getting Started (C Programming)

    In addition to WingedPanther's post, it's possible in various compilers to change the libraries the compiler is using for finding the header-files, and in that way make it possible to use the chevrons with custom headers.

  8. #8
    MeTh0Dz is a name known to all MeTh0Dz is a name known to all MeTh0Dz is a name known to all MeTh0Dz is a name known to all MeTh0Dz is a name known to all MeTh0Dz is a name known to all
    Join Date
    May 2008
    Posts
    473

    Re: Getting Started (C Programming)

    The difference between using........

    #include "this.h"

    and

    #include <this.h>

    is that using '< >' the compiler will search a predefined path for the header files. So if your using like 'conio.h' you are going to use '< >' because it is going to be in the folder that can be reached with the defined path.

    However if you use ' " " ' , that will make the compiler look in the same path that your application is located in. So basically it looks in the folder that your application is in for header files. That is the reason you will see ' " " ' used for custom header files.

  9. #9
    Newbie Minor is an unknown quantity at this point
    Join Date
    May 2008
    Posts
    11

    Re: Getting Started (C Programming)

    + 1 for all the work mates

  10. #10
    Learning Programmer Scheme is on a distinguished road Scheme's Avatar
    Join Date
    Jul 2008
    Location
    South Wales
    Age
    20
    Posts
    37

    Re: Getting Started (C Programming)

    I found this really helpful. Nice one

+ Reply to Thread
Page 1 of 4
1 2 3 ... LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Perl Programming: Pack()..bit of hex
    By noobjason in forum Perl
    Replies: 0
    Last Post: 05-06-2008, 04:41 AM
  2. What should I do next....(general programming)
    By InfiniteSpawn in forum General Programming
    Replies: 26
    Last Post: 04-17-2008, 02:34 PM
  3. Replies: 1
    Last Post: 04-07-2008, 03:15 PM
  4. General programming and logic
    By Chinmoy in forum Tutorials
    Replies: 0
    Last Post: 03-20-2008, 04:58 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts