Sie sind auf Seite 1von 13

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

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 operators can be classified into a number of categories. This includes:
1.
2.
3.
4.
5.
6.
7.

Arithmetic Operators
Unary Operators
Relational Operators
Logical Operators
Conditional Operators
Bitwise Operators
Increment and Decrement Operators.

1. Unary Operators:
Unary Operator acts upon a single operand to produce a new value.

The most well known unary operator is minus, where a minus sign precedes
a constant, variable or expressions in C. All numeric constants are positive.
Therefore, a negative number is actually a positive constant preceded by a
unary minus.
For example: -3
2. Conditional Operators:
The Conditional Operator (ternary operator) pair ?: is available in C to
construct conditional expressions of the form
expr1?expr2:expr3
Where expr1, expr2, expr3 are expressions.
The operator ? : works as follows: expr1 is evaluated first. If it is nonzero
(true), then the expr2 is evaluated and becomes the value of the expression.
If expr1 is false, expr3 is evaluated and its value becomes the value of the
expression.
For example: a=100, b=200, c=(a>b)?a:b;
In the above example c will be assigned the value of b.
3. Arithmetic Operators:
The basic operators for performing arithmetic are the same in many
computer languages:

+
*
/
%

Addition
Subtraction
Multiplication
Division
Modulus (Remainder)

The operator can be used in two ways: to subtract two numbers or to


negate one number.
When applied to integer, the division operator / discards any remainder, so
1/2 is 0 and 7/4 is 1.
The modulus operator % gives the remainder when two integers are divided.
Multiplication, division, and modulus all have higher precedence than addition
and subtraction.
4. Relational and Logical Operators:
The relational operators take two values, look at them and return a value of
1 or 0 depending on whether the tested relation was true or false. The
complete set of relational operators in C is:
<
<=
>
>=
==
!=

less than
less than or equal
greater than
greater than or equal
equal
not equal

The relational operators work with arbitrary numbers and generate true/false
values.
Logical Operators can combine true/false values by using the Boolean
operators which takes true/false values as operands and compute new
true/false values. The Logical operators are also called as Boolean Operators.
The 3 types of logical operators are:
&&
||
!

AND
OR
NOT

The && (AND) operator takes two true/false values and produces a true (1)
result if both operands are true. The || (OR) operator takes two true / false
values and produces a true (1) result if either operand is true. The ! (NOT)
operator takes the single true/false value and negates it, turning false to true
and true to false. The logical operators && and || are used when we want to
test more than one condition and make decision.

2. Differentiate between while and do-while statements.


While Statement:
A while loop has one control expression and executes as long as that
expression is true. Here before executing that body of the loop, the condition
is tested. Therefore it is called entry-controlled loop.
The general syntax of a while loop is:
while( expression )
statement(s)
A while loop starts out like an if statement: if the condition expressed by
the expression is true, the statement is executed. However after executing
the statement, the condition is tested again, and if it is still true, the
statement is executed again. As long as condition remains true, the body of
the loop is executed over and over again.
You use a while loop when you have a statement or group of statements
which may have to be executed a number of times to complete their task.
In while loop if the condition is false right at the start, the body of the loop is
not executed at all.
Example: Program to find largest of n numbers using while loop.
main()
{
int num, large, n, i;
clrscr();
printf(Enter the total number of numbers \n);
scanf(%d, &n);
large=0;
i=0;
while(i<n)
{
printf(enter number: );
scanf(%d\n , &num);
if(large<num)
large = num;
i++
}
printf(large: %d, large);
}

