+ All Categories
Home > Engineering > C basic questions&ansrs by shiva kumar kella

C basic questions&ansrs by shiva kumar kella

Date post: 14-Apr-2017
Category:
Upload: manoj-kothagulla
View: 39 times
Download: 3 times
Share this document with a friend
16
Name :shiva kumar kella Email : [email protected] Page 1 1)what is function? Ans: A function is a group of statements that together perform a task. Every C program has at least one function, which is main(), and all the most trivial programs can define additional functions. 2)what is pointer? Ans: Pointer is a user defined data type which creates special types of variables which can hold the address of primitive data type like char, int, float, double or user defined data type like function, pointer etc. or derived data type like array, structure, union, enum. 3)what is array? Ans: C programming language provides a data structure called the array, which can store a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type. Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent individual variables. A specific element in an array is accessed by an index. All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element. 4)what is string? Ans: a one-dimensional array of characters which is terminated by a null character '\0'. Thus a null-terminated string contains the characters that comprise the string followed by a null. 5)what is structure? Ans: A structure is a collection of variables under a single name. These variables can be of different types, and each has a name which is used to select it from the structure. A structure is a convenient way of grouping several pieces of related information together. A structure can be defined as a new named type, thus extending the number of available types. It can use other structures, arrays or pointers as some of its members, though this can get complicated unless you are careful.
Transcript
Page 1: C basic questions&ansrs by shiva kumar kella

Name :shiva kumar kella Email : [email protected] Page 1

1)what is function?

Ans:

A function is a group of statements that together perform a task. Every C program has at least one function, which is

main(), and all the most trivial programs can define additional functions.

2)what is pointer?

Ans:

Pointer is a user defined data type which creates special types of variables which can hold the address of primitive data

type like char, int, float, double or user defined data type like function, pointer etc. or derived data type like array, structure,

union, enum.

3)what is array?

Ans:

C programming language provides a data structure called the array, which can store a fixed-size sequential

collection of elements of the same type. An array is used to store a collection of data, but it is often more useful

to think of an array as a collection of variables of the same type.

Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare one array

variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent individual

variables. A specific element in an array is accessed by an index.

All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the

highest address to the last element.

4)what is string?

Ans:

a one-dimensional array of characters which is terminated by a null character '\0'. Thus a null-terminated string contains the

characters that comprise the string followed by a null.

5)what is structure?

Ans:

A structure is a collection of variables under a single name. These variables can be of different types, and each has a name which is

used to select it from the structure. A structure is a convenient way of grouping several pieces of related information together.

A structure can be defined as a new named type, thus extending the number of available types. It can use other structures, arrays or pointers as some of its members, though this can get complicated unless you are careful.

Page 2: C basic questions&ansrs by shiva kumar kella

Name :shiva kumar kella Email : [email protected] Page 2

6)what is union?

Ans:

A union is a special data type available in C that enables you to store different data types in the same memory location. You can define a union with many members, but only one member can contain a value at any given time. Unions provide an efficient way of using the same memory location for multi-purpose.

7)what is calloc?

Ans:

The C library function void *calloc(size_t nitems, size_t size) allocates the requested memory and returns a pointer to it. The difference in malloc and calloc is that malloc does not set the memory to zero where as calloc sets allocated memory to zero.

8)what is malloc?

Ans :

the library function malloc is used to allocate a block of memory on the heap. The program accesses this block of memory via a pointer that malloc returns. When the memory is no longer needed, the pointer is passed to free which deallocates the memory so that it can be used for other purposes.

9)what is macro?

Ans:

A macro is a fragment of code which has been given a name. Whenever the name is used, it is replaced by the contents of the macro. There are two kinds of macros

10)what is typedef?

Ans:

"C allows you to explicitly define new data type names by using the keyword typedef. You are not actually creating a new data type, but rather defining a new name for an existing type." ^ Typedef as a Synonym, It allows you to introduce synonyms for types which could have been declared some other way.

11)what is function prototype?

Ans:

