w3schools Logo
Monday 21 May 2012
W3schools.in Developer Network

C programming tutorial

C Introduction
C Data types C Variables and constants C Decisions C Loops
C Pointers C Arrays C Strings C Functions C Structures C Text and data files

c Articles

C program to display the multiplication table of a given numC program to generate the Fibonacci seriesC program to print a semicolon without using a semicolonC program to compare two strings without using strcmpC program to concatenate two strings without using strcatC program to delete a specified line from a text fileC program to replace a specified line in a text fileC program to find the number of lines in a text fileC program which asks the user for a number between 1 to 9C program to check whether the given string is a palindromeC program to check whether the given number is a palindromicC program to find factorial of the given numberC program to check whether the given number is even or oddC program to swap two numbers using a temporary variableC program to swap two numbers using bitwise operatorsC program to find the greatest of three numbersC program to find the greatest among ten numbersC program to check whether the given number is a primeHow to create pyramid in cFun with C language.


C-Programming Tutorials

Learn C - Introduction

What is C and why learn it

C-Programming Tutorials C was developed in the early 1970s by Dennis Ritchie at Bell Laboratories. C was originally designed for writing system software but today a variety of software programs are written in C. C can be used on many different types of computers but is mostly used with the UNIX operating system.

It is a good idea to learn C because it has been around for a long time which means there is a lot of information available on it. Quite a few other programming languages such as C++ and Java are also based on C which means you will be able to learn them more easily in the future.

What you will need

This tutorial is written for both Windows and UNIX/Linux users. All the code in the examples has been tested and works the same on both operating systems.

If you are using Windows then you need to download borland C++ compiler [Click here to Download]. Once you have downloaded and installed it you need to add "C:\Borland\BCC55\Bin" to your path. To add it to your path in Windows 95/98/ME you need to add the line "PATH=C:\Borland\BCC55\Bin" to c:\autoexec.bat using notepad. If you are using Windows NT/2000/XP then you need to open the control panel and then double click on "system". Click on the "Advanced" tab and then click "Environment Variables". Select the item called "Path" and then click "Edit". Add ";C:\Borland\BCC55\Bin" to "Variable value" in the dialog box that appears and then click Ok and close everything.

All Windows users will now have to create a text file called "bcc32.cfg" in "C:\Borland\BCC55\Bin" and save the following lines of text in it:

-I"C:\Borland\BCC55\include"
-L"C:\Borland\BCC55\lib"

If you are using UNIX/Linux then you will most probably have a C compiler installed called GCC. To check if you have it installed type "cc" or "gcc"at the command prompt. If for some reason you don't have it then you can download it from http://gcc.gnu.org.

 

Your first program

The first thing we must do is open a text editor. Windows users can use Notepad and UNIX/Linux users can use emacs or vi. Now type the following lines of code and then I will explain it. Make sure that you type it exactly as I have or else you will have problems. Also don't be scared if you think it is too complicated because it is all very easy once you understand it.

Code:
1
2
3
4
5
6
7
#include<stdio.h>

int main()
{
printf("Hello World\n");
return 0;
}

#include<stdio.h>
This includes a file called stdio.h which lets us use certain commands. stdio is short for Standard Input/Output which means it has commands for input like reading from the keyboard and output like printing things on the screen.

int main()
int is what is called the return value which will be explained in a while. main is the name of the point where the program starts and the brackets are there for a reason that you will learn in the future but they have to be there.

{}
The 2 curly brackets are used to group all the commands together so it is known that the commands belong to main. These curly brackets are used very often in C to group things together.

printf("Hello World\n");
This is the printf command and it prints text on the screen. The data that is to be printed is put inside brackets. You will also notice that the words are inside inverted commas because they are what is called a string. Each letter is called a character and a series of characters that is grouped together is called a string. Strings must always be put between inverted commas. The \n is called an escape sequence and represents a newline character and is used because when you press ENTER it doesn't insert a new line character but instead takes you onto the next line in the text editor. You have to put a semi-colon after every command to show that it is the end of the command.

Table of commonly used escape sequences:

\a Audible signal
\b Backspace
\t Tab
\n Newline
\v Vertical tab
\f New page\Clear screen
\r Carriage return

