Warning message errors in c [closed] - c

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed last month.
Improve this question
warning: unknown escape sequence: '\!' [enabled by default]
printf("\\ this is a back-slash character\!\n");
^
w1p2.c:25:9: warning: unknown escape sequence: '\!' [enabled by default]
printf("%% this is a percent sign character\!\n");
^
w1p2.c:26:9: warning: unknown escape sequence: '\!' [enabled by default]
printf("\" this is a double - quote character\!\n");
I made a simple printf program and I keep getting these messages.
Exactly what's going on and how do I fix it?
I tried using the \ symbol before it but didn't do much.

Exactly what's going on and how do I fix it?
Only some characters may be escaped (C11 §6.4.4.4, §6.4.5, §6.4.3).
! is not one of them and a \ is not needed to print a !.
// printf("\\ this is a back-slash character\!\n");
printf("\\ this is a back-slash character!\n");
// ^
simple-escape-sequence: one of
\’ \" \? \\
\a \b \f \n \r \t \v
octal-escape-sequence:
\ octal-digit
\ octal-digit octal-digit
\ octal-digit octal-digit octal-digit
hexadecimal-escape-sequence:
\x hexadecimal-digit
hexadecimal-escape-sequence hexadecimal-digit
universal-character-name:
\u hex-quad
\U hex-quad hex-quad

Related

What is the function of a \ between two strings in a C language definition? [duplicate]

This question already has answers here:
What does it mean to have a block of c++ code with a backslash after each semicolon?
(1 answer)
backslash usage in C language
(3 answers)
Closed 8 months ago.
I have a JSON object in a definition as follows:
#define REGISTRER_DEVICE \
"{" \
"\"Type\":1" \
"," \
"\"Uid\":\"92a5aa89c771de67\""\
"," \
"\"Port\":6787" \
"," \
"\"TypeConn\":0" \
\
"}"
My intention is to stop using a #define to put the information in an array of type char, but I don't understand is what does the \ between each string do.
Any comment or suggestion is welcome.
Regards
If \ is immediately followed by a new-line character1 in the source code, both are removed logically so that the next physical line of source code is logically joined to the current line. This happens at an early phase of the compilation (translation phase 2 in the C standard), before preprocessor directives such as #define are processed (at translation phase 4).
Logically, preprocessor directives such as #define begin and end on a single logical line, so \ can be used to split the directive over several physical lines.
In OP's example code:
#define REGISTRER_DEVICE \
"{" \
"\"Type\":1" \
"," \
"\"Uid\":\"92a5aa89c771de67\""\
"," \
"\"Port\":6787" \
"," \
"\"TypeConn\":0" \
\
"}"
The physical lines are joined into a single logical line:
#define REGISTRER_DEVICE "{" "\"Type\":1" "," "\"Uid\":\"92a5aa89c771de67\"""," "\"Port\":6787" "," "\"TypeConn\":0" "}"
Each non-empty sequence of white-space characters other than new-line might be replaced (at translation phase 3) by a single space, or it might not. It is implementation-defined. For clarity, the replacement would result look like this:
#define REGISTRER_DEVICE "{" "\"Type\":1" "," "\"Uid\":\"92a5aa89c771de67\"""," "\"Port\":6787" "," "\"TypeConn\":0" "}"
The expansion of the REGISTRER_DEVICE macro consists of a sequence of concatenated string literals. If the macro is expanded somewhere in the source code (during translation phase 4), that sequence of concatenated string literals in the expansion will be converted (during translation phase 6) to the equivalent single string literal:
"{\"Type\":1,\"Uid\":\"92a5aa89c771de67\",\"Port\":6787,\"TypeConn\":0}"
That string literal contains backslash escape sequences and encodes the following null-terminated sequence of characters, which is the JSON object source:
{"Type":1,"Uid":"92a5aa89c771de67","Port":6787,"TypeConn":0}
(There is a null terminator at the end of the array of characters represented by the string literal to mark the end of the string. This null terminator is not shown above and is not part of the JSON source for the object.)
1 The end of line sequence in the physical source file will have been mapped to the new-line character during translation phase 1 in the C standard. For example MS-DOS/Windows text files mark the end of line with an (ASCII) CR+LF sequence, but that sequence will have been mapped to a single new-line character by the compiler.
This is just line continuation. In this case it helps us define a string on multiple lines to make it more readable. The macro will replace REGISTRER_DEVICE with "{\"Type\":1,\"Uid.. but this would not be easy for you or any other reader of the code to parse.

C lexer: token concatenation of unterminated string literals

