How writing the First C Program? | Files Used in a C Program | How Compiling and Executing C Program?

How to write the First C Program?


To write a C program, we first need to write the code. For this, open a text editor. If you are a windows user you may use Notepad Once the text is opened on your screen, type the following statement:
/------------------------------------------------------------------------------------------------------------------------/
#include <stdio.h>
    int main()
    {
       printf ("\nWelcome to the world of C"0;
       return 0;
     }
/------------------------------------------------------------------------------------------------------------------------/

Output:
  Welcome to the word of C

Descriptions of different part is as Follows:

#include <stdio.h>
This is a preprocessor command thats comes comes as the first statement in our code. The #include statement tells the compiler to include the standard input/output library or header files (stdio.h)in the program. This file has some in build functions. So simply by including this file in our code we can use these functions directly.

int main()
int is the return value of main functions. After all the statement in the program have been written, the last statement of the program will return in an integer value of the operating system.
                 {}The two curly brackets are used group all the related statement of functions main. All the statement between the braces form the functions body. the functions body contains a set of instructions to perform the given task.


Printf("\n Welcome to the world of C")

        The printf functions is defined in the stdio.h file and is used to print text on the screen. The message that has to be displayed on the screen is enclosed within double quotes and put inside brackets.
       The message is quoted because in C a text (also known as a string or a sequence of characters) is always put between inverted commas. The '\n' is escape sequence and represents a newline character. It is used to print the message in new line on the screen.

return 0;
This is a return command that is used to return the value 0 to the operating system to give an indication that there were no error during the excution of the program.



What are the Files Used in C Program?

Every C program has four kinds of files associated with it. These are show on following Picture.


Files in a C Program


  • Source Code File

    The source code file contains the source code of the program. The file extension of any C source code file is .C. This file contains C source code that define the main functions and other functions. The main() is the starting point of execution when you successfully compile and run the program.
  • Header Files

    A header file is a file containing C declarations and macro definitions to be shared between several source files. You request the use of a header file in your program by including it, with the C preprocessing directive #include. Header files serve two purpose.
                   (1) System header files declare the interfaces to parts of the operating system. You decide them in your program to supply the definitions and declaration you need to invoke system calls and libraries.
                  (2) Your own header file contain declarations for interfaces between the source file of your program. Each time you have a group of related declarations and macro definitions all or most of which are needed in several different source files, it is a good idea to create a header file for them.

    Including a header file produces the same result as coping the header file into each source file thats need it. Such copying would be time-consuming and error-prone. With a header file, the related declarations appear in only one place. If they need to be changed, they can be changed in one place, and program that include the header file will automatically use the new version when next recompiled. The header file eliminates the labour of finding and changing all the copies as well as the risk that a failure to find one copy will result in inconsistencies within a program
  • Object Files

    Object files are generated by the compiler as a result of processing the source code file. Object files contains compact binary code of the function definitions. Linker uses this object files to produce an executable file (.exe file) by combining the of object files together. Object files have a '.o' extension, although some operating systems including Windows and MS-DOS have a '.obj' extension for the object file.
  • Binary Executable File

    The binary executable file is generated by the linker. The linker links the various object files to produce a binary file that can be directly executed. On Windows operating system, the executable files have '.exe' extension.



How to Compiling and Executing the C programs?

C is a compiler language. So once the C program is written, you must runit through a C compiler that can create an executable file to be run by the computer.

Compilation and Execution Process

Creating of Program :

The program should be written in C editor. The file name does not necessarily include extension .C. The default extension is .C. The user can specify his own extension.

Compilation and Linking a Program :

With alt-c keys, the source program statement should be translated into object program, which is suitable for execution by the computer. The translation is done after correcting each statement as per C syntax. If there is no error, compilation proceeds and translated program is store in another file with the same file name, but with an extension .obj. If at all error are there, the programmer should correct them. The linking is an essential process. it puts all other program files and function together that are required by the program. For example, if the programmer is using pow() function, then the object code of this function should be brought from math.h() library of the system and linked to the main() program.

Executing the Program :

After the compilation, the executable object code will be loaded in the computer main memory and the program is executed. This is done with keys alt-r.

Post a Comment

0 Comments