Sie sind auf Seite 1von 5

Name priyanka kumari

Subject code - MCA 1020(Programming in C)


Roll no.-1408013126

1. Define operators. Briefly explain about any four Operators in C.


Ans:- C supports a rich set of operators. An operator is a symbol that tells the
computer to perform certain mathematical or logical manipulations. Operators
are used in programs to manipulate data and variables . They usually form a part
of the mathematical or logical expressions .
C operator can be classified into a number of categories. They include:

Arthemetic operator
Unary operator
Relational operators
Logical operators
Conditional operator

Arithmetic Opertors
The basic operators for performing arithmetic are the same in many
computer languages:
+ addition
- Subtraction
*multiplication
/ division
% modulus(remainder)
Unary operator
A unary operator acts upon a single operand to produce a new value.
Relational and logical operators

An if statement like
If(x > max)
Max = x;
Is perhaps deceptivery simple. Conceptually ,we say that it checks whether
the condition x > max is true or false.
Conditional Operator
The Conditional operator (ternary operator) pair ? . is available in C to
construct conditional expressions of the form
Expr1?expr2?:expr3?
Where expr1,expr2, and expr3 are expressions.
2 . Differentiate between while and do-while statements.
Ans:- While loop :loops generally consist of two parts: one or more control expressions which
control the execution of the loop,and the body, which is the statement or set of
statements which is executed over and over.
The most basic loop in C is while loop. A while loop has one control
expression,and executes as long as that expression,and executes as long as that
expression is true. Here before executing the body of the loop,the condition is
tested. Therefore it is called an entry-controlled loop.
Do-while loop:The do.while loop is used in a situation where we need to execute the body of
the loop before the test is performed. Therefore,the body of the loop may not be
executed at all if the condition is not satisfied at the very first attempt. Where as
while loop makes a test of condition before the body of the loop is executed.
3. Describe about static and external variables.
Ans:- Static Variables
Static variables are defined within individual functions and therefore have
the same scope as automatic variables,i.e. they are local to the function in

which they are declared. Unlike automatic variables,however ,static


variables retain their values throughout the life of the program. As a result
,if a function is exited and then reentered later, the static variable defined
within that function will retain their previous values. This feture allows
functions to retain information permanently throughout the execution of a
program.
External variables:It is possible to split a function up into several source files,for easier
maintenance. When several source files are combined into one program
the compiler must have a way of correlating the variables which might be
used to communicate between the several source files. Furthermore, if a
variable is going to be useful for communication,there must be exactly one
of it; you wouldnt want one function in one source file to store a value in
one variable named externvar, and then have another function in another
source file read from a different variable named extrnvar.

4. Define structure. What is the syntax for defining a structure?


Write a simple program using structure.
Ans:- C supports a constructed data type known as structure ,which is a
method for packing data of different types. A structure is a convenient tool
for handling a group of logically related data items.Structures help to
organize complex data in a more meaningful way. A structure definition
creates a format that may be used to declare structure variables.
Void main()
{
Struct st_record
{
Char name [20];
Int weight;
Float height;
};

Static struct st_record student1 = {Suresh, 60,180,75};


Static struct st_record student2 = {Umesh, 53,170,60};
}
5 . Define macro. How we can declare a macro statement?
Explain with an example.
Ans:- A preprocessor line of the form
#define name text
Defines a macro with the given name , having as its value the given
replacement text. After that (for the rest of the current source
file),wherever the preprocessor sees that name,it will replace it with
the replacement text. The name follows the same rules as ordinary
identifiers .since macros behave quite differently from normal
variables,it is customary to give them names which are all capital
letters . The replacement text can be absolutely anything its not
restricted to numbers ,or simple strings,or anything.The most
common use for macros is to propagate various constants around
and to make them more self-documenting .Weve been saying thing
like
char line[100];
..
Getline (line, 100);
But trhis is neither readable nor reliable ;its not necessarily obvious
what all those 100s scattered around the program are ,and if we ever
deside that 100 is to small for the size of the array to hold lines,well
have to remember to change the number in two (or more)places. A
much better solution is to use a macro:
#define MAXLINE 100
Char line[MAXLINE];
..
Getline(line, MAXLINE);
6 . What is the use of fopen () and fclose () function? List and
explain different modes for opening a file.
Ans:-

fopen() :-

One thing to beware of when opening files is that its an operation which
may fail.The requested file might not exist, or it might be protected against
reading or writing.fopen returns a null pointer if it cant open the requested
file,and its important to check for this case before going off and using
fopens return value as a file pointer.Every call to fopen will typically be
followed with a test, like this:
Ifp = fopen(input.dat,r);
If(ifp == NULL)
{
Printf(cant open file\n);
Exit or return
}
Fclose() :Although you can open multiple files,theres a limit to how many you can
have open at once. If your program will open many files in succession,youll
want to close each one as youre done with it; otherwise the standard I/O
library could run out of the resources it uses to keep track of open files.
Closing a file simply involves calling fclose with the file pointer as its
argument:
Fclose()
Calling fclose arranges that any last ,buffered output is finally written to the
file, and those resources used by the operating system for this file are
released.

Das könnte Ihnen auch gefallen