Beruflich Dokumente
Kultur Dokumente
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)
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.
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);
}
#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();
/* WRONG */
/* 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.