introduction to C

introduction and history of c

C is a middle-level procedure oriented programming language, used in general purpose programming, developed by Dennis Ritchie at AT&T Bell labs, USA between 1969 and 1973.

C Programming Language Version History:
Importance of C language:
  1. In 1988, the American National Standards Institute (ANSI) has formalized the C language.
  2. C was invented to write UNIX operating system.
  3. UNIX is considered to be one of the most robust and secured operating system. C was used to write it, So C is very efficient that it can be used to create an OS.
  4. C is a successor of ‘Basic Combined Programming Language’ (BCPL) called B language.
  5. Linux OS, PHP and MySQL are written in C.
  6. C has been written in assembly language.
  7. C has high flexibility means a function can be written and can be used so many times at so many places. It increases the modularity.
  8. Rich set of library support.
  9. It can be compiled on a variety of computer platforms.
  10. The C language is used for developing system applications that forms a major portion of operating systems such as Windows, UNIX and Linux. Below are some examples
    where C language is being used:
    • a) Database systems
    • b) Graphics packages
    • c) Word processors
    • d) Spreadsheets
    • e) Operating system development
    • f) Compilers and Assemblers
character Set:
  1. A character represents any alphabet, digit, special character used to form words, numbers and expressions
  2. While space characters are ignored by ‘ C ’ compiler in cases where they are not part of a string constant.
  3. C character set can be divided into following groups :
  4. Alphabets             : A……..Z to a……z.
  5. Digits                    : 0……….9.
  6. Special character : ~ , # ,$ , % ,^ ,* , & , + , -,\ , | , ( , ) etc.
C Tokens or Lexical units:

Smallest individual units of a ‘c’ program are known as tokens.

C tokens are of following types. They are,
  • Keywords: Example: int, continue, float etc.
  • Identifiers: Example: main, total, printf etc.
  • Constants: Example: 10, 20, 21.21 etc.
  • Strings: Example: “sum”, “Amol” etc.
  • Special symbols or separators: Example: (), {}, ; etc.
  • Operators: Example: +, / ,- ,* etc.
        
               void main() 
               {
               int a,b,sum;
               a=10; b=11;
               sum=a+b;
               printf (“Sum = %d \n”,sum);
               getch();
               } 
           

Here in above code, a, b, sum, +, printf, main all are tokens also known as lexical units of a program

Keywords in C language:
  1. Keywords are nothing but pre-defined or reserved words in a C language.
  2. Each keyword is meant to perform a specific function in a C program.
  3. Since keywords are reserved words for compiler, they can’t be used as variable name.
  4. C language supports 32 keywords which are given below.
  5. C99 version adds 5 more keywords.
  6. Most of the keywords are assigned for Data Types, Storage Classes, Control Statements etc.
Identifiers in C language:

Names given to identify variables, functions, constants are called as identifiers.
Example: a, b, sum are a names given to integer variables are called identifiers in above example.
main, printf are the names given to function are called identifiers in above example.

Rules for constructing identifier name in C:
  1. First character should be an alphabet or underscore.
  2. Succeeding characters might be digits or letter.
  3. Punctuation and special characters are not allowed except underscore.
  4. Identifiers should not be keywords.
C Constants:

These are also like normal variables. But, only difference is, their values cannot be modified by the program once they are defined.

Structure of C programming:
Documentation section
Header files section
Symbolic constants section
Global variables declaration
Function declaration or prototype
// main function section return_type main() { Variable declaration or initialization Executable statements }
//user-defined function return_type Function_name (list of arguments) { Body of function }
Data types in C
Fundamental/Basic/Primitive/Primary data types in C:

1.integer data type:
  1. Integer data type allows a variable to store numeric values.
  2. “int” keyword is used to refer integer data type.
  3. The storage size of int data type is 2 or 4 or 8 byte.
  4. It varies depend upon the processor in the CPU that we use. If we are using 16 bit processor, 2 byte (16 bit) of memory will be allocated for int data type.
  5. Like wise, 4 byte (32 bit) of memory for 32 bit processor and 8 byte (64 bit) of memory for 64 bit processor is allocated for int datatype.
  6. Depends on version if traditional C language int data type’s size is 2 bytes and in ANSI C int data type requires 4 bytes.
  7. int (4 byte) can store values from -2,147,483,648 to +2,147,483,647.
  8. int (2 byte) can store values from -32,768 to +32,767.
2.Character data type:
  1. Character data type allows a variable to store only one character.
  2. Storage size of character data type is 1. We can store only one character using character data type.
  3. “char” keyword is used to refer character data type.
  4. For example, ‘S’ can be stored using char data type. You can’t store more than one character using char data type.
3 Floating point data type:

Floating point data type consists of 2 types. They are,

  1. float- Storage size of float data type is 4. This also varies depend upon the processor in the CPU as “int” data type.
  2. double-•Double data type is also same as float data type which allows up-to 10 digits after decimal. The range for double data type is from 1E–37 to 1E+37.
Derived data types in C:

A derived types are inherited from fundamental or user defined data types like int, float, structure etc. Using derived types, an infinite variety of new types can be formed. Examples: Array, pointer, function etc.

A pointer is a variable which stores the address of another variable. Array is a collection of similar data elements.

User defined Data types in C:

Sometimes, a basic set of data types defined in C language are insufficient for our application. Programmer can create their own data types as per application needs are called as user defined data types.

Examples: Structure, union, enum, typedef etc.

void data type in C:

Void is an empty data type that has no value.
This can be used as return type of functions and void pointers means generic pointers.

Modifiers in C:

Signed means that variable can take both positive and negative values and unsigned means only the positive values. Int means data type is by default signed int and unsigned int data type is unsigned int. Last bit in the int data type is reserved for sign, if last bit is 0 then that is positive number and if last bit contains 1 then that is negative number. Below table gives the detail about the storage size of each C basic data type in 16 bit processor.
Please keep in mind that storage size and range for int and float data type will vary depend on the CPU processor and versions of C (8, 16, 32 and 64 bit).

sr.no C data type Storage size Range
1 char 1 -127 to 127
2 int 2 –32,768 to 32,767
3 float 4 1E–37 to 1E+37 with six digits of precision
4 double 8 1E–37 to 1E+37 with ten digits of precision
5 long double 10 1E–37 to 1E+37 with ten digits of precision
6 long int 4 –2,147,483,647 to 2,147,483,647
7 short int 2 –32,768 to 32,767
8 unsigned short int 2 0 to 65,535
9 signed short int 2 –32,768 to 32,767
10 long long int 8 –(2power(63) –1) to 2(power)63 –1
11 signed long int 4 –2,147,483,647 to 2,147,483,647
12 unsigned long int 4 0 to 4,294,967,295
13 unsigned long long int 8 2(power)64 –1