Skip to main content
Engineering LibreTexts

1.1: Introduction

  • Page ID
    25599
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    The main objective of this initial programming exercise is to become familiar with using the programming language tools. The programs in this exercise will be fairly trivial, but serve as a springboard to later work. We will be using programs similar to the ones examined in lecture.

    The precise C language package in use is not of extreme importance. Various companies will create different programming tools and although the features and fine points may differ, the basics remain the same. All C language tools need to compile C code and assemble it. Further, they need to link this assembled code with other assembled modules and libraries in order to create a finished executable program. In the simplest case, these tools will be in the form of command line utilities, i.e.; they run from a DOS prompt or shell. Ordinarily, the tools are part of a graphical integrated development environment, or IDE. IDEs normally include a text editor. C compilers expect to work on raw text. Do not attempt to “feed” them the output of a word processor, such as a .doc file. If you are using simple command line tools instead of an IDE, you will create your source files with a basic text editor such as Notepad.

    C source code files utilize a “.c” extension. The output of the compiler is called an object file. It will normally have a “.o” or “.obj” extension. In the Windows world, finished executables usually have a “.exe” extension. Many IDEs require that you create a project before you start entering your code. The project includes many attributes such as the file names used for source code (there may be several in larger projects), the appropriate libraries to link, the name of the finished executable, and so on. For simple programming chores involving small amounts of source code, this can be a bit of a pain, however, it is wonderful for larger endeavors. All the C source code of all of our exercises during this course can easily fit on a single very modestly-sized (64 MB!) USB drive. This includes the project files that can become much larger (many megabytes) than the C source. In networked labs on campus, project files and source files can be saved to the student’s network storage area. For those performing labs off-campus, it will probably be easiest to simply create new projects on your hard drive as needed.

    This lab will use the Pelles C application. There is nothing magical about Pelles C though and other systems are perfectly acceptable. We shall only be using Pelles C for the introductory exercises anyway. Once we get rolling we shall shift our emphasis to the Arduino development board.

    Our first exercise focuses on creating a project, editing source code, compiling and linking it, and testing it. We shall then edit it and repeat the process. We shall also look at error reporting. If you’re using command line utilities, see the note at the end of this exercise before continuing.

    To begin, open the C language IDE. In Pelles C select “Start a new project” from the start pane. The new project can be one of many things. We will not be creating Windows-GUI programs, but rather DOS shell utilities, so select Win32 or Win64 Console Application (depending on your operating system) and give the project a name. To create C source code, you will need to create a new text file. Select New>>Source code under the File menu. A blank text edit window will pop open.

    Type the following code into the editor:

    #include <stdio.h>
    
    /* It all begins here */
    
    int main( void )
    {
          printf(“Hello world!\n”);
    }
    

    Save this as hello.c. using File>>Save as. A dialog box will pop up asking if you want to add this file to the current project. Select “Yes”. Note that in some IDEs you will have to manually insert the file into the project (look for the appropriate menu item in such a case).

    While it is possible to separately compile and link modules, most developers use the Build shortcut. This will compile and link all files as needed. In Pelles C you can select Build from either the Project menu or from the toolbar. As the project is built, you will notice messages in the status area at the bottom. If everything works out properly, it will say Building <name of project> Done. You can now check the program. Select either the Execute toolbar button or Project>>Execute from the menu bar. A small DOS shell should pop open with the message “Hello World!”. Press any key to clear this shell window and return to the IDE. You have successfully completed your first program!

    Depending on the settings for your IDE, you may notice different colors on the text. In many IDEs you can specify different colors for different items such as keywords, constants, math operators, comments, and so forth. This makes reading the code a little easier.

    Let’s edit the source code and have it do something else. Alter the text so that it looks like this:

    #include <stdio.h>
    
    /* Program two */
    
    int main( void )
    {
          int x, y;
    
          x = 10;
          y = x + 20;
          printf(“The result is %d\n”,y);
    }
    

    Rebuild and test the resulting code. You should get a message that says “The result is 30”. Most IDE editors have the usual functionality of cut/copy/paste along with search and replace. Some also have automatic indenting, matching brace creation, and other advanced features.

    OK, what happens if you make an error in your code? We shall insert a few on purpose and see what happens. Alter the program above so that it looks like this:

    #include <stdio.h>
    
    /* Program three, with errors */
    
    int main( void )
    {
          int x, y;
    
          x = 10
          y = x + 20;
          printf(The result is %d\n”,y);
    }
    

    Note that we have left off the trailing semi-colon on x=10; as well as the leading quote on the printf() function. Rebuild the project. This time you will receive a bunch of errors and warnings. They may differ in wording from development system to development system, but you should see something about a missing semi-colon before the y. You’ll probably also see an error concerning “The” being an undeclared identifier. You may see many warnings as well. Usually, double clicking on the error message will highlight the corresponding line in the code. Sometimes a single omission can cause the compiler to emit dozens of error messages. This is because the compiler sort of “loses track” of where it is and starts flagging perfectly good code as having errors. For this reason, if you get errors (and you will), always look at the first reported error and fix it. Do not look at the last reported error as it may lead you on a wild goose chase.

    Finally, you may wish to save your code for backup. Simply select File>>Save as and choose an appropriate name. Again, C source files should use a “.c” extension. Note that you can create, read, or edit C source files without the IDE. All you need is a simple text editor. You won’t be able to compile or build it, but you can at least get some work done on an assignment without a compiler handy.

    For those using a command line system (no IDE), the process is similar to what has been described, although less automatic. You will need to create your source file in a text editor. You then invoke the compiler from a DOS shell window, usually with a command something like:

    cc hello.c
    

    This will create the object file. You then invoke the linker, usually with a command like:

    ln hello.exe hello.obj stdio.lib
    

    You will have to consult your documentation from the proper commands and syntax. Once the executable is created, you test it from the shell by typing its name:

    hello.exe
    

    The appropriate output will be sent to the shell window. To edit the program, reopen the C source file in the text editor, make the changes, save the file, and then repeat compile/link commands. If errors occur, the error messages will be printed in shell window.


    This page titled 1.1: Introduction is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by James M. Fiore via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.