These are some common questions that come up during this lab.
Files listed within Visual Studio’s Solution Explorer do not correspond to the file structure on disk. Therefore, it is likely you have added an existing file to your project which is not located within the project’s folder (this operation does not copy or move the file).
There are 4 locations Visual Studio looks for include files:
Any include in the form
#include <random.h> (with the chevrons
< >) is considered a system header, so will only work for system headers. Your own files should be included as
#include "random.h" with double quotes instead.
Your project’s directory
You can locate the project’s folder, by right clicking your project in the Solution Explorer and selecting “Open Folder in File Explorer”.
It’s recommended that you move all your source files into this folder or a subdirectory, and specify all includes relative to this directory.
Relative to the source file being compiled
If the header you are including is in the same directory as the source file, you can include it by name, irrespective of where both are on disk. This is not recommended, and considered bad practice, as you are encouraged to specify the full path to headers relative to the root of the project’s source/include directories.
Additional include directories
Within the project settings (
Project Properties > C/C++ > General > Additional Include Directories), you can specify additional directories that the compiler will look for include files. You probably don’t need to use this, it primarily exists for 3rd party dependencies that sit outside of the project’s own source.
The C preprocessor is a bit different to C itself, when you use
#define you only need the symbol and the value it should be replaced by.
#define FOO 12 // Correct #define FOO 12; // Incorrect #define FOO=12 // Incorrect
printf() expects the first argument to be a character array (technically it expects a pointer to a null-terminated e.g.
\0 character array/buffer).
If you pass anything other than a character array, it will interpret that value as a pointer to a location in memory and start reading from there until it hits a null-terminating character. Hence, you might get random data stored in memory printed to console, or your program might crash with an access violation for reading something inaccessible.
int foo = 12; printf("My Num: %d\n", foo); // Correct printf("%d\n", foo); // Correct printf("%f", (float)foo); // Correct printf(foo); // Incorrect printf(foo, "\n"); // Incorrect
If you’re confused by format specifiers, you can find official documentation here. There are hundreds of combinations but you will mostly require
%s with the occasional precision specifier e.g.
This a common issue people have with the exercise, normally it is a result of incorrect types and the implicit casts which result from them. This can be subtle to spot, therefore I would recommend you carefully compare your solution to the lab’s solution code, and carefully check the types used.
You also may have problems if you are using the incorrect format specifiers, a number stored in an
unsigned long long which exceeds the max value of
int, will print incorrectly if using the
%d format specifier instead of the
%llu format specifier.
The IO functions provided by
stdio.h are considered ‘unsafe’ as they take buffers without knowing the buffer’s length, which may allow the function to write beyond the end of the buffer.
Microsoft has safe version of these functions, which it enforces use of by default, however they are not cross-platform (compatible with Linux/GCC) so we don’t use them.
There are 3 ways to suppress this warning/error:
Project Properties > Configuration Properties > C/C++ > Preprocessor -> Preprocessor Definitions.
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> ...
#pragma warningbefore including
#pragma warning (disable : 4996) #include <stdio.h> ...
As of the 2023 the managed desktops have a new AntiVirus “CrowdStrike Falcon Sensor”, however it’s rather overzealous and regularly raises false positives for even basic C/C++ code.
It’s currently a work in progress finding an appropriate solution to this, however we have a workaround:
Three requirements must be met for your executable to not be blocked:
You can update the compiled binary’s name via
Project Properties > General > Target Name (
.exe should not be included).
This is a live issue and I’m hoping a smoother resolution can be found in the near future.
For queries relating to collaborating with the RSE team on projects: email@example.com
Join our mailing list so as to be notified when we advertise talks and workshops by subscribing to this Google Group.