Skip to the content of the web site.

Lesson 152: Whitespace doesn't matter much

Previous lesson Next lesson


Up to this point, we have been very careful in every single example to use proper indentation, always the end of a statement at the end of a line, etc. This will, we hope, make you agree that code can be quite readable and comprehensible. Whitespace in C++ is, however, not very necessary, as the following example, which you can cut-and-paste into cpp.sh will show.

#include <stdio.h>
main(int t,int _,char *a){return!0<t?t<3?main(-79,-13,a+main(-87,1-_,main(-
86,0,a+1)+a)):1,t<_?main(t+1,_,a):3,main(-94,-27+t,a)&&t==2?_<13?main(2,_+1
,"%s %d %d\n"):9:16:t<0?t<-72?main(_,t,"@n'+,#'/*{}w+/w#cdnr/+,{}r/*de}+,/\
*{*+,/w{%+,/w#q#n+,/#{l+,/n{n+,/+#n+,/#;#q#n+,/+k#;*+,/'r :'d*'3,}{w+K w'K\
:'+}e#';dq#'l q#'+d'K#!/+k#;q#'r}eKK#}w'r}eKK{nl]'/#;#q#n'){)#}w'){){nl]'/\
+#n';d}rw' i;# ){nl]!/n{n#'; r{#w'r nc{nl]'/#{l,+'K {rw' iK{;[{nl]'/w#q#n'\
wk nw' iwk{KK{nl]!/w{%'l##w#' i; :{nl]'/*{q#'ld;r'}{nlwb!/*de}'c ;;{nl'-{}\
rw]'/+,}##'*}#nc,',#nw]'/+kd'+e}+;#'rdq#w! nr'/ ') }+}{rl#'{n' ')#}'+}##(!\
!/"):t<-50?_==*a?putchar(31[a]):main(-65,_,a+1):main((*a=='/')+t,_,a+1):0<t
?main(2,2,"%s"):*a=='/'||main(0,main(-61,*a,"!ek;dc i@bK'(q)-[w]*%n+r3#l,{\
}:\nuwloca-O;m .vpbks,fxntdCeghiry"),a+1);}

Challenge: Try running it, find the typo, and then fix the typo in the source file.

The only common place you need white space is between a keyword and either a variable name or literal or a type name and a variable name (there are a few more, but they tend to be less obvious). For example, the following is an implementation of the greatest common denominator.

int gcd(int m,int n){if(m<n)std::swap(m,n);while(n>0){int t=m%n;m=n;n=t;}return m;}

The following is also valid:

                                             int

                                                               gcd
                           (
                  int
                              m
                                                                     ,
            int
                        n

                                             )

                                                {
                                    if
                                                                           (
      m
                                                <
                     n
                                       )
      std
                                                ::


                     swap
                                                                           (
            m
                              ,
         n
                                                   )
                        ;
   while

                                                                  (
                                                n
                                    >
                                                                           0

                  )
            {
                                             int
                                 t

            =
                                                                           m
               %
                                                n
                                 ;
      m
            =

                                                                  n

                     ;
                           n
                                                   =
            t
                                                               ;

                           }
   return
                                          m
                           ;
            }

as is the following representation (of what?) equally acceptable to the compiler:

          int
            gcd                 (int
             m  ,         int  n   )  {  if   (
              m   <    n       )    std      ::  swap
                (   m      ,   n )     ;                while
                 (   n               >  0    )  {    int    t
                   = m                  %            n    ; m
                       =n;n=t ; }  return m               ; }

However, just because something is possible, this does not mean it is good. Always use whitespace to clarify your code, including:

  • Whitespace around all operators except binary *, /, . and ->.
  • In general, blank lines around conditional and repetition statements, with certain
  • A space after any comma.
  • Tabs for every successive level of nested blocks of statement.
  • One statement per line.
template 
void f();

void f() {
	if ( ... ) {
		if 
}

Previous lesson Next lesson