Wednesday, January 16, 2013

C Reference Notes

Contents

1. Introduction to C..
2. C Fundamentals.
2.1.       Program Structure.
2.2.      Character Set
2.3.       Identifiers
2.4.       Keywords.
2.5.       Constants.
2.6.       Variables
2.7.       Comments.
2.8.       Expressions and Operators.
2.8.1.    Arithmetic Operators.
2.8.2.       Assignment Operators
2.8.3.       Relational Operators.
2.8.4.       Logical Operators.
2.8.5.       Bitwise Operators.
2.8.6.       Memory Access Operators.
2.8.7.       Other Operators.

3.        Data Types.
3.1.       Predefined Data Types.
3.2.       Derived Data Types.
3.2.1.    Array
3.2.2.    Structure
3.2.3.    Union.
3.2.4.    Enumerator
3.3.       Range for Data Types.

4.        Type Modifier
4.1.       Signed.
4.2.       Unsigned.
4.3.       Long.
4.4.       Short

5.        C Memory Map..
5.1.       Stack.
5.2.       Heap.
5.3.       Global Variables.
5.4.       Program code.

6.        Scope..
6.1.       Function prototype scope.
6.2.       Function scope.
6.3.       Block scope.
6.4.       File scope.

7.        Storage Class Specifier.
7.1.       Automatic.
7.2.       Static.
7.3.       Register
7.4.       Extern.

8.        Type Qualifier.
8.1.       Const
8.2.       Volatile.

9.        Program Control Statements.
9.1.       Decision/Selection Statements.
9.1.1.    If Else.
9.1.2.    If Else if ladder
9.2.       Jump Statements.
9.2.1.    Break;
9.2.2.    Continue;
9.2.3.    Exit()
9.2.4.    Return.
9.2.5.    Goto.
9.3.       Looping/Iterative Statements.
9.3.1.    For
9.3.2.    While.
9.3.3.    Do-While.

10.      Functions.
10.1.     Call by Reference.
10.2.     Call by Value
10.3.     Return types for a function.
10.4.     Recursion.

11.      I/O Console..
11.1.     Printf()
11.2.     Scanf()
11.3.     Getchar()
11.4.     Putchar()

12.      Pointers.
12.1.     Variable Pointer
12.2.     Function Pointer

13.     Preprocessor Directives.
13.1.     #define :
13.2.     #error
13.3.     #include.

13.4.     Conditional compilation.
13.4.1.   #if …. #else…..#endif.
13.4.2.   #ifdef.
13.4.3.   #ifndef.
13.4.4.   #pragma.

14.      Dynamic Memory Allocation..
14.1.     Malloc()
14.2.     Calloc()
14.3.     Free()

15.      Command Line Arguments.
16.      File I/O..
16.1.     Fclose.
16.2.     Feof.
16.3.     Ferror
16.4.     Fflush.
16.5.     Fgetc.
16.6.     Fgetpos.
16.7.     Fgets.
16.8.     Fopen.
16.9.     Fprintf.
16.10.    Fputs.
16.11.    Fread.
16.12.    Freopen.
16.13.    Fscanf.
16.14.    Fseek.
16.15.    Fsetpos.
16.16.    Ftell
16.17.    Fwrite.

17.      Library Functions.
17.1.     Getc.
17.2.     Getchar
17.3.     Perror
17.4.     Printf.
17.5.     Putc.
17.6.     Putchar
17.7.     Puts.
17.8.     Remove.
17.9.     Rename.
17.10.    Rewind.
17.11.    Scanf.
17.12.    Setbuf.
17.13.    Setvbuf.
17.14.    Snprintf.
17.15.    Sprint
17.16.    Sscanf.
17.17.    Tmpfile.
17.18.    Tmpnam...
17.19.    Ungetc.
17.20.    Vprintf.
17.21.    Vfprintf.
17.22.    Vsnpfintf.
17.23.    Vscanf.
17.24.    Vfscanf.
17.25.    Vsscanf.

