Jump to content
  • Advertisement
Sign in to follow this  

C If Statement Question

This topic is 2081 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts



I am taking a C programming class and my teacher uses the "puts statement" to combine an if statements with the action if the condition is met. I saw too different examples of doing the same thing and was just wondering which method is more "standardized" and which method is not recommended using? It's nothing too serious, but it got me curious. thanks! 


PS - here's an example:

#include <stdio.h>

int main(int argc, const char * argv[])
    int grade;
    // gets user input
    printf("What is the students grade? ");
    scanf("%d", &grade);
    // alerts user if passed or failed
    (grade >= 60) ? printf("Passed!\n") : printf("Uh-ohhh...\n");
    // puts statement version
    puts(grade >= 60 ? "Passed!\n" : "Uh-ohhh...\n");
    // displays grade
    if(grade >= 90)
        printf("Grade = A\n");
    else if(grade >= 80)
        printf("Grade = B\n");
    else if(grade >= 70)
        printf("Grade  = C\n");
    else if(grade >= 60)
        printf("Grade = D\n");
        printf("Student failed.\n");
    return 0;

Share this post

Link to post
Share on other sites

Run that program with any invalid input (any non-numerical value will do) and your program is instantly in "undefined behaviour" territory.
I can even see it happening: all output happening virtually instantly and it returns 0, as if nothing had gone wrong.

Share this post

Link to post
Share on other sites

This is just a nitpick, but I believe that `int main(int argc, const char * argv[])` is, in fact, not a valid signature for `main` function. Standard sayeth `int main(void)`, `int main(int argc, char* argv[])` or equivalent.

Share this post

Link to post
Share on other sites

I personally don't 'like' the 'large' set of if statements (personal probably..).

But honestly I don't know how you can handle it differently, since a switch statement won't take operators < or >

Share this post

Link to post
Share on other sites

It is generally a good idea to keep input, output and computation separate. In that spirit, it would be good to have something like this function:


enum Grade {

enum Grade compute_grade(int score) {
  static int const grade_cuts[4] = {60, 70, 80, 90};
  int grade;

  for (grade = 0; grade < 4; ++grade)
    if (score < grade_cuts[grade])

  return grade;

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!