do-while statement:
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
be executed at all is the condition is not satisfied at the very first attempt.
Whereas while loop makes a test of condition before the body of loop
executed.
For above reasons while loop is called an entry-controlled loop and do...while
loop is called an exit-controlled loop.
Syntax for do...while statement:
do
{
Body of the Loop
}
while (expression);
On reaching the do statement, the program proceeds to evaluate the body of
the loop first. At the end of the loop, the conditional expression in the while
statement is evaluated. If the expression is true, the program continues to
evaluate the body of the loop once again. This process continues as long as
the expression is true. When the expression becomes false, the loop will be
terminated and the control goes to the statement that appears immediately
after the while statement.
On using do loop, the body of the loop is always executed at least once
irrespective of the expression.
Example for do...while loop:
void main()
{
int i=0, x=0;
do
{
if(i%5==0)
{
x++;
printf(%d\t, x);
}
i++;
}while (i<20);
printf(\n x : , x);
}

3. Describe about static and external variables.


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 functions 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 exited and then re-entered later the static variables defined within
that function will retain their previous values. This feature allows functions to
retain information permanently throughout the execution of a program.
Static variables can be utilized within the function in the same manner as
other variables. They cannot be accessed outside of their defining function.
In order to declare static variables the keyword static is used as shown
below:
static int count;
We can define automatic or static variables having the same name as global
variables. In such situations the local variables will take precedence over the
global variables, though the values of global variables will be unaffected by
manipulation of the local variables.
Initial values can be included in static variable declarations. The rules
associated with the initialization remain same as the initialization of
automatic or global variables. They are:
1. The initial values must be constants, not expressions.
2. The initial values are assigned to their respective variables at the
beginning of the program execution. The variables retain these values
throughout the life of the program, unless different values are assigned
during the course of computation.
3. Zeros will be assigned to all static variables whose declarations do not
include explicit initial values.
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 externvar. Therefore a variable should have
exactly one defining instance, in one place in one source file. If the same

variable is to be used anywhere else, the variable is declared in those other


file(s) with an external declaration, which is not a defining instance. The
external declaration says the compiler that the variable will be used in this
source file but defined in some other source file. Thus the compiler doesnt
allocate space for that variable with the source file.
To make a variable as an external declaration, which is defined somewhere
else; you precede it with the keyword extern:
Extern int j;
Example for illustrate the concept of external functions:
Type and save the following program in a file externfunction.h
Void output(void)
{
printf(Hii Manipal!! \n);
return;
}
#include<stdio.h>
#include externfunction.h
extern void output(void);
main()
{
outout();
}
Compile and execute the above program.

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


Write simple program using structure.
Structure Definition:
C supports a constructed data type known as structure, which is a method of
packing data of different types. A structure is a convenient tool for handling a
group of logically related data items. Structures help to organize logically
related data items.
A Structure definition creates a format that may be used to declare structure
variables. For e.g. consider a book database consisting of book name, author,
number of pages and price.
Struct book_name
{
char title[20];
char author[15];
int pages;
int price;
};
Syntax for defining a structure:
A general format of a structure definition is as fallowsstruct tag_name
{
data_type member1;
data_type member2;
------};
The keyword struct declares a structure to hold the details of fields. These
fields are called elements or members. Each member may belong to a
different type of data. tag_name is the name of the structure and is called
the structure tag.
The above declaration has not declared any variables. It simply describes a
format called template to represent information.
We can declare structure variables using the tag name anywhere in the
program. Example the statement:
struct book_name book1, book2, book3;
declares book1, book2, book3 as variables of type book_name;

Simple Program using Structure: To print the Date of Joining of a person.

#include<conio.h>
#include<stdio.h>
struct personal
{
char name[30];
int day;
int month;
int year;
float salary;
};

