0

This code shows a function intended to determine if a year is between a range, and if it belongs to a leap year (Bisiesto in spanish: those years with 366 days, each 4 years in the graegorian calendar)

int bisiesto(int anyo)
{
  printf("%d\n",anyo);

  if(anyo >= 1601 && anyo <= 3000)
  {
    if((anyo%4 == 0 && anyo%100 !=0) || anyo%400 ==0)
    {
      return(1);
    }
    else
    {
      return(0);
    }
  }
  else
  {
    return(0);
  }
}

And this function is called from the main() function with:

if (bisiesto(anyo) == 1)
{
  printf("Es bisiesto");
}
else
{
  printf("No es bisiesto");
}

Well, the problem is that even the conditions are defined in the bisiesto() function, the program always tell us if we have a 'bisiesto' year or not. Why it passes over the IF in the bisiesto function?

Cœur
  • 37,241
  • 25
  • 195
  • 267
heptagono
  • 69
  • 7
  • 3
    i am not sure i understand your problem.. – Haris Oct 21 '14 at 14:24
  • 1
    What's the value for `anyo`? Your code looks like it should work. – Mr. Llama Oct 21 '14 at 14:25
  • 1
    What value are you passing for anyo? is this what you are looking for http://stackoverflow.com/questions/3220163/how-to-find-leap-year-programatically-in-c – Gopi Oct 21 '14 at 14:25
  • Seems to be working correctly. I'm not sure what the problem is: http://ideone.com/LMhwSZ – Mr. Llama Oct 21 '14 at 14:30
  • Why do you have this characters "^*/" probably your code is commented? – Javier Oct 21 '14 at 14:30
  • 1
    Please note that `return` is not a function, no `()s` needed. – unwind Oct 21 '14 at 14:30
  • 2
    This is a duplicate of many, many previous questions. Search this site for `[c] leap year` to find them. There are examples in various other languages as well; just leave off the `[c]` in my previous search expression. **Always search here for previous questions before posting a new one.** – Ken White Oct 21 '14 at 14:31
  • 1
    I'd like to see all curly braces removed from the `bisiesto` function (except the outermost pair, of course), as well as all `else`-s and one `return 0`. That would make the code much more readable... – CiaPan Oct 21 '14 at 14:32
  • 1
    @CiaPan: I disagree. It's a matter of style, on which there will never be complete agreement, but I personally prefer to use braces on all `if` and other control statements, even if they're not strictly necessary. Especially in a case like this with nested `if` statements, braces can make the structure of the code more obvious. It also makes the code easier to edit if you need to add more sub-statements. (It's a habit I picked up from Perl, which requires the braces, but IMHO it's a good idea in C.) – Keith Thompson Oct 21 '14 at 14:36
  • 1
    @CiaPan Whether or not braces or no braces is the more readable, is a subjective optinion. That being said, statements without curly braces are more bug-prone than those without, which is the reason why you should always use them. Code with braces everywhere, as in this post, is considered good programming practice for _safety reasons_ by most of the widely-recognized coding standards (such as for example MISRA-C). – Lundin Oct 21 '14 at 14:37
  • 2
    @CiaPan: Adding braces does use up more vertical space, but that can be alleviated by using K&R style, putting the opening brace at the end of the line rather than on a line by itself. (That's probably even more controversial than the question of whether to use braces at all.) – Keith Thompson Oct 21 '14 at 14:39
  • Anyway, it would seem that the logic of the function is incorrect: do as suggested in the answers of the duplicate and all problems will go away. I'll close this as a duplicate now. – Lundin Oct 21 '14 at 14:40
  • 1
    "*the program always tell us if we have a 'bisiesto' year or not*" -- which is exactly what it's supposed to do; it always prints either `"Es bisiesto"` or `"No es bisiesto"`. If you meant that it always prints `"Es bisiesto"`, please update your question to make that clear -- and tell us what argument you're passing to the function. In fact, a complete self-contained program that illustrates the problem would be *extremely* helpful. Read this: http://sscce.org/ – Keith Thompson Oct 21 '14 at 14:41
  • BTW: 1) The [Gregorian calendar](http://en.wikipedia.org/wiki/Gregorian_calendar) leap year rule starts in 1582 and does not end with 3000. 2) I hope you are not testing this with only 2 digit `anyo`. – chux - Reinstate Monica Oct 21 '14 at 14:41
  • @chux: The Gregorian calendar starts in different years in different places, up to the early 20th century. – Keith Thompson Oct 21 '14 at 14:46
  • @Lundin The function logic test correct: 2000-->leap, 1900-> not leap, etc. Please provide example year in which it fails. – chux - Reinstate Monica Oct 21 '14 at 14:47
  • 1
    @Lundin: I'm not convinced this is a duplicate. The linked question asks how to determine leap years. This question shows *correct* code to determine leap years and asks why it doesn't seem to work. I would have closed it as unclear. (Not that it matters a great deal one way or the other.) – Keith Thompson Oct 21 '14 at 14:47
  • @Keith Thompson The Gregorian calendar starts in 1582. Various locations did not adopt the Gregorian calender until various years as you suggest. Some religious groups continue to use the earlier Julian calender, even in 2014. – chux - Reinstate Monica Oct 21 '14 at 14:49
  • @chux: Yet another annoyingly pedantic quibble: http://en.m.wikipedia.org/wiki/Proleptic_Gregorian_calendar – Keith Thompson Oct 21 '14 at 14:53
  • 1
    @Keith Thompson Happy [223 Vendémiaire III 9](https://www.fourmilab.ch/documents/calendar/) ;-) – chux - Reinstate Monica Oct 21 '14 at 14:59

0 Answers0