hex escape sequence out of range for numeric escape of C - c

When I printf following code, I get hex escape sequence out of range. \x1b is numeric escape for ESC. If my string is literally the 1ESC1, how to represent it?
#include <stdlib.h>
#include <stdio.h>
int main(void){
printf("1\x1b1\n");
return EXIT_SUCCESS;
}

"\x1b is numeric escape for ESC" is true, but the hex escape sequence here is \x1b1, not \x1b.
Avoid printf(st) to print a string st. The first argument to printf() is for a format. OK in OP's case, but better to avoid that maybe some escape sequence was used that was the same as %.
Try puts("1" "\x1b" "1");.

suggest correcting the syntax:
printf("1\x1b\1\n");

Related

Are there other ways to specify or enter a Unicode code point in C other than using string literals?

In the following program I am trying to provide a Unicode code point to the ncurses function setcchar() as an array string instead of as a string literal. However the output that I'm getting is the first character of the array only, namely the backslash character.
Is there another way to specify a Unicode code point other than as a string literal? And why are the two expressions L"\u4e09" and wcsarr not producing the same result in this context...
#define _XOPEN_SOURCE_EXTENDED 1
#include <curses.h>
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <wchar.h>
#include <time.h>
int main() {
setlocale(LC_ALL, "");
cchar_t kanji;
wchar_t wcsarr[7];
wcsarr[0] = L'\\';
wcsarr[1] = L'u';
wcsarr[2] = L'4';
wcsarr[3] = L'e';
wcsarr[4] = L'0';
wcsarr[5] = L'9';
wcsarr[6] = L'\0';
initscr();
setcchar(&kanji, wcsarr, WA_NORMAL, 5, NULL);
addstr("Code point entered as an array string: ");
add_wch(&kanji);
addstr("\n");
setcchar(&kanji, L"\u4e09", WA_NORMAL, 5, NULL);
addstr("Code point entered as a string literal: ");
add_wch(&kanji);
addstr("\n");
refresh();
getch();
endwin();
return EXIT_SUCCESS;
}
An array containing the six characters \u4e09 is an array containing six characters, just as an array containing a backslash followed by an n is an array of two characters, not a newline. The compiler converts escape sequence in literals. Nothing (except what you yourself write) does anything to character arrays.
So your array wcsarr is not a single wide character. It's a (null-terminated) wide string using six wchar_t values to encode six ascii characters. setcchar requires that its second argument contain only one spacing character (possibly followed by several non-spacing combining characters), and your program does not conform to this specification.
You could do something like this:
wchar_t wcsarr[] = {0, 0};
wcsarr[0] = L'\u4e09`;
If you knew that your locale used Unicode code points as wide character codes, you could write:
wcsarr[0] = 0x4e09;
since wchar_t, like char, is an integer type. That's occasionally useful if you need to compute a character code (such as non-latin digits), but normally it's considered better style to use wide character literals.
If you really need to decode a character string containing an escape sequence, you'll need to verify that the syntax is correct and then use something like strtol with the base parameter set to 16. Note, however, that strtol does not have any mechanism to restrict its argument to exactly four digits, so if the escape sequence appears in text where it might be followed by what looks like a hexadecimal digit, you will have to somehow extract it. Either copy it to a temporary buffer, or null-terminate it if the character string can be modified. Or you could write your own hexadecimal decoder; it's not hard.

Why do '?' and '\?' give the same output in C?

