Is my code correctly indented? Please help. I'm not sure it is correctly indented according to the CS50 Standard.


Take a look at this style guide and see if your code meets it. Also you may use style50 which is a tool in the CS50 appliance that guides you to have a better coding style. Execute

style50 path/to/source/code/file

For example

style50 ~/test.c

Assuming that there's a source code file, test.c that fells under the home directory.


Indentation! Its just for the sake of your code to look beautiful. Even if write code in a badly indented way, it won't effect your grade. It has nothing to do with what exactly your program does. So you are free to choose any the indentation style you wish, and not bound with a particular one for submission of psets.

Quoting Wikipedia

Indentation is not a requirement of most programming languages, where it is used as secondary notation. Rather, programmers indent to better convey the structure of their programs to human readers. In particular, indentation is used to show the relationship between control flow constructs such as conditions or loops and code contained within and outside them. However, some programming languages (such as Python and occam) use the indentation to determine the structure instead of using braces or keywords; this is known as the off-side rule, and in these languages indentation is meaningful to the compiler, not just a matter of style.

Indenting your code is a good habit because it helps your fellow programmers to understand it well. And since there is no indentation sensitive programming language in the course, we are free to use it anyway, Wikipedia has mentioned a number of such styles.

Talking about your code snippet, it is indented properly, you used Allman Style, me too personally prefer the same, but again that depends on what conventions your colleagues follow.

Some tips I would like to give :

  1. Replace tab with n spaces in your text editor.
  2. Take n to be 3 or 4, not more than that, it will help you run windows simultaneously on each side of your screen, giving you ease to debug.

Good Luck.

  • But don't we get graded on Style on the psets? I know the computer doesn't care but I'm pretty sure we would get points off for not using correct indentations, according to the CS50 videos...
    – i_am_david
    Mar 25 '15 at 2:25
  • Indenting a code is good, and I encourage you to do that independently of it being the grading criterion. But consider the fact that one can't check submission of 1000s of students whether they are correctly indented and appropriately commented manually! At last, that comes to your end, whether you do those things or not.
    – sinister
    Mar 25 '15 at 2:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .