Convenient generic print() for C

Overview

generic-print

Convenient generic print() for C inspired by Python/JavaScript and other high-level languages.

Still using printf("%i\n", result) for debugging?

#include "print.h"
print("number:", 25, "fractional number:", 1.2345, "expression:", (2.0 + 5) / 3);

output

output

Passing variables of various primitive types:

char *s = "abc";
void *p = main;
long l = 1234567890123456789;
print("string:", s, "pointer:", p, "long:", l);

output

output

Primitive array support:

int x[] = { 1, 2, 3 };
char *args[] = { "gcc", "hello.c", "-o", "hello" };
print(x, args);

output

output

Extra information for byte/char:

unsigned char byte = 222;
char ch = 'A';
print(byte, ch)

output

output

You can setup your own colors, arguments are: (normal, number, string, hex, fractional), defaults are (-1, 4, 1, 2, 5).

__print_setup_colors(249,236,239,244,232);

Disabling colors completely:

__print_enable_color = 0;

All code is in single no dependency header print.h.

The implementation is based on builtins that check types, variadic macros abuse and variable array initializers.

Tested on Ubuntu 20.04 x86_64, GCC 5.1+, Clang 3.4.1+, Intel C 17.0+, TinyC 0.9.27.

Tested on macOS 11.2 arm64 with Apple Clang 12.0.0.

May not work on 32-bit systems. Does not work with Microsoft C Compiler.

There is a scary looking macro code, what it compiles to? It is basically optimized out. See what GCC -O1 has done to print(42, 43), note that all type information bolied down to 0x00840084, which is type 4 (alias for int), size 8 bytes.

xor     eax, eax
mov     ecx, 43
mov     edx, 42
lea     rsi, [rsp+12]
mov     edi, 2
mov     DWORD PTR [rsp+12], 0x00840084
call    __print_func
Issues
  • fix fprint to more intuitive and generic usage

    fix fprint to more intuitive and generic usage

    The design of fprint could force the user to write code that is not intuitive. They may have to ignore the semicolon after fprint function. For example:

    if(1)
        print("True");
    else
        print("False");
    

    The above code couldn't work, and that's because a semicolon is put at the end of fprint. So the compiler would say:

    error: ‘else’ without a previous ‘if’
    

    We can fix this by removing the last semicolon in fprint macro. However, considering that the statement expressions ({...}) is non-standard C, so it could become the limitation for "generic" usage. Also, since we don't actually assign fprint to others(because __print_func return void type), I think we don't need to use statement expressions as well.

    Note that the other solution could be to write fprint as a function directly.

    opened by RinHizakura 1
  • Problem when print.h included in many files

    Problem when print.h included in many files

    Global variables and function definitions are put in a single header file and this may cause multiple definitions when print.h is included in two or more files. It could be solved by many approaches and depending on your trade-off, so I just leave it as an issue here.

    opened by RinHizakura 0
  • It's not compatible with the Microsoft C compiler

    It's not compatible with the Microsoft C compiler

    Just a quick test so you can update the readme:

    C:\git\generic-print>cl demo.c
    Microsoft (R) C/C++ Optimizing Compiler Version 19.26.28806 for x64
    Copyright (C) Microsoft Corporation.  All rights reserved.
    
    demo.c
    C:\git\generic-print\print.h(166): error C2010: '.': unexpected in macro parameter list
    demo.c(5): error C2059: syntax error: 'type'
    demo.c(11): error C2059: syntax error: 'type'
    demo.c(16): error C2059: syntax error: 'type'
    demo.c(21): error C2059: syntax error: 'type'
    
    opened by StephenWassell 4
Owner
exebook
exebook
RTTI in C and type-generic print

RTTI in C and type-generic print What This repository encompasses two purposes: a small header that enables type_name and also an user extensible type

null 0 Dec 17, 2021
Random for modern C++ with convenient API

Random for modern C++ with convenient API Design goals Supported compilers Integration Five-minute tutorial Number range Common type number range Char

Ilya Polishchuk 686 Jul 1, 2022
Small and convenient C2 tool for Windows targets

