COSC 1315 Fundamentals of Programming

C/C++ Syntax
  1. Character set
      a .. z
      A .. Z
      0 .. 9
      ! # % ^ & * ( ) - _
      + = ~ [ } \ | ; : '
      " { } , . < > / ?
    
  2. Comments
    Any sequence that begins with /* and ends with */. Or starts with // to the end of the line. C++ ignores anything within comments. Comments may not be nested.

  3. Tokens
    The lexical structure of a C++ programs is divided into 5 classes
    1. Operators
      !	%	^	&	*
      -	+	 = 	~	|
      .	<	>	/	? :
      ->	++	--	<<	>>
      <=	>=	==	!=	&&
      ||	+=	-=	*=	/=
      %=	<<=	>>=	&=	^=
      |=	->	::
      
    2. Separators
       ( ) [ ] { } , ; :
      
      and a blank.

    3. Identifiers
      A sequence of letters and digits and underscores, which is not a reserved word. Must start with a letter. Note case is important. Identifiers may either be variable, structure, class or function names.

    4. Reserved words
      asm auto break bool case catch char class
      const const_class continue default delete do double
      dynamic_cast else enum explicit extern
      false float for friend goto if inline int long mutable
      namespace new operator private protected public
      register return short signed sizeof static static_cast struct
      switch template this throw true try typedef
      typeid typename union unsigned using virtual void
      volatile wchar_t while

    5. Constants
      1. Integer
        whole numbers specified in the program.
      2. Octal
        preceded with 0 as in 077
      3. Hex
        preceded with 0x as in 0xa7
      4. Floating point
        1.0
        1.0e3
        2e+9
        
      5. Character
        enclosed in single quotes. special sequence with \
        \n  newline
        \t  horizontal tab
        \v  vertical tab
        \b  backspace
        \r  carriage return
        \f  formfeed
        
      6. String
        constants are enclosed in double quotes like "hello".
  4. A C++ program
    is made up of expressions and statements. Statements are terminated with a semicolon (;) or compound statements are enclosed in { }.
    1. Expression
         x++
      
    2. Simple statement
         x++:
      
    3. Compound statement
      {
          x++;
          y--;
      }
      

  5. C++ Functions
    The general format of a C++ function is:
     function_name (formal argument list)
     {
      body of function
     }
    
    Every C++ program MUST have at least one function called main. This function receives control from the Operating system and is executed first. The simplest program given in most C++ texts is the ubiquitous Hello World.
    #include <iostream>
    
    using namespace std;
    
    void main()
    {
      cout << "Hello World" << endl;
    }
    


© Allan Kochis