How to print a parenthesis in C with printf? - c

I am trying to print a parenthesis using:
printf("\)");
However, it is giving me the following warning:
warning: unknown escape sequence '\)'
I can't seem to find a clear explanation anywhere on how to fix this.
I realize it's just a warning, but since it's still treating that as a parenthesis it's throwing off all my other parentheses and giving me errors so that the code does not compile.
EDIT: Treating it as a regular character and just saying printf(")") is not working. It's still mismatching all the parentheses and I have gone through multiple times to make sure I'm not actually missing any.

The warning is coming from the C compiler. It is telling you that \ is not a known escape sequence in C. You need to double-escape the slash, like so: \\
Edit: if you just want to print the parenthesis, i.e. the ) then drop the slash altogether and use:
printf(")");

try this:
#include <stdio.h>
int main()
{
printf("Printing quotation mark \")\" ");
}
you need to add an escape character to get the quote to print which in this case is \"
This will result in Printing quotation mark ")"

just write parenthesis in double quote " " ,because parenthesis is not a escape character .
try this :
#include<stdio.h>
int main(){
printf( "( )" ); // print parenthesis here
}

Hope this helps.
Using variables seems to be a viable solution using my compiler.
#include <stdio.h>
int main() {
char var = ')';
printf("Hello, World!\n");
printf("Success :%c",var); //As you can see this is one way to go about the problem
return 0;
}

Related

Printing a character from a 2D Array

Using pointers, I am able to get to the actual character('i' in ElGenerico) that I want to print. But some weird character is getting printed on the screen, rather than my desired character.
#include<stdio.h>
int main()
{
char *name[]={"Sami","Kevin","ElGenerico"};
printf("%c",(*(name+2)+7));
return 0;
}
With my use of %s output specifier, the output of this code is "ico".
But I want to print only the character 'i'. So I tried using %c, instead of %s. It doesn't work. Instead, a double headed arrow is printed.
Can anyone please tell me where I am going wrong?
I recommend using bracket notation for improved readability.
Initially, you had this:
printf("%c",(*(name+2)+7));
You would still need to dereference with * to get the desired output.
That would give you this:
printf("%c",*(*(name+2)+7));
However, that's still a bit confusing and not very readable. You could make it much cleaner by using bracket notation, like this:
printf("%c", name[2][7]);
Now there's much less room for error and you still get the expected output.
Your final code would look something like this:
#include <stdio.h>
int main()
{
char* name[] = {"Sami", "Kevin", "ElGenerico"};
printf("%c", name[2][7]);
return 0;
}
You are pointing to the wrong address. Use this instead:
printf("%c",(*(name[2]+7)));
Since you are referring to the third element in the name array, you can use
name[2]
Then you wanted to put the 7th element of the word ElGenerico, hence we need to add 7 to the address:
name[2] + 7
After we get the correct address, then we print out the value of that address, and we use the * sign:
*(name[2] + 7)
printf("%c",*(*(name+2)+7));
I used this, and it's solved, thanks for the help #David and #rcs...

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

Printing out backslash in C quine program

