r/C_Programming • u/artymadeit • Mar 28 '26
Question Insane amount of yellow warnings
Hello,
i recently taught myself how to program in C by using a learning app. I already learned the python basics in uni beforehand (I'm studying mechanical engineering - don't expect too much of me), so it was quite easy to do. Now I am doing my first project, a little banking system that reads account info from a file into an array of structures, lets you do some basic operations with the accounts (e.g. make new ones, transfer, ...) and then writes the info back into the same file.
I would say that I managed to create an ugly-looking (the code is bilingual :P), but smart source code that is quite foolproof to use. However in my ~400 lines of code, CLion gives me 44 warnings. The entire scrollbar is just made up of yellow lines, even though I tested the program for glitches a lot and managed to repair all that I found. Is that normal?
PS: I used 'scanf' quite a lot, which makes up maybe 10-15 of these errors. Could someone explain to me why it wants me to use 'strtol'?
4
u/[deleted] Mar 28 '26
400LOC? Post it
4
u/SmokeMuch7356 Mar 28 '26 edited Mar 28 '26
Ideally your code should compile without any diagnostics; your compiler is telling you these things need attention for a reason.
It would really help if you could post at least some of the code and the associated warnings.
Re:
scanf- it works really well when your input is always well-behaved, but it falls down hard when it isn't.For example, if your code is
and your input is
12w3,scanfwill successfully convert and assign12toxand leave thew3in the input stream to foul up the next read. Ideally, you'd like to reject the whole input, butscanfmakes that hard to do.Alternately, you can read that input as a string and convert it with
strtol:2
u/epasveer Mar 28 '26
Maybe, for reference, you can post some of those warnings.
25
u/TheThiefMaster Mar 28 '26
Some warnings are for potential bugs, others are just for efficiency.
sscanf is less efficient than dedicated string to number conversion functions, for example.
24
u/latkde Mar 28 '26
C is a punishingly difficult language. The basics are easy, but you really have to know what you're doing in order to avoid all of the subtle traps and footguns. Python is relatively foolproof and will give you nice exceptions when something is wrong, but C's “undefined behavior” will turn against you in unpredictable ways (including looking like everything is working, even though the code is broken).
That is why C programmers rely on tools like compiler warnings, linters, sanitizers, and fuzzers to give us a fighting chance to write code that actually works. Some warnings that you encounter might seem excessively pedantic, but when writing C it's best to get into the habit of treating every warning as a hard error, and to enable any optional warnings (e.g.
-Wall -Wextra -Wpedanticin GCC/Clang). Manual testing is generally not sufficient, especially if you're not familiar with typical sources of UB.With the
scanf()family of functions, it's easy to end up with an undefined variable. Consider this style of code:This is unsafe! If the input doesn't match an integer pattern, then the variable
xwill not have been initialized and contains undefined contents. You must not read that variable in that case. Instead, you're supposed to check the scanf() return value to see whether all variables have been assigned successfully:Another common error is to use a format that doesn't match the type of the destination variable, e.g.
size_t x; scanf("%lu", &x)– should be%zu.If you're using sscanf() to parse a number from a string, then the strol() family of functions is generally easier to use safely. It also lets you discover where parsing stopped, which is helpful for dealing with syntax errors (e.g. discovering leftover junk). (You can also do that with
scanf("... %n", ..., &len), but doing it correctly takes more effort). Remember that you MUST checkerrnoafter calling strol(), and SHOULD check if there's leftover content that wasn't parsed.When trying to understand functions in the C standard library, I recommend reading the documentation on cppreference.com, e.g. https://en.cppreference.com/w/c/string/byte/strtol.html. The information on that site can be difficult to understand, but the material is close to the standard, it properly differentiates between different versions of the standard, and examples are generally high-quality.
7
u/non-existing-person Mar 28 '26
Even if most of these warnings may be "useless" and not applicable to your code, ALWAYS fix ALL warnings ASAP. If you start leaving "safe" warnings, you will start skipping the real ones, because you won't be checking 100s of warnings for some new one. If you have false positive warning and you absolutely know what you are doing, whiteflag it with pragma for that single line.
Warnings quite often will "work" on your PC, but as soon as someone tries to run it on different arch or environment he may get a crash.
In short, during development time, always use -Wall -Wextra and -Werror. Remove -Werror when you publish your code or production build is made.
1
u/glasket_ Mar 28 '26
Given your PS, it sounds like a clang-tidy message, which is a linter warning rather than a regular warning. The IDE should provide a way to ignore the warnings that you aren't concerned with.
Beginner's guide away from scanf is also worth reading, given the context.
Also, be aware that testing for bugs alone and ignoring warnings isn't foolproof in C due to undefined behavior. A program can behave normally until it suddenly doesn't.
3
u/Plane_Dust2555 Mar 28 '26
BTW... what is an "yellow" warning?
3
u/FUZxxl Mar 28 '26
To find out what the warning means, read the warning text. Then decide if it's something worth addressing.
2
u/tomysshadow Mar 28 '26 edited Mar 28 '26
The finicky annoying thing about C and C++ is that what works in one compiler or OS may not work in another. Warnings are typically trying to warn you "hey, this happens to work in CLion on this OS, but if somebody else tries to compile this on their machine using something else it might not work for them."
Key thing is, it doesn't really matter how well you've tested your code to ensure that it works in your environment, because as long as you're using a single compiler on a single machine, the program will be built in that same specific way that happens to work by coincidence. That's the point of warnings: to tell you about things you probably wouldn't catch via a test because it'd only fail under some different configuration or extremely specific circumstance. With very little exception, you should treat every warning as a message "there's a situation you haven't foreseen where this is not going to work as intended."
Unless the problem code lives in some library header you can't touch, it is virtually always possible to do something that will fix the root problem the warning is telling you about - if you catch it early enough. Often it's just a matter of explicitly casting a variable to a specific type instead of relying on the cast to happen implicitly (for example, you tried to compare a signed and unsigned integer - very easy to do without realizing.) Occasionally it points to a bigger flaw in your design that requires some reworking.
1
u/Plane_Dust2555 Mar 28 '26
One confusion made by new developers is that
printfmeans print function andscanfis an input funcion.printfmeans "print formatted string to stdout stream" andscanfmeans "scan stdin stream for formatted string".As u/TheThiefMaster says they are useful when you have some formatting, but they have lousy performance when compared to equivalent functions that don't use formats.
The
sprintfandsscanffunctions, of course, don't deal with streams and are a little bit better, but they deal with formatting as well.In essence,
strtolis way faster thansscanf. Unfortunately, there's noitoaorltoaanymore, sincesprintfcan do this and you may need to format the string. But there's anatoi,atolandatollto convert an string to integers (as well asatofandstrtod, for floating point).My tip is that you should use the approriate function to what you want to do, not only the "easiest" to remember ones.
1
u/tobdomo Mar 28 '26
Scanf and friends are dangerous and mostly misunderstood functions. They should be removed from libc AFAIC. fgets() in combination with strtol() is a safer solution, it offers you more control.
Anyway, warnings are there for a reason. They identify things that may be correct, but may hide sloppy code. Point in case: when using fgets(), it's easy to ignore the return value. getchar() returns... an int. Yet very few people check on EOF. Sloppy.
You should check what each warning says against the language. cppreference is a good source to check instead of the official language specification. Then repair the source so that you end up with 0 warnings.
Note: the compiler I worked on a long time ago used to respond "none of the errors was found" when no errors or warnings were generated in compilation. That should already indicate to you that, even if the source compiles neatly, that's not a guarantee for correctness.
Good luck and as someone here used to say: "learn something new!".
1
u/grimvian Mar 29 '26 edited Mar 29 '26
I use this in Code::Blocks
gcc -Wall -g -Winit-self -Wredundant-decls -Wcast-align -Wfloat-equal -Wunreachable-code -Wmissing-declarations -Wmissing-include-dirs -Wswitch-enum -Wmain -pedantic-errors -pedantic -Wfatal-errors -Wextra -std=gnu99
When I have any warning or/and err, I hunt it down.
1
u/NoSpite4410 Apr 01 '26
It is probably that you are ignoring the return value of scanf, which is the number of successful conversions.
scanf has no idea if the scan is going to succeed before it runs, so the way to check is to make sure all the items were successfully scanned. It is also a good idea to make sure all the variables you are putting the converted data into are fully allocated and initialized before you call scanf with them.
1
u/[deleted] Apr 01 '26
[removed] — view removed comment