C Language Question Answer
C Language Question Answer
C Language Question Answer
keyword in declaration statement, then compiler will take not volatile as default quantifier .There is not
any special keyword for not volatile. Not volatile means when any variable has qualified by volatile
keyword in declaration statement then value of variable cannot be changed by any external device or
hardware interrupt.
(vi) Explain significance of main ( ).
Answer: Function main() is the applications main routine where a program starts execution.It is the first
user-written function run when a program starts.Object-oriented C++ programs consist mostly of classes,
but theres always at least one C-like function: main(). main() is called more or less at the beginning of the
programs execution, and when main() ends, the runtime system shuts down the program. main() always
returns an int, as shown below:
int main() {
// Your Code goes here
}
main() has a special feature: Theres an implicit return 0; at the end. Thus if the flow of control simply falls
off the end of main(), the value 0 is implicitly returned to the operating system. Most operating systems
interpret a return value of 0 to mean program completed successfully.
main () is the only function that has an implicit return 0; at the end. All other routines that return an int
must have an explicit return statement that returns the appropriate int value.
Note that this example shows main() without any parameters. However, main() can optionally declare
parameters so that it can access the command line arguments, just as in C
(vii) Given statement int a-10,b=20,C; Determine
(i) a+=10;
(ii) b+3/2 a+=10 ;
Is an assignment statement, in which value of a will added with 10 and then the resulted value will be
assigned to a value of a is 10,
a += 10 means a=a+10; thus a=20 b+3/2
Value of b i.e. 20 plus 3/2 i.e. 1.5, thus the total value will be 21.5
Q-2(a) Explain different type of type conversions.
Answer:Type Conversion:
Type conversion means to convert a higher data type to lower data type. For the purpose of type
conversion we required to use the type case operator, which lets you manually promote or demote a
value. It is a unary operator which appears as the data type name followed by the operand inside a set of
parentheses.
There are two type of type conversion:
1.
2.
1.
1.
C permits mixing of constant and variables of different type in an expression. C automatically converts
any intermediate values to proper type so that the expression can be evaluated without losing any
significance
2.
3.
The final result of an expression is converted to type of the variable on the left of the assignment sign
before assigning the value to it. However, the following changes are introduced during the final
assignment.
1.
2.
3.
long int to int causes dropping of the excess higher order bits
The process of such a local conversion is known as explicit conversion or casting a value.
Where type-name is one of the standard C data types. The expression may be constant, variable
or an expression.
b= (double)sum/n
y= (int) (a+b)
z= (int)a+b
1.
2.
Simple if
3.
4.
Nested if
5.
Switch case
If..else
Ladder elseif
Q-2 (c) Write a program to print the following Patten for n=5.
Answer:#include<stdio.h>
#include<conio.h>
void main ()
{
int n,i,j,k,a; clrscr();
printf(ENTER THE ANY VALUE IS n ); scanf(%d,&n);
a=n;
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf( );
}
for(k=n;k>=i;k--)
{
printf(%d,k);
}
printf(\n); a--;
}
getch();
}
INPUT:
ENTER THE ANY VALUE IS n 5
Q-2 (c) Write a program to find the value of sum for given series up to X. Sum=1+2/2! +3/3! +
+X/X!
Answer:#include<stdio.h>
#include<conio.h>
void main ()
{
int n,i,j;
float Sum=0,a=1,k,c; clrscr();
{
char alphabet; printf(enter an alphabet); putchar (\n); alphabet=getchar
();
if (is lower (alphabet))
{
Put char (topper (alphabet));
}
else
{
putchar (to lower (alphabet));
}
}
getch ();
}
printf()
We have seen the use of printf function for printing caption and numerical result. It is highly desirable
that output are produced in such a way that they understandable and are an in an easy-to use form. It is
therefore necessary for the programmer to give careful consideration to the appearance and clarity of the
output produced by his program.
The printf statement provides certain features that can be effectively exploited to control the alignment
and spacing of print-outs on the terminals.
The general form of printf statement is:
Printf(control string,arg1,arg2,,argn);
Format specification that define the output format for display of each item.
The control string indicates how many arguments follow and what there types are. The
arguments arg1, arg2,,argn are the variables whose value are formatted and printed according to the
specifications of the control string. The arguments should match in number, order and type with the
format specifications.
A simple format specification has the following form:
%w.p type-spcifire
Where w is an integer number that specific the total number of columns for the output values and p is
onther integer number that specifies the number of digits to the right of the decimals point or the number
of character to be printed from a string. Both w and p are optional. Some examples of format printf
statements are:
printf(programming in c);
printf( );
printf( \n);
printf(%d,x);
printf(sum=%d,11234);
printf(\n\n);
printf never supplies a newline automatically and therefore multiply printf statements may be used to
build one line of output. A newline can be introduced by the help of a new line character \n.
Q-3 (b) Explain for loop in detail with proper examples.
Answer:The for loop is another entry-controlled loop that provides a more concise loop control structure the
general of the loop is. The general form of the loop is
The execution of the four statements is as follow:
1.
Initialization of the control variables is done first, using assignment statement such as i=1 and count=0.The variable I and
count are known as loop control variable.
The value of the control variable is tested using the test-condition is a relational expression, such as I <10 that determines when the
loop will exit. If the condition is true, the body of the loop is executed; otherwise the loop is terminated and the execution
continues with the statement that immediately follows the loop.
2 When the body of the loop is executed, the control is transferred block to the for statement after
evaluating the last statement in the loop. Now, the control variable is increment using an assignment
statement such as I =i+1 and the new value of the control variable is again tested to see whether it
satisfies the loop condition. If the condition is satisfied, the body of the loop is again executed. This
process
continues till the value of the control variable fails to satisfy the test-condition. Consider the following segment of a program:
for(x=0;x<=9;x++)
{
printf("%d",x);
}
This for loop is executed 10 times and prints the digit 0 to 9 in one line. The three section enclosed within
parentheses must be separated by semicolon at the end of the increment section, x=x+1.
The for statements allows for negative increment. For example, the loop discussed above can be written as
follow:
For (x=9 ; x<9 ; x--)
Printf(%d,x);
Printf(\n)
Will never be executed because the test condition fails at the very beginning itself.
Let us again consider the problem of sum of squares of integers discussed in section.
This problem can be coded using the for statement as follows:
for(x=0;x<=9;x++)
{
printf("%d",x);
}
The body of the loop
Sum=sum + n*n;
Is executed 10 times for n=1,2,3,..,10 each time incrementing the sum by the square of the value of n.
Q-4 (a) Explain any 2 string functions with example.
Answer:
strcpy ()
This library function is used to copy a string and can be used like this strcpy (destination, source).(It is not
possible in C to do this : string1=string2 ).
Take a look at the following example:
str_one=abc;
str_two=def; strcpy(str_one,str_two);
strcmp()
This library function is used to compare two strings andcan be usd like this:strcmp(str1,str2).
If the first string is greater than the second string a number greater than null is returned.
If the first string is less than the second string a number less than null is returned.
If the first and the second string are equal a null is returned. The look at an example:
printf(ENTER TH NAME :);
scanf(%s,name);
if (strcmp(name,india)==0)
printf(Hello,india!\n);
Q-4 (b) Explain need of array variable .what is the difference between character array and numeric
array.
Answer:
An array lets you declare and work with a collocation of values of the same type. Lets say you want to
declare four intergers.with the knowledge from the last few tutorials you would do something like this:
int a ,b ,c, d;
What if you wanted to declared a thousand variables? That will take you a long time to type. This is
where array are come in handy.
An easier way is to declare an array of integers.
int a [4];
The four separate integers inside this array are accessed by an index. Each element can be accessed by
using square brackets with the element number inside. All arrays start at element zero and will go to n-1.
(in this case from 0 to 3). So if we want to fill each element you get something like this :
int a [4];
a[0]=1;
a[1]=2;
a[2]=3;
a[3]=4;
if you want to use an element , for example for printing you can do this:
printf(%d,a[1]);
Q-4 (c) Write a program to do following on a given string.
INPUT: Patel Shiv Kumar
OUTPUT:
Patel
Shiv
Kumar
#include<stdio.h>
#includ<conio.h>
void main ()
{
char str[50]; int i;
puts (ENTER A STRING); gets (str);
for (i=0; i<=str[i]! =\0; i++)
{
if (str [i] == ) printf(\n);
else
puts (str[i]);
}
getch();
}
Q-5 (a) what is advantages of structure over array?
Answer:With an array you can store only one type of component .However; we cannot use an array if we want to
represent a collection of data items of different types using a single name.
While in a structure you can add as you prefer, and also include methods for them.
A structure contains a group of data types (ints, floats, chars, and even other structs).
If you want to store the details of 10 student names, markers of three subjects, total & percentage, one
would declare the following array variables.
In both these examples, the members can be accessed as dave.a, dave.b, and dave.c. An anonymous union
must be a number of, or nested within another anonymous union that is a member of, a named structure
or union. If a union is declared at file scope without a following union only declares the union tag tom:
Union tom
{
Int b; Float c;
};
The variables b and c from this union cannot be used at file scope, and the following statement will
generate errors:
b=5;
c=2.5;
Uses of bit fields:
The following are the uses of bit fields:
The storage space required to store the details in a structure is economically used by the bit fields.
The saving of storage space is considerably high when a large quantity of such data is stored.