18.      String & Character functions.
18.1.     Isalnumt
18.2.     Isalpha.
18.3.     Isblank.
18.4.     Iscntrl
18.5.     Isdigit
18.6.     Isgraph.
18.7.     Islower
18.8.     Isupper
18.9.     Isprint
18.10.    Ispunct
18.11.    Isspace.
18.12.    Isxdigit
18.13.    memchr
18.14.    memcmp.
18.15.    memcpy.
18.16.    memmove.
18.17.    memset
18.18.    strchr
18.19.    strcat
18.20.    strcmp.
18.21.    strcoll
18.22.    strcpy.
18.23.    strcspn.
18.24.    strerror
18.25.    strlen.
18.26.    strncat
18.27.    strncmp.
18.28.    strncpy.
18.29.    strpbrk.
18.30.    strrchr
18.31.    strspn.
18.32.    strstr
18.33.    strtok.
18.34.    strxfrm...
18.35.    tolower
18.36.    toupper

19.      Data Structures.
19.1.     Stack.
19.2.     Queue.
19.3.     Linked list

20.      Algorithms.
20.1.     Sorting.
20.1.1.      Bubble sort
20.1.2.      Insertion sort
20.1.3.      Selection sort

20.2.     Insertion.
20.3.     Deletion
20.4.     Searching.
20.4.1.      Linear search.
20.4.2.      Binary search.

21.      Simple Applications.
21.1.     Palindrome Number
21.2.     Armstrong Number
21.3.     Fibonacci Series.
21.4.     Swapping.
21.5.     Prime numbers.
21.6.     Reverse string/numbers.

22.       Compilation steps.


1. Introduction to C

The programming language C was developed in the 1970s by Dennis Ritchie at AT & T Bell Labs
C is a Middle level language
C is a Structured Language
C is Highly Portable
Designed for Compilation


2. C Fundamentals

2.1.  Program Structure

/*Program to add 2 numbers*/      // Comments
#include <stdio.h>                // Pre-Processor Directives
#include “add.h”
Int sum(int a,int b)          // Function Definition/Prototype
Int main()                     // Main Function
{                                                               
Int a,b;                                                       // Variable Declaration
Int Res = 0 ;                                              //Variable Declaration + Initialization
Printf(“Enter 2 Numbers:”);
Scanf(“%d%d”,&a,&b);
Res =sum( a,b);                   // Function Call
Printf(“\nThe Sum = %d”,Res);
Return 0;                                                   //Returns to Main Function
}
Int Res(int x,int y)             // Function Definition
{
Return(x+y);                      //Return to Function res
}


2.2. Character Set                                                            

Alphabets
 A-Z , a-z
 26 Upper + 26 lower Case

Digits
 0 - 9
  10 Digits

Special Symbols
 !@#$%^&*..
 29 Special Characters

White Spaces
 Space, Horizontal/vertical Tab, newline
 5 white spaces

Escape Sequence
 \n - Newline
\b - Baskspace
\r - Carriage Return
\t - Horizontal Tab
\v - Vertical Tab
  
Trigraph Sequence
 ‘??=’ for #

2.3.      Identifiers

Identifiers are names of Variables, functions, macros, types etc .
Rules :     
a) First Letter of an identifier should be a letter
b) Can use only _ in special character.
c) They are Case_Sensitive.
d) only first 31 character of an identifier is significant
e) Should not use Keywords as they are reserved

2.4.      Keywords

Keywords are words that have special meaning to the compiler. 32 in number as defned in c89 Eg:
Auto                    enum               restrict             unsigned
break                    extern              return               void
case                      float                 short                volatile
char                      for                   signed             while
const                    goto                 sizeof               _Bool(*)
continue               if                      static               _Complex(*)
default                 inline(*)           struct              _Imaginary(*)
do                        int                    switch             union
double                  long                 typedef                        else                register

2.5.      Constants

Constants can be Primary or Secondary

Primary Constants can be (Integer Constants, Real Constants, Character Constants )

