C Tutorial–The C Programming Language for Beginners

Write C Programs With This Programming Tutorial - Picture by Michelle Meiklejohn
Write C Programs With This Programming Tutorial - Picture by Michelle Meiklejohn
Programming in C is so simple that even people with no background in programming can start learning how to program immediately.

Although computer programming is thought to be a hard thing to do, everyone can actually learn programming. The C programming language is one of the most simple and most used languages.

Development of the C language

It was initially developed by the AT&T Bell Labs between 1969 and 1973, and it is still in use today.

Programming With Integrated Development Environments

An IDE (Integrated Development Environment) is a program which provides facilities to programmers. It often includes a source code editor, a debugger, a compiler, and much more. It features clever syntax highlighting, code completion, code breakpoints with conditions, etc. An IDE can help you write the source code faster, and it makes Learning C much more easy.

Install an IDE and write a simple C program

Download and install for free the Code::Blocks IDE.

When prompted to choose a compiler, choose the first option (“GNU GCC Compiler”) and press OK.

Run Code::Blocks and choose “File” -> “New” -> “Project...” from the upper left corner. Choose the “Empty project” (its icon looks like an empty paper). When prompted by the wizard, write the name “Project1” for your project in the first box, and browse for a location in the second. Then press Next and Finish. Choose then “File” -> “New” -> “Empty File”. Press Yes and save the file with the name “main.c”, and then press OK.

Now you are ready to write the source code. Type the following lines in the code file:

#include
int main()
{
printf("\nHello! This is a simple C program! Press Enter to continue.\n");
getchar();
return 0;
}

Meaning of the C Functions

#include

This is a preprocessor directive which tells the compiler to include in the file the code inside “stdio.h” file (which stands for “standard input/output”). It gives you access to the basic input/output functions of C.

main()
{
}

Every C program has exactly one function named main(). It is where the program starts running. The commands between the braces (“{}”) are executed in order.

printf("\nHello! This is a simple C program! Press Enter to continue.\n");

Printf() is a function that prints its input on the screen. In this case its input is "\nHello! This is a simple C program! Press Enter to continue.\n.” The “\n” means Newline (it places the cursor on the next line). Notice the trailing semicolon at the end of the line. It tells the compiler that the printf function ends there.

getchar();

This function “gets” the next letter which is pressed. The program will wait for you to press Enter before it terminates.

return 0;

The return function returns a value to the operating system. By default, 0 is the value which indicates a complete and correct execution (no errors occurred while the program was running).

Compile and Run a C Program

To compile (i.e. convert to machine code, which is understood by the CPU) and run the program, press F9. A console window will appear on the screen, displaying the text “Hello! This is a simple C program! Press Enter to continue.” The executable file can be found at the location which you entered in the box when creating the project, in the folder bin/Debug, and should have the name “Project1.exe.” You can open it every time you want. Congratulations! You have just made your first basic C program.

Tips for a Good Programming

If the compiler displays errors at compile time, it is likely that you had made a mistake in the typing of the program. Read the error message and you may be able to fix it.

The best way to learn a programming language is to type the code by yourself! Moreover, you can change things in the code, in order to see what happens. Try to change the output of the printf() function, or the return value. Try to change the order of the function, or to delete a few semicolons and braces. It will help you familiarize with the functions.

Advertisement
Leave a comment

NOTE: Because you are not a Suite101 member, your comment will be moderated before it is viewable.
Submit
What is 1+4?

Comments

Mar 17, 2011 4:09 AM
Guest :
That is good but can be more in details.
1
Helpful?
Advertisement
Advertisement