Skip to the task
Layout
Back to classic view

You are looking ahead. Your place is still task 1: Style.

EDITTask 6 of 23 · step 4: Reformat CodePress Ctrl+Alt+L
Press Ctrl+Alt+L in practice/04/

Open main.c and press Ctrl+Alt+L. Don't change anything by hand.

Checklist

  • Every task is one commit with a clear message.
  • Zero compiler warnings: the code builds with -Werror.
  • Once per topic you step through your code in the debugger (CLion, gdb).

Numbered notes in the code explain the important lines.

Details & hint
  • Each statement on its own line, indented by 4 spaces.
  • Spaces around =, !=, +, after commas and before {.
  • The if and else branches on separate lines.
  • The #includes get sorted: in one block "input.h" lands before <stdio.h>, because " comes before < in the alphabet.

Stuck? If the result looks different, CLion can't find .clang-format: open the project from the root folder, where CMakeLists.txt and .clang-format are.

practice 04 main.c Code Diff +16 −8
M · MODIFIED git diff: red = old code, green = new.
--- a/practice/04/main.c
+++ b/practice/04/main.c
@@ -1,9 +1,17 @@
− #include <stdio.h>
#include "input.h"
− int main(void){
− int a=read_int("Grade (0 - stop): ");int s=0;int n=1;
− while(a!=0){s=s+a;n++;
− a=read_int("Grade (0 - stop): ");}
− if(n>0) printf("Average: %.2f\n",(double)s/n);
− else printf("No grades.\n");
− return 0;}
+ #include <stdio.h>
+ int main(void) {
+ int a = read_int("Grade (0 - stop): ");
+ int s = 0;
+ int n = 1;
+ while (a != 0) {
+ s = s + a;
+ n++;
+ a = read_int("Grade (0 - stop): ");
+ }
+ if (n > 0)
+ printf("Average: %.2f\n", (double)s / n);
+ else
+ printf("No grades.\n");
+ return 0;
+ }