a) Integer Constant Can be represented as follows
                 (Base-10) Decimal Constants(Begins without a 0) eg: 1934
                 (Base-8) OctalConstants(Begins with 0) eg: 012
                 (Base-16) HexadecimalConstants(Begins with 0X/0x) eg: 0x12A/0x12a

b) Real Constants
                                    eg: 2.35, -3.42, 0.128, 3.12e7, -0.25E24

c) Character Constant (Single digit/letter/symbol in single quotes)
eg: ‘A’, ‘e’ , ‘5’, ‘$’

Secondary Constants are (Array, Pointer, Enumerator, Union….)

2.6.      Variables

Variables are names given to memory location. Rules are similar to identifier.

2.7.      Comments

Comments are used to give readability/ understanding to the program eg:

// This is a single line comment    <- used for single line comment

/* This is a block
 or multiline comment */              <- Multiline comment

2.8.  Expressions and Operators

An expression is the combination of operators and operands. Operators are of the following type. It can be classified as number of operands used
Unary Operator: One Operand only Required eg: -5, &a
Binary Operator: Two Operands required eg:  a+b, c%2
Ternary Operator: Three Operands required ?: eg (a>2)?c= 1:c=2;

They can also be classified as follows

2.8.1.   Arithmetic Operators

Used for arithmetic operations
+  (Addition)                     2+5 = 7
-          (Subtraction)              8-3 =5
*  (Multiplication)            4*3 = 12
/  (Division)                      7/2 = 3 (int) , 7/2 = 3.5 (Float) [Quotient]
% (Modulus)                    7/2 = 1 , 6/3=0 [Remainder]

Precedence of operators are as follows BODMAS(Brackets of Division Multiplication Additon Subtraction)

2.8.2.                    Assignment Operators

Used to assign some value to lvalue (can be simple and compound)

c = x          Simple Assignment                              Assign the value x to c
c+=x          Compound Assignment                 c = c+x   (Instead of + we can use -,* etc)

2.8.3.                    Relational Operators

Used for comparison which yields 1 if true and 0 if false eg:

<                Less Than
>                Greater Than
<=              Less Than OR Equal to
>=              Greater Than OR Equal to
!=               Not Equal to
==              Equivalent

2.8.4.                    Logical Operators
The Logical operators can be used to combine the results of several comparison expressions into one logical expression.

 
OPERATOR
SYMBOL
EXPRESSION
RESULT
AND
&&
x &&y
0 if x or y is 0
OR
||
x||y
1 if x or y is 1
NOT
!
!x
1 if x=0, 0 if x=1

      The Left side of logical operators are always evaluated first.

  

2.8.5.                    Bitwise Operators
Used for bitwise operation
And                       x & y               1, if both x & y are 1
Or                          x | y                 1,If any x or y are 1
Xor                        x ^ y                1, if x and y are different
Not                        ~ x                   1,if x =0, 0 , if x=1
Shift Left              x << y              Each bit in x is shifted y positions to the left (Multiplication)
Shift Right            x>>y                Each bit in x is shifted y positions to the right (Division)

2.8.6.                    Memory Access Operators

&                     Address of                              &x
*                      Indirection                              *x
[ ]                     Array Element                         x[i]
.                       Member of a

Structure/Union                      s.x                     x Member of structure s
Structure/Union                      s->x                    x Member of structure s (When s is a pointer to a  structure)       

2.8.7.                    Other Operators

( )                     Function call
(type)               Cast
(size of)           Size in Bytes
?:                     Conditional Evaluation
,                       Sequence Operator


3.  Data Types

3.1.      Predefined Data Types

Data Types define how much space does a variable take in memory, the range of data it can handle and the type of data. The Predefined Data types are as follows

Integer (int) – Used to store integer type data (15,80…). Storage dependent on the microprocessor user (16 Bit, 32 Bit).

Character(char) – Single character stored. 1 Byte
Floating Point (float) – Decimal Numbers (12.23) . use #include<float.h> . 4 Bytes
Double Precision floating point (double)-  8 Bytes
Void – Used for functions that does not
1) Take in any value
2) Does not return any value