Assignment of Programming Language C
Question
No-1. Define operators. Briefly explain about any four Operators in c.
Answer-
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.
Operators are used in programs
to manipulate data and variables.
C operator can be classified
into a number of categories. They include:
1. Arithmetic operators
2. Unary operator
3. Relational operators
4. Conditional operator
6. bitwise operators
7. Increment and Decrement
operators.
Arithmetic Operators:-
The basic operators for
performing arithmetic are the same in many computer languages:
+ addition
- subtraction
* multiplication
/ division
% modulus(remainder).
An additional arithmetic
operation you might be wondering about is exponentiation. The operator ca be
used in two ways to subtract two numbers ( as in a-b ) or to negate on number (
as in –a+b)
Relational Operator:-
The relational operator such as
<, <=,>,and >=are in fact operators, just
like +,-,*,and /.The relational
operators take two values, look at them, and "return" a value of 1 to
0
depending on whether the tested
restion was true or false.
The complete set of relational operators in c
< less then
<= less than or equal
> greater than
>= greater than or equal
== equal
!= not equal
Fro example 1< 2 is true (1), 3>4 is false (0)
Logical Operators:-
Logical operators which take true / false values as operands and
cmpute new true/false values. The three logical operators
&& AND
|| OR
! NOT
For example to tes whether the variable I lies between 1 and 10, your
might use
If (1< I && < 10)
Here we are expressing the relation
“ I is between 1 and10” as “ 1 is less than I and I is less than 10”
Conditional operator:-
The Conditional operator
(ternary operator) pair "?:" is available in c to construct
conditional of the form.
Expr1?expr2:expr3
Where expr1, expr2, expr3 are expressions
The operators ? : works as follows: exp1 is evaluated firs, if it is
nonzero (true). Then the expression expr2 is evaluated and becomes the value of
the expression. If expr1 is false expr3 is evalated and its value become the
value of the expression.
For example consider the following statements
a=100;
b=200;
c=(a>b)?a:b;
c will be assigned the vlaue of b. the can be achieved using the
if..else statements as follows:
if (a>b)
c=a;
else
c+b;
Bitwise Operators:-
The bitwise operator &,|,^,and ~ operate on integers thought of as
binary numbers or strings of bits.
The & operator is bitwise AND,the
| operator is bitiwise OR, the ^ operator is bitwise exclusive-OR (XOR),
and the ~ operator is a bitwise
negation or complement. ( & ,|, and ^ are “binary” in that they take tow
operands; ~ is unary ) these operators let you work with the individual bits of
a variable; one common use is to treat an integer as a set of single bt flags.
You might difine the 3rd bit as the “verbose” flag bit by defining
# define VERBOSE 4
Then you can “turn the verbose bit on” in an integer variable
.
Question No.2. Differentiate between formal parameters
and actual parameters with example.
Formal
Parameter
1) Consider y = f(x) // where x is independent & value of y is
dependent on value returned by f.Here f(x) is formal parameter or argument to f
2) Formal parameters are place holders for values of "Actual
parameter"
3) Formal parameters are declared/ defined during function definition.
information can be passed to the function .
A function has a body containing
the actual instructions (statements) for carrying out the task the
function is supposed to perform; and it
may give you back a return value, of a particular type.
In general terms, the first
line can be written as
data-type name (data-type
parameter 1, data-type parameter 2,...., data-type parameter n).
For example
Int multbyfonr (int x)
{
int retval;
retval= x*4;
return retval;
}
Actual parameter
1) In C prg. values that appears inside the parentheses are called
"Actual Parameter"
2) Formal parameters must match in position, number & type
3) When a function call is made, a memory location is allocated for
each formal parameter & a copy of corresponding actual parameter is kept in
that memory location. All calculations involving the formal parameter use this
memory location.
4) Actual parameters are always "Passed" & variables
that are declared & passed as arguments to procedure by caller
#incude<stdio.h>
Extern int multbyfour(int);
Int main()
{
Int I, j;
I =5;
J =multbyfour(i);
Printf(“%d\n”,j);
Return 0;
}
Example:- Let's
look at calculate bill again:
#include <stdio.h>
int main (void);
int calculate_bill (int, int, int);
int main()
{
int bill;
int fred = 25;
int frank = 32;
int franny = 27;
bill = calculate_bill (fred,
frank, franny);
printf("The total bill
comes to $%d.00.\n", bill);
exit (0);
}
int calculate_bill (int diner1, int diner2, int diner3)
{
int total;
total = diner1 + diner2 +
diner3;
return total;
}
In the function main in the example above, fred, frank, and franny are
all actual parameters when used to call calculate bill. On the other hand, the
corresponding variables in calculate_bill (namely diner1, diner2 and diner3,
respectively) are all formal parameters because they appear in a function
definition.
Q.NO.3.
Describe about static and external variables.
ANS-
Static Variables:-
A static
variable can be either a
global or local variable. Both are created by preceding the variable
declaration with the keyword static.A local static variable is a variable that can maintain its value from one function call to another and it will exist until the program ends.
When a local static variable is created, it should be assigned an initial value. If it's not, the value will default to 0.
Static variables are defined within individual function 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 is exited and
then reentered later, the static variable c 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 utilized within the function in the same manner as other variables. They
cannot be accessed outside of their defining function.
In order to declare a static variable the keyword static is used as
shown below:
static int
count;
Initial values can be included in included in static variable
declaration. the rules associated with the initialization remain same as the unitization
of automatic or global variables.
External variables:-
A external
variable is a variable that
is defined outside all functions and available to all functions.These variables are unaffected by scopes and are always available, which means that external variable exists until the program ends.
It is possible to create a global variable in one file and access it from another file. In order to do this, the variable must be declared in both files, but the keyword extern must precede the "second" declaration.
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.
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
(i.e.in some other source file or files), the variable is declared in those
other file(s) with an external declaration, which is not a defining instance.
To make a variable as
an external declaration, which is defined somewhere else; you precede it with
the keyword extern:
extern int j;
Question No 4:- Distinguish between pass by value and pass by reference with the help of an example ?
Call by Value
If
data is passed by value, the data is copied from the variable used in for
example main() to a variable used by the function. So if the data passed (that
is stored in the function variable) is modified inside the function, the value
is only changed in the variable used inside the function. Let’s take a look at
a call by value example:
#include <stdio.h>
void call_by_value(int
x) {
printf("Inside call_by_value x = %d before adding
10.\n", x);
x += 10;
printf("Inside call_by_value x = %d after adding 10.\n",
x);
}
int main() {
int a=10;
printf("a = %d before function
call_by_value.\n", a);
call_by_value(a);
printf("a = %d after function
call_by_value.\n", a);
return 0;
}
The
output of this call by value code example will look like this:
a = 10 before function
call_by_value.
Inside call_by_value x =
10 before adding 10.
Inside call_by_value x =
20 after adding 10.
a = 10 after function
call_by_value.
Ok,
let’s take a look at what is happening in this call-by-value source code
example. In the main() we create a integer that has the value of 10. We print
some information at every stage, beginning by printing our variable a. Then
function call_by_value is called and we input the variable a. This variable (a)
is then copied to the function variable x. In the function we add 10 to x (and
also call some print statements). Then when the next statement is called in
main() the value of variable a is printed. We can see that the value of
variable a isn’t changed by the call of the function call_by_value().
Call by Reference
If
data is passed by reference, a pointer to the data is copied instead of the
actual variable as is done in a call by value. Because a pointer is copied, if
the value at that pointers address is changed in the function, the value is
also changed in main(). Let’s take a look at a code example:
#include <stdio.h>
void
call_by_reference(int *y) {
printf("Inside call_by_reference y = %d before
adding 10.\n", *y);
(*y) += 10;
printf("Inside call_by_reference y = %d after
adding 10.\n", *y);
}
int main() {
int b=10;
printf("b = %d before function
call_by_reference.\n", b);
call_by_reference(&b);
printf("b = %d after function
call_by_reference.\n", b);
return 0;
}
The
output of this call by reference source code example will look like this:
b = 10 before function call_by_reference.
Inside call_by_reference y = 10 before adding 10.
Inside call_by_reference y = 20 after adding 10.
b = 20 after function call_by_reference.
Question No.5.
Define macro. How can we declare a macro statement? Explain with an example.
ANSWER-
Definition of macro
defines a macro statement with the given name having its value the given
replacement text. After that (for rest
of the current source file where ever the preprocessor sees that name it will
replace it with the replacement text.
A pre
processor line of the form
#define name text
2. macro behave differently from normal
variables(or functions, it is customary to give
them names which all capital letter?(or at lest which begin with capital
letter);
The most common use for macros is to propagate
various constants around and to make them self -documenting
char line[100];
........
getline[line ,100];
but this statement is neither readable nor
reliable;
#define MAXLINE 100;
char line []MAXLINE];
............
getline(line,MAXLINE);
MACROS WITH ARGUEMENTS:
The preprocessor permits us to define more
complex and more useful form of replacement./IT takes the form
#define identifier(f1,f2.......fn)string
in this there is no space between the macro identifier and left parenthesis. the identifier f1 and f2...fn
are the formal macro arguments that are analogous to the formal arguments in
a functions definitions .
there
is a basic difference between the simple replacement discussed above and the
replacement of macros with arguments ;subsequent ' occurrence of a macro with
arguments is known as macro call;
A simple example of a macro with arguments is
#define CUBE(X) (X*X*X)
if THE FOLLOWING STATEMENTS APPPEARS LATER IN
THE PROGRAM
volume =CUBE(side);
then the preprocessor would expand this
statement to;
volume=side*side*side
consider the following;
volume=cube (a+b);
NESTING OF MACROS;
WE can also use one macros in the definition
of another macro. that is macro definitions may be nested. for instance
consider the following macro definitions.
#define M
5
#define N
M+1
#define SQUARE(X) ((X)*(X))
#define CUBE(X) (SQUARE(X)*(X))
#define SIXTH(X) (CUBE(X)*CUBE(X))
Question No.6. WHAT is the use of fopen() and fclose() functions ?LIST and explain different modes for opening a file ;
Opening a file (
fopen()
ANS- Whenever you want
to read from or write to one of the files you are working with ,you identify
that file by using its file pointer (that is the file pointer you obtained when
you opened the file );
it is possible to have
several files open ,as open as you distinct variables to store the file
pointer;
declare a variable to
store a file pointer like this ;
FILE *fp;
the type of file is predefined
for you by<'stdio.h>.it data structure which holds the information the
standard I/O library needs to keep the track of the file for you
the name of the
variable can (as for any variables )be anything we choose;
it is the traditional
to use the letters fp in the variables name (?since we're talking about a file
pointer;
If you re reading from
two files at once you had probably use two file pointers
FILE *fp1*fp2;
IF you are reading
from one file and writing to another you might declare an input file pointer
and an output file pointer;
FILE *ifp,*ofp;
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 the writing mode
so we could open output.dat for output like this ;
ofp=fopen("output.dat","w");
the most important
thing to take care of when you are opening a file is that its an operation which may fail; the requested file not exist ,or it
might be not existing, 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
to check for this case
bofore going off and using fopen return value
a file pointer
ifp=fopen("input.dat","r");
ifp(ifp==null)
{
printf("cant open file\n");
exit or return
}
CLOSING
FILES ( fclose() )
Although you can open
a multiple files there's a limit to how many you can have to open at once ;if
your program will open many files in succession ;you want to close each one as
you re done with it; otherwise the standard i/o could run out of the resources .
it uses to keep track of open files. Closing a file simply involves calling
fclose with the file pinter as its arguments;
SYNTAX;
fclose(fp)
calling fclose
arranges that (if the file was open for output )any last ,buffered output is
finally written to the file and that those resources used by the operating
system (and the c library) for this file are this file are released. If you
forget to close a file, it will be closed automatically the program exit
No comments:
Post a Comment