Using indent on C code

this slamcoded program works fine, but looks like shit:

#include <unistd.h>

int main (int argc, char **argv)
{
  if (argc < 2) { printf ("Hva har du kog i fister?\n"); }
  else if (!strcmp (argv[1], "-GiB"))
           { printf ("Gibibyte\n");  system ("df -h");
} else if (!strcmp (argv[1], "-GB"))
       { printf ("Gigabyte\n");
         system ("df -H");
} else {
 printf ("%s [-GiB|-GB]\n", argv[0]);
}
}

if you use the program indent on it, it will look real nice:

GNU style:

indent <file>

#include <unistd.h>

int
main (int argc, char **argv)
{
  if (argc < 2)
    {
      printf ("Hva har du kog i fister?\n");
    }
  else if (!strcmp (argv[1], "-GiB"))
    {
      printf ("Gibibyte\n");
      system ("df -h");
    }
  else if (!strcmp (argv[1], "-GB"))
    {
      printf ("Gigabyte\n");
      system ("df -H");
    }
  else
    {
      printf ("%s [-GiB|-GB]\n", argv[0]);
    }
}

Kernighan  & Ritchie style:

indent -kr <file>

#include <unistd.h>

int main(int argc, char **argv)
{
    if (argc < 2) {
        printf("Hva har du kog i fister?\n");
    } else if (!strcmp(argv[1], "-GiB")) {
        printf("Gibibyte\n");
        system("df -h");
    } else if (!strcmp(argv[1], "-GB")) {
        printf("Gigabyte\n");
        system("df -H");
    } else {
        printf("%s [-GiB|-GB]\n", argv[0]);
    }
}

have fun slamcoding :D

 

Dette indlæg blev udgivet i Knowledge Base, Old Base, Programmering. Bogmærk permalinket.

Skriv et svar