Starting with Ubuntu

Image
Hardware is nothing but finely designed machinery. A machine is ultimately a machine only, which is always made to work. It is the kernel on an operating system that makes the hardware alive. There is a hugely popular operating system Linux which is mostly used in most sincere applications.  Linux is an open source operating system (i.e., its code is also available) created by a Finnish student Linus Torvalds . Linux is available in multiple distributions such as Ubuntu, Red Hat, Linux Mint, Fedora, Debian, CentOS and many more. In this session, you shall learn to work with Ubuntu distribution of Linux. It's derived from Debian and composed mostly of free and open-source software. Ubuntu is officially released in multiple editions: Desktop, Server, and Core for Internet of things devices and robots. The operating system is developed by the British company Canonical and a community of other developers, under a meritocratic governance model. Starting Ubuntu When you boot your comput

Preprocessor Directives in C

 Preprocessing is an initial phase to process text before compilation. Preprocessor directives are lines of the source file where the first non-whitespace character is #, which distinguishes them from other lines of text. It operates under the control of preprocessor directive which is placed in the source program before the main().


Before the source code is passed through the compiler, it is examined by the preprocessor for any preprocessor directives. The preprocessor directives are always preceded by a hash sign (#). The preprocessor is executed before the actual compilation of program code begins. 

Therefore, the preprocessor expands all the directives and take the corresponding actions before any code is generated by the program statements. No semicolon (;) can be placed at the end of a preprocessor directive. 


Advantages of preprocessor directives

The advantages of using preprocessor directives in a C program include:

  • Program becomes readable and easy to understand 
  • Program can be easily modified or updated
  • Program becomes portable as preprocessor directives makes it easy to compile the program in 
  • different  execution environments
  •  Due to the aforesaid reason the program also becomes more efficient to use


Types of C Preprocessors

There are 4 Main Types of Preprocessor Directives:  
  • Macros
  • File Inclusion
  • Conditional Compilation
  • Other directives



Types Of Preprocessor Directives

#define

To define preprocessor macros we use #define. The #define statement is also known as macro definition or simply a macro. There are two types of macros- object like macro and function like macro.

 Object like macro
  •  An object-like macro is a simple identifier which will be replaced by a code fragment. They are usually used to give symbolic names to numeric constants. Object like macros do not take an argument. It is the same what we have been using to declare constants using #define directive. 
  • The general syntax of defining a macro can be given as: 
  • #define identifier string
  • The preprocessor replaces every occurrence of the identifier in the source code by a string. 
  • #define PI 3.14
Function-like macros
  • They are used to stimulate functions. When a function is stimulated using a macro, the macro definition replaces the function definition. 
  • The name of the macro serves as the header and the macro body serves as the function body. The name of the macro will then be used to replace the function call. 
  • The function-like macro includes a list of parameters. 
  • References to such macros look like function calls. However, when a macro is referenced, 
  • source code is inserted into the program at compile time. The parameters are replaced by the corresponding arguments, and the text is inserted into the program stream. Therefore, macros are considered to be much more efficient than functions as they avoid the overhead involved in calling a function. 
  • The syntax of defining a function like macro can be given as
  •  # define identifier(arg1,arg2,...argn) string
  • The following line defines the macro MUL as having two parameters a and b and the 
  • replacement string (a * b): 
  • #define MUL(a,b) (a*b) 
  • Look how the preprocessor changes the following statement provided it appears after the macro definition. 
  •  int a=2, b=3,c;
  • c = MUL(a,b); 

#include 
  • An external file containing function, variables or macro definitions can be included as a part of our program. This avoids the effort to re-write the code that is already written. 
  • The #include directive is used to inform the preprocessor to treat the contents of a specified file as if those contents had appeared in the source program at the point where the directive 
  • appears. 
  • The #include directive can be used in two forms. Both forms makes the preprocessor insert  the entire contents of the specified file into the source code of our program. However, the difference between the two is the way in which they search for the specified. #include <filename> 
  • This variant is used for system header files. When we include a file using angular brackets, a search is made for the file named filename in a standard list of system directories. #include "filename" 
  • This variant is used for header files of your own program. When we include a file using double quotes, the preprocessor searches the file named filename first in the directory containing the current file, then in the quote directories and then the same directories used for <filename>. 

#undef

As the name suggests, the #undef directive undefines or removes a macro name previously
created with #define. Undefining a macro means to cancel its definition. This is done by writing #undef followed by the macro name that has to be undefined.


#line
  • The #line directive enables the users to control the line numbers within the code files as well as the file name that we want that appears when an error takes place. The syntax of #line directive is:
  • #line line_number filename
  • Here, line_number is the new line number that will be assigned to the next code line. The line numbers of successive lines will be increased one by one from this point on.
  • Filename is an optional parameter that redefines the file name that will appear in case an error occurs. The filename must be enclosed within double quotes. If no filename is specified then the compiler will show the original file name. For example:

  • #include<stdio.h>
  • main()
  • {
  • #line 10 "Error.C"
  • int a=10:
  • #line 20
  • printf("%d, a);
  • }

#ifdef 
  • This directive is the simplest conditional directive.The controlled text will get included in the preprocessor output if the macroname is defined. 
  • The controlled text inside a conditional will embrace preprocessing directives. They are executed only if the conditional succeeds. You can nest these in multiple layers, but they must be completely nested. 
  • In other words, ‘#endif’ always matches the nearest ‘#ifdef’ (or ‘#ifndef’, or ‘#if’). Also, you can’t begin a conditional group in one file and finish it in another. 
  • Syntax:
  • #ifdef MACRO
  •     controlled text
  • #endif

#if, #else and #elif
  • All these directives works together and control compilation of portions of the program using some conditions. 
  • If the condition with the #if directive results in a non zero value, then the group of line immediately after the #if directive will be executed otherwise if the condition with the #elif directive evaluates to a non zero value, then the group of line immediately after the #elif directive will be executed else the lines after #else directive will be executed. 
  • Syntax:
  • #if macro_condition
  •    statements
  • #elif macro_condition
  •    statements
  • #else
  •    statements
  • #endif

#error

The #error directive aborts the compilation process when it is found in the program during compilation and produces an error which is optional and can be specified as a parameter.


 #pragma

The #pragma directive is used to provide the compiler some extra information and hence it is compiler dependent so we can say that it’s behavior vary according to the compiler we are using. The pragma directive basically gives the instructions to the compiler to turn on or turn off some feature and also to give some custom messages required.


Comments

Popular posts from this blog

Function Generator

The World Of Linux