Thursday 30 June 2016

C Tokens

C-Tokens

  •  Identifier
  •  Variable
  •  Constants
  •  Keywords
  •  Data types

Identifier

  •  Simply references to memory locations,which can hold values (data).
  • Formed by combining
  • Letters (both upper and lowercase)
  • Digits (0–9)
  • Underscore ( _ )

Identifier Rules


  • The first character of an identifier must be a letter,an underscore ( _ ) also counts as a letter
  • The blank or white space character(ascii value) is not permitted in an identifier.
  • Can be any length. Internal identifier (do not have the external linkage) such as preprocessor macro names at least the first 31 characters are significant, also implementation dependent.
  • Reserved words/keywords and characters such as main and # also cannot be used.

Variable


  • Identifier that value may change during the program execution.
  • Initializing a variable means, give a value to the variable, that is the variable’s initial value and can be changed later on.

Syntax - variable

  1.  Type
  2.  Name
  3.  Value

Syntax :
            datatype name = <value>;
                   Ex: int x=4;

Keywords

cprogramming keywords
 C - keyword List

  • Keywords are the words whose meaning has already been explained to the ‘C’ compiler.
  • The keywords are also called as reserved words.
  • There are 32 keywords available in ‘C’.
Constants

  • integer-constant
  • character-constant
  • floating-constant
  • enumeration-constant

Integer-constant

  • consisting of a sequence of digits
  • number preceded by 0 : octal number
  • number preceded by 0x: hexadecimal
  • integer constant may be suffixed by letter

     – The suffix U (or u) forces the constant to be unsigned. It is                unsigned long if the value of the number itself is greater than            decimal 2,147,483,647, regardless of which base is used.
     – The suffix L (or l) attached to any constant forces the constant          to be represented as a long.
      – UL or ul : unsigned long


character-constant

  • sequence of one or more characters enclosed in single quotes    Ex: ‘a’
  • Character constants do not contain the newlines character in order to represent them following escape sequences may be used
  • There is an extended set of characters that cannot be represented in the char type. 
  • constant in this extended set is written with a preceding L
                     Ex: L‘a’

Float & Enumeration Constant

Floating constant
  • floating constant consists of a decimal part and a fraction part.
  • A floating-point constant that is not suffixed is of type double.
  • F or f for a float and L and I for long double.
Enumeration constant
  • Identifiers declared as enumerators are constants of type int.
  • Enumeration constants are identifiers defined in enum type declarations
        Ex: enum group { green, blue, red, white}


Why data type?

Variable
  • VALUE (100, 1000 etc…)
  • TYPE (char, int etc…)

                    ------- data type

int

  • int will normally be the natural size for aparticular machine
  • To develop portable programs, two suffixes are used : “short” and “long”
  • Short is often 16 bits, long 32 bits
Categories in data types

Type Specifiers:
                     char       double         enum
                      int         struct           typedef
                      float      union           void

Special type specifiers:

  • signed short
  • unsigned long


 Type qualifiers

  • Const
  • Volatile

Special Type Specifiers

Signed & unsigned

  • shows effect on sign of the variable.
  • can be used with int and char.
  • cannot be used with float and double.

Long & short

  • shows effect on size of the variable.
  • can be used with int and char.
  • cannot be used with float and double.
  • long can be used with double.



Wednesday 29 June 2016

How to write a C Program & what are Requirements ?

Creating the program

• A programmer uses a text editor to create or modify files                 containing C code
• The contents must obey C syntax
• The filename must end by convention “.c”
   Ex: sample.c
• Code is also known as source code
• A file containing source code is called a source file

Compiling the program

• There are many C compilers around.
• Sun compiler - cc
• GNU C - gcc
• Borland compiler, Turbo C compiler, Quick C compiler
• root@home> gcc –Wall sample.c
• Note: There may be a minor variations of
  library functions between different compilers

Running the sample program in Linux based Operating Systems

