Introduction: Your First Program in C

Picture of Your First Program in C

This tutorial will help you create your first program in the C programming language.  

Here is what you will need:

Windows 7 Operating System (or other)
In this tutorial, you will be walked through creating your program using a Windows 7 Operating system.  Most of what you will be doing will be able to translate over the different versions of Windows, however for older versions of Windows you may have to download a different version of the software.  If you do not have a Windows 7 or other Windows based system, you may want to consider running a virtual machine on your computer running either a Windows operating system or a Linux operating system.  Tutorials on how to do this can be found on Google.

Dev-C++
This is a free piece of software you will be using in this tutorial to create your program.  It requires either a Windows based system or a Linux based system to run.  If you do not have one of these, there are clients online that will allow you to run a virtual machine that runs either of these systems.  This tutorial will go over how to download and install this program to your computer.

Step 1: Download Dev/C++

Picture of Download Dev/C++

For this section, I am running on a Windows 7 operating system.  If you are on a different operating system, the steps may be similar, but make sure that you download the correct version of the software.

1. Go to Download Site

Follow this link to take you to the website to download the software: http://www.bloodshed.net/dev/devcpp.html


2. Select and Download

In this tutorial, I will be using Dev-C++ beta 9.2 (4.9.9.2) with Mingw/GCC. Select this by clicking the SourceForge link underneath the title.  The version is outlined in red, and the download link is outlined in blue as see in the figure above.

The download should begin automatically.  Select the location to save the file and hit save.


Step 2: Install Dev/C++

Picture of Install Dev/C++

1. Open the file

When the file is done downloading, locate the downloaded file and open the executable file (devcpp-4.9.9.2_setup.exe).

2. Instal Dev/C++

After you open the executable file, you will be ran through an installation set-up.  You can choose where to store the program files, but use the default settings for everything else.  

At the end of the installation, you can leave the box that says Run Dev-C ++ 5 beta 9 release (4.9.9.2) selected as shown in the figure so the program will open.

Step 3: Create First Project

Picture of Create First Project

1. Run Dev/C++

**If you selected to run the program at the end of installation, you can skip to step 2,**

Go to Start Menu > All Programs > Bloodshed Dev-C++ > Dev-C++.  This should start the program.

2. Create a New Source File

**If you are prompted with a set up window, use all the default settings**

Once the program opens, you need to create a new source file so you can start writing your first program.  To do this select File > New > Source File.  The path is shown in the figure above.  This will open up an area where you be able to type out your code.

Step 4: Write Your Program

Picture of Write Your Program

Here we will write the first simple program to simply have the computer say display text to the console. 

1. Copy the following code.

Copy this code into the text area:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_NAME_SZ 256

int main(int argC, char *argV[]) {
    /* Initalize variables */
    static char name[MAX_NAME_SZ+1];

    /* Ask user for name. */
    printf("What is your name? ");

    /* Get the name, with size limit. */
    fgets (name, MAX_NAME_SZ, stdin);

    /* Remove trailing newline, if there. */
    if ((strlen(name)>0) && (name[strlen (name) - 1] == '\n')){
        name[strlen (name) - 1] = '\0';
    }

    /* Print Message. */
    printf("\nCongradulations %s!\n\nYou have made your first functional program in C.\n", name);
    printf("Have fun learning more about programming!\n\n");

    /* Pause the Console */
    system("pause");

    /* Free memory and exit. */
    free (name);
    return 0;
}


2. Examine Code

To begin learning about what the different funtions are doing, read the comments seen in blue font in the code between the /* and the */.  This will describe the general idea of what is happening in the code following.

Step 5: Save and Compile Code

Picture of Save and Compile Code

1. Save File

Save your file by going to File > Save As.  Select save location and name your file

**IMPORTANT**
Make sure you change the file type to a C source file (*.c).  This is under the save as type.

2. Compile Code

Compile your code by going to Execute > Compile.  This will check through the code to make sure that it is a valid C file.  When the compilation is complete, close the compile window.

