简体   繁体   中英

Using main function in C?

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?

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)

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';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM