Showing posts with label introduction to the pre-processor in C. Show all posts
Showing posts with label introduction to the pre-processor in C. Show all posts

Tuesday, March 5, 2024

INTRODUCTION TO THE PRE PROCESSOR IN C

Introduction to the pre-processor in C

  • Introduction to pre-processor 
  • Pre-preprocessor 
  • Macro substitution 
  • File inclusion directives 
  • Conditional compilation 

The compilation does not begin with the C Preprocessor; rather, it is an independent procedure. A simple text replacement tool that tells the compiler to do any necessary preparation before compilation is a C preprocessor.


A macro processor called the C preprocessor is automatically used by the C compiler to make certain alterations before compiling your program. Because it can define macros—shorthand for more complicated constructions—the program is termed a macro processor
.

There are four independent features available to you in the C preprocessor:

  •       The header files are a part of this. You can replace these declaration files in your software.
  •       The C preprocessor can replace actual macros in the program with designed macros with the use of macro extensions. Any element of C code can have its definition shortened by a macro.
  •        Conditional collection and precise preprocessing instructions enable the program components to be included or excluded based on different scenarios.
  •       Line management when you use a program to merge or reorganize source files into an intermediate file that is then compiled, you can use line control to tell the compiler where the source line originally came from.

 

Preprocessor directives

Beginning with the hash symbol (#), all preprocessor commands go here. For readability, preprocessor directives must begin with the first nonblank character in the first column. This is a complete guide to the most important preprocessor commands –



Macro substitution

The name and text that should be changed in a macro are specified by the #define directive. The preprocessor simply replaces the macro's name with the replacement text in the line of code where the macro is created.

Now we will try the C program


File inclusion directives
  1. The document contains Directories used to include user-defined header files in C programs.
  2. The file-inclusive directory looks for the header file inside the same directory if the path is left blank.
  3. The first step in file-inclusive directives is #include.
  4. By giving its path, you can add a certain header file to the current scope.
  5. Instead of using triangular brackets to include user-defined header files, we employ "Double Quote".
  6. It instructs the compiler to incorporate each named file.

Conditional compilation

Conditional Compilation: We can choose not to compile code that does not match specific requirements or to only compile code that does by using conditional compilation directives.

  1. #ifdef: This is the simplest type of conditional directive. This type of block is known as a conditional group. The controlled text will appear in the preprocessor output if the macro name is defined. The controlled text will include preprocessing directives inside a conditional. Their implementation is reliant on the conditional's success. You can stack them in layers, but they have to be fully nested. Put simply, '#endif' always matches the nearest '#ifdef' (or '#ifndef') or '#if'. Furthermore, you cannot create and terminate a conditional group in different files.ss

Syntax:

#ifdef MACRO

    controlled text

#endif /* macroname */

INTRODUCTION TO POINTERS IN C

Introduction to pointers in C
  • DIFFERENT ASPECTS OF USING POINTERS IN OTHER METHODS IN C
  • C POINTERS OPERATORS 
  • ADVANTAGES 
  • USES
  • NULL POINTER
  • COMPLEX POINTER 

Going back to what we discussed before, a pointer is a variable in C that stores the address of a location in memory. To access and modify data stored in a variable's memory, a pointer is a useful tool.

One of the most interesting and unusual aspects of C is its pointers. The tongue becomes more flexible and potent as a result. The advice can seem complicated and challenging at first, but if you get it, trust me—C opens up a world of opportunities.

When a variable is declared in a program, the system specifies where in memory the value should be stored. According to the software up above, we can get this place's address.

Assume for the purposes of argument that the system has reserved 80F in memory.

 

Int n=10

Int p=&n

 

Declaring a pointer

 

In C, a pointer can be declared using the * (letter symbol). Also known as an indirect pointer, it is used to dereference a pointer.

 

  1. int *a;//pointer to int  
  2. char *c;//pointer to char

the help of * (indirection operator), we can print the value of pointer variable p.

Let's see the pointer example as explained in the above figure.



 

Now we will understand different aspects of using pointers in other methods using C


 C Pointer Operators

Two distinct kinds of pointer operators exist in C:

  •    operator and
  •     & operato

Operators in C have been examined in detail in distinct sections.

The operand's memory address is returned by the & operator. As an illustration,

a = &b;

The memory address of variable b will be saved in variable a.

The counterpart of & is the * operator. The value found at the specified location is returned by this operator.

For instance, if the variable's memory location is included in a, then the code,

c is equal to *a;

will cause the variable's value to be stored in c.

let us understand the pointer in C with a C program


Advantages of using pointer

·       When dealing with arrays and C structures, pointers are the way to go.
It is simpler to pass functions as parameters to other functions when pointers are used because they permit references to functions.
• A C function can change its arguments sent to it using Pointers.
• The program's size and runtime are reduced thereafter.
• It makes it possible for C's dynamic memory management to work.

Usage of pointers

The C language has several uses for pointers.

1) Allocating memory dynamically

We may utilize the pointer-based malloc() and calloc() methods in the C language to dynamically allocate memory.

2) Structures, Functions, and Arrays

In C, pointers are often utilized in arrays, functions, and structures. It increases performance and decreases code.

NULL Pointer

The NULL pointer is a pointer that is only NULL and has no value assigned to it. If you declare the pointer without an address to provide, you can set its value to NULL. It will provide a better approach.

Int *p=NULL;

Most of the time in libraries of it may have value 0

Let us try one C program to swap two numbers using 3rd variable


Reading complex pointers

Several things must be taken into consideration while reading the complex pointers in C. Let’s see the precedence and associativity of the operators that are used regarding pointers.

  • Here in the above table, please mark

    • The bracket operator, denoted by the symbol (), is used to declare and define the function.
    • An array subscript operator is used here: This is known as the pointer operator.
    • A pointer's name serves as its identifier. There is never going to be anything more important than this.
    • Kind of information: A pointer's data type identifies the sort of variable it is pointing to. It also includes other modifiers such as signed int and long.
How to read the pointer: int (*p)[10].
  • To interpret the pointer, we must recognize that () and [] are equally precedential. Consequently, we must consider their associativity. The precedence is with (), as the associativity is climbing from left to right.

    The pointer name (identifier) p and the pointer operation * have the same precedence inside the() bracket. Due to their right-to-left associativity, p has precedence over *, with * coming in second.

    As the data type comes first, assign [] the third priority. The pointer will therefore look something like this:


The pointer will be read as p is a pointer to an array of integers of size 10.

Example

How to read the following pointer?


It is read as

Here, "p" designates a function that accepts a void pointer as its second parameter and a two-dimensional array of integers as its first, with an integer as the return type of the letter.

Featured Post

ASSOCIATION RULE IN MACHINE LEARNING/PYTHON/ARTIFICIAL INTELLIGENCE

Association rule   Rule Evaluation Metrics Applications of Association Rule Learning Advantages of Association Rule Mining Disadvantages of ...

Popular