Sunday, 11 July 2021

Programming in C

 Programming in C:

C is a middle level structure and procedural programming language is developed at "Bell Laboratory" (AT and T) in the year 1972 by "DENNIS RITCHIE".

Turbo 'C' was developed by borland international in the year 1989.

'C' character set :

  1. Alphabets (both in upper and lower case). 
  2. Digits (0 - 9) 
  3. Special symbols ( !, #, &, $, +, -, =, <, >, {}, [], @, ;, etc) 

'C' data types:

1) primary data types - (character, integer, floating point, long integer, double, void) 

2) secondary data types - (array, pointer, etc) 

3) user-defined data types - (structure, union, enum etc). 

'C' data types, 'C' prefix and conversion specification:

Data type

‘C’ prefix

Conversion specification

character

char

%c

integer

int

%d

floating point

float

%f

long integer

long

%ld

double

double

%lf

void

void

0


Example of a program:

/* write a 'C' program to enter any two numbers and find out their sum and average */

# include < stdio.h>
# include < conio.h>
void main ()
{
     float a, b, sum, average ;
     clrscr () ;
     printf ( " enter any two number =  " ) ;
     scanf ( " %f%f%f ", and a, & b ) ;
     sum = a + b ;
     average = a + b/2 ;
     printf ( " \n sum = %f ", sum ) ;
     printf ( " \n average = %f ", average ) ;
     getch () ;
}

output:
enter any two number = 
     a = 5
     b = 7
sum = 5 + 7 = 12
average = 5 + 7/2 = 6

C Tokens and its Types:

 Each and every smallest individual unit in a C program are known as C tokens. 

C tokens are the basic building blocks in a C language which are constructed together to write a C program. 

Types-

C tokens are 6 types -

  1. Identifier
  2. Constant
  3. String
  4. Special symbols
  5. Keywords
  6. Operators

1. Identifier -

      Each C program elements are given a name called identifier. 

     Names are given to identify variable, functions, array etc. 

Rules for constructing identifier:-

  • First character must be an alphabet. 
  • Identifiers shouldn't be keywords. 
  • Punctuation on a special character are not allowed except underscore. 
  • Most identifiers are in lower case letter. 

2. Constant -

      Any value reference to constant. 

Variable :-

     Variable is the name given to computer memory cell where we can store constant. 

3.  String :-

       Collection of character enclose with a pair of ("  ") ( double quate) is known as string. 

