In our previous lessons on Introduction to C programming, we discussed two important features of C program which are portability and less lines of code. In this lesson, we will look at more features and write our first C program.
- Procedural Programming
C is a procedural language. You can divide you large tasks into smaller programs called procedures or functions so we can easily work on them.
- Middle Level Language Degree of Abstraction.
Programming languages are classed based on the degree of abstraction into high and low level language. High level of abstraction implies hiding system level detail. This means less effort and more user friendliness. Examples of high level languages are COBOL, FORTRANB, C++, LISP, Pascal, Prolog etc. On the other hand, low degree of abstraction means more effort and requires knowing internal details about the computer. Example of low level language is Assembly Language. C language is a middle level language. It is neither high nor low. Programming in C is simpler but it also allows to access system level function such as direct access to memory through pointers, bit manipulation using bitwise operators and writing assembly code with C code.
- Popular Choice for System Level Apps
Because of the unique system level features highlight above, C becomes popular for developing both system level applications like kernel device drivers, operating systems etc and various software applications like games, editors.
- Wide Variety of Built in Functions
C offers wide varieties of built in functions, standard libraries and header files such as math.h, string.h, stdio.h, scanf(), malloc() etc which make programming easier for programmers.
Let’s move forward and see how a C code looks like. In this lesson, I will be showing the code as quote. We will be using snippets from code blocks in subsequent lessons. See this blog on how to install code blocks on ubuntu.
// This program will print ‘Hello world’
#include <stdio.h>
int main()
{
printf(“Hello World”
return 0;
}
Let’s break down what each line of the code block above means. The first line is a comment. Compiler ignores the comment. They are only useful for the programmers as a way of documenting and understanding what the code is doing.
Next is #include <stdio.h>. This command is called the pre-processor directives. Preprocessor replaces text (starting with #) with the actual content. So the preprocessor will replace the line with the actual file stdio.h. This happens during the preprocessing stage before the actual compilation begins. The intitial code written is refer to as the source code. The preprocessor converts this to an expanded source code by replacing the #include <stdio.h> line with the actual content of the stdio.h file. The stdio.h is a standard input output file with .h extension. The file with the .h extension is called header file. Header files usually consists of prototype or declarations of functions that we need. stdio.h contains declarations of functions like printf (print what is passed as argument), scanf(take user input at runtim) etc.
Next is int main() line followed by a curly bracket which has some statements inside. This is a syntax for defining a function in C. The function is named main. The action the function performs are the statements inside the curly bracket. In our case, it prints the world ‘hello world’ and return 0. Note that there may be several functions in your program but the entry point from where the main compilation begins is the main function. Compiler looks for the main function and it is the starting point of your programs. The word int is the return type of the function. In general, the basix syntax of defining a function looks like:
Return_type name_of_function (parameter_type name of parameter, parameter_type name of parameter, …)
{
set of statements;
}
The statement in our example calls the standard printf function. This print the word ‘Hello Word’. Remember, C programs has many functions in the C standard library. Printf is one of them. This functions declaration is available in the stdio.h file. A good question to ask at this time is why do we need header file? The header file contains function declarations or prototypes. This is mapped to C standard library which contains the actual definitions of functions by the Linker. In the next lesson, we will talked about variables in C.