A function prototype is basically a definition for a function. It is structured in the same way that a normal function is structured, except instead of containing code, the prototype ends in a semicolon. Normally, the C compiler will make a

Page 3: C basic questions&ansrs by shiva kumar kella

Name :shiva kumar kella Email : [email protected] Page 3

single pass over each file you compile. If it encounters a call to a function that it has not yet been defined, the compiler has no idea what to do and throws an error.

12)difference between call by value and call by reference?

Ans:

Call by Value :If data is passed by value, the data is copied from the variable used in for example main() to a variable used by the function. So if the data passed (that is stored in the function variable) is modified inside the function, the value is only changed in the variable used inside the function

Call by Reference :If data is passed by reference, a pointer to the data is copied instead of the actual variable as is done in a call by value. Because a pointer is copied, if the value at that pointers address is changed in the function, the value is also changed in main()

13)difference between calloc and malloc?

Ans:

malloc() takes a single argument (memory required in bytes), while calloc() needs two arguments. Secondly, malloc() does not initialize the memory allocated, while calloc() initializes the allocated memory to ZERO. calloc() allocates a memory area, the length will be the product of its parameters

14)difference between structure and union?

Ans:

The difference between structure and union in c are:

1. union allocates the memory equal to the maximum memory required by the member of the union but structure allocates the memory equal to the total memory required by the members.

2. In union, one block is used by all the member of the union but in case of structure, each member have their own memory space .

15)difference between ++*p and *p++?

Ans:

++*p means prefix of the variable.Ex1:class {int *p=4;int *q=++*p;s.o.p(*q);}output:the value of *q is 5.*p++ means postfix of the variable.Ex2:class{int *p=4;int *q=*p++;s.o.p(*q);}output:the value of *q is 4.Ex3:class{int i=5; int j=i++; int k=i++; int t=i;s.o.p(t);}output:the value of j is 5the value of k is 6the value of t is 7.

Page 4: C basic questions&ansrs by shiva kumar kella

Name :shiva kumar kella Email : [email protected] Page 4

16)difference between static and auto keyword?

Ans:

"automatic" means they have the storage duration of the current block, and are destroyed when leaving the block. "local" means they have the scope of the current block, and can't be accessed from outside the block. If a local variable is static, then it is not destroyed when leaving the block; it just becomes inaccessible until the block is reentered. See the for-loop examples in my answer for the difference between automatic and static storage duration for local variables.

17)difference between function and macro?

Ans:

1.Macro consumes less time: When a function is called, arguments have to be passed to it, those arguments are accepted by corresponding dummy variables in the function. Then they are processed, and finally the function returns a value that is assigned to a variable (except for a void function). If a function is invoked number of times, the times add up, and compilation is delayed. On the other hand, the macro expansion had already taken place and replaced each occurrence of the macro in the source code before the source code starts compiling, so it requires no additional time to execute.

2. Function consumes less memory: Prior to compilation, all the macro-presences are replaced by their corresponding macro expansions, which consumes considerable memory. On the other hand, even if a function is invoked 100 times, it still occupies the same space. Hence function consumes less memory.

18)what is storage class in c?

Ans:

A storage class defines the scope (visibility) and life time of variables and/or functions within a C Program.

There are following storage classes which can be used in a C Program

auto register static extern

auto - Storage Class

auto is the default storage class for all local variables.

register - Storage Class

register is used to define local variables that should be stored in a register instead of RAM. This means that the variable has a maximum size equal to the register size (usually one word) and cant have the unary '&' operator applied to it (as it does not have a memory location).

Page 5: C basic questions&ansrs by shiva kumar kella

Name :shiva kumar kella Email : [email protected] Page 5

static - Storage Class

static is the default storage class for global variables. The two variables below (count and road) both have a static storage class

extern - Storage Class

extern is used to give a reference of a global variable that is visible to ALL the program files. When you use 'extern' the variable cannot be initalized as all it does is point the variable name at a storage location that has been previously defined

19)how to execute c program(steps)?

Ans:

Steps are :

1.Save: f2

2.Compile :alt+f9