4. Special symbols :-

     ( +,  -,  =, < , >, @, !,  #,  ;  , $, [], ) are special symbols. 

Escape sequence character :-

 These are non printing characters use for special operations inside a program. 

 \o = lulu

\n = new line

\" = double quate

\' = single quate

\b = back space

\t = horizontal tab

\v = vertical tab 

\\ =  back slash

5. Keywords :-

   Keywords are print find words in C compiler. These words have fix meaning and cannot be changed. 

   C language supports 32 keywords which are given below :-

auto

double

int

struct

break

case

long

switch

case

enum

register

typedof

char

extern

return

union

const

float

short

unsigned

continue

for

signed

void

default

goto

sizeof

volatile

do

if

static

while


6. Operators :-

        Operators are the special symbols use for the specific operation inside the expression. 

   Expression :-

       Constant, variable and function reference are joined together by various operators to form expression. 

Example -

   C=X + 3 ;

      ( here '+' is operator) 

Types of operators :-

      Operators are 6 types -

  • arithmetical operator  -  ( + , - , / , % ,) 
  • assignment operator -  (= , += , -= , \=) 
  • relation operator - (< , > , <= , >= , ==) 
  • logical operator - (&& (and) , ||(or) , >! (not) ) 
  • increment &decrement operator- (++, --)
  • conditional or ternary operator - (?, :) 
  • size of operator- (  ) 
  • bitwise operator - ( << , >> , & , ^) 

Statement use in 'C' language:

The statement are real building blocks of a C program. We can impliment our logic through different type of statement. 

Basically statement is two types :

  1. Non executable statement
  2. Executable statement.

  1. Non executable statement :                             Non executable statement are not      compiled with C compiler. This is for program reference. 
  2. Executable statement :                                   Executable statement are compiled         with C compiler. 

Executable statement basically 5 types :

  • Declaration statement :  use to the declaration of variables, functions, modules to be use in program. 
  • Input / output statement :  use to get input and give output to the program. 
  • Conditional statement :  use to execution of program based on condition. 
  • Iteration statement :  continue execution of statement. 
  • Operational statement :  use to the execution of different operations through variable and operators. 
print f ( ) : To print the constant or contents                             of variable. 
scan f ( ) : To take input. 
clrscr ( ) : To clear the output print. 
getch ( ) :  To press the key from keyboard                          and display the output. 

As we know C is a middle level and procedural programming language. In this language we can solve and write a lot of programming problems. But when we come to write a C program we have to obey some rules. We can fulfill C program by obeying rules. Now we discuss the rules below. 

Rules to write a C program :

1. A 'C' program is a collection of functions or modules.
 
2. Each C program should be at least one function i.e main function / main ( ). 

3. Statements are written inside { }.

4. Each statement should be ended with (;). 

5. We need to include a required header file. 
     Syntax : 
        # include < header file name >
     Header file such as :
  • # include < stdio. h >
  • # include < conio. h >
  • # include < math. h >
  • # include < string. h >
  • # include < graphics. h > etc
6. A comment line is a line i.e not compiled 
    with C compiler. This is given for user
    reference and used for documentation of
    program. 
    Note :
  • A single line comment starts with //
  • A multi line comment starts with /* and ends with */
    Example of a C program :                                 
    /* write a C program to print sum of 1 to 
    10 */
    # include < stdio. h >
    # include < conio. h >
     void main ( ) 
    {
      int sum = 0, n = 0 ;
      clrscr ( ) ;
      while ( n<=10) ;
      {
        sum = sum+10 ;
        n = n+1 ;
      }
      print f ( " the sum of 1 to 10 = %d", sum) ;
      getch ( ) ;
   }
 
Output :
 The sum of 1 to 10 = 55

7. Upper and lower case letters have the  
    rules where to be use in C language. 
8. Most statements, functions and modules   
    should be lower case letters. 

 Functions use in 'C' language:

Functions are predefined procedure and calculations which are already named for a easy use. 

Type of functions :

  1. library function
  2. user defined function 
1. Library function : These functions are present in 'C' library and these are predefined. 
Some library functions are us follow :   
  •  print f ( ) 
  •  scan f ( ) 
  •  clrscr  ( ) 
  •  getch  ( ) 
  •  main  ( ) etc
2. user defined function : User can create their own functions for performing any specific task of the program. These type of function are called user defined function (UDF) . 
 To create and use these functions, we 
 should know these three things which are shown below. 
  • function definition
  • function declaration
  • function calling

   Example : 
   
 /* write a C program to enter any two            numbers and find out their sum using UDF */ 

  # include < stdio. h >
  # include < conio. h >
  void main ( ) 
  {
     int a, b, c, s = 0;
     int sum ( int, int);
     clrscr ( );
     print f ( " enter any two numbers = " );
     scan f ( " %d %d ", & a, & b ) ;
     s = sum ( a, b );
     print f ( " sum of two numbers = %d ",s ); 
     getch ( ) ;
  }
  int sum ( int x, int y ) 
  {
    int p ;
       p = x + y ;
       return p ;
  }

Output:
Enter any two numbers =
                  8
                  2
Sum of two number = 10

No comments:

Post a Comment

PHOTO ELECTRIC EFFECT - DEFINITION, LAW AND EINSTEIN'S THEORY

 Photo electric effect Consider a negativity charged zinc plate connected to a gold leaf electroscope (Fig. 1). The divergence in the leaves...