I'm trying to write a quine program for the follow C source code:
#include<stdio.h>
char name[] = "Jacob Stinson";
int main(){
char *c="#include<stdio.h> char name[] = \"Jacob Stinson\"; int main(){char *c=%c%s%c; prinf(c,34,c,34);}";
printf(c,34,c,34);
}
I need to include the backslash before the " in the string in order to properly print out line 3, however, when I print out *c, I want those backslashes to be present, as to correctly copy the source code. Currently it omits the backslashes from the output.
Wanted to see if anyone knows how to go about doing this.
As the compiler interprets escape sequences in only one direction (deescaping them) I think there's no possibility to include an escape sequence in the code and make it appear as such in the listing. The compiler will always eliminate one of the backslashes on input of the source file, making it appear different on output. The printf uses %s format to allow for the recursive part of the problem and allow you to shelf print, and, as you have guessed correctly, you have to use integer versions of delimiting chars for the " delimiting chars. Why to use %c to be able to delimit the strings in your program if there's an alternative method to include escape sequences? By the same reason, I was not able to include any end of line delimiter, so I wrote the same problem in one line (without using the #include <stdio.h> line) My solution was a one line (without the final end of line.)

print text via pointer

i am learning now c and i come up with this example, where i can print a text using pointers.
#include <stdio.h>
main ()
{
char *quotes = "One good thing about music, when it hits you, you feel no pain. \"Bob Marley\"\n";
printf(quotes);
}
I get a warning from the compiler "format not a string literal and no format arguments" and when I execute the program it runs successfully.I read some other questions here that they had the same warning from the compiler but I didn't find an answer that fits me. I understood the reason why i get this message:
This warning is gcc's way of telling you that it cannot verify the format string argument to the printf style function (printf, fprintf... etc). This warning is generated when the compiler can't manually peek into the string and ensure that everything will go as you intend during runtime...
Case 3. Now this is somewhat your case. You are taking a string generated at runtime and trying to print it. The warning you are getting is the compiler warning you that there could be a format specifier in the string. Say for eg "bad%sdata". In this case, the runtime will try to access a non-existent argument to match the %s. Even worse, this could be a user trying to exploit your program (causing it to read data that is not safe to read).
(See the answer)
but what i have to add in my case to in order to have not warnings from the compiler?
Change it to printf("%s", quotes); which adds the specifier that quotes is a 'string', or array of char.
You need to tell printf what is it that you are printing. %s descriptor will tell printf that you are printing a string.
Format of printf = ("descriptor of what type of data you are printing",variable holding the data);
descriptor for strings is %s, for characters %c, for int %d
Change printf to:
printf("%s",quotes);
You have to specify format string - in simplest form:
char *quotes = "One good thing about music(...)\n";
printf("%s", quotes);
or, you can use format string to decorate output:
char *quotes = "One good thing about music(...)"; // no newline
printf("%s\n", quotes); // newline added here
or, if you don't want to mess with format strings:
char *quotes = "One good thing about music(...)"; // no newline
puts(quotes); // puts() adds newline
or
char *quotes = "One good thing about music(...)\n";
fputs(quotes,stdout);
This warning is gcc's way of telling you that it cannot verify the format string argument to the printf style function (printf, fprintf... etc). This warning is generated when the compiler can't manually peek into the string and ensure that everything will go as you intend during runtime. Lets look at a couple of examples.
So as other suggested explicitly use format specifier to tell the compiler...i.e.
printf("%s",quotes);
You are getting the warning because it is dangerous when the string you are printing contains '%'. In this line it makes no sense for percents but when you want to print this for instance:
int main ()
{
int percent = 10;
char *s = "%discount: %d\n";
printf(s, percent);
return 0;
}
your program will likely crash when printf encounters the second percent and it tries to pop a value from the stack from printf.
When you want to print a percent sign use: "%%discount:"
Try this:
#include <stdio.h>
main ()
{
char *quotes = "One good thing about music, when it hits you, you feel no pain. \"Bob Marley\"\n";
puts(quotes); //Either
printf("%s",quotes);//or
return 0;
}

Compilation Error in C-File handling

#include<stdio.h>
#include<conio.h>
FILE *fp;
int main()
{
int val;
char line[80];
fp=fopen("\Users\P\Desktop\Java\a.txt","rt");
while( fgets(line,80,fp)!=NULL )
{
sscanf(line,"%d",&val);
printf("val is:: %d",val);
}
fclose(fp);
return 0;
}
Why is there a compile error in the line fp=fopen("\Users\P\Desktop\Java\a.txt","rt")?
Escape your backslashes.
fp=fopen("\\Users\\P\\Desktop\\Java\\a.txt","rt");
xx.c:8:12: error: \u used with no following hex digits
fp=fopen("\Users\P\Desktop\Java\a.txt","rt");
^
xx.c:8:12: warning: unknown escape sequence '\P'
xx.c:8:12: warning: unknown escape sequence '\D'
xx.c:8:12: warning: unknown escape sequence '\J'
The issue with the backslash. Backslash is an escape in a C char string.
Try this
fp=fopen("\\Users\\P\Desktop\\Java\\a.txt","rt");
or this depending on your OS:
fp=fopen("/Users/P/Desktop/Java/a.txt","rt");
You may be familiar with how "\n" (newline) and "\t" (tab) are used in C-strings.
The compiler will look at any \<Character> and try to interpret it as an Escape-Sequence.
So, where you wrote "\Users\P\Desktop\Java\a.txt", the compiler is trying to treat
\U, \P, \D, \J and \a as special escape-sequences.
(The only one that seems to be valid is \a, which is the Bell/Beep sequence. The others should all generate errors)
As others have said, use \\ to insert a literal Backslash character, and not start an escape sequence.
P.S. Shame on you for not including the the compiler message in your question.
The worst questions all say, "I got an error", without ever describing what the error was.

Resources