void main()
{
struct personal p;
printf(Enter the Name: \n);
gets(p.name);
printf(Enter the day of Joining: \n);
scanf(%d, &p.day);
printf(Enter the month of Joining: \n);
scanf(%d, &p.month);
printf(Enter the year of Joining: \n);
scanf(%d, &p.year);
printf(Enter the day Salary: \n);
scanf(%f, &p.salary);
printf(\n Name:, p.name);
printf(\n Date of Joining :%d-%d-%d, p.day, p.month, p.year);
printf(\n Salary:, p.salary);
getch();

5. Define Macro. How we declare a macro statement. Explain with


example.
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 wherever the pre-processor sees that name, it
will replace it with the replacement text. The name fallows the same users 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, we have been saying things like
char line[100];
---getline(line, 100);
But this is neither readable nor reliable; it is not necessarily obvious what all
those 100s scattered around the program are, and if we ever decide that 100
is too small for the size of the array to be hold lines, well have to remember
to change the number in two places. A much better solution is to use a
macro:
#define MAXLINE 100;
char line[MAXLINE];
----getline(line, MAXLINE);
Now if ever we want to change the size, we only have to do it in one place,
and it is more obvious what the words MAXLINE sprinkled through the
program mean that the magic number 100 did.
Since the replacement text of a pre-processor macro can be anything, it can
also be an expression, although you have to realize that, as always, the text
is substituted later. No evaluation is performed when the macro is defined.
For example, suppose that you write something like:
#define A 2
#define B 3
#define C A+B
Then later, suppose that you write:
int x = C*2;

If A, B and C were ordinary variables youd expect x to end up with the


value 10. But lets see what happens:
The pre-processor always substitutes text for macros exactly as you have
written it. So it first substitutes the replacement text for the macro C,
resulting in
int x = A + B * 2;
then it substitutes the macros A and B, resulting in
int x = 2 + 3 * 2;
Only when the pre-processor is done doing all this substituting does the
compiler get into the act. But when it evaluates that expression, it ends up
initializing x with the value 8!
To guard against this sort of problem, it is always good idea to include
explicit parentheses in the definitions of macros which contain expressions.
If we were define macro C as:
#define C (A + B)
Then the declaration of x would ultimately expand to
int x = (2 + 3) * 2;
And x would be initialized to 10, as we probably expected.
Notice that there does not have to be a semicolon at the end of the #define
line, if you accidentally type
#define MAXLINE 100;

/* WRONG */

Then you have later declare


char line[MAXLINE];
the pre-processor will expand it to
char line[100;];

/* WRONG */

Which is syntax error. This is what we mean when we say that the preprocessor does not know much of anything about the syntax of C-- in this
last example, the value or the replacement text for the mmacro MAXLINE
was the 4 characters 1 0 0 ; , and that is exactly what the pre-processor
substituted.

6. What is the use of fopen() and fclose() function. List and explain
different modes of opening a file.
fopen() : To open a file for either read or writing purpose fopen() function is
used in C.
To actually open a file and receive the token which you shall store in your file
pointer variable, you can call fopen. Fopen accepts a file name (as s string)
and a mode value indicating among other things whether you intend to read
or write this file. The mode variable is also string. To open the file input.dat
for reading you might call
ifp=fopen(input.dat, r);
the mode string r indicates reading. Mode w indicates writing, so we
could open output.dat for output like this
ifp=fopen(output.dat, w);
The other values for mode string are less frequently used. The third major
mode is a for append. You may also add + character to the mode string
indicates that you want both read and write, or a b character to indicate
that you want to do binary I/O.
The fopen returns null pointer if it cant open the requested file, and it is
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(can not open file\n);
}

exit or return

If fopen returns a null pointer and you store it in your file pointer variable &
go off and try to do I/O with it, your program will typically crash.
fclose() : To close already opened file in C language fclose() function is used.
Closing a file simply involves calling fclose with the file pointer as its
argument.
fclose(fp);
Calling fclose arranges that any last, buffered output is finally written to the
file, and that those resources used by the operating system for this file are

released. If you forget to close the file, it will be close automatically when the
program exits.
The Different Modes of opening files:
1. read mode r : This mode is used to open a file for reading.
2. write mode w : This mode used to open a file for writing.
3. append mode a : This mode will be used to append new strings /
characters to existing data in file.
4. + mode : This mode string indicate that you want both read and write.
5. b mode : this mode indicates you want to do binary I/O.

Das könnte Ihnen auch gefallen