Home>
I tried to implement FizzBuzz for learning C language First of all, implement it obediently
#include<stdio.h>
int main (void) {
  for (int i = 1;i<= 100;++ i) {
    if (i% 15 == 0) {
      printf ("fizzbuzz \ n");
    }
    else if (i% 3 == 0) {
      printf ("fizz \ n");
    }
    else if (i% 5 == 0) {
      printf ("buzz \ n");
    }
    else {
      printf ("% d \ n", i);
    }
  }
  return 0;
}
I wanted to make it a function

I couldn't think of an implementation in C, so I wrote it in Ruby.

#This is a subroutine
def fizzbuzz (num)
  if num% 15 == 0
    result = "fizzbuzz"
  elsif num% 3 == 0
    result = "fizz"
  elsif num% 5 == 0
    result = "buzz"
  else else
    result = num
  end
  result result
end
# As an image, main function from here
i = 1
while i<= 100 do
  puts (fizzbuzz (i))
  i = i.succ
end

When I try to rewrite it in C language from here (try to make it a function), it just doesn't work and I get stuck as follows.

#include<stdio.h>
Type fizzbuzz (int num)
{
  // Processing
}

int main (void)
{
  int i = 1;
  while (i<= 100) {
    // Function call and standard output
    ++ i;
  }
  return 0;
}

I would like to know how to write a functionalized FizzBuzz in C language.

  • Answer # 1

    I would like to know how to write a functionalized FizzBuzz in C language.

    This is an example.

    #include<stdio.h>
    #include<string.h>
    / * (1) A pattern that returns its own static string in the function * /
    const char * fizzbuzz (int num)
    {
        const char * r = "";
        if (num% 15 == 0) {
            r = "fizzbuzz";
        } else if (num% 3 == 0) {
            r = "fizz";
        } else if (num% 5 == 0) {
            r = "buzz";
        } else {
            static char snum [16];
            sprintf (snum, "% d", num);
            r = snum;
        }
        return r;
    }
    / * (2) Pattern that uses the buffer for the character string prepared by the caller * /
    const char * fizzbuzz2 (int num, char * snum, size_t snum_len)
    {
        if (num% 15 == 0) {
            strncpy (snum, "fizzbuzz", snum_len);
        } else if (num% 3 == 0) {
            strncpy (snum, "fizz", snum_len);
        } else if (num% 5 == 0) {
            strncpy (snum, "buzz", snum_len);
        } else {
            snprintf (snum, snum_len, "% d", num);
        }
        return snum;
    }
    int main (void)
    {
        int i = 1;
        char snum [16];/* Enough space to store'\ 0'* /
        while (i<= 100) {
           /* Function call and standard output * /
           /* puts is equivalent to printf ("% s \ n", string) * /
            // puts (fizzbuzz (i));
            puts (fizzbuzz2 (i, snum, sizeof (snum)));
            ++ i;
        }
        return 0;
    }

    There are various possible forms (signatures) of functions. In the example, I prepared two. As much as possible to what the questioner defined as a Ruby methodfizzbuzz ()However, according to the custom of C language functions, it returns a string to the caller.fizzbuzz2 ()May be more appropriate. The return value is C languagestr ...Like many standard functions in the system, the address of a stringconst char *Will return with.