Home > C Programming, C Tutorials > Introduction to C Programming

Introduction to C Programming

As a programming language, C is rather like Pascal or Fortran. Values are stored in variables. Programs are structured by defining and calling functions. Program flow is controlled using loops, if statements and function calls. Input and output can be directed to the terminal or to files. Related data can be stored together in arrays or structures.

Of the three languages, C allows the most precise control of input and output. C is also rather more terse than Fortran or Pascal. This can result in short efficient programs, where the programmer has made wise use of C’s range of powerful operators. It also allows the programmer to produce programs which are impossible to understand.

Programmers who are familiar with the use of pointers (or indirect addressing, to use the correct term) will welcome the ease of use compared with some other languages. Undisciplined use of pointers can lead to errors which are very hard to trace. This course only deals with the simplest applications of pointers.

It is hoped that newcomers will find C a useful and friendly language. Care must be taken in using C. Many of the extra facilities which it offers can lead to extra types of programming error. You will have to learn to deal with these to successfully make the transition to being a C programmer.

The course aims to introduce programmers to the C language. Previous programming experience is assumed, so we can quickly progress to looking at the features of C and their uses. Students with little programming experience will need to do some homework in order to keep up with the lectures.

Structure of a C Program

Example

/*A simple C example program*/
#include <stdio.h>
void main(void)
{
	printf(”Hello World”);
}

The output of the program is shown below. This is what appears on the screen when the program runs.

Hello World

Let’s examine the program line by line. Here’s the first line.

/*A simple C example program*/

Other than the two forward slash marks that begin the line, it looks pretty much like ordinary English. The /* marks the beginning of a comment. The compiler ignores everything from the double slash to the end of the line. That means you can type anything you want on that line and the complier will never complain. To end the comment you must close it with */

Although comments are not required, they are very important to programmers. Real programs are much more complicated than the example above, and comments help explain what’s going on.

The next line looks like this:

#include <stdio.h>

Because this line starts with a #, it is called a preprocessor directive. The preprocessor reads your program before it is complied and only executes those lines beginning with a # symbol. The word inside the angular brackets <> is a header file name, which is required to do the work of Input / Output. The contents of stdio.h are included at the point where the #include statement appears. The preprocessor directives should not end with a semicolon because preprocessors are messages to the compiler.

The next line reads:

void main(void)

This starts the beginning of a function. A function can be thought of as a group of one or more programming statements that collectively has a name. The name of this function is main, and the set of parentheses enclosing the word void indicate that it is a function.

Although most C programs have more than one function, every C program must have a function called main. It is the starting point of the program. If you are reading someone else’s C program and want to find where it starts, just look for the function called main.

The next line is a

     {

This is called the left brace and marks the opening or beginning of a function, in our case main. All statements of the main are enclosed in a set of braces.

After the opening brace you see the following line:

	printf(“Hello World”);

This statement displays the string Hello World on screen. We will discuss more about the printf()later on in this chapter.

The last line is a

	}

This marks the end of the function main. Since main is the only function in the program it also marks the end of our program.

A special mention of the semicolon

A semicolon ; is used to mark the end of a statement in C. This allows the complier to understand that the end of a line has been reached. Usually a common mistake is that programmers omit the semicolon and try to compile the program.




The C Character Set

A character denotes any alphabet, digit or special symbol used to represent information. The C character set can be defined as a set of characters permissible under the C language to represent information. Under C, the upper case letters A-Z, the lower case letters a-z, the digits 0-9 and certain special symbols are all valid characters that can be used as building blocks to construct basic program elements.

Following is a table of character sets that are available under C

Alphabets
A, B, C, ………………… Z
a, b, c, ………………… z

Digits
0, 1, 2, 3, 4, 5, 6, 7, 8, 9

Special Characters or Symbols
+ – * & ^ % $ # @ ! ? > < { } [ ] ( ) | \ / “ ‘ : ; . , _

Indentifiers in C

In a C program every word is either classified as an identifier or a keyword.

Identifiers are used to identify or name various program-elements such as variables, symbolic constants, functions, etc.

Rules for Identifiers

  1. An identifier can contain letters or letters in any order except that the first character must begin with a letter.
  2. C is case-sensitive, therefore upper-case identifiers are different from lower-case characters. E.g. name is different from NAME.
  3. An identifier cannot contain any special character. The only exception is the under score character (_), which can also be taken as the first character of an identifier.
  4. An identifier cannot be a keyword.

Example of valid identifiers

name, rate1, rate_2, _myname, NAME

Example of invalid identifiers

1stname, my-name, rate 2

Keywords

Keywords are words, which have special meaning for the C compiler. Keywords cannot be used as identifiers because the programmer would then change the meaning of its implementation. Keywords are also sometimes known as reserved words.

Following is a list of keywords in C

auto break case char const
continue default do double else
enum extern float for goto
if int long register return
short signed sizeof static struct
switch typedef union unsigned virtual
void volatile while

In the next series of tutorials, you will learn Data Types, Constants and Data type modifiers.

Subscribe now to receive updates when a new c tutorial is released.





Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /home/sunilb/www.sunilb.com/wp-content/plugins/all-in-one-seo-pack/aioseop.class.php:245) in /home/sunilb/www.sunilb.com/wp-content/plugins/mycaptcha/MyCaptcha.php on line 41

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/sunilb/www.sunilb.com/wp-content/plugins/all-in-one-seo-pack/aioseop.class.php:245) in /home/sunilb/www.sunilb.com/wp-content/plugins/mycaptcha/MyCaptcha.php on line 41
  1. vipin
    May 23rd, 2008 at 11:05 | #1

    in Rules for Identifiers

    Is it possible to use a whitespace….? because whitespace chars and special chars are different. I am correct..?

  2. admin
    May 24th, 2008 at 20:08 | #2

    Vipin, A space cannot be used as an identifier

  1. No trackbacks yet.
Enter this code to leave comment (Sorry, but bots get me crazy :) )