In C, why do these two pieces of code give the same output?
#include<stdio.h>
int main(void)
{
const char c='\?';
printf("%c",c);
}
and
#include<stdio.h>
int main(void)
{
const char c='?';
printf("%c",c);
}
I understand that a backslash is used to make quotes (" or ') and a backslash obvious to the compiler when we use printf(), but why does this work for the '?'?
\? is an escape sequence exactly equivalent to ?, and is used to escape trigraphs:
#include <stdio.h>
int main(void) {
printf("%s %s", "??=", "?\?="); // output is # ??=
}
Quoting C11, chapter §6.4.4.4p4
The double-quote " and question-mark ? are representable either by themselves or by the escape sequences \" and \?, respectively, but ... .
Emphasis mine
So the escape sequence \? is treated the same as ?.
Because '\?' is a valid escape code, and is equal to a question-mark.
when you're defining a char or string the compiler parses backslash in that char or string as an escape sequence.
**
the simple answer of your question is
\? means ?. instead of using \? you can using ? .
\? is escape representation and ? is character representation means both are same.
i have linked a image so that you understand it more easily..
**
"click here to see the image " --> in this image you need to find \? in Escape character

How does one use shift sequences to output a character from another character set

Reading about how to use shift sequences to print characters from other character sets I've arrived at the following code (of which I'm sure the escape sequence is incorrect, however I do not know why):
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("\x1B\x28\x49\x0E\xB3"); /* Should print: ウ */
return 0;
}
This however is not working for me as it outputs a "?" in the terminal rather than the character "ウ". My font does indeed have support for the character. If someone could explain what I'm doing incorrectly and how I would go about correcting this(still using shift sequences), that would be greatly appreciated.
Thank you
Your are using ISO-2022-JP-3. Hence you need to write your program as follows:
int main ()
{
// switch to JIS X 0201-1976 Kana set (1 byte per character)
printf ("\x1B(I");
printf ("\x33"); /* ウ */
// mandatory switch back to ASCII before end of line
printf ("\x1B(B");
printf ("\n");
return 0;
}
Note however that it is unlikely to be the character set expected by the terminal (on linux, this is most likely UTF-8). You can use iconv to perform the conversion:
$ ./main | iconv -f ISO-2022-JP-3
Alternatively you can use iconv(3) to perform the conversion inside your program.
What happens if you do echo 'ウ' >/tmp/x && od -x /tmp/x - do you see the same hex characters as you are using in the example above? I'm betting not, and I've based this answer on that bet.
Your cat works because ウ is encoded in your source file as UTF-8.
You have your terminal set to UTF-8 (or more likely it's just defaulting to UTF-8) so UTF-8 works, but Shift-JIS does not.

Greek characters in gcc and warning

I have a simple problem.
#include <stdio.h>
#include <stdlib.h>
int main()
{
char c=getc(stdin);
if (c=='φ')
{
printf("in");
}
}
This piece of code does behave strangely.Try it yourself if you want to.If you input the Greek character φ in console,the if statement is false.If you change 'φ' to 'f' and repeat it works like a charm.Also been getting the warning multi-character character constant [-Wmultichar]|.Any advice?Thanks.
Very probably your terminal emulator is using UTF8 encoding. In that encoding φ (U+03C6 GREEK SMALL LETTER PHI) is two bytes : "\317\206" (octal string) or "\xCF\x86" (hex string)
You can find a lot of explanation about reading UTF8 in C e.g. this blog entry

How to escape the % (percent) sign in C's printf

How do you escape the % sign when using printf in C?
printf("hello\%"); /* not like this */
You can escape it by posting a double '%' like this: %%
Using your example:
printf("hello%%");
Escaping the '%' sign is only for printf. If you do:
char a[5];
strcpy(a, "%%");
printf("This is a's value: %s\n", a);
It will print: This is a's value: %%
As others have said, %% will escape the %.
Note, however, that you should never do this:
char c[100];
char *c2;
...
printf(c); /* OR */
printf(c2);
Whenever you have to print a string, always, always, always print it using
printf("%s", c)
to prevent an embedded % from causing problems (memory violations, segmentation faults, etc.).
If there are no formats in the string, you can use puts (or fputs):
puts("hello%");
if there is a format in the string:
printf("%.2f%%", 53.2);
As noted in the comments, puts appends a \n to the output and fputs does not.
With itself...
printf("hello%%"); /* like this */
Use a double %%:
printf("hello%%");
Nitpick:
You don't really escape the % in the string that specifies the format for the printf() (and scanf()) family of functions.
The %, in the printf() (and scanf()) family of functions, starts a conversion specification. One of the rules for conversion specification states that a % as a conversion specifier (immediately following the % that started the conversion specification) causes a '%' character to be written with no argument converted.
The string really has 2 '%' characters inside (as opposed to escaping characters: "a\bc" is a string with 3 non null characters; "a%%b" is a string with 4 non null characters).
Like this:
printf("hello%%");
//-----------^^ inside printf, use two percent signs together
You can use %%:
printf("100%%");
The result is:
100%
You are using the incorrect format specifier. You should use %% for printing %. Your code should be:
printf("hello%%");
Read more all format specifiers used in C.
The backslash in C is used to escape characters in strings. Strings would not recognize % as a special character, and therefore no escape would be necessary. printf is another matter: use %% to print one %.
You can simply use % twice, that is "%%"
Example:
printf("You gave me 12.3 %% of profit");
Yup, use printf("hello%%"); and it's done.
The double '%' works also in ".Format(…).
Example (with iDrawApertureMask == 87, fCornerRadMask == 0.05):
csCurrentLine.Format("\%ADD%2d%C,%6.4f*\%",iDrawApertureMask,fCornerRadMask) ;
gives the desired and expected value of (string contents in) csCurrentLine;
"%ADD87C, 0.0500*%"

Resources