STRUCTURE OF A C PROGRAM
Preprocessor directives: The preprocessor directives contain special instructions that indicate how to prepare the program for compilation. One of the most important and commonly used preprocessor commands is include .
Global declarations: The global declaration contain the global variables. The variables that are available for all the functions in the program are known as global variables.
A C program contains one or more functions, where a function is defined as a group of C statements that are executed together to perform a specific task.
The main () function is the most important function and is a part of every C program. The execution of a C program begins at this function.
All the functions in a c program can be divided into two sections:
Local definition The data declared within a function are known as local declaration as that data will be visible only within that function. Stated in other terms, the life-time of the data will be only till the function ends.
Statements: The statement section in a function contains the code that manipulates the data to perform a specified task.
From the structure given above we can conclude that a C program can have any number of functions depending on the tasks that have to be performed, and each function
WRITING THE FIRST C PROGRAM
To write a C program, we first need to write the code. For this, open a text editor(Here i am using Code block). Once the text editor is opened on your screen, type the following statements:
#include <stdio.h>
int main()
{
printf("\n Welcome to the world of C");
return 0;
}
Output:
Welcome to the world of C
#include <stdio.h>
This is a preprocessor command that comes as the first statement in our code. All preprocessor commands start with symbol hash (#). The #include statement tells the compiler to include the standard input/output library or header file (stdio.h) in the program. This file has some in-built functions. By simply including this file in our code we can use these functions directly. The standard input/output header file contains functions for input and output of data like reading values from the keyboard and printing the results on the screen.
int main()
Every C program contains a main () function which is the starting point of the program. int is the return value of the main() function. After all the statements in the program have been written, the last statement of the program will return an integer value to the operating system.
{ } The two curly brackets are used to group all the related statements of the main function. All the statements between the braces form the function body. The function body contains a set of instructions to perform the given task.
printf("\n Welcome to the world of C ");
The printf function 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.
'\n' is an escape sequence and represents a newline character. It is used to print the message on a new line on the screen.
Like the newline character, the other escape sequences supported by C language are shown in Table
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 errors during the execution of the program.
Every statement in the main function ends with a semi-colon (;).
Now that you have written all the statements using the text editor, save the text file as first. C.
How to compile and run C program in CodeBlocks
Once you created your first C program it’s time to compile and run the program.
To compile and run a C program, click Build → Build and run to compile and build your C program, alternatively use the shortcut key F9.
In case, your program contains any errors. Error messages are shown in the Build messages tab below the code editor.
Every C program has four kinds of files associated with it. These include:
Source Code Files
The source code file contains the source code of the program. The file extension of any C source code file is c'.
Header Files
When working with large projects, it is often desirable to separate out certain subroutines from the main() function of the program.
The programmer wants to use the same subroutines in different programs. For this, he simply has to compile the source code of the subroutines once, and then link to the resulting object file in any other program in which the functionalities of these sub-routines are required.
The programmer wants to change or add subroutines, and have those changes reflected in all the other programs. In this case, he just needs to change the source file for the subroutines, recompile its source code, and then re-link programs that use them. This way time can be saved as compared to editing the subroutines in every individual program that uses them.
Standard Header Files In the program that we have written till now, we used printf() function that has not. been written by us. We do not know the details of how this function works. Such functions that are provided by all C compilers are included in standard header files.
Examples of these standard header files include:
string.h: for string handling functions
stdlib.h for some miscellaneous functions
stdio.h for standardized input and output functions
math.h for mathematical functions
alloc.h for dynamic memory allocation
conio.h for clearing the screen
Object Files
Object files are generated by the compiler as a result of processing the source code file. Object files contain compact binary code of the function definitions. Linker uses these object files to produce an executable file (.exe file) by combining the 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 Files
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 an “.exe” extension.
COMPILING AND EXECUTING C PROGRAMS
The programming process starts with creating a source file that consists of the statements of the program written in C language.
The source file is then processed by a special program called a compiler.
The compiler translates the source code into an object code. The object code contains the machine instructions for the CPU, and calls to the operating system API (Application Programming Interface). However, even the object file is not an executable file.
Therefore, in the next step, the object file is processed with another special program called a linker.
While there is a different compiler for every individual language, the same linker is used for object files regardless of the original language in which the new program was written. The output of the linker is an executable or runnable file.
In C language programs, there are two kinds of source files. In addition to the main (c) source file, which contains executable statements there are also header (.h) source files. Since all input and output in C programs is done through library functions, every C program therefore uses standard header files. These header files should be written as part of the source code for modular C programs.
The compilation process shown in Figure is done in two steps. In the first step, the preprocessor program reads the source file as text, and produces another text file as output. Source code lines which begin with the # symbol are actually not written in C but in the preprocessor language. The output of the preprocessor is a text file which does not contain any preprocessor statements. This file is ready to be processed by the compiler. The linker combines the object file with library routines (supplied with the compiler) to produce the final executable file.
USING COMMENTS
Comments are just a way of explaining what a program does. The compiler ignores the comments when forming the object file. This means that the comments are non-executable statements.
C supports two types of comments.
// is used to comment a single statement. This is known as a line comment. A line comment can be placed anywhere on the line and it does not require to be specifically ended as the end of the line automatically ends the line.
/* is used to comment multiple statements. A is ended with and all statements that lie within these characters are commented. This type of comment is known as block comment.
No comments:
Post a Comment