Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม C

C Puzzle: รวมตัวเลขสองตัวโดยไม่ใช้ arithmetic/string functions ในภาษา C

ปริศนา: รวมตัวเลข 12 และ 54 ให้ได้ 1254 (concatenate) โดยห้ามใช้ arithmetic, string หรือฟังก์ชันอื่นๆ

วิธีแก้

ใช้ Token-pasting operator ## ใน macro preprocessor

โค้ดตัวอย่าง

#include <stdio.h>
#define merge(a, b) b##a

int main() {
    printf("%d", merge(432, 23)); // ได้ 23432
    return 0;
}

ผลลัพธ์

23432