How to return a char function in C prog? [closed]
I am not sure how to store the return char function so I can return it to be used in the main function,
char process_3 (int step_2)
{
if (step_2 % 2 == 0)
{
printf ("A");
}
else if (step_2 % 3 == 0)
{
printf ("F");
}
if (step_2 % 5 == 0)
{
printf ("K");
}
else if (step_2 % 7 == 0)
{
printf ("P");
}
if (step_2 % 11 == 0 || step_2 % 13 == 开发者_如何学编程0)
{
printf ("T");
}
else
{
printf ("Z");
}
return process_3;
}
Try this:
char process_3 (int step_2)
{
char c;
if (step_2 % 2 == 0)
{
c = 'A';
}
else if (step_2 % 3 == 0)
{
c = 'F';
}
if (step_2 % 5 == 0)
{
c = 'K';
}
else if (step_2 % 7 == 0)
{
c = 'P';
}
if (step_2 % 11 == 0 || step_2 % 13 == 0)
{
c = 'T';
}
else
{
c = 'Z';
}
return c;
}
You can assign chars to a variable using single quotes and return them from functions as well.
A character literal in C is enclosed in single quotation marks, like this:
'a'
To return a character from your function you'd write a return statement with the character literal. For example:
if (step_2 % 2 == 0)
return 'A';
This is very basic C stuff. I suggest Googling some tutorials and/or getting a book on C to learn the basics of the language.
精彩评论