☠️
smadi0x86 Playground
  • 💀Welcome to smadi0x86 Playground
    • 🍷Resources
    • 🚬Projects
    • 🎓Certifications
    • 📌Pinned
    • ❓Questions
    • 📞Contact
  • 🏞️Cloud Native
    • Docker
      • Quick Reference
      • Introduction
      • Containers
      • Images
      • Storage & Volumes
      • Security
      • Cheatsheet
    • Git
    • Serverless Framework
    • YAML
  • 🔨Software Engineering
    • System Design
    • Environment Variables
    • JSON Web Tokens
  • 👾Architecture
    • C Language
      • Introduction
      • Calling Conventions
      • GCC Compilation
      • Libraries & Linking
      • I/O
      • Files
      • Pointers
      • Dynamic Memory Allocation
      • Data Types
      • Strings Manipulation
      • Bit Manipulation
      • Pre-processors
      • Macros
      • Type Qualifiers
    • C/C++ Build Systems
      • Fundamentals for Linking
      • Symbolic Linking
      • Cross-Platform Compilation
      • CMake for Building and Linking
      • Shared Libraries
      • Dynamic Linking and Dependency Management
    • Operating Systems
      • OS & Architecture
      • Processes
      • CPU Scheduling
      • Memory Management
  • 🛩️Cyber Warfare
    • Flight Physics
    • Communication
      • PWM & PPM
      • MAVLink
  • 🏴‍☠️Offensive Security
    • Active Directory
      • Introduction
    • Web Attacks
      • Server Side
        • OS Command Injection
        • Information Disclosure
        • Directory Traversal
        • Business Logic
        • Authentication
        • File Upload
        • SSRF
      • Client Side
        • CSRF
        • XSS
    • Recon
      • Active
        • Host discovery
        • Nmap
        • Mass Scan
      • Passive
        • Metadata
      • Web Applications
        • Discovery
        • Subdomains & Directories
        • SSL Certs
        • CMS
        • WAF Detection
      • Firewall Evasion
  • Binary Exploitation
    • Stack Smashing
      • x86
      • x86_64
    • pwntools
      • Processes and Communication
      • Logging and Context
      • Cyclic
      • Packing
      • ELF
      • ROP
  • 😈Advanced Persistent Threat
    • C2
      • Sliver
    • Malware
      • Windows Internals
        • PEB
      • Academy
        • Basics
      • Sektor7
        • Essentials
  • 💌Certifications
    • AWS Certified Cloud Practitioner (CLF-C01)
      • Cloud Foundations
      • Domain 1: Cloud Concepts
      • Domain 2: Security and Compliance
      • Domain 3: Technology
      • Domain 4: Billing and Pricing
    • AWS Certified Solutions Architect - Associate (SAA-C03)
      • Foundation
    • Certified Kubernetes Administrator (CKA)
      • Core Concepts
      • Scheduling
      • Logging & Monitoring
      • Application Lifecycle Management
      • Cluster Maintenance
      • Security
      • Storage
      • Networking
      • Design Kubernetes Cluster
      • Kubernetes The Kubeadm Way
      • Troubleshooting
      • JSONPATH
      • Lightning Lab
      • Mock Exams
      • Killer Shell
    • Certified Kubernetes Security (CKS)
      • Foundation
      • Cluster Setup
      • Cluster Hardening
      • Supply Chain Security
      • Runtime Security
      • System Hardening
      • Killer Shell
    • (KGAC-101) Kong Gateway Foundations
      • Introduction to APIs and API Management
      • Introduction to Kong Gateway
      • Getting Started with Kong Enterprise
      • Getting Started with Kong Konnect
      • Introduction to Kong Plugins
  • 📜Blog Posts
    • Modern Solutions For Preventing Ransomware Attacks
Powered by GitBook
On this page
  • Conditional compilation
  • Pragma
  • Error directive
  • # and ## operators
  1. Architecture
  2. C Language

