SHAREPOINT

SHAREPOINT
Spotlight on share server 2010

Tuesday, September 18, 2012

c programming



Q1.      Explain the importance of C language.
Ans.    1. C is portable means program written for one computer may run successfully on other computers.
2. C is fast means the executable program obtained after compiling and linking run very fast.
3. C is compact i.e. the statements in C-language are generally short but are very powerful; several operators can be combined together in just one statement.
4. The C language has both the simplicity of high-level language and low-level language.
Q2.      What is the structure of ‘c’ program?
Ans.    i) Any C program is the combination of functions. main() is one such function.
ii) The set of statements belonging to a function are enclosed within a pair of braces.
iii) Any variable used in the program must be declared before using it.
iv) Any c statement always ends with a ;
The structure of c program is as follow:
# Preprocessor directives
....................................
....................................
void main ()
{          
body of the main function;
........................;
........................;    }
Q3.      What are the relational operators available in C?
Ans.    The various relational operators available in ‘C’ are: -
Operator
Description
Greater then
Less then
>=
Greater then and equal to
<=
Less then and equal to
==
Equal to
!=
No Equal to
Q4.      What is the purpose of adding comments in a program? Can comments span more then a line.
Ans.    Comments are non-executable statements. With the help of comments we can give description about the program or any particular statement. Comments are enclosed with in /* and */. Any number of comments can be given at any place in the program. Comments can't not be nested.
A comment can be split over more than one line, as in,
/* this is
comment  */
Q5.      What is the syntax of PRINTF function of  'C'?
Ans.    printf() is function, which is used to print on the screen the value contained in a variable.
The general form of printf statement is ,
printf ("format string",list of variable);
format string could be ,
%f for printing real values
%d for printing integer values
%c for printing character values
Q6.      What is the syntax of nested if statement?
Ans.    It is perfectly alright if we write an entire if-else construct within either the body of the if statement or the body of an else statement. This is called of 'nesting' of if statement. The syntax is as follow:
if (condition)
            {
                        if (condition)
                                    do this;
                        else
                        {
                                    do this;
                                    and this;
                        }
            }
            else
                        do this;
Q7.      What is the syntax of switch statement?
Ans.    The control statement, which allows us to make a decision from the number of choices, is called switch. Or more correctly switch-case-default, since these three keywords go together to make up the control statement. They most often appear as follows:
switch (integer expression)
{
            case constant 1:
                        do this;
                                    case constant 2:
            do this;
            case constant 3:
                                    do this;
            default:
                        do this;
}
Q8.      What is the syntax of FOR statement?
Ans.    The FOR loop allows us to specify three things about a loop in a single line:
a)    Setting a loop counter to an initial value.
b)    Testing the loop counter to determine whether its value has reached the number of repetitions desired.
c)    Increasing the value of loop counter each time the program segment within the loop has been executed.
The general form of for statement is as under:

for (initial counter; test counter; increment counter)
{
                        do this;
            and this;
and this;
}
Q9.      What is format specifier?
Ans.    Format specifier is used to specify the data type of the variable. The format specifiers for various data types are shown below:
%d-------Integer data
%c------Character data
%f-------Floating point data
%ld-----Long Integer
Q10.    What is the scope of local variable?
Ans.    The scope of local variable is that, the variable is accessible with the block where it is declared. Local variable can't be accessed outside of the block, which contains its declaration.
Q11.    Which are the conditional operators available in C?
Ans.    The conditional operators ? and : are sometimes called ternary operators since they take three arguments. In fact, they form a kind of foreshortened if-then-else. Their general form is,
expression 1 ? expression 2 : expression 3
If expression 1 is true then the value returned will be expression 2 otherwise the value returned will be expression 3.
Q12.    What are the formatted output statements of C?
Ans.    I/O functions can be of two types  (i) formatted and (ii) unformatted functions. The basic difference between them is that the formatted functions allow the input read from the keyboard or the output displayed on the VDU to be formatted as per our requirements.  For example, if values of average marks and percentage marks are to be displayed on the screen, then details like where this output would appear on the screen, how many spaces would be present between the two values, the number of places after the decimal points etc. can be controlled using formatted functions. printf() and fprintf() are formatted output functions in C.
Q13.    What is pseudo code?
Ans.    Pseudo code is programming code written in general English language, so that no programmers can understand the code properly.
Q14.    Write a short note on type casting?
Ans.    Type casting refers to enforcement of explicitly converting the value of an expression to a particular data type. It is used to convert the data type from lower data type to higher data type and converse for temporary period. So that calculation can be performed with both the data types. e.g.
void  main()
            {           int a=5;float b;
printf(“value of a %d”,a);        
                        b=(float)a;
            printf(“value of b %d”,b);
}
Q15.    What do you mean by type conversion?
Ans.    In an expression there might be cases when you need to involve different types of variable in a single expression. For example, an expression may contain addition of two integers and the result is required to be multiplied by a float. The expressions that contain variables and constants are evaluated using the following methods, known as Type conversions. The data types are required to be converted for easy calculation of the expression. These conversions are implicitly done by the system.
Q16.    What are the derived data types used in C?
Ans.    The derived data types are
Arrays (array[10])
Functions (function())

