Let us see C

                                               Let us see  C

 

About C:

The C is a general-purpose, procedural, imperative computer programming language developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system.C is the most widely used computer language.

                                        Program Structure
       A C program basically has the following form:
  • Preprocessor Commands
  • Functions
  • Variables
  • Statements & Expressions
  • Comments

    #include <stdio.h>
    
    int main()
    {
       /* My first program */
       printf("Hello, World! \n");
       
       return 0;
    } 
     
    Preprocessor Commands: 
     
    #include <stdio.h> is a preprocessor command which tells a C compiler to include stdio.h file before going to actual compilation. 

    Functions:
    • are main building blocks of any C Program.  
    • main() function is mandatory 
    Variables: 
             used to hold numbers, strings and complex data for manipulation  
          Comments:
            used to give additional useful information inside a C Program.

   C - Reserved Keywords:
auto else long switch
break enum register typedef
case extern return union
char float short unsigned
const for signed void
continue goto sizeof volatile
default if static while
do int struct Packed
double

C - Basic Datatypes:
  C has a concept of 'data types' which are used to define a variable before its use. The definition of a variable will assign storage for the variable and define the type of data that will be held in the location.
C has the following basic built-in datatypes.
  • int
  • float
  • double
  • char

    int - data type

    int is used to define integer numbers.
        {
            int Count;
            Count = 5;
        }
    

    float - data type

    float is used to define floating point numbers.
        {
            float Miles;
            Miles = 5.6;
        }
    

    double - data type

    double is used to define BIG floating point numbers. It reserves twice the storage for the number. On PCs this is likely to be 8 bytes.
        {
            double Atoms;
            Atoms = 2500000;
        }
    

    char - data type

    char defines characters.
        {
            char Letter;
            Letter = 'x';
        }
     

    Modifiers

    The modifiers define the amount of storage allocated to the variable.
    The data types explained above have the following modifiers.
  • short
  • long
  • signed
  • unsigned

            short int  2          -32,768 -> +32,767          (32kb)
   unsigned short int  2                0 -> +65,535          (64Kb)
         unsigned int  4                0 -> +4,294,967,295   ( 4Gb)
                  int  4   -2,147,483,648 -> +2,147,483,647   ( 2Gb)
             long int  4   -2,147,483,648 -> +2,147,483,647   ( 2Gb)
          signed char  1             -128 -> +127
        unsigned char  1                0 -> +255
                float  4 
               double  8  
          long double 12 
 PROGRAM TO FIND THE MEMORY ALLOCATED FOR RESPECTIVE MODIFIERS:
int
main()
{
  printf("sizeof(char) == %d\n", sizeof(char));
  printf("sizeof(short) == %d\n", sizeof(short));
  printf("sizeof(int) == %d\n", sizeof(int));
  printf("sizeof(long) == %d\n", sizeof(long));
  printf("sizeof(float) == %d\n", sizeof(float));
  printf("sizeof(double) == %d\n", sizeof(double));
  printf("sizeof(long double) == %d\n", sizeof(long double));
  printf("sizeof(long long) == %d\n", sizeof(long long));

  return 0;
}


Qualifiers

A type qualifier is used to refine the declaration of a variable, a function, and parameters, by specifying whether:
  • The value of a variable can be changed.
  • The value of a variable must always be read from memory rather than from a register
Standard C language recognizes the following two qualifiers:
  • const
  • volatile
The const qualifier is used to tell C that the variable value can not change after initialisation.
const float pi=3.14159;
VOLATILE :
The volatile qualifier declares a data type that can have its value changed in ways outside the control or detection of the compiler.

What are Arrays:

 In C language it is possible to make arrays whose elements are basic types
int x[10];
The square brackets mean subscripting; parentheses are used only for function references. Array indexes begin at zero, so the elements of x are:
Thus Array are special type of variables which can be used to store multiple values of same data type.
 Those values are stored and accessed using subscript or index.
Arrays occupy consecutive memory slots in the computer's memory.
x[0], x[1], x[2], ..., x[9]
If an array has n elements, the largest subscript is n-1.
Multiple-dimension arrays are provided. The declaration and use look like:
      int name[10] [20];
      n = name[i+j] [1] + name[k] [2];
Subscripts can be arbitrary integer expressions. Multi-dimension arrays are stored by row so the rightmost subscript varies fastest. In above example name has 10 rows and 20 columns.
Same way, arrays can be defined for any data type. Text is usually kept as an array of characters. By convention in C, the last character in a character array should be a `\0' because most programs that manipulate character arrays expect it. For example, printf uses the `\0' to detect the end of a character array when printing it out with a `%s'.
Here is a program which reads a line, stores it in a buffer, and prints its length (excluding the newline at the end).
       main( ) {
               int n, c;
               char line[100];
               n = 0;
               while( (c=getchar( )) != '\n' ) {
                       if( n < 100 )
                               line[n] = c;
                       n++;
               }
               printf("length = %d\n", n);
       }

Array Initialization

  • As with other declarations, array declarations can include an optional initialization
  • Scalar variables are initialized with a single value
  • Arrays are initialized with a list of values
  • The list is enclosed in curly braces
int array [8] = {2, 4, 6, 8, 10, 12, 14, 16};
The number of initializers cannot be more than the number of elements in the array but it can be less in which case, the remaining elements are initialized to 0.if you like, the array size can be inferred from the number of initializers by leaving the square brackets empty so these are identical declarations:
int array1 [8] = {2, 4, 6, 8, 10, 12, 14, 16};
int array2 [] = {2, 4, 6, 8, 10, 12, 14, 16};
An array of characters ie string can be initialized as follows:
char string[10] = "Hello";