开发者

How to return a char function in C prog? [closed]

开发者 https://www.devze.com 2023-04-05 01:37 出处:网络
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time,or an extraordinarily narrow situation that is not generally applic
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 11 years ago.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消