Why do (only) some compilers use the same address for identical string literals?

This is not undefined behavior, but unspecified behavior. For string literals, The compiler is allowed, but not required, to combine storage for equal or overlapping string literals. That means that identical string literals may or may not compare equal when compared by pointer. That means the result of A == B might be true or … Read more

C -> sizeof string is always 8

There is no string data type in C. Is this C++? Or is string a typedef? Assuming string is a typedef for char *, what you probably want is strlen, not sizeof. The 8 that you are getting with sizeof is actually the size of the pointer (to the first character in the string).

How do I encode Unicode character codes in a PowerShell string literal?

Replace ‘\u’ with ‘0x’ and cast it to System.Char: PS > [char]0x0048 H You can also use the “$()” syntax to embed a Unicode character into a string: PS > “Acme$([char]0x2122) Company” AcmeT Company Where T is PowerShell’s representation of the character for non-registered trademarks. Note: this method works only for characters in Plane 0, … Read more

GCC 4.7 Source Character Encoding and Execution Character Encoding For String Literals?

I don’t know how well these options actually work (not using them atm; I still prefer treating string literals as ‘ASCII only’, since localized strings come from external files anyway so it’s mostly things like format strings or filenames), but they have added options like -fexec-charset=charset Set the execution character set, used for string and … Read more

How to create a formatted String out of a literal in Rust?

Use the format! macro: fn hello_world(name: Option<&str>) -> String { match name { Some(n) => format!(“Hello, World {n}”), None => format!(“Who are you?”), } } In Rust, formatting strings uses the macro system because the format arguments are typechecked at compile time, which is implemented through a procedural macro. There are other issues with your … Read more

C++ Comparison of String Literals

You are comparing memory addresses. Apparently your compiler places the string literals in memory in the order it encounters them, so the first is “lesser” than the second. Since in the first snippet it sees “A” first and “Z” second, “A” is lesser. Since it sees “Z” first in the second, “Z” is lesser. In … Read more