• If there are no errors in sample.c, this command produces an executable file in the same directory by name a.out
• root@home> ./a.out
• This executes your program, printing any results to the screen

Structure of C program

program header comment

preprocessor directives (if any)

int main ( )
{
statement(s)
return 0 ;
}

Description of hello.c 

  •  A C program starts execution with a function called main()‫‏‬
  • When a C program is started by the OS, a special startup routine is called before main(). 
  • This startup routine takes values from the OS (command line arguments and the environment) and sets things up so that main() is called first.
  • The startup routine also invoked exit() on return from main(). exit() releases all to reusable resources and then return to the kernel

Comments

  • A comment is descriptive text used to help a reader of the program understand its content.
  • All comments must begin with characters /*and end with the characters */.
  • These are called comment delimiters.
  • Nested comments are not allowed.

Preprocessor Directives


  • Lines that begin with a # are called preprocessor directives   
  • Example: the #include <stdio.h> directive causes the preprocessor to include a copy of the standard input/output header file stdio.h at this point in the code.
  • This header file was included because it contains information about the printf ( ) function that is used in this program.

int main ()


  • Every program must have a function called main. This is where program execution begins.
  • The reserved word “int” indicates that main() returns an integer value.
  • The parentheses following the reserved word “main” indicate that it is a function

The Function Body


  • A left brace (curly bracket) -- { -- begins the body of every function.
  • A corresponding right brace -- } -- ends the function body

printf (“Hello, World!\n”) ;

  • This line is a C statement
  •  It is a call to the function printf ( ) with a single argument (parameter), namely the string “Hello, World!\n”
  • Notice that this line ends with a semicolon
  • Note: All statements in C end with a semicolon

return 0

  •  Because function main() returns an integer value, there must be a statement that indicates what this value is.
  • The statement “return 0 ;” indicates that main() returns a value of zero to the operating system.
  • A value of 0 indicates that the program successfully terminated execution.
  • Don't worry about this concept now. Just remember to use the statement.



Why C ?


  •  'C' is popular because
  • It is reliable, simple and easy to use
  • It encourages modularity and good program organization
  • It is portable
  • It is a middle level
  • It provides good data-structuring facilities
Loop holes in 'C'
  • close to the hardware & malpractice prone to undesired behavior of system.
  • ‘C’ offers only straightforward, single-thread control flow: tests, loops, grouping and subprograms, but not multi programming, parallel operations & synchronization.
  • C provides no operations to deal directly with composite objects such as strings or arrays.

INTRODUCTION TO C PROGRAMMING LANGUAGE

C ? 


  • C is a programming language.
  • Developed at AT &T’s Bell Laboratories of USA in 1972.
  •  It was designed and written by Dennis Ritchie.
Versions of ‘C’
  • K & R C
  •  ANSI C
  •  ISO C
  •  ISO 9899
  •  C99
First Edition of ‘C’

  •  In 1978, the first edition of Kernighan and Ritchie's The C  Programming Language was published.
  •  It introduced the following features to the existing versions of  C
  •  structure data type
  •  long int data type
  •  unsigned int data type
  • The += operator

c programming
C Programming Tutorial-Dennis Ritchie

ANSI C

In 1989, C was first officially standardized by ANSI standards committees also included following new features
  1.  functions returning struct or union
  2.  void * data type
  3.  const qualifier to make an object Read Only
  4.  assignment for struct data types
ISO 9899
  •  The ANSI C standard, with a few minor modifications, was adopted as ISO standard number ISO 9899. 
  • The first ISO edition of this document was published in 1990(ISO 9899:1990)
  • ISO 9899:1999, which was published in 1999 is commonly referred to as "C99". 
  • It was adopted as an ANSI standard in March 2000.
New features in C99
  • Inline functions
  • Freeing of restrictions on the location of variable declarations 
  • The addition of several new data types, including long long int
  • An explicit Boolean data type
  • Official support for comments beginning with “// ” as in C++
  • Several new library functions and Header files