☠️
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
  • First Program
  • Detect the version of C
  • Comments
  • Taking command line arguments
  • NULL "\0"
  • Modular programming
  1. Architecture
  2. C Language

Introduction

First Program

#include <stdio.h>

int main()
{
    printf("Rust is not good enough!");
    return 0;
}

Detect the version of C

#include <stdio.h>

int main(int argc, char **argv) {

#if __STDC_VERSION__ >=  201710L
  printf("We are using C18!\n");
  
#elif __STDC_VERSION__ >= 201112L
  printf("We are using C11!\n");
  
#elif __STDC_VERSION__ >= 199901L
  printf("We are using C99!\n");
#else
  printf("We are using C89/C90!\n");
  
#endif

  return 0;
}

Comments

/* 
     multiline comment
*/
  
// single line comment

Taking command line arguments

#include <stdio.h>

int main(int argc, char *argv[])

{
  int numberOfArgs = argc;
  char *argument = argv[0];
  char *argument2 = argv[1];

  printf("Number of arguments: %d\n", numberOfArgs);
  printf("Argument number 1 is the program name: %s\n", argument);
  printf("Argument2 is the command line argument: %s\n", argument2);
 
  return 0;
}

NULL "\0"

A special character with the code value 0 is added to the end of each string to mark where it ends.

A string is always terminated by a null character so the length of a string is always one greater than the number of characters in the string that's why "length - 1" is commonly used.

NULL is a symbol that represents memory address that doesn't reference anything.

Modular programming

We can put our code in multiple separate files and include the headers in the main file and use another source file to import functions and other instructions.

To do this:

  1. Create a header file pointing to a source file:

Create other.h → header file.

#ifndef UNTITLED_OTHER_H
#define UNTITLED_OTHER_H

int getme(void); 

#endif // UNTITLED_OTHER_H
  1. Create the source file for the header:

Create other.c → source file for header.

 int getme(void){
   return 3;
}

Here we define the getme() function which was referred to by header and will be used in the main.c source file.

We don't need to include anything in this source file.

  1. Include the header in main.c and use the function:

Create main.c, include other.h, and use getme() function.

#include <stdio.h>
#include "other.h"

int main(){

  printf("%d\n", getme());

  return 0;
}

Here we have to include the header file with "double quotations" cause we know its in the same directory so we don't need to look for it in the whole system.

  1. To compile without an IDE from command line & compile all .c source files, headers are checked in compile time and are not included in the command:

gcc *.c -o [program name].

Use with -c flag instead of -o flag to get object files.

PreviousC LanguageNextCalling Conventions

Last updated 10 months ago

👾