2 C Program Structure
2 C Program Structure
2 C Program Structure
www.ccatpreparation.com
Telegram Channel: @ccatpreparations
1 - Pre-processor
As the name suggests Preprocessors are programs that process our source code
before compilation.
2 - Header file
A Header file is a collection of built-in(readymade) functions, which we can directly
use in our program. Header files contain definitions of the functions which can be
incorporated into any C program by using pre-processor #include statement with
the header file. Standard header files are provided with each compiler, and covers
a range of areas like string handling, mathematical functions, data conversion,
printing and reading of variables.
With time, you will have a clear picture of what header files are, as of now consider
as a readymade piece of function which comes packaged with the C language and
you can use them without worrying about how they work, all you have to do is
include the header file in your program.
www.ccatpreparation.com
Telegram Channel: @ccatpreparations
Before using any function, we have to first include the required file, also known as
a header file (.h).
You can also create your own functions, group them in header files and declare
them at the top of the program to use them. To include a file in a program, use pre-
processor directive
#include <file-name>.h
File-name is the name of a file in which the functions are stored. Pre-processor
directives are always placed at the beginning of the program.
www.ccatpreparation.com
Telegram Channel: @ccatpreparations
• main()
• int main()
• void main()
• main(void)
• void main(void)
• int main(void)
The empty parentheses indicate that this function does not take any argument,
value or a parameter. You can also represent this explicitly by placing the keyword
void inside the parentheses.
The keyword void means the function does not return any value, in this case, the
last statement is always getch ().
In the above example, the keyword int means the function will return an integer
value. In this case, the last statement should always return 0.
www.ccatpreparation.com
Telegram Channel: @ccatpreparations
All the program code is written inside these brackets, such as declarative and
executable part.
The printf function generates the output by passing the text "Hello World!"
The semicolon; determines the end of the statement. In C, each statement must
end with a semicolon.
So, we have successfully installed the compiler and now can begin working in 'C.'
We will write a simple program that will say hello to us. Let's start.
Variable Declaration:
Any C program is the variable declaration. It refers to the variables that are to be
used in the function. Please note that in the C program, no variable can be used
without being declared. Also, in a C program, the variables are to be declared
before any operation in the function.
int ccat;
Semicolon;
Semicolon; is used to mark the end of a statement and beginning of another
statement. Absence of semicolon at the end of any statement, will mislead the
compiler to think that this statement is not yet finished and it will add the next
consecutive statement after it, which may lead to compilation(syntax) error.
www.ccatpreparation.com
Telegram Channel: @ccatpreparations
Comment
Comments can be inserted into C programs by bracketing text with the /* and */
delimiters. As will be discussed later, comments are useful for a variety of reasons.
Primarily they serve as internal documentation for program structure and
functionality.
Using /* */: The statements enclosed within /* and */ , are used to write multi-line comments.
All the C programs can be written and edited in normal text editors like Notepad or
Notepad++ and must be saved with a file name with extension as .c
If you do not add the extension .c then the compiler will not recognise it as a C
language program file.
www.ccatpreparation.com
Telegram Channel: @ccatpreparations
Execution Process
1. Firstly, the input file, i.e., HelloWorld.c, is passed to the pre-processor, and
the pre-processor converts the source code into expanded source code. The
extension of the expanded source code would be HelloWorld.i.
2. The expanded source code is passed to the compiler, and the compiler
converts this expanded source code into assembly code. The extension of
the assembly code would be HelloWorld.s.
3. This assembly code is then sent to the assembler, which converts the
assembly code into object code.
4. After the creation of an object code, the linker creates the executable file.
The loader will then load the executable file for the execution.
www.ccatpreparation.com
Telegram Channel: @ccatpreparations
When we run a compiled program, then it actually executes the statements inside
the main () function
Summary
1. C is a case sensitive language so all C instructions must be written in lower
case letter.
2. The main function is a mandatory part of every 'C' program.
3. To use the functionality of a header file, we have to include the file at the
beginning of our program.
4. Every 'C' program follows a basic structure.
5. All C statement must end with a semicolon.
6. C has a free-form line structure. End of each statement must be marked with
a semicolon.
7. The two braces, { and }, signify the begin and end segments of the program.
8. Whitespace is used in C to describe blanks and tabs
9. Whitespace is required between keywords and identifiers. We will learn
about keywords and identifiers in the next tutorial.
www.ccatpreparation.com