Reputation: 33
I am creating a main function to test the limits of a C function called mchar. mchar takes a char as an argument.
int main ()
{
mchar();
mchar('A');
mchar('\n');
mchar('');
mchar(NULL);
}
I am trying to think of all possible use cases that could possible call the method to go wrong. Will all of these be able to be called properly? And are there any use cases that I am missing?
Upvotes: 0
Views: 154
Reputation: 362157
There are only 256 characters so you can easily call it with all of them:
#include <limits.h>
int main(void)
{
for (int c = CHAR_MIN; c <= CHAR_MAX; ++c) {
mchar(c);
}
return 0;
}
If you specifically want to test "interesting" characters, then you might try these ones.
mchar('\''); // Single quote
mchar('"'); // Double quote
mchar('\\'); // Backslash
mchar(' '); // Space
mchar('\t'); // Tab
mchar('\n'); // Line feed
mchar('\r'); // Carriage return
mchar('\0'); // NUL
mchar('\b'); // Backspace
mchar('\f'); // Form feed
mchar('\v'); // Vertical tab
mchar('\a'); // Bell (alert)
Upvotes: 7
Reputation: 34296
Regarding your question
Will all of these be able to be called properly?
Firstly
mchar('A');
mchar('\n');
Here 'A' , '\n' are valid characters. So this will work properly
mchar('');
is illegal and will give compile time error. Eg : for gcc the error shown is empty character constant.
mchar();
is also illegal, since the function expects a char
as argument, but you are not passing any. You will get a compile time error (something like 'too few arguments to function').
mchar(NULL);
will compile in normal case (Well if you haven't set any strict compiler flags set), but with a warning.
warning: incompatible pointer to integer conversion passing 'void *' to parameter of type 'char';
Upvotes: 0