Consider the following c code:
#include <stdio.h>
#define PRE(a) " ## a
int main() {
printf("%s\n", PRE("));
return 0;
}
If we adhere strictly to the tokenization rules of c99, I would expect it to break up as:
...
[#] [define] [PRE] [(] [a] [)] ["]* [##] [a]
...
[printf] [(] ["%s\n"] [,] [PRE] [(] ["]* [)] [)] [;]
...
* A single non-whitespace character that does not match any preprocessing-token pattern
And thus, after running preprocessing directives, the printf line should become:
printf("%s\n", "");
And parse normally. But instead, it throws an error when compiled with gcc, even when using the flags -std=c99 -pedantic. What am I missing?
From C11 6.4. Lexical elements:
preprocessing-token:
header-name
identifier
pp-number
character-constant
string-literal
punctuator
each non-white-space character that cannot be one of the above
3 [...] The categories of preprocessing tokens are: header names,
identifiers, preprocessing numbers, character constants, string
literals, punctuators, and single non-white-space characters that do
not lexically match the other preprocessing token categories.69) If a
' or a " character matches the last category, the behavior is
undefined. [...]
So if " is not part of a string-literal, but is a non-white-space character, the behavior is undefined. I do not know why it's undefined and not a hard error - I think it's to allow compilers to parse multiline string literals.
it throws an error
But on godbolt:
<source>:3:16: warning: missing terminating " character
3 | #define PRE(a) " ## a
| ^
<source>:6:24: warning: missing terminating " character
6 | printf("%s\n", PRE("));
| ^
<source>:8: error: unterminated argument list invoking macro "PRE"
8 | }
|
<source>: In function 'int main()':
<source>:6:20: error: 'PRE' was not declared in this scope
6 | printf("%s\n", PRE("));
| ^~~
<source>:6:20: error: expected '}' at end of input
<source>:5:12: note: to match this '{'
5 | int main() {
| ^
it throws an error not on #define PRE line (it could), but on PRE(") line. Tokens are recognized before macro substitutions (phase 3 vs phase 4), so whatever you do you can't like "create" new lexical string literals as a result of macro substitution by for example gluing two macros or like you want to do. Note that -pedantic will not change the warning into error - -pedantic throws errors where standard tells to throw error, but the standard tells the behavior is undefined, so no error is needed there.

Warning: unknown escape sequence: '\040', why not '\x20'? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
When I compile a C file has contents below:
#include <stdio.h>
#define FILE_NAME "text\ 1"
int main()
{
FILE* file_ptr = fopen(FILE_NAME, "w");
fclose(file_ptr);
return 0;
}
get warning:
tt.c: In function ‘main’:
tt.c:6:37: warning: unknown escape sequence: '\040'
6 | FILE* file_ptr = fopen(FILE_NAME, "w");
|
I know it caused by \ in a string of my C language code and 40 is decimal 32 as ASCII of SPACE. Why the warning is '\040' not '\x20'?
And seems also in bash \ transfer to \040 seed to binaries (not sure).
Is there a rule to force it?
Update:delete '\32' which used to represent ASCII of SPACE to decimal.
How I encounter this problem?
I just wanna know how Bash process ESCAPED SPACE, I thougt bash turn it to SPACE, but after I check source code of Bash (hard for me). I found maybe Bash treat \ as normal string of characters as below source code not involved \ :
#define slashify_in_quotes "\\`$\"\n"
#define slashify_in_here_document "\\`$"
#define shell_meta_chars "()<>;&|"
#define shell_break_chars "()<>;&| \t\n"
#define shell_quote_chars "\"`'"
So I think Bash turn the \ to the command or binary to process, so I write above simple C file to check how C treat \
So my question is Why gcc warning '\040' not '\x20'?
For how Bash treat \ still need me to check...
Answer to Updated Question
Why the warning is '\040' not '\x20'?
This is merely a choice by the compiler implementors. When you have \ in a string or character constant followed by something that is not a recognized escape sequence, the compiler warns you. For example, if you had \g, the compiler would warn you that \g is not recognized. When the character after the \ may be unclear, because it is a white space character that cannot be distinguished from others (like space from tab) or is not a printable character, the compiler shows it by value in the error message. This helps you find the exact character in your text editor, in case some unprintable character has slipped into the source code. The compiler authors could have used hexadecimal but simply chose to use octal.
I will fault them for using an inconsistent style. In GCC 10.2, \g results in the message unknown escape sequence: '\g', but \ results in the message unknown escape sequence: '\040'. These should either be:
unknown escape sequence: 'g' and unknown escape sequence: '\040' or
unknown escape sequence: '\g' and unknown escape sequence: '\\040'.
Answer to Original Vague Question
C 2018 6.4.4.4 specific character constants in C source code, and paragraph 1 lists four choices for escape-sequence: simple-escape-sequence, octal-escape-sequence, hexdecimal-escape-sequence, and univesal-char-name.
An octal-escape-sequence is \ followed by one to three octal digits. Thus, \040 the character with code 0408 = 32, and \32 is the character with code 328 = 26.
There is no decimal escape sequence; \32 is an octal escape sequence, not decimal. (Also note that because octal escape sequences can have various lengths, if one wishes to follow it by an octal digit, one must use all three allowed digits. \324 will be parsed as one character, not as \32 followed by 4, whereas \0324 is \032 followed by 4.)
A hexadecimal-escape-sequence is \x followed by any positive integer number of hexadecimal digits. \x20 is equal to \040.
(A simple-escape-sequence is one of \', \", \?, \\, \a, \b, \f, \n, \r, \t, or \v. A universal-character-name is \u followed by four hexadecimal digits or \U followed by eight hexadecimal digits.)

warning: unknown escape sequence '\

I'm trying to run a regex through a system command in the code, I have gone through the threads in StackOverflow on similar warnings but I couldn't understand on how to fix the below warnings, it seems to come only for the closed brackets on doing \\}. The warnings seem to disappear but not able to get the exact output in the redirected file.
#include<stdio.h>
int main(){
FILE *in;
char buff[512];
if(system("grep -o '[0-9]\{1,3\}\\.[0-9]\{1,3\}\\.[0-9]\{1,3\}\\.[0-9]\{1,3\}' /home/santosh/Test/text >t2.txt") < 0){
printf("system failed:");
exit(1);
}
}
Warnings:
dup.c:9:11: warning: unknown escape sequence '\}'
dup.c:9:11: warning: unknown escape sequence '\}'
dup.c:9:11: warning: unknown escape sequence '\}'
dup.c:9:11: warning: unknown escape sequence '\}'
dup.c: In function 'main':
In C string literals the \ has a special meaning, it's for representing characters such as line endings \n. If you want to put a \ in a string, you need to use \\.
For example
"\\Hello\\Test"
will actually result in "\Hello\Test".
So your regexp needs to be written as:
"[0-9]\\{1,3\}\\\\.[0-9]\\{1,3\}\\\\.[0-9]\\{1,3\\}\\\\.[0-9]\\{1,3\\}"
instead of:
"[0-9]\{1,3\}\\.[0-9]\{1,3\}\\.[0-9]\{1,3\}\\.[0-9]\{1,3\}"
Sure this is painful because \ is used as escape character for the regexp and again as escape character for the string literal.
So basically: when you want to put a \ you need to write \\.

Difference between \% and %% [duplicate]

This question already has answers here:
Why is percentage character not escaped with backslash in C?
(4 answers)
How to escape the % (percent) sign in C's printf
(13 answers)
Closed 9 years ago.
After reading over some K&R C I saw that printf can "recognize %% for itself" I tested this and it printed out "%", I then tried "\%" which also printed "%".
So, is there any difference?
Edit for code request:
#include <stdio.h>
int main()
{
printf("%%\n");
printf("\%\n");
return 0;
}
Output:
%
%
Compiled with GCC using -o
GCC version: gcc (SUSE Linux) 4.8.1 20130909 [gcc-4_8-branch revision 202388]
%% is not a C escape sequence, but a printf formatter acting like an escape for its own special character.
\% is illegal because it has the syntax of a C escape sequence, but no defined meaning. Escape sequences besides the few listed as standard are compiler-specific. In all likelihood the compiler ignored the backslash, and printf did not see any backslash at runtime. If it had, it would have printed the backslash in the output, because backslash is not special to printf.
Both are not the same. The second one will print %, but in case of the first one, you will get compiler warning:
[Warning] unknown escape sequence: '%' [enabled by default]
The warning is self explanatory that there is no escape sequence like \% in C.
6.4.4.4 Character constants;
says
The double-quote " and question-mark ? are representable either by themselves or by the escape sequences \" and \?, respectively, but the single-quote ' and the backslash \ shall be represented, respectively, by the escape sequences \' and \\.
It is clear that % can't be represented as \%. There isn't any \% in C.
When "%%" is passed to printf it will print % to standard output, but "\%" in not an valid escape sequence in C. Hence the program will compile, but it will not print anything and will generate a warning:
warning: spurious trailing ‘%’ in format [-Wformat=] printf("%");
The list of escape sequences in C can be found in Escape sequences in C.
This won't print % for the second printf.
int main()
{
printf("%%\n");
printf("\%");
printf("\n");
return 0;
}
Output:
%

Resources