Pre-processors

These statements are identified by the presence of a pound sign "#" which must be the first non-space character on the line like:

#include <stdio.h>

There are two ways to #include header files in a program:

  1. Using angle brackets ( #include ) tells the preprocessor to look for the file in one or more standard system directories.

  2. Using double quotes ( #include "smadi.h" ) tells the preprocessor to first look in the current working directory.

Conditional compilation

#define #undef #ifdef #ifndef #if #elif #else #endif Used to create one program that can be compiled to run on different computer systems.

If you had a large program that had many dependencies on specific hardware or software we might end up with many defines whose values would have to be changed when the program was moved to another computer system. We can help reduce this problem by incorporating the values of these defines for each different machine into the program by using the conditional compilation capabilities of the preprocessor. Its also used to switch on or off various statements in the program, trace the flow of a program or debug statements that print out the values of various variables.

#if → Test the value of a constant expression.

Every #if construct ends with an #endif.

#else → To complement #ifdef/#ifndef and #if.

#define → Defines an identifier, we can define them from the command line too.

#ifdef → Checks whether an identifier is currently defined.

#ifndef → The negative of the ifdef.

Directives #ifdef and #ifndef are provided as shorthand for:

if defined(name)

if !defined(name)

Example:

#ifndef SIZE

#define SIZE 100

Example:

#define UNIX 1 OR #define UNIX

Example:

#ifdef UNIX
#define DATADIR "/dev/data"
#elif UNIX = "/data"

#define DATADIR "/usr/data"
#else
#define DATADIR "/dev/null"

#endif

Most compilers also permit you to define a name to the preprocessor when the program is compiled using the special option -D:

gcc -D UNIX main.c

Pragma

Lets you place compiler instructions in the source code.

To control the amount of memory set aside for automatic variables to set the strictness of error checking to enable nonstandard language features.

Pragma token_name → token_name is a command for the compiler to obey.

gcc has the following pragmas:

#pragma GCC dependency
#pragma GCC poison
#pragma GCC system_header
#pragma once
#pragma GCC warning
#pragma GCC error
#pragma message

Pragma GCC dependency → Check the relative date of the current file and another file, if the other file is more recent than the current file, a warning is issued.

Example:

#pragma GCC dependency "parse.y"

Pragma GCC poison → used to remove an identifier completely from the program.

Example:

#pragma GCC poison printf sprintf fprintf

sprintf(some_string,"hello"); // This will raise an error.

Pragma GCC system_header → Tells the compiler to consider the rest of the current include file as a system header.

Pragma once → Specifies that the header file containing the directive is included only once even if the programmer includes it multiple times.

Pragma GCC warning "message" → Causes the preprocessor to issue a warning diagnostic with the text "message".

Pragma GCC error "message" → Cause the preprocessor to issue an error with the text "message".

Pragma GCC message "message" → Prints string as a compiler message on compilation. the message is information only not warning or error.

Error directive

Causes the preprocessor to issue an error message that includes any text in the directive.

Error message is a sequence of characters by spaces.

You do not have to in-close the text in quotes the message is optional.

Example:

#if STDC_VERSION != 201112L // Failes if the compiler uses an older standard and successes when it uses C11.

Error Not C11

#endif

# and ## operators

These are used for concatenation.

Often useful to merge two tokens into one while expanding macros (called token pasting or token concatenation).

# → Used within a macro definition causes a replacement text token to be converted to a string surrounded by quotes.

Example:

#define HELLO(x) printf("hello," #x "\n");

// When HELLO(john) apears in a program file it is expanded to:

printf("Hello," "john" "\n");

## → Performs token pasting, cats two tokens.

Example:

#define concat(x,y) x ## y
...
int xy = 10;
...

// This will make the compiler turn:
printf("%d", concat(x,y));

// To: 
printf("%d", xy);
PreviousBit ManipulationNextMacros
👾