3.Run: ctrl+f9 ,and

4.Output :alt +f5

20)what is bit wise operator’s?

Ans:

C provides a compound assignment operator for each binary arithmetic and bitwise operation (i.e. each operation which accepts two operands). Each of the compound bitwise assignment operators perform the appropriate binary operation and store the result in the left operand.

PROGRAMS:-

1) write a program to find weather the given year is leap year or not?

Ans:

#include <stdio.h> int main() { int year; printf("Enter a year to check if it is a leap year\n"); scanf("%d", &year);

Page 6: C basic questions&ansrs by shiva kumar kella

Name :shiva kumar kella Email : [email protected] Page 6

if ( year%400 == 0) printf("%d is a leap year.\n", year); else if ( year%100 == 0) printf("%d is not a leap year.\n", year); else if ( year%4 == 0 ) printf("%d is a leap year.\n", year); else printf("%d is not a leap year.\n", year); return 0; }

2) write a program to find weather the given number is prime number or not?

Ans:

#include<stdio.h> int main() { int n, i = 3, count, c; printf("Enter the number of prime numbers required\n"); scanf("%d",&n); if ( n >= 1 ) { printf("First %d prime numbers are :\n",n); printf("2\n"); } for ( count = 2 ; count <= n ; ) { for ( c = 2 ; c <= i - 1 ; c++ ) { if ( i%c == 0 ) break; } if ( c == i ) { printf("%d\n",i); count++; } i++;

Page 7: C basic questions&ansrs by shiva kumar kella

Name :shiva kumar kella Email : [email protected] Page 7

} return 0; }

3) write a program to find factorial of given number ? Ans: without using recursion :

#include <stdio.h> int main() { int c, n, fact = 1; printf("Enter a number to calculate it's factorial\n"); scanf("%d", &n); for (c = 1; c <= n; c++) fact = fact * c; printf("Factorial of %d = %d\n", n, fact); return 0; }

with using recursion :

#include<stdio.h> int factorial(int n); int main() { int n; printf("Enter an positive integer: "); scanf("%d",&n); printf("Factorial of %d = %ld", n, factorial(n)); return 0; }

Page 8: C basic questions&ansrs by shiva kumar kella

Name :shiva kumar kella Email : [email protected] Page 8

int factorial(int n) { if(n!=1) return n*factorial(n-1); }

4) write a program to fibonacci without using Recursion & using Recursion ?

ans:

Fibonacci series in c using for loop

/* Fibonacci Series c language */ #include<stdio.h> int main() { int n, first = 0, second = 1, next, c; printf("Enter the number of terms\n"); scanf("%d",&n); printf("First %d terms of Fibonacci series are :-\n",n); for ( c = 0 ; c < n ; c++ ) { if ( c <= 1 ) next = c; else { next = first + second; first = second; second = next; } printf("%d\n",next);

Page 9: C basic questions&ansrs by shiva kumar kella

Name :shiva kumar kella Email : [email protected] Page 9

} return 0; }

Fibonacci series program in c using recursion

#include<stdio.h> int Fibonacci(int); main() { int n, i = 0, c; scanf("%d",&n); printf("Fibonacci series\n"); for ( c = 1 ; c <= n ; c++ ) { printf("%d\n", Fibonacci(i)); i++; } return 0; } int Fibonacci(int n) { if ( n == 0 ) return 0; else if ( n == 1 ) return 1; else return ( Fibonacci(n-1) + Fibonacci(n-2) ); }

Page 10: C basic questions&ansrs by shiva kumar kella

Name :shiva kumar kella Email : [email protected] Page 10

5) write a program to swap two numbers without using third variable ?

Ans:

#include <stdio.h>

void main()