return 0;
The int in int main() is short for integer which is another word for number. We need to use the return command to return the value 0 to the operating system to tell it that there were no errors while the program was running. Notice that it is a command so it also has to have a semi-colon after it.

Save the text file as hello.c and if you are using Notepad make sure you select All Files from the save dialog or else you won't be able to compile your program.

You now need to open a command prompt. If you are using Windows then click Start->Run and type "command" and Click Ok. If you are using UNIX/Linux and are not using a graphical user interface then you will have to exit the text editor.

Using the command prompt, change to the directory that you saved your file in. Windows users must then type:

C:\>bcc32 hello.c

UNIX/Linux users must type:

$cc hello.c -ohello

The -o is for the output file name. If you leave out the -o then the file name a.out is used.

This will compile your C program. If you made any mistakes then it will tell you which line you made it on and you will have to type out the code again and this time make sure you do it exactly as I did it. If you did everything right then you will not see any error messages and your program will have been compiled into an exe. You can now run this program by typing "hello.exe" for Windows and "./hello" for UNIX/Linux.

You should now see the words "Hello World" printed on the screen. Congratulations! You have just made your first program in C.

Indentation

You will see that the printf and return commands have been indented or moved away from the left side. This is used to make the code more readable. It seems like a stupid thing to do because it just wastes time but when you start writing longer, more complex programs, you will understand why indentation is needed.

Using comments

Comments are a way of explaining what a program does. They are put after // or between /* */. Comments are ignored by the compiler and are used by you and other people to understand your code. You should always put a comment at the top of a program that tells you what the program does because one day if you come back and look at a program you might not be able to understand what it does but the comment will tell you. You can also use comments in between your code to explain a piece of code that is very complex. Here is an example of how to comment the Hello World program:

Code:
1
2
3
4
5
6
7
8
9
10
11
12
/* Author: w3schools.in
Date: 2009-09-05
Description:
Writes the words "Hello World" on the screen */

#include<stdio.h>

int main()
{
printf("Hello World\n"); //prints "Hello World"
return 0;
}

 

   




Chenna Reddy
2010-04-21 09:04:24 760day

excellent work guys this helps a lot for many students

 

Lalboi
2010-07-29 14:07:01 661day

always confused about c programming,am happy here because I can learn in free.

 

Satish
2010-09-25 09:09:50 603day

always confused about c programming,am happy here because I can learn in free. 
excellent work guys this helps a lot for many students

 

Ankit Mantri
2010-12-16 14:12:33 521day

This is a very tremendous site for the beginners.


Thanx a lot guys...

 

Shekhar
2011-02-13 13:02:55 462day

very nic site to learn thanks...

 

Nidhi
2011-02-17 13:02:57 458day

xcellant job guys..hope u ll upload some stuff on c#,c++ soon..

 

Stalyn
2011-03-09 19:03:43 438day

this is very awesome continue good job

 

Shoaib
2011-03-29 00:03:51 419day

very very nice, good for beginners(excellent website).

 

Ashish
2011-04-20 02:04:29 397day

gr8 work...very much understandable..!!!!!!!!!

 

Narender
2011-06-15 11:06:11 340day

all visitors are learning much programming languages visit this site this site is good for all learners 

 

Sachin
2011-04-12 13:04:31 404day

hi , it s very usefully work on tis

 

Yogi
2011-04-25 13:04:52 391day

usful site

 

Ruban
2012-02-16 14:02:09 94day

Great Job W3skools :))

 

Lovish Sharma
2012-03-29 22:03:17 52day

it provide good results n make satisfaction to the user..

 

Avinash
2012-03-15 12:03:46 66day

excellent website for beginners

 

Shanthini
2012-04-20 16:04:47 30day

tiz website s really awesome..daily some1 else sent some basic concepts abt c and c++ to ma mail..thanks in advance....:)

 

Arjun
2012-03-08 09:03:13 73day

I HAVE STUDIED SO MANY BOOKS TO UNDERSTAND THE C LANGUAGE, BUT THIS 
WEBSITE REALLY AMAZING.

 

Akshay deshpande
2012-01-13 20:01:36 128day

I like this & it's very usuful for my life.

 

Mishti
2012-01-24 21:01:54 117day

it's really nice n vry much helpful for the beginners.......:-)

 


Add Comments / Scripts
Name:
E-mail:
Comment:
 


Can't read the image? click here to refresh