如何在C语言main函数中传递X'221A等特殊字符参数?
main() & Fixing Shell Quotation Errors Hey there! Let's break down how to pass the X'221A' character to your C program's main(int argc, char *argv[]) and fix that frustrating "Unterminated quoted string" shell error—this is a common gotcha with special characters, so let's get you sorted.
First: Clarify What X'221A' Actually Is
X'221A' is hexadecimal notation. If we're talking Unicode, that's the square root symbol (√, U+221A). If you’re referring to single-byte values, it’d be a double quote (", 0x22) plus a SUB control character (0x1A)—but I’ll assume you mean the Unicode symbol since that’s the more common use case for special character arguments.
Fixing the "Unterminated quoted string" Error
This error pops up because your shell is misinterpreting the special character as part of a quotation structure. Here’s how to fix it:
Option 1: Wrap the Character in Single Quotes
Single quotes (' ') tell the shell to treat everything inside as a literal, no parsing or expansion. This is the simplest fix:
./your_c_program '√'
Or if X'221A' refers to the double low-9 quotation mark:
./your_c_program '„'
Option 2: Use Shell Hex/Unicode Escaping
If typing the character directly isn’t an option (e.g., your terminal doesn’t support it), use bash/zsh’s $'...' extended syntax to escape the hex/Unicode value:
# For Unicode U+221A (√) ./your_c_program $'\u221A' # For raw hex bytes (0x22 + 0x1A) ./your_c_program $'\x22\x1A'
The $'...' syntax lets you use \uXXXX for Unicode code points or \xXX for single-byte hex values—this guarantees the shell parses the character correctly without triggering quotation errors.
Option 3: For POSIX sh (No bash Extensions)
If you’re stuck with plain sh (which doesn’t support $'...'), use printf to generate the character and pass it:
./your_c_program "$(printf '\u221A')"
The double quotes around $(...) ensure the output is treated as a single argument.
Receiving the Argument in Your C Program
Your main() function will receive the special character just like any other argument—argv[1] will hold the value. To verify it’s correct, you can print the argument and its raw byte values:
#include <stdio.h> int main(int argc, char *argv[]) { if (argc > 1) { printf("Received argument: %s\n", argv[1]); // Print raw byte hex values to confirm correct encoding for (char *ptr = argv[1]; *ptr != '\0'; ptr++) { printf("0x%02X ", (unsigned char)*ptr); } printf("\n"); } return 0; }
Compile this with gcc test.c -o test, then run ./test $'\u221A'—you’ll see output like:
Received argument: √ 0xE2 0x88 0x9A
Those three bytes are the UTF-8 encoding of U+221A, confirming the argument was passed correctly.
Quick Pro Tips
- Always quote arguments with special characters—this prevents the shell from splitting them into multiple arguments or interpreting them as commands.
- Make sure your terminal and C program use the same character encoding (UTF-8 is standard these days) to avoid garbled output.
内容的提问来源于stack exchange,提问作者FatTommy

