Pages

Tuesday, 26 February 2019

Campus Drive Questions and Answers

Campus Drive Questions and Answers 

Q) What are main characteristics of C language?

Ans)

C is a procedural language. The main features of C language include low-level access to memory, simple set of keywords, and clean style. These features make it suitable for system programming like operating system or compiler development.

Q) What is difference between i++ and ++i?

Ans

1) The expression ‘i++’ returns the old value and then increments i. The expression ++i increments the value and returns new value.

2) Precedence of postfix ++ is higher than that of prefix ++.

3) Associativity of postfix ++ is left to right and associativity of prefix ++ is right to left.
4) In C++, ++i can be used as l-value, but i++ cannot be. In C, they both cannot be used as l-value.


Q) What are entry control and exit control loops?

Ans.

 C support only 2 loops:
1.     Entry Control: This loop is categorized in 2 part

a. while loop

b. for loop
2.     Exit control: In this category, there is one type of loop known as

a. do while loop


Q) Why pre-processor directive does not have a semi-colon at last?

Ans

Semi-colon is needed by the compiler and as the name suggests Preprocessors are programs that process our source code before compilation.Therefore the semi-colon is not required.

Q) What is the difference between including the header file with-in angular braces < > and double quotes ” “?

Ans

 If a header file is included within < > then the compiler searches for the particular header file only within the built-in include path. If a header file is included within ” “, then the compiler searches for the particular header file first in the current working directory, if not found then in the built-in include path.

Q) How will you print “Hello World” without semicolon?
Ans
Void main()
{
            if (printf("Hello World"))
}


Program 1:
#include <stdio.h> 
void fun() 
            // static variables get the default value as 0. 
            static int x; 
            printf("%d ", x); 
            x = x + 1; 

int main() 
            fun(); 
            fun(); 
            return 0; 

Output: 0 1

Program 2:
#include <stdio.h>
main()
{
            int i;
            int *pi = &i;
            scanf("%d", pi);
            printf("%d\n", i+5);
}

Output: On execution, the value printed is 5 more than the integer value entered.


Program 3:
void main()
{
            int i=-3,j=2,k=0,m;
            m=++i && ++j || ++k;
            printf("%d %d %d %d",i,j,k,m);
}
Ans
-2  3 0 1

Program 4:
int f(int j)
{
static int i = 50;
int k;
if (i == j)
{
            printf("something");
            k = f(i);
            return 0;
}
else return 0;
}

Ans
The function will exhaust the runtime stack or run into an infinite loop when j = 50