Step 6: Run Your Code

Picture of Run Your Code

1. Run Code

To run your code, select Execute > Run.  This will start the program and should bring up a new console as seen in the figure below.

2. Follow the Code

Follow and execute what the code is saying.  When it prompts you to enter your name, type your name and hit Enter using your keyboard.  A video demonstration of this step can be seen below.

3. Close Program

After the program has finished running, the console will print out "Press any key to continue . . ."  To end the program, simply type any key.

Step 7: More Resources

Picture of More Resources

You have now completed your first program.  But don't stop here there is still much to learn about this great programming language, including arrays, loops, and pointers.  There are many great resources available on the internet and in bookstores.  Here are a few good reference books that you can check out if you want to continue learning about C

- The C Programming Language by Brian W. Kernighan and Dennis M. Ritchie
- C in a Nutshell by Peter Prinz and Tony Crawford
- C for Dummies by  Dan Gookin

A great online reference that includes documentation about all of the classes available to you in the C library is also avaliable for you for free online at http://www.cplusplus.com/reference/clibrary/.

Photo credit Amazon.com

Comments

StephenK127 (author)2017-07-15

First of all this is the original C program:


#include <stdio.h>

#include <stdlib.h>

int main()

{

printf("Hello,world!");

return 0;

}

ZGM (author)2016-02-29

One thing i do not understand. It says "Make your first program". First program should print Hello World on screen not some fancy thing. I am not pro i just started learning C and first program shoud be this

#include

int main(void)
{
printf("Hello world");
return 0;
}

PS: Written on android so sorry for mistakes if any.

Scrubben (author)ZGM2016-05-11

I've never down anything with C at all but I think that you should write int main() instead of int main(void). Void means that you don't return anything and the program freaks out when you give it return 0;.

KristófL3 (author)Scrubben2016-05-24

That is not true, because in "int main(void) {}" the 'void' between the parenthesis means that id doesn't take any parameters, the actual return type of it is 'int', thats why you write "*int* main(void){}".

saubao (author)2015-06-08

Thanks for the instructable. I encountered a warning for line 30 "attempt to free a non-heap object 'name' ". If I'm not wrong, it's safe to remove that line.

grendel54 (author)2013-09-24

says "Source file not compiled" but I compiled it.

cgcentre (author)grendel542014-10-04

You need to save the project as a .C ext, then compile and run.

spfinn (author)grendel542013-09-25

It was probably a result of the file not passing the compile. Make sure that the code is copied exactly like listed and try to compile again.

tukstiki (author)2014-03-19

Thanks Guys

albertaclimber (author)2013-12-15

This instructable inspired me to give Dev-C++ a whirl as I'm an old guy from waaay back in the Basic, QBasic days. But I struggled and struggled with it, depreciated codes, etc., etc. then I ran across this article: http://www.cplusplus.com/forum/articles/36896/ and took their advice and downloaded a different (supported & updated) compiler. As soon as I tried the traditional "Hello World" in Code::Blocks it revealed one error and suggestions on how to fix it, after which it ran perfectly.

For newbies, I would suggest they do the same as Dev-C++ hasn't been updated since 2005. If they plan to continue exploring and learning to code, doing so with something like Code::Blocks will save them hours of frustration. (I'm not affiliated with or have no personal interest in  Code::Blocks in particular, it was just highly recommended and the one I happened to try. People should do their own research and choose for themselves).

But thank you for this instructable, which without I wouldn't have tried either!

russ_hensel (author)2013-09-22

Nice but this should be your second program, your first should print "Hello World". This is a tradition.

spfinn (author)russ_hensel2013-09-25

Thanks. I know that the first program is more traditionally the stock "Hello World" program, but for my class that I made this for we needed to include a video of some sort, so I decided to go with more of an interactive program that would look more interesting when ran. Thanks for viewing!

About This Instructable

51,857views

53favorites

License:

More by spfinn:Your First Program in C
Add instructable to: