C Programming language

Write compile run a first C program
Previous Home Next
adplus-dvertising

How to write a first C program

Rules:

  1. In C program each instruction written as a separate statement.
  2. The statement of a program must be in the sequence by which we want to execute.
  3. Blank space are not allowed in the variable name but you can use blank spaces between two words which increase readability.
  4. Statements should be in small case letters.
  5. Every statement ends with a semicolon (;).
  6. Semicolon is called statement terminator.
  7. You can put the comment within a program by using /* …….*/.
  8. Comments are not necessary to use in a program but it is a good habit to use comments in a program.

Compilation and Execution of a C Program:

  1. Write a program using editor.
  2. Save the file with .c extension.
  3. To compile the program you can select compile from compile menu and press enter or you can press Alt+f9
  4. It will check the errors if program has any error it will terminate the program then you can find out he error and you can correct it. After correcting the error you can compile the program again. After compiling successfully it will create a object (.obj) file.
  5. Once the source program has been converted into an object file, it is still not in the form that you can run. The reason behind this is that there may be some references to standard library functions or user-defined functions in other object files, which are compiled separately. Therefore these object files are to be linked together along with standard library functions that are contained in library files. To link our object file, select Link from the Compile menu. After this the .OBJ file will be compiled with the one or more library files. The result of this linking process produces an executable file with .EXE extension. Figure-4 depicts this linking process.
  6. .exe file is a stand alone executable file. which can direct execute from command prompt
  7. After pressing Alt+f9 it will complete the whole process creating .OBJ and .EXE.
  8. To display the output of the program press Alt+f5.

Examples of a simple C program:


/* Calculation of simple interest */
main( )
{
int p, n ;
float r, si ;
p = 1000 ;
n = 3 ;
r = 8.5 ;
/* formula for simple interest */
si = p * n * r / 100 ;
printf ( "%f" , si ) ;
}

main():

  1. main() is a function which is entry point of any program.
  2. main() is collective name which is given to a set of statements.
  3. This name has to be main(). It can not be change.
  4. Statements within a main() should be enclosed within a pair of { }.
  5. 
     main()
            {
            statement 1;
            statement 2;
            }
    
    
  6. Every function used in a program must be declared.

Difference between main() and other functions:

  1. parameters to main() are passed from command line,
  2. main() is the only function declared by the compiler and defined by the user,
  3. main() is by convention a unique external function,
  4. main() is the only function with implicit return 0; at the end of main(). When control crosses the ending ‘}’ for main it returns control to the operating system by returning 0 to it (if there is no explicit return statement with a return value). The OS normally treats return 0 as the successful termination of the program.
  5. return type for main() is always is an int, (some compilers may accept void main() or any other return type, but they are actually treated as if it is declared as int main(). It makes the code non-portable. Always use int main().

Example:


main()
{
Int i;
Static int j;
}

NOTES:

  1. Parameters are passed from command line to main ().
  2. It is the only function declares by the compiler and defines by the user.
  3. Return type for main() is always is an int, (some compilers may accept void main() or any other return type, but they are actually treated as if it is declared as int main(). It makes the code non-portable. Always use int main() ).
  4. Receiving Input:

    To receive the input from user we use scanf() function in C Language. scanf() is a function that reads data with specified format and is present in many other programming languages.

    int scanf(const char *format, ...);

    "scanf" stands for "scan format", because it scans the input for valid tokens and parses them according to a specified format.

    
    #include <stdio.h>
    int
    main(void)
    {
     int n;
     while (scanf("%d", &n) == 1)
     printf("%d\n", n);
     return 0;
    }
    
    
    Previous Home Next