C Programming Language learn c tutorial

A Simple C Program

                  Every C program must have one special function main (). This is the point where execution begins when the program is running. We will see later that this does not have to be the first statement in the program, but it must exist as the entry point. The group of   statements defining the main () enclosed in a pair of braces ({}) are executed sequentially. Each expression statement must end with a semicolon.  The closing brace of the main function signals the end of the program. The main function can be located anywhere in the program but the general practice is to place it as the first function.

Here is an elementary C program.

main ()

     {

     }

                  There is no way to simplify this program, or to leave anything out. Unluckily, the program does not do anything. Following the "main" program name is a couple of parentheses, which are an indication to the compiler that this is a function. The 2 curly brackets { }, properly called braces, are used to specify the limits of the program itself. The actual program statements go between the 2 braces and in this case, there are no statements because the program does absolutely nothing. You will be able to compile and run this program, but since it has no executable statements, it does nothing. Keep in mind however, that it is a legal C program.

                main ( ) function should return zero or one. Turbo C accepts both int and void main ( ) and Turbo C coders use both int and void main ( ) in their programs. But in my belief, void main ( ) is not a standard usage. The reason is, whenever a program gets executed it returns an integer to the OS. If it returns 'zero', the program is executed successfully. Otherwise it means the program has been ended with error. So main ( ) shouldn't be declared as void.d as void.

main( ) should be declared as

int main( )

   {

    ……………..

    ……………..

   return  0 ;     

  }

 .For a much more interesting program, load the program

int main ()

      {

       printf (“Welcome to C language”);

       return 0;

      }

and display it on your monitor. It is  same as the previous program except that it has one executable statement between the braces.                 

               The executable statement is another function. Again, we won't care about what a function is, but only how to use this one. In order to output text to the monitor, it's placed within the function parentheses and bounded by quotes. The end result is that whatever is included between the quotes will be showed on the monitor when the program is run.

                Notice the semi-colon; at the end of the line. C uses a semi-colon as a statement terminator, so semi-colon is required as a signal to the compiler that this line is complete. This program is also executable, so you'll be able to compile and run it to see if it does what you think it should. With some compilers, you may get an error message while compiling, indicating that the function printf () should have a prototype.

#include<stdio.h>

#include<conio.h>

int main ()

        {

         printf (“Welcome to C language”);

         return 0;
        }

              Here you'll be able to see #include at the beginning of the program. It is a pre-processor directive. It's not a part of our program; it's an instruction to the compiler to make it do something. It says the C compiler to include the contents of a file, in this case the system file stdio.h. This is the name of the standard library definition file for all Standard Input Output. Your program will almost certainly want to send stuff to the screen and read things from the keyboard. stdio.h is the name of the file in which the functions that we want to use are defined. A function is simply a group of  related statements that we can use later. Here the function we used is  printf . To use printf correctly C needs to know what it looks like, i.e. what things it can work on and what value it returns. The actual code which performs the printf will be tied in later by the linker. Note that without the definition of what printf looks like the compiler makes a guess when it sees the use of it. This can lead to the call failing when the program runs, a common cause of programs crashing.

                The <> characters around the name tell C to look in the system area for the file stdio.h. If  I had given the name "abc.h" instead it would tell the compiler to look in the current directory. This means that I can arrange libraries of my own routines and use them in my programs.

                 Imagine you run above program and then change it and run it again you may find that the previous output is still stucked there itself, at this time clrscr(); would clear the previous screen.
One more thing to remember while using clrscr()  is that it should be called only after the variable declaration, like
int p,q,r;
clrscr()

Here is an example of minimal C program that displays the string Welcome to C language (this famous example included in all languages ​​moreover been done originally in C in 1978 from the creators of the language, Brian Kernighan and Dennis Ritchie) Example

#include<stdio.h>
#include<conio.h>

int main ()

       {

        clrscr();

        printf (“Welcome to C language”);

        return 0;

       }

              When you execute above program you won’t see ‘Welcome  to C language’ on the  console  because  the screen will just flash and go away .If you want to see the line you can use getch() function just  below the printf() statement. Actually it waits until a key is pressed.

Example:

A Simple C Program

Turbo C++ IDE Home Identifiers