Enumerated (enumerated_variable)
Pointers (*pointer)
Structures (structure.member)
Union (union_name.member)
Q17.    What do you mean by local and global variables?
Ans.    Local variables: Local variables are defined with in the pair of braces or between the body of the function. The scope of the variable is with in the function when function finished they will obsolete.
Global variables: These variables defined out side all the function. And can be used to any function with in the file because the scope of these variable are global to the file.
Q18.    Explain various programming techniques?
Ans.    Programming techniques are used to make the program simple, efficient, understandable and easy to maintain. The three programming techniques commonly used are:-
Top down design: - Using this design program is divided into the smaller blocks, which can be linked and called whenever needed. The programmers design the program in different levels and can be called any level at any time.
Bottom up design: - It is also designed in the same manner and divide the whole program into the smaller blocks, but when it is called it begins from the smaller block to the bigger till the program end.
Modular design: - A level consists of one or more modules. The first level is a complete main program and the module of the successive level contains sub modules Execution is controlled by the main program
Q19.    What is sizeof operator?
Ans.    The sizeof operator returns the size, in bytes, of given operand.
            Syntax is: -     
sizeof (exp)
Example: -     
 sizeof (float)
Result: -          
returns value 4
Q20.    Differentiate between expression and statement.
Ans.    An expression refers to variables, constants and operators. It represents a single data item, such as a numbers or characters. One or more operators may interconnect these variables or constants. In C, very complex expressions can be solved easily. An algebraic expression in C is represented as follows:
Algebraic expression: a x b – c x d
Its relative C expression will be written as: a * b – c* d.
Q21.    What is the use of #define directives statement?
Ans.    Define directives are used to define macro and the constants. This preprocessor starts with #(hash) symbol.
Q22.    What is a macro? How is it different from preprocessor?
Ans.    The macro is substitution string that is placed in a program. It is replaced by the definition when program is compiled. It is different from preprocessor as preprocessor links the files with the object code.
Q23.    What do you mean by ternary operator?
Ans.    Both if and ? : (Ternary operator) perform the same conditional operation, but ?: process fast as compare to the 'if' statement. The ? (ternary condition) operator is a more efficient form for expressing simple if statements. We can also nested the ternary operator. It has the following form:
  Condition? True: False
Q24.    Distinguish between unary and binary minus.
Ans.    Unary operators work on one operand and binary operators work on two operands. The unary operators negate the value of the operand
For example
            Int a=5;
a=-a;
            Then the value of a is now –5
            In case of binary minus it perform arithmetic subtraction on the given operands
            For example
                        Int a,b,c;
                        a=90;
            b=80;
                        c=a+b
Q25.    Differentiate between comparison and equality operator.
Ans.    A character or symbol indicating a relationship between two or more values or expressions called an operator. These operators include less than (<), less than or equal to (<=), greater than (>), greater than or equal to (>=), not equal (<>), and equal (==).
Q26.    Explain various types of errors with examples.
Ans.    Syntax errors: The errors, which occur due the incorrect way to write the code. It may be due the missing comma, semi colon etc.
As the line:
                        printf(hello);
will show the error because string is not enclosed in "  " .
Logical error it is not due to any syntax mistake, but due to incorrect logic. The compiler will not show the error due to this. The programmers himself have to find and correct it.
As:  for(I=10;I<5 span="span">
Here the logic is wrong as I is already 10 which is never less then 5.
Q27.    What is the syntax of while statement?
Ans.    initialization of looping variable;
            While (conditional expression)
            {
body of the loop
....................
....................
....................
Increment/decrement;
}
Q28.    What do you mean by conditional execution?
Ans.    When execution of set of commands is based upon some condition. This is known as conditional execution. Depending upon true or false path we can have different set of instructions to execute.
Q29.    What is difference between if and ? : (Ternary operator)?
Ans.    Both if and ?: operator perform the same conditional operation , but ?: process fast as compare to the 'if' statement.

No comments:

Post a Comment