Wednesday, October 14, 2009

Introduction

               C is a both high level and low level language. Thus we can also call it a middle level language.In high level language, program is divided into blocks. Blocks include functions, loops, conditional statements, etc. The data is not visible outside these blocks(abstraction). It is also known as a structural language. Low level language can interact with hardware (not directly). Bit manipulation, bitwise operators, sizeof uses low level language features.

History 
         The development of C started in 1970. The first standard, C89, was made in 1989. The current standard in use is C99

Features 
  • Case sensitive.
  • Statements are terminated with a semicolon(;).
  • According to C89 standard, all declarations are made before their first use. This is removed in C99 standard.
  •  It supports ASCII code(256 characters) which include alphabets, numbers, white space(space,\n,\t), special characters(*,/,@,etc). The first 32 characters are known as control characters(not printable).
  • C is a procedural language. the focus is on "how to do" work(code). Data is given secondary treatment. Code works on any data. Code and data are loosely coupled. 
  • C is a compiled language. 

Tokens 
   A statement/instruction is a set of tokens. Tokens include :
  • Keywords
  • Identifiers
  • Literals
  • Seperators
  • Operators

Keywords
         Keywords are predefined words whose meaning is known to compiler. These are reserved words. C89 has 32 keywords, all in lower case. Five more are added in C99. These are Bool,Complex,Imaginary(not in lower case),restrict,inline. "restrict" is used with pointers and "inline" with functions.

Identifiers 
            An identifier is a user defined name for program elements. The program elements include variables, functions, struct, union, enum, pointers.
  Rules / Conventions :
  • Only alphabets (A-Z,a-z), numbers (0-9), "_" (underscore) are allowed.
  • Cannot start with a number.
  • The length of an identifier is implementation dependent. Windows support a maximum of 255 characters. We can have an identifier of any length in unix/linux.
  • The first 31 characters should be unique(C89 standard). In C99 standard, the first 63 characters should be unique.
  • Keywords cannot be used.
  • Should be unique with in the same scope.
  Guideline: use long meaningful names in your projects.

  Examples: 
    hello1 -----> valid
    h?ello -----> invalid (special characters cannot be used)
    1hello -----> invalid (cannot start with a number)
     _hello-----> valid

Literals
         Literals are constant values (Eg: 10,12.5,"ram",'A' ). The various literals are:
  • Integer
  • Real
  • Character
  • String
  Integer :
  • decimal       --> default
  • octal           --> prefix with "0"(zero)
  • hexadecimal --> prefix with "0x"(x can be in lower/uppercase)
   Real :
  • fractional/standard (Eg: 123.456)
  • scientific/exponential(Eg: 1.2345E+2)
                   In scientific notation,mantissa(1.2345) can be both integer and real. But exponent(+2) can be only an integer but not real.

  Character : 
  • Single character enclosed in single quotes(Eg: 'A','5','*').
  • Escape sequences: Character literals do not contain the ' character or newlines; in order to represent them, and certain other characters, the following escape sequences may be used.
            \n -> newline          \t -> horizontal tab 
            \v -> vertical tab      \b -> backspace
            \r -> carriage return  \f -> formfeed
            \a -> audible alert    \\ -> backslash
            \' -> single quote      \" -> double quote
            \ooo -> octal number \xhh -> hex number  

   String : 
  • sequence of characters in double quotes (Eg : "ram","A").
  • system appends '\0'(null character) automatically to string.
  • String is implemented as character array.
       Example: 
                    char ch1 = ' A ';           //1 Byte
                    char ch1[2] = "A";        //2 Bytes          

 Separators : 
  • used to separate program elements
  • some of the separators are [],(),{},,,;,.,:,#
 
other blogs :