Programming, website development forum Get latest updates by RSS Follow TechnicalTalk on Twitter Follow TechnicalTalk on Facebook 
HomeSearchRecent PostsLoginRegisterContact Us

Username  
Password    
  Forgot your password?  

Pages: [1]   Go Down
 
  Email this topic  |  Print
0 Members and 1 Guest are viewing this topic.

C program tutorial in simple English with examples - Lesson 1

 
webmaster forum
Kevin  Offline
A computer Science student
Dream - To become a computer expert
Activity
20%
 
Skilled Coder
Real name: Kevin
Location: India
Gender: Male
Age: 18
Posts: 199
Topics: 51
kevin.cmaker
August 22, 2011, 06:25:51 AM



Introduction to C language:

Today's world is computerized and computer is nothing without applications and games. But, how are these application (or games) created? Just by various programming languages like Java, Python, C#,C++ etc... One of such is the C programming language. Of all the other, it is the easiest and more powerful programming language than many other. Powerful means that it can be used to make more complex functions. Let us see about the history and the basic structure of C programming in this article.

History of C language:

This language was originally developed by Dennis Ritchie. C was written in the Bell laboratories in the year 1978. It got its name C since it was created(written) in order to overcome the advantages of another language named B programming language. Since then, this language was subjected to many changes and upgrades to make it even more powerful. Currently, the ANSI (American National Standards Institute) is having the control to make further upgrades of the language.

Where to run C?

C programming is generally typed in a compiler. The work of a compiler is to convert the language written in C to 0s and 1s ie. binary language that could be understood by the computer. However, if you use Linux you can directly use the Terminal (called as command prompt in Windows) as a compiler. The most common compiler used is the "Borland Turbo C". However, a C programming could also be typed in a C++ compiler.

Start Programming:

The basic structure of a C program is as follows:
Code:
#include<stdio.h>
int main()
{
statement 1;
statement 2;
...
return o;
}
Explanation:

The #include at the top of the program is called the preprocessor. It is the same for almost all the programs in C. As it's name indicates, it is used to include some other thing called header.

stdio.h is called a header which must be always enclosed in "<>". It is a library. For example, consider the library "stdio". This library has many books (technically called functions) like main(), printf(). So, if we include any of these functions in our program, the compiler will refer to the stdio library and learns how to respond to the command (function). There are various other headers in addition to this which will be discussed later.

int main()
{
return 0
}


The above thing always comes together.

Here int refers to an integer; main() refers to the main function of the program. The program would not be compiled by the computer if the main() function is missing.

The return 0; statement is used because int is used. This is to return a value of 0 (null) which is also an integer. So, if you neglect int, you can also neglect return 0; statement. Hence a program can also be written like this:
Code:
#include<stdio.h>
main()
{
statement 1;
statement 2;
...
}
In the above statement, both int and return 0; are neglected.

The last but important thing to be defined are the statements. Statements are nothing but the commands that are given to the computer in order to do some specific functions. The functions may be to print (display) a value, to perform arithmetic operations like addition, multiplication, etc. An important thing to be noted is that each and every function should end with a semicolon ; . Hence, obviously, return 0 should also end with a semicolon since it is a statement.

Example 1:
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
printf("Hello world!");
getch();
return 0;
}

"Also note that #include<conio.h> is included in the above program in which the header conio.h is included in order to do the function getch()."



Output:
Quote
Hello world!
In this above program, after displaying the output, the screen will wait for any key to be pressed so that it will return to the editable part of the program. This is because we have used the function namely getch(). If we do not use this function, the output Hello world! will be printed but not be displayed on the screen.

Steps to get the output after giving the input:

To get the output, the following two things are to be done:
  • (i)Compilation : In this process, the compiler will compile the program that we have written. Consider a simple example. For a clock to work, we must first compile all its parts together. Only after then we can make the clock work. Similarly, here the program is compiled in an order ready to be performed.
  • (ii)Running : After compiling, we must run the program which is similar to switching on the clock to get the desired output.

Example 2:
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
printf("Hello world!  \n  How are you?");
getch();
return 0;
}


"Also note that #include<conio.h> is included in the above program in which the header conio.h is included in order to do the function getch()."

(Note : The slash used above is a backward \ slash)

Output:
Quote
Hello world!
How are you?

Here a new thing \n is included (n - new line). This instructs the compiler to go to the next line and print the remaining characters How are you? there.

Example 3: (comments)
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
/*The following function is to print(display) the text on the screen*/
printf("Hello world! /n How are you?");
getch();
return 0;
}

"Also note that #include<conio.h> is included in the above program in which the header conio.h is included in order to do the function getch()."




Output:

Quote
Hello world!
How are you?

In the above program, the text within /* and */ is not displayed. That text is called a comment. Comments are actually not a part of a program, but they are useful to write any valuable information like the date at which the program was written, name of the author, why the function is written, etc. While running, the compiler will ignore the values within /* and */.


Interesting notes :

(i)C program is case sensitive. That is, if you type Printf() instead of printf(), your program will not run. All the functions and statements in C are written in small letters.

(ii) For every program that you create and save, a corresponding file in the .exe type will be created and be saved along with the original program. So, every time when you want to run the program, you can open that file and get the output. You will open the same .exe type of file to open any application or a game but you might not note it.

In this above program, after displaying the output, the screen will wait for any key to be pressed so that it will return to the editable part of the program. This is because we have used the function namely getch(). If we do not use this function, the output Hello world! will be printed but not be displayed on the screen

There are further lots of things to be learned in C programming which will be explained in the successive articles. Thanks for reading!


The second part of this article can be found at: C programming in simple English with examples - Lesson 2





You can find more information here :-



*** Please do not forget to leave your comments about this article ***

« Last Edit: August 31, 2011, 09:49:38 PM by Admin »

Glad to help.

A TechnicalTalk member!
 
webmaster forum
Activity
0%
 
New Poster
Posts: 1
Topics: 0
August 30, 2011, 08:34:20 PM

I'm really glad to have stumbled you post! This is great and helpful!


san bernadino seo firm
 
webmaster forum
polas  Offline
Activity
33.33%
 
Code Guru
Gender: Male
Posts: 1399
Topics: 85
WWW
September 16, 2011, 10:28:06 AM

You should always specify the return type of main (int) and return a value, this is required by the C standard. Returning 0 is absolutely different from null, 0 is a value and tells the calling os the status of the program. Returning 0 means it executed succesfully, by convention -1 that it failed. These are also defined in a header

Mesham Type Oriented Parallel Programming Language, Free online technical support
 
  Email this topic  |  Print
Pages: [1]   Go Up
 
Jump to:  



Powered by SMF 1.1.15 | SMF © 2011, Simple Machines


Google visited last this page February 07, 2012, 01:18:11 PM