Showing posts with label C theory. Show all posts
Showing posts with label C theory. Show all posts

Tuesday, 14 August 2018

Recursion used in C language

What is recursion in C ?

When a function call to itself or when a function call to a second function and in function definition of second function again a function call statement for first function is encountered, it is known as recursion and this types of function is called recursive function.

Types of recursion in C ?

1. Direct recursion :

                ABC ()
                {
                 "
                 "
                 "
                ABC () ;
                }

2. Indirect recursion :

                ABC ()
                  {
                   "
                   XYZ () ;
                   "
                 XYZ ()
                 {
                 "
                ABC () ;
                "

Program of recursive function to find factorial in C:

#include < stdio.h>
int factorial ( int ) ;
void main ()
{
int a , b ;
printf ("\n Enter a number:") ;
scanf ("%d", &a) ;
b = factorial (a) ;
printf ("%d" , b) ;
getch () ;
}
int factorial ( int a )
{
if ( a == 0 )
return 1 ;
else
return ( a * factorial ( a - 1 ) ) ;
}



Saturday, 21 July 2018

Basic function and operator used in C programming

1. What are input statement in C ?

Input statement are used to input the data from the user .some important input function are as the follows :

(a).  getchar () : This function is used to input a character from the user .

 Syntax :   character variable name = getchar () ;

(b).  gets () :  This function is used to input string from the user .

Syntax :      string (" character variable name ") ;
                    gets (" character variable name ") ;

(c). scanf () :  This is used to input any types of function.

Syntax :   scanf ("format specifier", & variable name )

2. What are output statement in c ?

Output statement are used to print the data on the output screen.some important function are as follows.

(a). Putchar () :  This function is used to output a character on the screen.

Syntax :  putchar (" character variable ") ;

(b).  puts ()  :  This function is used to displays string on the output screen.

Syntax :  puts (" string variable name")

(c). printf ()  :  This function is used to displays any types of data on the output screen.

Syntax :  printf (" any statement ")
                  or
               printf (" format specifiers ", variable name ) ;

3. what are extension of operating system ?

Ms word = .doc
Ms excel = .xls
Ms powerpoint = .ppt
C files = .c
c++ files = .ccp
database = .dbf

4. What are different keys used in C programming?

ALT + F = Find
ALT + F9 = compile
ALT + S = Save
ALT + F5 = Full or Short 
Flash + N = New Line
Ctrl + F9 = Run

Void main () :  To return not any value

clrscr () ;  This function is used to clear the screen.

                    

Thursday, 19 July 2018

Some important basic question of C that you have to know

1.  What is history of C ?

C was developed in 1960 by " Dennis ritchie " in america. It was growth of two earlier language .

       CPL :  Combined programming language 
       Bcpl : Basics combined programming language

C is middle language because we can do programming high level as well as the low level.

2.  What is # include ?

# include is preprocessor directive , it acts as inclusion to include header file in a program.

3.  What is header file ?

Header file contain description about the library function used in c program. There are number f library which are used as according to the use of user .

Exam :     <stdio.h>
                 <math.h>

4.  What is main () ?

Operating system always starts the execution of program from the main (). A C program must contain one main().

5.  What is debugging ?

Debugging is method of detection of error and after detection there must be having correction of error. 

6.  What are variables ?

A variable is special types of identifiers which is used to include special types of information

7.  How to declare a variable ?  

Syntax :     data type variable name :
 Exam  :      int a ;
                   char ch ;
                  float a ;

8.  What do you mean by variable initialization ?

It means to assign a value to the variable for which ( = equal to ) operator is used .

9.  What are rule for declare a variable ?

a.  Variable name should not start with a number.

b.  The name of variable should not be greater than 8.

c.  Any reserve words can not be name of variable.

d.  In case of variable name hyphen (-) symbols is not allowed.

10. What is string ? 

String is collection of character.

11. what are some special symbols used in C ?

( ) :  Unparamaterized Function

(a) :  Para metrical Function

Tuesday, 17 July 2018

Unions used in C programming language and uses union of structure

Union is the types of user define data structure which is same as like the structure but engaged style of both union and structure is totally different. As we knows that structure is used for storing different types of variable but each variable is stored in different spaces of memory whereas the union enables to store the different variable in same space of memory .the declaration of union is some similar to the structure used in C.The size of union is declared according to the size of variable of the union.

Declaration of union

union  a
{
short int x ;
char [5] ;
};
union a student ;

Program to uses union in C :

/* simple program to use union */
# include <stdio.h>
void main ()
{
union x
{
int i ;
char ch[3] ;
};
union a student ;
student.i=100 ;
printf ("\n student.i=%d", student.i) ;
printf ("student.ch[0] = %d\n",student.ch[0]) ;
printf ("student.ch[1] = %d\n",student.ch[1]) ;
printf ("student.ch[2] =%d\n",student.ch[2]) ;
}

As you seen in the written simple program we have declare a variable union x , and then a variable student for the union x thus it looks similar to the declaration of a structure variable in C. If you write any program for storing information about student of schools then you have to input different information about the student then you uses structure or union but there is too much wastage of memory in case of structure so for this we need the union .

Union of structure :

As we uses nested if else statement in basics of C here we uses structure nested into union and there are a union can be also nested into an another union.

/* structure nested in union */
# include <stdio.h>
void main ()
{
struct x
{
int i ;
char ch[2] ;
};
struct y
{
int j ;
char ab[2] ;
};
union z
{
struct x student ;
struct y values ;
};
union z odd ;
odd.student.i =102 ;
odd.values.ab[0] = 0 ;
odd.values.ab[1] = 55 ;
printf ('\n%d",odd.student.i) ;
printf ('\n%d",odd.values.j) ;
printf ('\n%d",odd.student.ch[0]) ;
printf ('\n%d",odd.values.ab[0]) ;
printf ('\n%d",odd.student.ch[1]) ;
printf ('\n%d",odd.values.ab[1]) ;
}

This is way by which we can declare a union variable in C . Read this before this structure in c.










                                                       

Saturday, 14 July 2018

Structures in C briefly explain with program of structure

A structure is data type which collect different atoms of the information that comprise a given entity and structure is also known as the collection of heterogeneous element . Structure is mainly used in the offices , schools . A structure contains a number of data types grouped together . These  data type may be same or different .

EXAMPLE :
                       if you want to store data about the student then there information is like name , age , roll no , fees , class etc then there are uses of different data structure like string , int , float , char so for that we have use these different methods.
1 . use a structure variable.
2 . you may use array for each different variables.

Program to use of structure :

# include<stdio.h>
 void main ()
{
struct student
{
float fee ;
int roll ;
char name ;
};
struct student s1 , s2 s3 ;
printf ("\n enter fee , roll and name of 3 students:") ;
scanf ("%f %d %c", &s1,fee, &s1,roll. &s1,name) ;
scanf ("%f %d %c", &s2,fee, &s2,roll. &s2,name) ;
scanf ("%f %d %c", &s3,fee, &s3,roll. &s3,name) ;
print ("\n Enter value are:") ;
printf ("%f %d %c", s1,fee, s1,roll, s1,name) ;
printf ("%f %d %c", s2,fee, s2,roll, s2,name) ;
printf ("%f %d %c", s3,fee, s3,roll, s3,name) ;
return 0 ;
}

How to uses structure in c :

(a)  declaration of a structure variables 

(b) accessing of structure element

(A)  Declaring a structure 

This is the first and important step in which we have to declare the name of variable name of variable and also write how much data types used in that given program. In the previous program we have use declaration of variable in which we have used three types of data types i.e float , char and int . Here you declare the data types as per the uses of the program. 
                                                                                             The general form for structure declaration statement is given below :

struct <structure name>
{
structure element 1 ;
structure element 2 ;
structure element 3 ;
..........
...........
...........
};
struct structure name structure variable name ;

This is the way by which you can declare a structure in C. 

(B) Accessing structure element :

In structure we can access different elements of an array using a subscript . we have used in the first sample program we have written about the structure ,
  
s1.name
s1.roll
s1.fee

structure variable.structure element

How to use array using structure :  

As we know array is collection of homogenous element ( of same type ) which have a common name and share a common property in this we have to declare more than a single elements.

/* usage of an array of structure *?
# include <stdio.h>
void main ()
{
struct student
{
char name ;
float fees ;
int roll no ;
}:
struct student s[50] ;
int i ;
for ( i = 0 ; i < 50 ; i++ )
{
printf ("\n enter name, fees and roll no:") ;
scanf ("%c %f %d", &s[i].name, &s[i].fees, &s[i].roll no) ;
}
for ( i = 0 ; i < 50 ; i++ )
printf ("%C %f %d\n", s[i].name,s[i].fees, s[i].roll no ) ;
}


By this method you can use an Array using structure element we further discuss in the next post .














Wednesday, 4 July 2018

User define function in C

A function can be define by the user as per the requirement of the user or programmer . To implement
a user define function , a programmer must contain the following three statement .

1. Function declaration or function prototype 

2. Function call statement

3. Function definition


# Function declaration or Function prototype :


The function provides the following information about a function .

A . What is return type of a function ? i.e which type of value is return by a function .

B . What is name of the function ?
  
C . How many argument or parameter is being used by a function ?

D . Type of argument or parameter being passed by a function ?

Syntax ;   return type function name ( data type 1 , data type 2 ) ;


Generally a function prototype is written before the definition of main function . It can also be written within main function . In this way it is written in two different ways and it is known as global or local. If it is written outside main function then it is called global function otherwise it is known as the local function prototype .

 # Function call statement : 

  A function call statement is an independent module which contains main statement which is written for the execution of program . This independent module is not part of the main function and always written out of the main function . 

Syntax : A . Function call statement when a function return any value .

                  variable name = function name ( argument 1 , argument 2 ) ;

Syntax : B . Function call statement when a function do not return any value .

                    function name ( argument 1 , argument 2 ) ;

# Function definition :

A function definition is that part of a program where the required exhaustible statement are written . It is main part of the program which is used to implement the concept of modularity in the program . This module is known as the user define function which is written or define as per the requirement of the user . 

Syntax : return type function name ( data type argument 1 , data type argument 2 ) ;
             {
             statement ;
              "
              "
              "
             }


The function definition is always written out of the definition of main function .

EXAMPLE : Program calculate the addition of two number using function .

                      /* user define function */
                      # include <stdio.h>
                      int sum ( int , int ) ;                  /* function prototype */
                      void main () 
                     {
                     int a , b ;
                     clrscr () ;
                     printf ("\n Enter value of a:") ;
                     scanf ("%d", &a) ;
                     printf ("\n Enter value of b:") ;
                     scanf ("%d", &b) ;
                     c = sum ( a , b ) ;                              /* function call statement */
                     printf ("\n sum = %d", c) ;
                     getch () ;
                     }
                     int sum ( int a , int b )               /* function definition */
                     {
                     int c ;
                     c = a + b ;
                     return c ; 
                     } 



  

 





C Function

When a program grow in size and complexity then we break that program into small module . These module are known as the function . Function increases the understandably of a program logic using function module independent logic can be defined .

Function provides the concept of modularity in a program which is best method to divide a complex program . 

Types of Function :

Function can be divided into two categories :

1. Library Function or  ( built in function )

2. User define or  ( Programmer define function )

1 . Library Function :

      These function are the ready made function provides by the library of the compiler itself . some library function are as the follows . 

A pow () ;-  This function is used to calculate the power of a given number this function works upon two arguments or parameter and after performing the operation always a integers types of variable . This required a header file for this function is < math.h>

Syntax :  int variable name = pow ( argument 1 , argument 2 ) ;

B . sqrt () ;-  This function is used to calculate the square root of a number . This function works on one argument and always return an integers type value .

Syntax :  int variable name = sqrt ( arg ) ;

C . toupper () ;  This function is used to convert a lower case character into upper case . the required header file is < c type.h >

Syntax :   character variable name = toupper ( char variable ) ;

D . tolower () ; This function is used to convert a upper case character into lower case required file    < c type .h >

syntax ; -  character variable name = tolower ( char variable ) ;


EXAMPLE :  Program power of two number using library function in C .

/* power of a number */
# include <stdio.h>
# include <math.h>
void main () 
{
int a ,b , c ;
clrscr () ;
printf ("\n Enter value of a:") ;
scanf ("%d", & a) ;
printf ("\n Enter value of b:") ;
scanf ("%d", & b) ;
c = pow ( a , b ) ;
printf ("\n %d", c) ;
getch () ;
}


EXAMPLE :  Program to convert a upper case character into lower case character  in C .

# include <stdio.h>
# include <math.h>
void main () ;
{
char ch ;
clrscr () ;
printf ("\n Enter a upper case character:") ;
scanf ("%d", & ch) ;
printf ("%c", tolower ( ch) ) ;
getch () ;
}

 User define function in next page 





Monday, 2 July 2018

C Array Types

In C programming array can be divided into two types :

1. Single dimensional array / Simple array / 1-D array / Linear array

2. Multidimensional array


# Single dimensional array :

When dimension is 1. or when there is a pair of square brackets with the name of array it is known as single dimension array . 

Declaration of single dimension array :

data type , variable name ;

# multidimensional array :

When there are number of pairs of square bracket . or , When there are more than one dimension it is known as multidimensional array . In C programming generally 2- D array are used for the implementation of matrices which is in forms rows and column . In double dimension array first dimension represent the no of rows and second dimension represent the no of columns . 

# Declaration of 2-D array : 

Syntax :    data type array name [ no of rows ] [ no of columns ] ;
                                            Or
                 data type  array name [ m ] [ n ] ;
                    Where  m = no of rows 
                                 n = no of columns
Example :
                 
int a [3] [3] ;
1 element = a [0] [0]
2 element = a [0] [1]
3 element = a [0] [2]
4 element = a [1] [0]
5 element = a [1[ [1]
6 element = a [1] [2]
7 element = a [2] [0]
8 element = a [2] [1]
9 element = a [2] [2]

# Input and output with double dimensional array :

# include <stdio.h>
void main ()
{
int a [3] [3] ;
for ( i = 0 ; i < 3 ; i++ )
{
for ( j = 0 ; j < 3 ; j++ )
{
printf ("\n Enter a number:") ;
scanf ("%d", & a[i] [j]) ;
}

#  Output with 2 - D array :

for ( i = 0 ; i < n ; i++ )
{
for ( j = 0 ; j < n ; j++ )
{
printf ("%d", a [i] [j]) ;
printf ("\n") ;
}
}








Sunday, 1 July 2018

Array in C

 An array is collection of homogenous element ( of same types ) consist a common property and share  a common name . An array is also known as collection of homogenous element from ( 0 to n-1 ) where n represent the maximum number of element of array .    
An ordinary variable can contain only one value of one types it is not possible to assign more than one value at a single variables.

EXAMPLE :
                     Consider the following variable declaration and initialization .
                     int a = 10 ;
                     Here , a denote a single memory location with a value of 10.
                     If we update the value of variable .
                     a = 20 ;
                     Now the same memory location will contain the value 20 .
                    

Declaration of an array :

An array can be declared as an ordinary variable including the size specification in pair of [ ] bracket.

Syntax :   data type  variable name[ size ] ;
                        or
                data type  variable name [n] ;
              where n = number of elements

EXAMPLE 
                       int a[3] ;
                       In this example the name of an array is " a " which can contain maximum value of 3 at the 3 different locations .
 These 3 different memory location are known as 3 element in c programming , these 3 element are also termed as the 3 subscript . that's why array is also known as " sub scripted variable" . The three different types of variable of a can be written as follows :
                                                    1 element : a [0]
                                                    2 element : a [1]
                                                    3 element : a [2]

Initialization of an Array :


An array can be initialized in three different ways :
(1.)  Element by Element 
    Exam :   int a [3] ;
                    a[0] = 22 ;
                    a[1] = 5 ;
                    a[2] = 66 ;

(2,) Declaration and initialization at same time
    Exam :  int a [3] = { 11 , 22 , 66 } ;
it means 
            a [0] = 11 ;
            a [1] = 22 ;
            a [2] = 66 ;

(3.) Initialization from the user 
    Exam :  
              {
              int i ;
              int a [3] ;
             for ( i = 0 ; i < 3 ; i++ )
             {
             printf ("\n Enter a number:") ;
             scanf ("%d", & a [i]) ;
             }

Output :
              for ( i = 0 ; i < 3 ; i++ )
              printf ("\n %d", a [i]) ;

Program to Access  an array in C :

/* uses of array */
# include <stdio.h>
void main () 
{
int a[5] , i ;
clrscr () ;
for ( i = 0 ; i < 5 ; i++ )
{
printf ("\n Enter a number:") ;
scanf ("%d", & a [i]) ;
}
for ( i = 0 ; i < 5 ; i++ )
printf ("\n %d", a[i]) ;
getch ()
}


When above result is compiled :

Here as from loop statement is compiled then a condition of 5 is check out and a element of a[0] to a[5] is initialized from the user



       
             





 

             
                                                               

Friday, 29 June 2018

Difference between Call by value and Call by references using program of swapping of variable in C

Using call by value method :
#include <stdio.h> 
void swap ( int , int ) ;
void main ()
{
int a , b , ;
clrscr () ;
printf ("\n Enter value of a:") ;
scanf ("%d", & a) ;
printf ("\n Enter value of b:") ;
scanf ("%d", & b) ;
swap ( a , b ) ;
getch () ;
}
void swap ( int a , int b )
{
int temp ;
temp = a ;?
a = b ;
b = temp ;
printf ("\n %d", a) ;
printf ("\n %d", b) ;
}

Working :   In this program the value of actual argument will be copied in the formal argument which is written in main () and function definition respectively .
                                                                                            These two types of argument will be totally different because of different address the changes which is done in the function definition will not be visible in the main function . This method in which the value of main function is copied in the variables of function definition is known as the call by value method or argument passed by value method .

Using call by references method : 

/* swapping of variables */
#include <stdio.h> 
void swap ( int* , int * ) ;
void main () 
{
int a ,b ; 
clrscr () ;
printf ("\n Enter a number:") ;
scanf ("%d", &a) ;
printf ("\n Enter a number:") ;
scanf ("%d", &b) ;
swap ( &a , &b ) ;
printf ("\n a = %d b = %d", a,b) ;
getch () ;
}
void swap ( int *a , int *b )
{
int temp ;
temp = *a ;
*a = *b ;
*b = temp ;
}

Working :   In these types of program if we pass the address from main function to the function definition then these address must be stored in a special types of variable and the changes will be visible throughout the program in the function definition and as well as the main function .











 

Pointers in C


  • what is Pointer in C !
pointer are the special type of variables which is used to store the address the address of another variable. As we know that the ordinary variable can contain only a value of its types it is not possible to assign an address to an ordinary variable.

How to declare a pointer variables .

Syntax :  data type * variable name ;                     *   means = in directional operator (asterisk symbol)

Examples :
                  int * a ;                                 /* pointer to integers */
                 char * a ;                              /* pointer to character */
                 float * a ;                             /* pointer to float */
The declaration tells the C compiler to :
1. Reserve space in memory to hold particular value .
2. Associate the name i with the memory location .
3. store the value the location

How to assign a pointer variables :

Syntax :  pointer variable name = address of variable ;
                                      OR
               pointer variable name = & name of another variable ;         & = reference operator 

Examples :  
                    a = &b ;        ( pointer assignment )

Program of use of pointer :

# include <stdio.h>
void main () 
{
int a = 10 ;               /* var. declaration and initialization */  
int *b ;                    /* pointer var. declaration */ 
b = &a ;                 /* pointer assignment */
clrscr () ;
printf ("\n %d". a) ;                    value of a = 10
printf ("\n %d", &a) ;                 address of a = FF01
printf ("\n %d", b) ;                   address of a = &a = FF01
printf ("\n %d", &b) ;                address of b = FF05
printf ("\n %d", *b) ;                 value of b becomes = 10
getch () ;
}

How to call a function in C  :  

1. Call by value        /* argument passed by value */                                                          
2. call by reference   /* argument passed by references*/ 

                          *    In call by value method the value of variable is passed in a function call statement and i.e stored on another address in function definition . In this way there will two types of argument .
                      1. Argument in main function  ( Actual argument )
                      2. Argument in function definition ( Formal argument )

                       *     In call by reference method address of argument are passed from main function to the function definition and these address are collected in the pointer variable thus by using call by reference method if we update the value of variable in the function definition than it will be reflected throughout the program .It shows by call by reference method we can return more than one value at a time , which is not possible with ordinary  .

                        
  NOTE :  Uninitialized pointer variable are known as the " Dangling pointer".


                             


Featured post

Kalgudi Interview Round!!

Those who cleared the second round of kalgudi got an email from exceller to complete their Kalgudi Interview Round. So here we will discuss ...