What does \a mean in C? [duplicate] - c

This question already has answers here:
This program sounds the bell!
(13 answers)
Closed 5 years ago.
I've just come across this:
#include <stdio.h>
int main() {
printf(" \a ");
}
and the output isn't " \a " as expected. Does anybody know why?

The '\a' in an escaped representation of the BEL charcater which has ascii code 7.
The \ is used to "escape" a character that otherwise has no representation and cannot be written in a string by other means. Another examples are the newline '\n' and carriage return '\r'.

Characters starting with a backslash \ are called escape sequences, these are special and aren't printed out. In this case, \a is the sequence for a bell ring.

Related

Why "$" is printed when I try to run the C code below? [duplicate]

This question already has an answer here:
What is the difference between %c and %s in C [duplicate]
(1 answer)
Closed 4 years ago.
#include <stdio.h>
int main()
{
printf("%c\n","\"\"\"\"\"");
}
This prints $ but not """""
Why "$" is printed when I try to run the C code above?
You pass a string to printf, not a char. Try '"'
What happens in your code is that the string is passed to printf, but printf expects a char. printf now tries to interpret the address of this string as a char. In general, this is undefined behavior.
%c is the format specifier for a single character; there is probably some conversion going on. You want %s (for strings).
It actually prints some garbage, because your input is not a single character, is a sequence of characters. Try using %s instead of %c

C input char and int separated by whitespace [duplicate]

This question already has answers here:
scanf() only sign and number
(2 answers)
Closed 7 years ago.
I need to parse input like this: "+ 704"
Into: switcher = "+" and c = 749
I got this:
scanf("%c %d", &switcher, &c);
This does not work.
Scanf returns 1 instead of 2, c = 4196080 and printf("%c", switcher) prints a newline.
What am i missing?
So basically, there's a newline waiting on the buffer. To get rid of it, simply add one space to the beginning of your formatting string.
scanf(" %c %d", &switcher, &c);

Get words which contains space in c [duplicate]

This question already has answers here:
Simple C scanf does not work? [duplicate]
(5 answers)
Closed 7 years ago.
I need to get words from user which contains space as I expressed at title with struct statement.
For example :
#include <stdio.h>
struct knowledge
{
char name[30];
}person;
int main()
{
scanf("%s",person.name);
printf("\n\n%s",person.name);
}
When I run this program and enter a sentence like "sentence" there is no problem. It show me again "sentence".
However, when I enter "sentence aaa" it shows me just first word ("sentence"). What is the matter here? Why it doesn't show me all ("sentence aaa") I entered?
Instead of scanf() use
fgets(person.name,sizeof(person.name),stdin);
It is always a bad idea to use scanf() to read strings. The best option is to use fgets() using which you avoid buffer overflows.
PS: fgets() comes with a newline character
%s format specifier stops scanning on encountering either whitespace or end of stream. hence, you cannot input a "sentence" with space using scanf() and %s.
To scan a "whole line" with space, you need to use fgets(), instead.

why one single \ in a printf function does not shown in output console screen but double \\ does it?

Why one single \ in a printf function does not show in the output console screen but double \\ does?
#include <stdio.h>
#include <stdlib.h>
int main()
{
//why is double \\ necessary?
printf("\\");
printf("\");
//why here shown error in the second printf?
printf(" " "dd " " ");
//why its executed successfully
printf(" " "" ");
//why it is shown errow
return 0;
}
The \ in the printf() statement is used for escaping characters. For example, \n means newline, \0 means null character. etc. Therefore, when you use simply a \, it expects some character which is to be escaped. In the second printf(), the character to be escaped becomes ", rendering your printf() statement incomplete. So it shows an error, whereas in the first code, there is a \ after the first \, so it is treated as a character to be displayed rather than an escape character.
From Wikipedia
An escape sequence is a sequence of characters that does not represent itself when used inside a character or string literal, but is translated into another character or a sequence of characters that may be difficult or impossible to represent directly.
\ is already used as the first character for escape sequences, for example \n is the new line and \t is the horizontal tab. To print a \ character you need the \\ escape sequence.
backslash is used for Escape sequences.
An escape sequence is a sequence of characters that does not represent itself when used inside a character or string literal, but is translated into another character or a sequence of characters that may be difficult or impossible to represent directly. (From wikipedia)
In C , look :
\n for new line , \t for tab are used. here back slash is used as an escape sequence which doesnot represent itself
printf("\\");
here , first backslash is used as escape sequence. so second \ backslash will be printed.
but
printf("\");
but for this case , only escape sequence . so it will show a error
because back slash i.e. \ is used for escape sequences. These escape sequences are used for performing special tasks like formatting, playing a beep (\a) and since it is treated as a special character the compiler thinks that you want to perform some special task when you write "\" in printf which means you won't be able to print \ so in order to print \ you just type it twice "\"

How to stop a program from storing more than a single character into a char variable? [duplicate]

This question already has answers here:
How to prevent scanf causing a buffer overflow in C?
(6 answers)
Closed 8 years ago.
My program uses a scanf as such:
scanf ("%c", &symbol);
is there a way to print an error if the user enters in a string > one character? e.g "abc" as it messes with the program later on
Use a string buffer, fgets() into it, check if the second character is a \n.

Resources