1) What is the use of a ‘\0’
character?
Ans:-It is
referred to as a terminating null character, and is used primarily to show the
end of a string value.
2) Write a C program to print hello
world without semi colon?
Ans:-#include<stdio.h>
void
main(){
if(printf("Hello world")){
}}
3) Write a c program to check given
number is perfect number or not.
Ans:-#include<stdio.h>
int
main(){
int n,i=1,sum=0;
printf("Enter a number: ");
scanf("%d",&n);
while(i<n){
if(n%i==0)
sum=sum+i;
i++;
}
if(sum==n)
printf("%d is a perfect
number",i);
else
printf("%d is not a perfect
number",i);
return 0;}
4) Write a C program to print
Fibonacci series using recursive function in C.
Ans:-#include<stdio.h>
int Fibonacci(int);
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)
);}
5) How to call C functions in a
program?
Ans:-(i)Call by
value
(ii)Call
by reference
6) What are the uses of C function?
Ans:-(i)C
functions are used to avoid rewriting same logic/code again and again in a
program.
(ii)There
is no limit in calling C functions to make use of same functionality wherever
required.
(iii)We
can call functions any number of times in a program and from any place in a
program.
(iv)A large
C program can easily be tracked when it is divided into functions.
(v)The
core concept of C functions are, re-usability, dividing a big task into small
pieces to achieve the functionality and to improve understandability of very
large C programs.
7)What is the difference between
Actual and Formal parameter?
Ans:-Actual
parameter – This is the argument which is used in function call.
Formal
parameter – This is the argument which is used in function definition
8)Write a C program to find the
greatest of 3 numbers?
Ans:-include<stdio.h>
void
main() {
int a, b, c;
printf("\nEnter value of a, b & c :
");
scanf("%d %d %d", &a, &b,
&c);
if ((a > b) && (a > c))
printf("\na is greatest");
if ((b > c) && (b > a))
printf("\nb is greatest");
if ((c > a) && (c > b))
printf("\nc is greatest");
}
9)Write a C program to compute the sum
of the array elements using pointers?
Ans:-#include<stdio.h>
#include<conio.h>
void
main() {
int numArray[10];
int i, sum = 0;
int *ptr;
printf("\nEnter 10 elements : ");
for (i = 0; i < 10; i++)
scanf("%d", &numArray[i]);
ptr = numArray; /* a=&a[0] */
for (i = 0; i < 10; i++) {
sum = sum + *ptr;
ptr++;
}
printf("The sum of array elements :
%d", sum);}
10) C Language has been developed in
which language?
Ans:-C language
has been developed using assembly level language.
11) What Is The Difference Between
Assembler, Compiler And Interpreter?
Ans:-(i)Assembler
is a program that converts assembly level language (low level language) into
machine level language.
(ii)Compiler
compiles entire C source code into machine code. Whereas, interpreters converts
source code into intermediate code and then this intermediate code is executed
line by line.
12)What is Constant in C?
Ans:-Constants
refer to fixed values. They are also called as literals.
C
Constants are also like normal variables. But, only difference is, constant
values can’t be modified by the program once they are defined. Constants may be
belonging to any of the data type.
13) What Is The Difference Between
Strcpy() & Strncpy() Functions In C?
Ans:-strcpy( )
function copies whole content of one string into another string. Whereas,
strncpy( ) function copies portion of contents of one string into another
string.
If
destination string length is less than source string, entire/specified source
string value won’t be copied into destination string in both cases.
14) What is const pointer in C?
Ans:-Const
pointer is a pointer that can’t change the address of the variable that is
pointing to.
Once const
pointer is made to point one variable, we can’t change this pointer to point to
any other variable. This pointer is called const pointer.
15) What Is Dangling Pointer In C?
Ans:-When a
pointer is pointing to non-existing memory location is called dangling pointer.
16) What is the difference between
malloc() and calloc()?
Ans:-(i)The name malloc stands for "memory allocation".The function malloc() reserves a block
of memory of specified size and return a pointer of type void which can be
casted into pointer of any form.
(ii) The name calloc stands for
"contiguous allocation".The only difference between malloc() and
calloc() is that, malloc() allocates single block of memory whereas calloc()
allocates multiple blocks of memory each of same size and sets all bytes to
zero.
17) Write a C program to find sum of n
elements entered by user. To perform this program, allocate memory dynamically
using malloc() function.
Ans:-#include
<stdio.h>
#include
<stdlib.h>
void main(){
int num, i, *ptr, sum = 0;
printf("Enter number of elements:
");
scanf("%d", &num);
ptr = (int*) malloc(num *
sizeof(int)); //memory allocated using
malloc
if(ptr == NULL)
{
printf("Error! memory not
allocated.");
exit(0); }
printf("Enter elements of array:
");
for(i = 0; i < num; ++i) {
scanf("%d", ptr + i);
sum += *(ptr + i); }
printf("Sum = %d", sum);
free(ptr);
}
18) How do you generate random numbers in C?
Ans:-Random
numbers are generated in C using the rand() command. For example: anyNum = rand
() will generate any integer number beginning from 0, assuming that anyNum is a
variable of type integer.
19) What are multidimensional arrays?
Ans:-Multidimensional
arrays are capable of storing data in a two or more dimensional structure. For
example, you can use a 2 dimensional array to store the current position of
pieces in a chess game, or position of players in a tic-tac-toe program.
20) What is Pointer on pointer?
Ans:-It’s a
pointer variable which can hold the address of another pointer variable. It
de-refers twice to point to the data held by the designated pointer variable.