Program 5:
Assume the following C variable declaration
int *a[10],b[10[10];
Of the following expressions

i a[2]

ii a[2][3]
iii b[1]
iv b[2][3]
which will  give compile-time errors

Ans:
b[1]

Program 6:
Consider the following declaration of a ‘two-dimensional array in C:
Char a[100][100];
Assuming that the main memory is byte-addressable and that the array is stored starting from memory address 0, the address of a[40][50]
Ans
Address a[i][j] = base address + element size (maximum number of columns * i +  j )
          a[40][50] = 0 + 1 (100 *40 + 50 )
          a[40][50] = 4050

Program 7:
Consider the following declaration of a ‘two-dimensional array in C:
Char a[10][20][30]; i.e a[M][N][O]
Assuming that the main memory is byte-addressable and that the array is stored starting from memory address 0, the address of a[4][5][6] i.e a[i][j][k]
Ans
Address a[i][j][k] = base address + element size (i*N*O + j*O  + k )
          a[4][5][6] = 0 + 1 (4 * 20 * 30 + 5 * 30 + 6 )
          a[4][5][6] =   2400 + 150 + 6
          a[4][5][6] =   2556

Program 8:

What will be the output of the following C program segment?
char inchar = 'A';
switch (inchar)
{
case 'A' :
            printf ("choice A \n") ;
case 'B' :
            printf ("choice B ") ;
case 'C' :
case 'D' :
case 'E' :
default:
            printf ("No Choice") ;
}

Ans:
choice A
choice B No Choice


Program 9:
What will be the output of the following C program segment?
char p[20];
char *s = "string";
int length = strlen(s);
int i;
for (i = 0; i < length; i++)
            p[i] = s[length — i];
printf("%s",p);

Ans
no output is printed
Explanation:
 Let us consider below line inside the for loop
p[i] = s[length — i];
For i = 0, p[i] will be s[6 — 0] and s[6] is ‘\0’
So p[0] becomes ‘\0’. It doesn’t matter what comes in p[1], p[2]….. as P[0] will not change for i >0. Nothing is printed if we print a string with first character ‘\0’


Program 10:
Consider the following C program
main()
{
   int x, y, m, n;
   scanf ("%d %d", &x, &y);
   /* x > 0 and y > 0 */
   m = x; n = y;
   while (m != n)
   { 
      if(m>n)
         m = m - n;
      else
         n = n - m;
   }
   printf("%d", n);
}

Ans:
GCD (greatest common divisor ) of x and y

Program 11:
#include<stdio.h>
void main()
{
float x=16.3;
if(x==16.3)
printf("HAI");
else
printf("Hello");
}


output :




Program 12:
#include<stdio.h>
#define size 2+4
void main()
{
printf("%d",size*size);
}


output:



Program 13:
#include<stdio.h>
void main()
{
int i=1;
printf("\n %d\n %d\n %d\n %d",i++,i++,i++,i++);
}



output:

Program 14:
#include<stdio.h>
void main()
{
int i=5;
printf("\n %d\n %d\n %d\n %d\n %d",i++,i--,++i,--i ,i);
}


output:



Program 15:
#include<stdio.h>
void main()
{
int a[]={1,2,3,4,5,6};
printf("%d",1[a]);
}


output:


Program 16:
#include<stdio.h>
int sum (int);
int sum (int n)
{
 if(n<1)
  return n;
 else
  return (n + sum(n-1));
}
void main()
{
 sum(5);
 printf("sum is %d ",sum(5));
}


Answer : 15


Program 17:
#include<stdio.h>
void sum (int ,int ,int );
void sum (int x,int y,int z)
{
 y=y+1;
 z=z+x;
}
void main()
{
 int a=2,b=3;
 sum((a+b),a,a);
 printf("sum is %d ",a);
}
 

Answer: 2
 


Program 17:


Saturday, 23 February 2019

Storage Classes in C

             Storage Classes are used to describe about the features of a variable/function. These features basically include the scope, visibility and life-time which help us to trace the existence of a particular variable during the runtime of a program.

C language uses 4 storage classes, namely:
1. auto
2. static
3. register
4. extern

1.Program to illustrate auto variable:
#include<stdio.h>
void autoStorageClass(void); // function declaration
int main()
{
    printf("\nA program to demonstrate auto Storage Classess in C");
    autoStorageClass(); //calling function
    return 0;
}

void autoStorageClass() //called function
{
  auto int a = 1;
   {
    auto int a = 2;
     {
      auto int a = 3;
      printf("\n auto int Value of the variable 'a' is : %d",a);
      }
     printf("\n auto int Value of the variable 'a' is : %d",a);
    }
  printf("\n auto int Value of the variable 'a' is : %d",a);
}

output:


2.Program to illustrate static variable: 
#include<stdio.h>
void staticStorageClass(void); // function declaration
int main()
{
  printf("\nA program to demonstrate static Storage Classess in C");
  staticStorageClass(); //calling function
  return 0;
}

void staticStorageClass() //called function
{
    int i = 0;
    printf("\nLoop started:\n");
    for (i = 1; i < 5; i++)   
    {
      static int y = 5;
      int p = 10;
      y++;
      printf("\nAt iteration %d The static int y value is %d\n",i, y);
      p++;
      printf("\nAt iteration %d The non static int p value is %d\n",i, p)
   }
    printf("\nLoop ended:\n");
}

output:

 3.
Program to illustrate register variable: 
#include<stdio.h>
void registerStorageClass(void); // function declaration
int main()
{
 
printf("\nA program to demonstrate register Storage Classess in C");
  registerStorageClass(); //calling function
  return 0;
}

void registerStorageClass() //called function
{
 
printf("\nDemonstrating register class\n\n");
  register char b = 'A' ;
  printf("\n Value of the register variable 'b' : %d", b); // prints ASCII value of A 
  register char x = 'a' ;
  printf("\n Va
lue of the register variable 'x': %d\n",x); // prints  ASCII value of a
}

output:


4. Program to illustrate extern variable:
consider two programs in two separate files  named by externfile1.c and externfile2.c

Program in externfile1.c
// A C program to demonstrate extern storage class
#include<stdio.h>
#include"externfile2.c"
int x=20; // global declaration
void externStorageClass(void); // function declaration
int main()
{
 
int x=10;
  printf("\n The value assigned with out extern to variable  x is :%d",x);
  externStorageClass(); //calling function
  printf("\n The value assigned with extern to variable 
                  x after sub program  is :%d",x); 
 return 0;

}

Program in externfile2.c
#include<stdio.h>
extern int x;
void externStorageClass(void) //called function
{
  printf("\n\n The value assigned with extern to variable  x is :%d",x);
}
 
Now compile the program one i.e externfile1.c the the output of extern storage class is  


Program to illustrate Fibonacci series using recursion

 Program to illustrate Fibonacci series using recursion
//fibonacci series of a given number using recursion
#include<stdio.h>
int s,x,a,b,r;  // global declaration
int fib(int); // function declaration
int main()
{
 int i,n,rev;
 printf("enter any number:\n");
 scanf("%d",&n);
 for(i=0;i<n;i++)
 {
  s=fib(i); // calling function
  printf("%d\t",s);
 }
}

int fib (int x)  // called function
 {
  int a,b;
  if(x <= 1)
    return (x);  // base case
  else
  {
   a=fib(x-1);
   b=fib(x-2);
   return (a+b); // recursive calling
  }
}
output:

Program to illustrate reverse of a given number using recursion

Program to illustrate reverse of a given number using recursion 

//reverse of a given number using recursion
#include<stdio.h>
int sum,r;  // global declaration
int reverse(int); // function declaration
int main()
{
 int n,rev;
 printf("enter any value:\n");
 scanf("%d",&n);
 rev=reverse(n);
 printf("%d reverse is = %d",n,rev);
 return 0;
}

int reverse(int n)
 {
 
  if(n != 0)
    {
     r= n % 10;
     sum=sum * 10 + r;
     n = n / 10;
     reverse(n);
    }
   else
     return sum;
}

output:

Friday, 22 February 2019

Program to find multiplication using recursion.

Program to find multiplication using recursion.

mul(a,b) = a                        b=1 // Base case
              = a + mul(a,b-1)    b>1 // Recursive case

// recursion program on multiplication
#include<stdio.h>
int a,b,c;   /* global variable */
int mul(int,int); /* function declaration */
void main()
{
 int c;
 printf("\n enter any two numbers:\n");
 scanf("%d%d",&a,&b);
 c=mul(a,b);
 printf("multiplication of %d and %d = %d",a,b,c);
}

int mul(int a,int b)
 {
  if(b==1)
  return a;
  else
  c=a+mul(a,b-1);
  return (c);
 }

output:

Program to find factorial using recursion.


Recursion:

            Recursion is the process by which a function calls itself repeatedly.

Program to illustrate factorial of a given number using recursion :

fact(n) = 1                         n=0 // Base case
            = n *  fact ( n -1)    n>0 // Recursive case
// Recursion program on factorial we can have the value upto n=15
#include<stdio.h>
int fact; // global declaration
int factorial(int); // function declaration
void main()
{
int n ;
printf("enter any value:\n");
scanf("%d",&n);
fact=factorial(n); // calling function
printf("factorial of %d = %d",n,fact);
}


int factorial(int n) // called function
{
if(n==0)
return 1;
else
fact=n*factorial(n-1);
return (fact);
}
output:


Program to illustrate Call By Value and Call By Reference

C provides two ways of passing arguments to a function.
 1. Call by value or Pass by value.
 2. Call by reference.

1. Call by value or Pass by value:
            In this method a copy of each of the actual arguments is made first then these values are assigned to the corresponding formal arguments.
            This means that the changes made by the called function have no effect on the values of actual arguments in the calling function.

Program to illustrate call by value or pass by value
#include<stdio.h>
#define pf printf
#define sf scanf
void cbv(int,int); // fuction declaration or prototype
void main()
{
 int a,b;
 pf("\n Enter a value :");
 sf("%d",&a);
 pf("\n Enter b value :");
 sf("%d",&b);
 pf("\n Initial values of a=%d and b=%d\n",a,b);
 pf("\n Calling the function \n");
 cbv(a,b);   // calling function
 pf("\n Final   values of a=%d and b=%d",a,b);
}

void cbv(int p,int q)  // called function
 {
  int temp;
  temp=p;
  p=q;
  q=temp;
  pf("\n Value of p (inside function) = %d\n ", p);
  pf("\n Value of q (inside function) = %d\n", q);
 }

output:


2. Call by reference :
To use call by reference we need to do two things:
 
1. Pass the addresses of the actual arguments instead of passing values to the function.
2. Declare the formal arguments of the function as pointer variables of an appropriate type.
Program to illustrate Call By Reference
#include<stdio.h>
#define pf printf
#define sf scanf
void cbr(int *,int *); // fuction declaration or prototype
void main()
{
 int a,b;
 pf("\n Enter a value :");
 sf("%d",&a);
 pf("\n Enter b value :");
 sf("%d",&b);
 pf("\n Initial values of a=%d and b=%d\n",a,b);
 pf("\n Calling the function \n");
 cbr(&a,&b);   // calling function
 pf("\n Final   values of a=%d and b=%d",a,b);
}

void cbr(int *p,int *q)  // called function
 {
  int temp;
  temp=*p;
  *p=*q;
  *q=temp;
  pf("\n Value of p (inside function) = %d\n ", *p);
  pf("\n Value of q (inside function) = %d\n", *q);
 }

output:

Wednesday, 20 February 2019

Program to illustrate Functions in c

Functions in c 
A function is a set of statements that takes input, do some specific computation and produces output.

Function Declaration
Function declaration tells compiler about number of parameters function takes, data-types of parameters and return type of function. 

Parameter Passing to functions
The parameters passed to function are called actual parameters ,and the parameters received by function are called formal parameters.

There are two most popular ways to pass parameters.
Pass by Value: In this parameter passing method, values of actual parameters are copied to function’s formal parameters and the two types of parameters are stored in different memory locations.
Pass by Reference Both actual and formal parameters refer to same locations, so any changes made inside the function are actually reflected in actual parameters of caller.



Types of Functions in C
  • Function with no argument and no Return value.
  • Function with argument and No Return value.
  • Function with no argument and Return value.
  • Function with argument and Return value

Type 1 Program:
Function with no argument and no return value

// program to illustrate Function with no argument and no Return value.
#include<stdio.h>
#define pf printf
#define sf scanf
void sum (void); // prototype or function declaration    
int main()
 {
  sum(); //calling funtion used parameters are called actual arguments
  return 0;
 }

void sum(void) // called function used parameters are called formal arguments
 {
  int a, b ,r;
  pf("\n Enter a nd b values : ");
  sf("%d %d",&a,&b);
  r=a+b;
  pf("\n sum is %d",r);
 }

output:


Type 2 Program:
Function with argument and no return value

// program to illustrate Function with argument and no Return value.
#include<stdio.h>
#define pf printf
#define sf scanf
void sum (int , int); // prototype or function declaration   
int main()
 {
  int a, b;
  pf("\n Enter a nd b values : ");
  sf("%d %d",&a,&b);
  sum(a,b); //calling function ,used parameters are called actual arguments
  return 0;
 }

void sum(int p , int q) // called function ,used parameters are called formal arguments
 {
  int r;
  r=p+q;
  pf("\n sum is %d",r);
 }


output:


Type 3 Program:
Function with no argument and return value

// program to illustrate Function with no argument and Return value.
#include<stdio.h>
#define pf printf
#define sf scanf
int sum (void); // prototype or function declaration    
int main()
 {
  int c;
  c=sum(); //calling funtion ,used parameters are called actual arguments
  pf("\n sum is %d",c);
  return 0;
 }

int sum (void) // called function ,used parameters are called formal arguments
 {
  int a,b,r;
  pf("\n Enter a nd b values : ");
  sf("%d %d",&a,&b);
  r=a+b;
  return r;
 }

output:


Type 4 Program:
Function with argument and return value

// program to illustrate Function with argument and Return value.
#include<stdio.h>
#define pf printf
#define sf scanf
int sum (int , int); // prototype or function declaration    
int main()
 {
  int a,b,c;
  pf("\n Enter a nd b values : ");
  sf("%d %d",&a,&b);
  c=sum(a,b); //calling function ,used parameters are called actual arguments
  pf("\n sum is %d",c);
  return 0;
 }

int sum (int p,int q) // called function ,used parameters are called formal arguments
 {
  int r;
  r=p+q;
  return r;
 }

output:


Programs in turboc3 : Files

File Handling in C File Handling concept in C language is used for store a data permanently in computer. Using this concept we can store our...