☠️
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
  • Macros vs functions
  • Alternatives
  • Creating macros
  • Symbolic constants
  • Function macros
  • Macros with arguments
  • Standard C pre-defined macros
  1. Architecture
  2. C Language

Macros

You should always use capital letters for macro functions and there are no spaces in names and its limited to one line only.

Example:

#define PI 3.14

#define PRNT(a, b))

    printf("value1 = %d\n", a); 
    printf("value2 = %d\n", b);
    
int main() {
    int x = 2;
    int y = 3;
    
    PRNT(x, y);
    
    return 0;
}

Macros vs functions

  • All macros are preprocessed which means all of them would be processed before your program compiles.

  • Functions are not preprocessed, they are compiled.

  • A macro is always faster than a function, functions take longer than inline code (macros for example).

  • For one time use a macro is not really a big deal but a macro inside a nested loop is a much better candidate for speed improvements.

  • We can use profilers to determine where a program spends the most time.

  • When calling a function it has to locate some data (a newly allocated stack frame).

  • Macros don't have this overhead. Macros insert directly into the program (textual program) so if we use the same macro 20 times we get 20 lines of code added into our program.

  • Functions are preferred over macros when writing large chunks of code.

  • With macros you don't have to worry about variable types.

  • Functions give us type checking. If a function expects a string but you give it an int, you will get an error.

  • Debugging a macro is much harder than debugging a function. A function can be stopped through by the debugger but a macro cannot.

Alternatives

Inline functions are the best alternative to macros.

When we add the inline keyword in front of the function it hints the compiler to embed the function body inside the caller (just like a macro).

Inline functions can be debugged and also have type checks.

However the inline keyword is just a hint and not a strict rule.

Creating macros

There are 2 ways of defining macros:

1. Symbolic constants (constants represented as symbols).

2. Function macros (operations defined as symbols).

Symbolic constants

Example:

#define NONFMAC some text here

Defines a macro and some replacement text. after definition the macro can be used as follows:

NOMAC
/* some text here */

Leading or trailing white space around the replacement text is discarded.

Example:

#include <stdio.h>
#define NOMAC 12

int main(){
    printf("%d\n",NOMAC);
    return 0;
}

Function macros

Example:

#define FMAC(a,b) a here, then b

#define macro_name(list_of_identifiers)substitution_string

Defines a macro and some replacement text.

The list of identifiers separated by commas appears between parentheses following the macro_name(FMAC).

Each identifier can apear one or more times in the substitution string.

Using:

FMAC(first text, some text)

Example:

#include <stdio.h>

#define Warning(...) fprintf(stderr, __VA_ARGS__)

int main() {
    Warning("%s: this program is here \n","json");
    
    return 0;
}

Output:

Macros with arguments

Example:

#include <stdio.h>
#define PI 3.14
#define CIRCLE_AREA(x) ((PI) * (x) * (x))

int main(){

    int c = 5;
    int area = CIRCLE_AREA(c + 2);
    
    area = PI * c + 2 * c + 2;
    
    printf ("area is %d\n",area);
    
    return 0;
}

Standard C pre-defined macros

__FILE__ β†’ represent the current file name (string)

__LINE__ β†’ represents the current line number of the current source code (an integer constant)

__func__ β†’ the name of any function when placed inside a function of the current file ( not part of the standard) 

__DATE__ β†’ the date the source file was compiled ( a string of the format "Mmm dd yyyy" such as "jan 19 2002")

__TIME__ β†’ the time the source file was compiled ( a string literal of the format "hh:mm:ss")

__STDC__ β†’ used to indicate if the compiler supports standard C by returning value 1

__STDC_VERSION__ β†’ expands to the C Standard’s version number, a long integer constant of the form yyyymmL where yyyy and mm are the year and month of the Standard version.The value 199409L signifies the 1989 C standard as amended in 1994

__cplusplus β†’ is defined when the C++ compiler is in use.
PreviousPre-processorsNextType Qualifiers

Last updated 8 months ago

πŸ‘Ύ