Brace yourself, young noob. This C FAQ is a mighty distillation of ancient wisdom, gathered through the ages and across countless forum threads. It's the result of long, perilous quests, deep dives into messages long forgotten by time - and by search engines. Among its original authors are legendary figures such as Phoenix, Devlin and Darth JarJar - names whispered with awe in the coding underworld.
Yes, dear noob, every effort has been made to dazzle your eyes and enlighten your brain. So if one day, while wandering the SiteRaw forums, you spot someone asking a question that's already been answered here - don't hesitate to guide them towards this sacred scroll of knowledge.
This FAQ was built (mostly) from the most frequently asked questions on the C programming forums of www.siteraw.com, along with the battle-tested experience of its authors.
List of questions:
- [1] User Inputs and scanf
- [1][1] Why is scanf considered bad?
- [1][2] Why shouldn't I use fflush on stdin?
- [1][3] How do I clean up after scanf?
- [1][4] How do I read integers with scanf/printf? What about floats?
- [2] The Console
- [3] Characters & Strings
- [3][1] Why shouldn't I use char to store negative integers?
- [3][2] How do I define a character?
- [3][3] How can I get the ASCII value of a character?
- [3][4] How do I define strings?
- [4] Other C Questions
User Inputs and scanf
99% of all programing problems come from the user. I don't know if this quote is actually true, but many developers certainly feel that way about user inputs in C. In this part, we will cover the infamous scanf
and printff
functions.
Why is scanf considered bad?
When scanf
reads the input exactly as expected, everything mostly works out fine. But the moment a user types something unexpected, scanf
basically gives up. It stops reading and leaves the input buffer in a messy state. If you try to use scanf
again right after, it'll just keep tripping over the same leftover garbage and make things even worse.
In short: scanf
doesn't clean up after itself — you have to do that part manually.
Here's a handy tutorial that shows how to live a happy, scanf
-free life, and another one that teaches you how to use it properly (if you must).
Why shouldn't I use fflush on stdin?
Because the C standard says... nothing. It's undefined behavior. Which is a fancy way of saying: anything can happen. On some systems, it might work. On others, it might do absolutely nothing. Or worse — it might break things in ways that make your debugger cry.
So yeah, using fflush(stdin)
is a bad idea. Just don't.
This rule applies to any input stream or file opened in read mode — or in read/write mode if the last operation was a read.
Nowadays fflush()
is mostly used for output stream only, anyway.
How do I clean up after scanf?
To flush out whatever junk scanf
left behind (especially when the user didn't follow the rules), you can loop through the input until you hit the newline character (\n
):
void cleanscan(void)
{
int c;
do {
c = getchar();
} while (c != '\n' && c != EOF);
}
Or, you can use this little two-line trick:
// This line skips everything up to (but not including) the newline
scanf("%*[^\n]");
// And this one gobbles up the newline itself
getchar();
Either way, you'll be left with a clean input buffer and a clear conscience.
How do I read integers with scanf/printf? What about floats?
Each variable type has its own specific format string for scanf
and printff
. If you blindly use %ld
and %lf
for everything, expect bugs to show up at your doorstep.
It gets a little technical — don't go using entries with "C99+" on older compilers — but here's a quick breakdown:
For integers:
%hhd
–signed char
(C99+)%hhu
–unsigned char
(C99+)%d
–int
%u
–unsigned int
%ld
–long int
%lu
–unsigned long int
For floating-point values:
%f
–float
ordouble
(forprintff
)%f
–float
(forscanf
)%lf
–double
(forprintff
in C99+)%lf
–double
(forscanf
)
Use the right format, and your code will thank you. Or at least it won't explode.
The Console
How do I clear the console?
In purely standard (and portable) C, you can't. You need to use commands or functions from third-party libraries, which isn't recommended.
system("cls");
and system("clear");
?This will just run the clearing command from Windows' Batch (with cls
) or Unix' Shell (with clear
). At best, you can use a #if defined(_WIN32) || defined(_WIN64)
condition for those.
Characters & Strings
Why shouldn't I use char to store negative integers?
Because char
is... ambiguous. Whether it can hold negative values depends on your compiler and system. It might work. Or it might not.
char
will behave like either signed char
or unsigned char
, at the implementation's discretion. Most other types have a default of unsigned. If you need to store both positive and negative numbers, use signed char
. It's explicitly made for that purpose.
How do I define a character?
You can define a character using either char
or int
. Here’s an example:
char char1 = 'a';
int char2 = 'a';
char char3 = 97; // 97 is the ASCII value for 'a'
int char4 = 97;
printf("%c\n", char1);
printf("%c\n", char2);
printf("%c\n", char3);
printf("%c\n", char4);
Output:
a a a a
No matter how you declare it — as a number or as a character — you'll get the same printed result if the value corresponds to a valid ASCII character.
How can I get the ASCII value of a character?
You've got two options:
- Look it up in an ASCII table
- Or just use this simple code
unsigned char c = '$'; // Let’s find the ASCII value of '$'
printf("%ld", (long int) c);
unsigned char
. If you use char
and the character comes from the extended ASCII set, you might end up with a negative value — which we told you is wrong.The example above will print:
36
(Which is indeed the ASCII code for $
.)
How do I define strings?
A string in C is just a character array, with one twist: it must end with a special \0
character that marks the end of the string.
// This string can hold 19 characters + the '\0' terminator
char s[20];
There are a few ways to initialize a string when you create it.
char str1[8];
// Initialize the second one right away
char str2[8] = "siteraw";
// Fill the first one manually
str1[0] = 's';
str1[1] = 'i';
str1[2] = 't';
str1[3] = 'e';
str1[4] = 'r';
str1[5] = 'a';
str1[6] = 'w';
str1[7] = '\0';
printf("%s\n", str1);
printf("%s\n", str2);
And the output:
siteraw siteraw
Both versions do the same thing. Just make sure you don't forget the \0
at the end — it's what tells C where the string ends.
Other C Questions
Does the goto statement really exist in C?
goto
exists and is not recommended for general use.
The rule is, in principle, that if you can do without goto
and without making the code completely obscure, convoluted, or too large, you should do without it. See the part about loops and conditions from the official tutorial.
This is the end of the FAQ for the C coding language.
Enjoyed this C / C++ course?
If you liked this lesson, you can find the book "Learn C Programing for Beginners" from the same authors, available on SiteRaw, in bookstores and in online libraries in either digital or paperback format. You will find a complete C / C++ workshop with many exclusive bonus chapters.