C - Tutorial 00

Discussion in 'Tutorials' started by SWH.PVT, Jan 28, 2012.

  1. SWH.PVT Lance Corporal

    Contributions:
    18
    C - Tutorial 00 - Overview

    C - Brief Overview

    C is a general-purpose computer programming language that is one of the most widely used in the world. It was one of the first structured languages and has influenced many languages that have since followed (e.g. C++, Java, Perl, etc.). Much of the world's software today is still written in C. It is the language upon which C++ was built.

    C is considered to be a "middle-level" language. For example, C allows the direct manipulation of bits, bytes, words, and pointers; yet C is also a structured language that has data types. The common data types are integer, character, and floating-point.

    C is portable. It is easy to change code written for one operating system so that it can run on another operating system. And C compilers exist for most computer architectures.

    Things you should keep in mind:

    C is not a strongly typed language. For example, integer values may freely be mixed with character values. Also, C allows an argument to be of any type as long as it can be converted into the type of the parameter. What makes this potentially difficult to debug is that C automatically provides these conversions.

    Be careful of run-time errors! For example, no checks are performed at all to ensure that array indexes are not overrun. These types of checks are the responsibility of the programmer. Again this could cause problems for debugging.

    ================================================================================================
    How to Compile Your First C Program

    Any computer language can be compiled or interpreted; however, C was designed to be a compiled language. This means that C source code is compiled from source code to object code (binary). A linker program is then used to link the compiled modules together to create an executable program. Most C++ compilers today can be used to compile C programs. In fact, I will be using Microsoft Visual C++ 2010 Express (VC++) for these tutorials.

    Note: This compiler only supports the 1989 C standard (C89/C90); this is the version of C that formed the basis for C++.

    An Example Program

    All C programs consist of one or more functions. Generally speaking, the only function that must be present is called main(), which is the first function called when program execution begins.
    As our first program, copy the following code and paste into your favorite text editor. Save the file as HelloWorld.C
    Code:
    #include<stdio.h>
     
    int main(void)
    {
      printf("Hello, World");
     
      printf("\n\nPress any key to exit...");
      getchar();
     
      return 0;
    }
    
    Next, open VC++, and select New Project. Give your project an appropriate name, e.g. HelloWorld

    Select Win32 Console Application and press ok. Then press next.
    Select console application and start with an empty project.
    Press finish!

    In the solution explorer, right click the name of your project, and select Add and then choose Existing Item. Locate HelloWorld.C and then add it to your project. You can then click on the added source file to view it.

    Right click the project name, select properties, C/C++, Code Generation, and then depending if you're in Debug or Release mode you'll want to select one of the following for the runtime library:

    Debug mode: Multi-threaded Debug (/MTd)
    Release mode: Multi-threaded (/MT)

    Finally, click on Debug, and then Start Debugging...and then the compiler should compile the source code to object code and also link the necessary modules to create an executable program.

    The source code should compile without error, and VC++ will then automatically execute the program. Setting the runtime libraries is necessary since otherwise other computers will likely not be able to run your program without getting errors about missing DLL files.

Share This Page