{

int a,b;

printf("enter number1: ie a");

scanf("%d",a);

printf("enter number2:ie b ");

scanf("%d",b);

printf(value of a and b before swapping is a=%d,b=%d"a,b);

a=a+b;

b=a-b;

a=a-b;

printf(value of a and b after swapping is a=%d,b=%d"a,b);

}

6) write a program to reverse a given number . ?

Ans:

#include <stdio.h> int main() { int n, reverse = 0; printf("Enter a number to reverse\n"); scanf("%d",&n); while (n != 0)

Page 11: C basic questions&ansrs by shiva kumar kella

Name :shiva kumar kella Email : [email protected] Page 11

{ reverse = reverse * 10; reverse = reverse + n%10; n = n/10; } printf("Reverse of entered number is = %d\n", reverse); return 0; }

7) write a program to reverse a given string ?

Ans:

#include <stdio.h> #include <string.h> int main() { char arr[100]; printf("Enter a string to reverse\n"); gets(arr); strrev(arr); printf("Reverse of entered string is \n%s\n",arr); return 0; }

8) write a program to check given number is palindrome or not ? Ans: #include <stdio.h> #include <string.h> int main() { char a[100], b[100]; printf("Enter the string to check if it is a palindrome\n"); gets(a);

Page 12: C basic questions&ansrs by shiva kumar kella

Name :shiva kumar kella Email : [email protected] Page 12

strcpy(b,a); strrev(b); if( strcmp(a,b) == 0 ) printf("Entered string is a palindrome.\n"); else printf("Entered string is not a palindrome.\n"); return 0; }

9) write a program to sorting the given array by using selection sort?

Ans:

#include <stdio.h> int main() { int array[100], n, c, d, position, swap; printf("Enter number of elements\n"); scanf("%d", &n); printf("Enter %d integers\n", n); for ( c = 0 ; c < n ; c++ ) scanf("%d", &array[c]); for ( c = 0 ; c < ( n - 1 ) ; c++ ) { position = c; for ( d = c + 1 ; d < n ; d++ ) { if ( array[position] > array[d] ) position = d; } if ( position != c ) { swap = array[c]; array[c] = array[position]; array[position] = swap; } } printf("Sorted list in ascending order:\n");

Page 13: C basic questions&ansrs by shiva kumar kella

Name :shiva kumar kella Email : [email protected] Page 13

for ( c = 0 ; c < n ; c++ ) printf("%d\n", array[c]); return 0; }

10) write a program to convert a given binary number to decimal number?

Ans:

#include <stdio.h> #include <math.h> int binary_decimal(int n); int decimal_binary(int n); int main() { int n; char c; printf("Instructions:\n"); printf("1. Enter alphabet 'd' to convert binary to decimal.\n"); printf("2. Enter alphabet 'b' to convert decimal to binary.\n"); scanf("%c",&c); if (c =='d' || c == 'D') { printf("Enter a binary number: "); scanf("%d", &n); printf("%d in binary = %d in decimal", n, binary_decimal(n)); } if (c =='b' || c == 'B') { printf("Enter a decimal number: "); scanf("%d", &n); printf("%d in decimal = %d in binary", n, decimal_binary(n)); } return 0;

Page 14: C basic questions&ansrs by shiva kumar kella

Name :shiva kumar kella Email : [email protected] Page 14

} int decimal_binary(int n) /* Function to convert decimal to binary.*/ { int rem, i=1, binary=0; while (n!=0) { rem=n%2; n/=2; binary+=rem*i; i*=10; } return binary; } int binary_decimal(int n) /* Function to convert binary to decimal.*/ { int decimal=0, i=0, rem; while (n!=0) { rem = n%10; n/=10; decimal += rem*pow(2,i); ++i; } return decimal; }

Page 15: C basic questions&ansrs by shiva kumar kella

Name :shiva kumar kella Email : [email protected] Page 15

11)write a program to convert a given decimal number to binary number?

Ans:

#include <stdio.h> int main() { int n, c, k; printf("Enter an integer in decimal number system\n"); scanf("%d", &n); printf("%d in binary number system is:\n", n); for (c = 31; c >= 0; c--) { k = n >> c; if (k & 1) printf("1"); else printf("0"); } printf("\n"); return 0; }

Page 16: C basic questions&ansrs by shiva kumar kella

Name :shiva kumar kella Email : [email protected] Page 16


Recommended