Micro Backdoor for Windows Micro Backdoor is C2 tool for Windows targets with easy customizable code base and small footprint. Micro Backdoor consists

Dmytro Oleksiuk 420 Jun 23, 2022
Memory Process File System (MemProcFS) is an easy and convenient way of viewing physical memory as files in a virtual file system

The Memory Process File System (MemProcFS) is an easy and convenient way of viewing physical memory as files in a virtual file system.

Ulf Frisk 1.3k Jun 23, 2022
Just a repository convenient for reviewing and retrieval practice.

The-art-of-multiprocessor-programming Purpose Just a repository convenient for reviewing and retrieval practice. The architecture of this repository(H

null 1 Nov 9, 2021
Convenient, high-performance RGB color and position control for console output

Oof (omnipotent output friend) It's common for C++ programs to write output to the console. But consoles are far more capable than what they are usual

Sebastian Werhausen 739 Jun 21, 2022
xsnip - a minimal and convenient screenshot utility for X11

xsnip - a minimal and convenient screenshot utility for X11 Most screenshot utilities compatible with X are clumsy, use bloated toolkits, and often do

null 24 May 26, 2022
DirectX 11 library that provides convenient access to compute-based triangle filtering (CTF)

AMD GeometryFX The GeometryFX library provides convenient access to compute-based triangle filtering (CTF), which improves triangle throughput by filt

GPUOpen Effects 212 Jun 23, 2022
A refactored Proof-of-concept originally developed in 2017 to print all function calls with their arguments data types and values using Ptrace during program execution.

print-function-args-debugger A refactored Proof-of-concept originally developed in 2017 to print all function calls with their arguments data types an

*finixbit 15 Jun 17, 2022
Bring your own print driver privilege escalation tool

Concealed Position Concealed Position is a local privilege escalation attack against Windows using the concept of "Bring Your Own Vulnerability". Spec

Jacob Baines 206 Jun 16, 2022
Phan Sang 4 Jun 21, 2022
The function is based on MQTT. When the original serial of ESP8266/ESP32 cannot be used, it can replace serial print.

MqttPrint and MqttMonitor The function is based on MQTT. When the original serial of ESP8266/ESP32 cannot be used, it can replace serial print. MqttPr

fw-box 3 Jan 10, 2022
evtest is a tool to print evdev kernel events

evtest is a tool to print evdev kernel events. It reads directly from the kernel device and prints a device description and the events with the value and the symbolic name.

Travis West 4 Jan 27, 2022
Port of my M5Stack Core 2 audio monitor project to generic ESP32s with TFT screens

ESP32 Audio Monitor This is a port of this project to work with any ESP32 device with a TFT display. You can watch a video explainer here (YouTube) wh

atomic14 41 Jun 23, 2022
ASMotor is a portable and generic assembler engine and development system written in ANSI C99

ASMotor is a portable and generic assembler engine and development system written in ANSI C99 and licensed under the GNU Public License v3. The package consists of the assembler, the librarian and the linker. It can be used as either a cross or native development system.

null 40 Jun 24, 2022
A generic and robust calibration toolbox for multi-camera systems

MC-Calib Toolbox described in the paper "MultiCamCalib: A Generic Calibration Toolbox for Multi-Camera Systems". Installation Requirements: Ceres, Boo

null 141 Jun 27, 2022
Generic force-feedback vibrator HAL for upstream haptics drivers

vibrator-ff, a generic vibrator HAL for force feedback haptics. Most downstream haptics / vibrator drivers are implemented as LED class devices, this

AOSP Mainline 4 Feb 2, 2022
Generic Linux 5.10 kernel sources.

How do I submit patches to Android Common Kernels BEST: Make all of your changes to upstream Linux. If appropriate, backport to the stable releases. T

GrapheneOS 4 Jun 3, 2022
SatDump - A generic satellite data processing software.

SatDump A generic satellite data processing software. Thanks Mnux for the icon! Still WIP Usage First of all, as with any program using volk, running

Altillimity 234 Jun 24, 2022