Structure of C Program
Include header file section
Global declaration section
/* comments */
main () //function name
{
Declaration part
Executable part
}
User defined functions
{
Declaration part
Executable part
}
Include header file section
- A C program depends upon some header files for the function declaration that are used in the program. Each header file by default has an extension ‘.h’. The file should be included using #include directive.
Ex: #include <stdio.h>
Global declaration
- This section declares some variables that are used in more than one function. These variables are known as global variables. This section must be declared outside of all the functions.
Function main()
- Every program written in C language must contain the main() function. Empty parentheses after main are necessary.
- The function main() is the starting point of every ‘C’ program.
- The execution of the program always begins with the function main().
- Except for the main() function, other sections may not be necessary.
- The program execution starts from the opening brace ({) and ends with the closing brace (}). Between these two braces, the program should declare declarations and executable parts.
Declaration part
- The declaration part declares all variables that are used in the executable part. Initialization of variables is also done in this section.
- Initialization means providing initial value to the variables.
Executable part
- This part contains the statements following the declaration of the variables. This part contains a set of statements or a single statement.
- These statements are enclosed between the braces.
User-defined function
- The functions defined by the user are called user-defined functions.
- These functions are generally defined after the main() function.
- They can also be defined before the main()function.
- This portion is not compulsory.
Comments
- comments are not necessary for the program. However, to understand the flow of the program, the programmer can include comments in the program.
- Comments are to be inserted by the program. These are useful for documentation.
Various steps in C program execution
- Editing
- Compiling
- Linking
- Execution
Editing
- The C program is entered into a file through a text editor in the editing phase. UNIX OS provides a text editor ‘vi’ editor, and modern compiler vendors provide IDES which consists of both editor and compilers.
- The file is saved on the disk with .c extension.
- Corrections in the program at later stages are done through these editors. Once the program has been written, it requires to be translating into machine language.
Compiling
- This phase is carried out by the compiler. A compiler translates the source code into the object code i.e. machine understandable code.
- The compilation phase cannot be proceeded successfully until and unless the source code is error-free.
- The compiler generates messages if it encounters syntactic errors in the source code.
- The error-free source code is translated into object code and stored in a separate file with an extension ‘.obj’.
Linking
- The linker links all the files and functions with the object code under execution.
- For example, if a user uses a ‘printf’ function in the program, the linker links the user programs object code with the object code of the printf function which is in the stdio.h library.
Execution
- Here, the executable object code is loaded into the memory and program execution begins.
- We may encounter errors during the execution phase even though the compilation phase is successful the errors may be run-time errors and logical errors.
Characteristics of ‘C’
Generality
- C language is a general-purpose language. It is used to write programs varying from simple to complex.
- Nowadays, it is used in developing applications which are effectively used in academic campus, multinational companies, etc.
Middle-level language
- C language possesses the capabilities of both low-level languages and high-level languages. Therefore, C can be used for writing system software and application software.
Extensive library functions
- C language has a huge set of built-in library functions making it a robust language. Any complex programs can be easily written using these built-in functions.
Efficiency and speed
- C language has a rich set of data types and operators making the language more efficient and fast.
- The language provides some operators (increment, decrement) which speed up the execution to a large extent. Further, it also provides user-defined data types (Ex: structures) through which miscellaneous data can be easily manipulated.
Portability
- C language is portable. It means that the programs written on one machine can be executed on a different machine with or without minor changes in the program.
Structured programming
- A c program can be defined as a collection of function modules (i.e., performing a unit of the task) and blocks. It also provides basic control flow statements which are essential for structured programming.
The First C program
void main() { printf(“WELCOME TO BALUTUTORIAL”); }
WELCOME TO BALUTUTORIALAbove program will print
on screen
Leaning C is similar and easier. Instead of straight-away learning how to write programs, we must know keywords, operators, separators, constants, predefine functions and syntax of C language.
What is Token?
- The smallest individual units in the C program are known as C tokens.
- C has 6 types of tokens: keywords, constants, identifiers, strings, special symbols, and operators.
- C programs are written using these tokens and syntax of the language.
Keywords
- A keyword is a word whose meaning has already been explained to the C compiler. In ANSI standardizes C language total number of keywords is 32.
Ex: while, if, else, case, break, etc.
auto | double | int | struct |
break | else | long | switch |
case | enum | register | typedef |
char | extern | return | union |
const | float | short | unsigned |
continue | for | signed | void |
default | goto | sizeof | volatile |
do | if | static | while |
Character set
- The characters used to form words, numbers, and expressions depend upon the computer on which the program runs. These are classified into 4 categories.
Letters | Capital A to ZSmall a to z |
Digits | All decimal digits 0 to 9 |
White spaces | Blank space, horizontal tab, vertical tab,New line, form feed |
Special characters | , . ; : ‘ “ ! | / \ ~ _ $ ? & ^ * – + < > () [] {} % # = @ |
Delimiters(Separators)
- These are special kind of symbols, which are called as delimiters. In general terms these delimiters known as separators.
Delimiters | Use |
: colon | Useful for label |
; semi colon | Terminates statement |
() parenthesis | Used in expression and function. |
[] square bracket | Used for array declaration |
{} curly brace | Scope of statement |
# hash | Pre-processor directive |
, comma | Variable separator |
Constants
- The constants in C are applicable to the values, which do not change during the execution of a program.
- Constants are classified into two types.
Numeric constants
- Integer constants: Ex:
10, +30, -15 etc.
- Real constants: Ex:
2.5, 5.521, 3.14 etc.
Character constants
- Single character constants: Ex:
‘a’, ‘8’, ‘ ’ etc. (Alpha numeric)
- String constants: Ex:
"Hello", "444", "a" etc.
Identifiers
- Identifiers are names of variables, functions, and arrays.
- They are user-defined names, consisting of a sequence of letters and digits, with the letter as the first character.
- Lower case letters are preferred. However, the upper case letters are also permitted. The (_) underscore symbol can be used as an identifier.
- In general, underscore is used as a link between two words in long identifiers. Ex:
total_salary
Variables
- A variable is a data name used for storing a data value.
- Its value may be changed during the program execution.
- The variable’s value keeps on changing during the execution of a program. In other words, a variable can be assigned different values at different times during the execution of a program. Ex:
height, average, sum etc.
Input/Output’s
Reading the data from the input devices and displaying the results on the screen are the two main tasks of any program. When a program needs data, it takes the data through the input functions and sends results obtained through the output functions. Thus, the input/output functions are the link between the user and the terminal. The I/O functions are classified into two types:
- Formatted functions
- Unformatted functions
Formatted functions
The formatted I/O functions read and write all types of data values. They require conversion symbol to identify the data type. Hence, they can be used for both reading and writing of all data values.
The formatted functions return the values after execution. The return value is equal to a number of variables successfully read/written. Using this value the user can find out the errors occurring during reading or writing the data.
Ex:printf(), scanf();
Unformatted functions
The unformatted I/O only works with the character data type. They do not require conversion symbol for identification of data types because they work only with a character data type.
There is no need to convert the data. In case the values of other data types are passed to these functions, they are treated as the character data. The unformatted functions also return values, but the return value of an unformatted function is always the same.
Types:
- Character I/O Ex:
getchar(), putchar(), getch() & getche(), putch().
- String I/O. Ex:
gets(), puts(), cgets(), cputs().