0% found this document useful (0 votes)
4 views30 pages

C Operators and Expressions Explained

This document provides an overview of C operators and expressions, detailing various types such as unary, binary, and assignment operators. It explains how to use increment and decrement operators, arithmetic operations, relational operators, logical operators, and bitwise operators, along with examples. Additionally, it covers special operators like the ternary and comma operators, and the significance of the assignment operator in variable manipulation.

Uploaded by

Mamatha C
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views30 pages

C Operators and Expressions Explained

This document provides an overview of C operators and expressions, detailing various types such as unary, binary, and assignment operators. It explains how to use increment and decrement operators, arithmetic operations, relational operators, logical operators, and bitwise operators, along with examples. Additionally, it covers special operators like the ternary and comma operators, and the significance of the assignment operator in variable manipulation.

Uploaded by

Mamatha C
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Programming in C Govt First Grade College,Tumkur

CHAPTER - 2
C Operators and
Expressions

Expression:

The combination of operators and operands is known as expression.

Ex: A + B

In above example A and B are operands. + Symbol is an operator.


Operator:

An operator indicates an operation to be performed on the data.


Operand:

An operand is an entity on which operators perform the operations.

C – Operators
C language provides different types of operators. They are

Unary Operators
An operator that acts upon only one operand is known as unary operator. The unary operators are

Department of BCA Page 1


Programming in C Govt First Grade College,Tumkur

Unary minus:

Any positive operand can be associated with unary minus operand then its value gets
changed to negative. It is indicates with – operator along with operand.

Syntax: - (operand)

Ex: consider a=3 then b= -a

b = -3

in the above example initially the value of a is 3 but when this a has associated with unary minus
its value gets changed to negative.

Increment and Decement Operator:

Increment Operator:

This operatoris used to increment the value of an integer quantity by one.

This is represented by “++” (double plus) symbol. This symbol can be placed either before or

after an integer variable.

This increment is two types. They are pre increment and post increment.

Pre increment:
When the increment operator can be placed before the integer variable
that is known aspre increment operation.

Syntax: + + operand ; (or) + + intvar ;

 In this the first the value of operand or integer variable must be incremented.

 And then use or store that value.

In short cut this referred as “increment and use”.

Ex: a= ++b

This is equivalent to 2 assignment statements. They are

b = b + 1;

a = b;

ex: a= 6 then ++a is 7


Department of BCA Page 2
Programming in C Govt First Grade College,Tumkur

Post increment:

When the increment operator can be placed after the integer variable that is known as
post increment operation.

Syntax: operand + + ; (or) intvar + + ;

 In this the first the value of operand or integer variable must be stored or used.

 And then increment that value.

In short cut this referred as “use and increment”.

Ex: a= b + +

This is equivalent to 2 assignment statements. They are

a = b;

b = b + 1;

ex: a= 6 then a+ + is 6 only.

/* program to demonstrate increment operators */

void main()
{
int m = 6 , n = 8;
printf(“ m is %d\n”, + + m);
printf(“n is %d\n” , n + + );
}
Decrement Operator:

This operatoris used to reduce the value of an integer quantity by one.

This is represented by “- -” (double minus) symbol. This symbol can be placed either before or

after an integer variable.

This decrement is two types. They are pre decrement and post decrement.

Pre decrement:

When the decrement operator can be placed before the integer variable that is known as pre
decrement operation.

Department of BCA Page 3


Programming in C Govt First Grade College,Tumkur

Syntax: - - operand ; (or) - - intvar ;

 In this the first the value of operand or integer variable must be decremented.
 And then use or store that value.

In short cut this referred as “decrement and use”.

Ex: a= - - b

This is equivalent to 2 assignment statements. They are

b = b - 1;

a = b;
ex: a= 6 then - - a is 5
Post decrement:

When the decrement operator can be placed after the integer variable that is known as
post decrement operation.

Syntax: operand - - ; (or) intvar - - ;


 In this the first the value of operand or integer variable must be stored or used.

 And then decrement that value.

In short cut this referred as “use and decrement”.

Ex: a= b - -
This is equivalent to 2 assignment statements. They are

a = b;

b = b - 1;
ex: a= 6 then a - - is 6 only.
/* program to demonstrate decrement operators */
void main()
{
int m = 6 , n = 8;
printf(“ m is %d\n”, - - m);
printf(“n is %d\n” , n - - );
}

Department of BCA Page 4


Programming in C Govt First Grade College,Tumkur

Binary Operators
An operator that acts upon minimum two operands is known as binary operator. The binary
operators are

Assignment operators

Arithmetic Operators:
These rea the operators used to perform the basic arithmetic operations such as addition ,
subtraction, multiplication and division. The modulus operator is added to the list of C arithmetic
operators. It is used to find the remainder value.

operations operator precedence associativity


addition + 2 L to R
subtraction - 2 L to R
multiplication * 1 L to R
division / 1 L to R
modulus % 1 L to R

/* program to demonstrate Arithmetic operations */

void main()
{
int a , b ,sum sub, mul, div ;
printf(“\n enter 2 numbers”);
scanf(“%d%d”, &a, &b);
sum = a + b ;
sub = a – b;
mul = a * b;
div = a / b;
printf(“\n sum is %d\n”, sum);
printf(“\n sub is %d\n”, sub);
printf(“\n product is %d\n”, mul);
printf(“\n quotient is %d\n”, div);
getch();
}

Department of BCA Page 5


Programming in C Govt First Grade College,Tumkur

Relational operators:

These are used to compare two operands given in an expression.

They define the relationship that exists between 2 constants or variables. They result in either a
true or false value. The value of true is nonzero that is 1 and that of false is zero.

There are 6 relational operators in C.

operator Description example result


> greater than 8>5 TRUE
< less than 8<5 FALSE
greater than or
>= equal to 8>=10 FALSE
<= less than or equal to 8<=8 TRUE
== equal to 5= = 5 TRUE
!= not equal to 5!=5 FALSE
void main()
{
int a=10, b=5;
printf (“ a==b %d\n”, a==b);
printf ( “a!=b %d\n”, a!=b);
}

Logical operators:

These are used to combine two or more expressions. A logical relationship between the
two expressions. A logical relationship between the expressions is checked with logical
operators. The logical operators are

operator description Example Returnvalue


&& LogicalAND 8>5 && 5>3 TRUE
|| logical OR 8<5 || 5<3 FALSE
! logical NOT ! (8<15) FALSE

Truth table for logical AND (&&) operator:

T T T
T F F
F T F
F F F

Department of BCA Page 6


Programming in C Govt First Grade College,Tumkur

Truth table for logical OR ( | | ) operator:

T T T
T F T
F T T
F F F

Truth table for logical NOT ( ! ) operator:

T F
F T

/* To demonstrate the logical operators */


void main()
{
printf (“5>3 && 3>10 %d\n”, (5>3) && (3>10));
printf (“!(8= = 8) %d\n”, !(8= =8));

C Assignment Operators
An assignment operator is used for assigning a value to a variable. The most common assignment operator is =
Operator Example Same as

= a=b a=b

+= a += b a = a+b

-= a -= b a = a-b

*= a *= b a = a*b

/= a /= b a = a/b

Department of BCA Page 7


Programming in C Govt First Grade College,Tumkur

Operator Example Same as

%= a %= b a = a%b

Example : Assignment Operators


#include <stdio.h>
int main()
{
int a=5,c;
c = a; // c is 5
printf(“c = %d\n”,c);
c += a; //c is 10
printf(“c = %d\n”,c);
c -= a; //c is 5
printf(“c = %d\n”,c);
c *= a; //c is 25
printf(“c = %d\n”,c);
c /= a; //c is 5
printf(“c = %d\n”,c);
c %= a; //c is 0
printf(“c = %d\n”,c);
return 0;
}

Output

c=5
c = 10
c=5
c = 25
c=5
c=0

Bitwise AND (&):

The bitwise AND operator is represented by a single &. This operator operates on two
integer operands. When two operands are one (1) then the result of this operation will be 1.
Otherwise zero (0).

Department of BCA Page 8


Programming in C Govt First Grade College,Tumkur

Truth table for bitwise AND (&) operator:

0 0 0
0 1 0
1 0 0
1 1 1

Ex: X = 5 : 00000101 Y = 3 : 00000011

X&Y= 00000001

Bitwise OR ( | ):

The bitwise OR operator is represented by a single | (pipeline). This operator operates on


two integer operands. When two operands are zero (0) then the result of this operation will be 0.
Otherwise one (1). (or)

If any one of the operands will be one then the result of this operation will be one
otherwise zero.

Truth table for bitwise OR ( | ) operator:

0 0 0
0 1 1
1 0 1
1 1 1

Ex: X = 5 : 00000101 Y = 3 : 00000011

X | Y= 00000111

Bitwise XOR operator ( ^ ):

The bitwise XOR operator is represented by a ^ (carot) symbol. This operator operates on
two integer operands. When two operands are zero (0) and one then the result of this operation
will be 0. otherwise one (1).

Department of BCA Page 9


Programming in C Govt First Grade College,Tumkur

Truth table for bitwise XOR ( ^ ) operator:

0 0 0
0 1 1
1 0 1
1 1 0

Ex: X = 5 : 00000101 Y = 3 : 00000011

X ^ Y= 00000110

Bitwise compliment operator ( ~ ):

This operator is also called as 1’s compliment operator. This operator changes 1 to 0 and
0 to 1. This is represented by ~ (tilde character).

Ex: if x=5 then bitwise compliment operator on ~x will be

X = 5 00000101

11111010

Bitwise left shift operator ( << ):

This shifting operator can change the input number.

Left shift operator is used to move bits from left side to right side. It is represented by << (double
less than).

Syntax: <variable> <shift operator> <number of bits>

Ex: a << 5

Ex: if X= 10 then its binary number is

X = 10  00001010

X << 3 then we will get 01010000

Bitwise right shift operator ( >> ):


This shifting operator can change the input number.

Right shift operator is used to move bits from right side to left side. It is represented by >>
(double greater than).
Department of BCA Page 10
Programming in C Govt First Grade College,Tumkur

Syntax: <variable> <shift operator> <number of bits>

Ex: a >> 5

Ex: if X= 10 then its binary number is

X = 10  00001010

X >> 3 then we will get 00000001

/* program to demonstrate bitwise operators */

void main()

printf(“12&8 is %d\n”, 12&8);

printf(“12|8 is %d\n”,12|8);

printf(“10<<4 is %d\n”, 10<<4);

Ternary Operator
This is known as Conditional operator. This is used to test the relationship between two
variables. This operator contains a condition followed by two statements or two values.

If the condition is true then the first statement 1 is executed otherwise second statement
will be executed. The condition operator is represented by “ ?: “. It requires two or more
arguments.

Syntax: (condition) ? expression1 : expression2

Ex: int x=10 , y=5, z;

z(x>y) ? x : y

the condition x>y is true then x value is printed otherwise y will be printed.

Special Operators
 Assignment Operator:

This operator is used to assign values to variables. This operator represented by “ = “


symbol.

Department of BCA Page 11


Programming in C Govt First Grade College,Tumkur

Syntax: variable = value ;

Ex: a = 5;

The difference between = and = = operators are

= is the assignment operator, which is used to assigning values to variables.

= = is the relational operator, which is used to compare two quantities means constants, variables
etc.

 Compound assignment operators:

Some times we may require to modify the value of a variable by incrementing,


subtracting, multiplying and dividing with the value of an expression using assignment operator.
In such situations we need compound assignment operator.

Ex: x = x + a it can also represents as x + = a

Y = y * z it can also represents as y * = z

operator Description Example meaning


= Assignment n=6 assign 6 to the number
+= sum and assign n+=6 adds 6 and assign to number
- = subtract and assign n-=6 subtract 6 and assign to number
multiply with 6 and assign to
*= multiply and assign n*=6 number
/= divide and assign n/=6 divide by 6 and assign to number
Bitwise AND and
&= Assign n1&n2 AND operation on n1 and n2

 Comma Operator:

The comma operator can be used to link the related expressions. It is also known as
delimiter or separator. Using comma operator two or more expressions may be combined into a
single expression.

Syntax: expression1, expression2, expression3 .......... expression n

Ex: temp = a;

a= b;

b = temp; it can also represents in a single line using comma operator

Department of BCA Page 12


Programming in C Govt First Grade College,Tumkur

temp = a; a = b; b = temp ;

 Size of ( ) Operator:

Size of ( ) operator returns the number of bytes used for the operands, constants, data
types etc.

Syntax: size of (operand);

Where the operand can be data type, constants or variables using this operator we can get
the size of any data type.

Ex: size of(char) = 1byte

size of(int) = 2bytes

int i;

size of (i) = 2 bytes

Precedence of Arithmetic expression


In one arithmetic expression contains one or more operators then computer evaluates this types
of expressions in systematic manner.

During this process computer consider precedence or priority of operators.

precedence level Operator


1 ()
2 ^ , unary increment and decrement
3 *,/,%
4 +,-

Ex: if a =20 , b = 10, c = 5

Then d = a + b * c

1
2

In the above example there are 2 operators. They are + and *. In those * has highest precedence
than + operator. For that

d = 20 + 10 * 5
Department of BCA Page 13
Programming in C Govt First Grade College,Tumkur

= 20 + 50

d = 70

if same precedence operators are appeared on a single expression in such situations associatively
from left to right the left most operator will be first executed.

Arithmetic Expressions
A group of constants, variables and operators arranged as per the syntax is known as arthemetic
expression.

Arthemetic expression can be classified into 3 types. They are

 Integer Arthemetic expression

 Floating or real Arthemetic expression

 Mixed mode Arthemetic expression

Integer Arthemetic:

In this expression all the values should be integers only.

Ex: 2 + 3 = 5

Floating Arthemetic:

In this expression all the values should be floatin values.

Ex: 2.5 + 5.6 = 8.1

Mixed mode Arthemetic:

In this some values are integers and some values are real values.

Ex: 2.5 + 2 = 4.5


In this mode by default it will display floating point output.

Department of BCA Page 14


Programming in C Govt First Grade College,Tumkur

Type Conversion (or) Type Casting


“ Type conversion is a method of converting a value of one data type to another data
type”.

The type conversions are classified into 2 types. They are

 Implicit Type conversion

 Explicit Type conversion

Implicit Type conversion:

In mixed mode arthemetic expression contains operands of different data types certain
type of conversions are automatically done by the computer itself this is known as implicit type
conversion.

Hierarchy of data types

highest long double

double

float

lowest int

Promotion:

When the variable or constant of lower data type is converted into higher data type is
known as promotion.

Ex: int i = 5;

float f;

f = i;

in the above example I is lowest data type that is integer and f is the highest data type that is
float.

Department of BCA Page 15


Programming in C Govt First Grade College,Tumkur

Demotion:

When the variable or constant of highest data type is converted into lowest data type is
known as demotion.

Ex: float f = 5.3;

int i;

i = f;

in the above example f is highest data type that is float and i is the lowest data type that is
integer.

Explicit Type Conversion (Type Casting)


In this method we can explicitly convert to the value of one data type to the another data type by
using casting operator. This is known as type casting.

Syntax: <casting operator> expression

Ex: x = (float) 6 + 5 ;

Here 5 and 6 both are integer numbers but the answer will be converted to float data type by
using float casting operator.

Then the answer will be 11.0

/* program to demonstrate type conversion */

void main( )

printf (“5/2 is %d\n”, 5/ 2);

printf (“5/2 is %f\n”,(float) 5/2);

printf (“5.0/2 is %f\n”, 5.0/ 2);

getch();

Department of BCA Page 16


Programming in C Govt First Grade College,Tumkur

Control Structures

C has mainly 3 types of control structures. They are

 Sequence

 Selection (or) decision (or) branching

 Looping (or) iteration


The C language programs presented until now follows a sequential form of execution of
statements.

Decision making (or) Branching Statement


Branching having different types
1. if statement.
2. if else statement.
3. Nested If statement.
4. Else – if ladder statement
5. Switch – case statement
6.
If Statement
It is one way branching statement. This is depends on the condition.

Whether that condition was true then execute the statements in that if block other wise we come
out from that condition.

 The general form of the if statement is.


Syntax:

if (condition)

statement;

Department of BCA Page 17


Programming in C Govt First Grade College,Tumkur

Ex:

#include<stdio.h>
main()
{
int i = 3,j = 5;

if(i < j)

printf("value of I is %d”, i);


}

If - else Statement
It is two way branching statement. This is also depends on the condition.

Whether that condition was true then execute the statements in that if block other wise else block
statements are executed.

The general format for an if-else statement is

Department of BCA Page 18


Programming in C Govt First Grade College,Tumkur

Ex:

#include<stdio.h>
void main()
{
int a=10, b=20;
clrscr();
if(a>b)
printf(“a is large %d”, a);
else
printf(“ b is large %d”,b);
getch();

Nested If statement

The if statement may itself contain another if statement is known as nested if statement.

Syntax:

if(condition1)

{
If(condition2)
statement1;

else

statement2;
}

else

{
If(condition3)
Statement3;
}

Department of BCA Page 19


Programming in C Govt First Grade College,Tumkur

Ex:
main()
{
int a,b,c;
printf(“\n enter 3 numbers”);
scanf(“%d%d%d”,&a,&b,&c);
if(a>b)
{
if(a>c)
printf(“ a is large %d”,a);
else
printf(“ c is large %d”,c);
}
else
{
if (c>b)
printf(“ a is large %d”,c);
else
printf(“ c is large %d”,b);
}
}

Else If ladder Statement


The else-if ladder is a multi-way decision making statement. Which contains two or more else-if,
from which any one block is executed.
The general format for the if-else if statement is
Syntax:
if (condition 1)
simple or group of statement
else if (condition 2)

simple or group of statement


else if ( condition 3)

simple or group of statement


.....
else if ( condition n )

simple or group of statement


else
statement n;

Department of BCA Page 20


Programming in C Govt First Grade College,Tumkur

Ex:
#include <stdio.h>
main()
{
int i = 2;

if(i == 0)
{
printf(" i == 0");

}
else if(i == 1)

{
printf(" i == 1. \n");
}
else if(i == 2)

{
printf(" i == 2. \n");
}
}

Switch Statement
Switch statement provides multi-way branching. It is an extension of if-else statement. Using if-
else a maximum 2 branches are allowed. If there is need for multiple branches then we use
switch is the best option.
 To take one of a number of possible actions.
 switch is preferred over multiple if...else statements.
Syntax:
switch (expression)
{
case label 1 : statements;
break;
case label 2 : statements;
break;
:
:
case label n : statements;
break;
default : default statements;
} statements;
}

Department of BCA Page 21


Programming in C Govt First Grade College,Tumkur

 The expression present within the parenthesis of switch statement must be of type int or
char.
 Based on the expression. The control is transferred to a particular case label are executed.
 The case label must be of type int or char.
 The case default is executed only when none of the cases are true.
 The default block is optional.
 Break is a keyword is used to terminate the switch construct. The control is transferred to
the first statement after the switch construct.
 The labels with several cases need not be in order.
 More than one statement with a case need not be enclosed in braces.
 It is not compulsory to have the statements for a particular case.
 There should not be a semicolon at the end of the closing braces after expression.
 Nested switch construct will be allowed.

Ex:

main()

int a=2; switch (a)

{
case 1 : printf(“number
is 1”);break;
case 2: printf(“ number
is 2”);break;
case 3 : printf(“number
is 3”);break;
default: printf(“invalid number”);
}
}

Department of BCA Page 22


Programming in C Vidyavahini First Grade College, Tumkur

Jumping control structures


We may come across a situation where we need to terminate the execution of a loop (or) skip
part of a loop. They are

 break statement
 goto statement.

Break Statement: The break statement ends execution of the nearest enclosing loop or
conditional statement in which it appears. (or)

The break keyword is used to terminate any of the looping constructs.

This keyword is always used in connection with if within a looping

[Link] flow of execution of break statement is shown below.

Syntax:

while(condition)
{
if(condition)
{
statement 1;
:
statement n
break;
}
}
statement n+1;

Ex: main()
{
int i=1;
while(i<=5)
{
if(i==3)
break;
printf(“%d”,i)
i++;
}
}
In the above program output will be 1 and 2.

Department of BCA Page 23


Programming in C Vidyavahini First Grade College, Tumkur

When I becomes 3 then break statement will be executed then the control transferred to the out
of the loop.

The continue: when this keyword is encountered, it transfers the control back to the loop
condition by skipping the rest of statements of the body of the loop.

This keyword works only with loops. This cannot be used in switch construct.
The transfer of control of continue statement is as shown below.
Syntax: while(condition)

{
if(condition)
{
statement 1;
:
statement n
continue;
}
}

Ex: main()
{

For (int i=1;i<=5;i++)


{
if(i==2)
continue;
printf(“%d”,i);
}
}

In the above example we will get the output of 1, 3, 4, 5.

Because when the value of I becomes 2, the condition (i==2) is satisfied and continue statement
will be executed. At the situation 2 will not be printed it will move to the next iteration.

goto statement:
this is an unconditional branching statement. In order to identify where we are in
program, we must and should needed one label. A label is a name that immediately followed by
semi colon.

Syntax: goto labelname;

goto even;

goto 5; here 5 is the line number.

Department of BCA Page 24


Programming in C Vidyavahini First Grade College, Tumkur

exit() statement:

exit() function is a standard library function that comes ready made with the C compiler. Its
purpose is to terminate the execution of the program.
Break statement terminates the execution of loop in which it is written, where as exit()
function terminates the execution of the program itself.

Looping Statement
Using decision making control structures statements are executed only once during execution of
the program. In some situations, it may be necessary to execute a list of statements more than
once. In such cases, the looping constructs are used.
“ looping construct is a construct that executes the statements repeatedly for a certain number
of times as long as the condition is true.
C has provided three types of iteration statements:
1. While loop.
2. do...while loop
3. For loop

While Loop

While loop is also known as “pre tested loop” or “entry controlled loop”.
Why means in this we are first check out the condition. Whether that condition was true then the
control transferred to the inside the loop. Otherwise exit from the loop.
In While looping statement allows a number of lines represent until the condition is satisfied

Ex:
Syntax: #include<stdio.h>
main()
while (condition) {
int i = 0;

simple or group of statement {


(body of the loop);
while (i<=5)

{
printf(" the value of i is %d\n", i);
i = i + 1;
}
}

Department of BCA Page 25


Programming in C Vidyavahini First Grade College, Tumkur

Flowchart:

Do While Loop

In while loop, the condition is tested in the beginning. If the condition becomes false for first
time, then the body of while loop will not executed even once. In some programming situations,
we may require to execute the body of a loop at least once even though condition is false for first
time. In such situations do-while loop is used.

Do-while loop is also known as “post tested loop” or “exit controlled loop”.

Because here we are checked the condition after one iteration. Whether that condition was true
then control transferred to next iteration otherwise comes out from the loop.

Syntax: Ex:

do #include<stdio.h>
{ void main()

statements; { int i, n=5;

} while(Expr); i=1;

do

{ printf(“ the numbers are %d \n”, i);

i = i+ 1;

} while(i<=n)

Department of BCA Page 26


Programming in C Vidyavahini First Grade College, Tumkur

Flowchart:

For Loop
For loop is also known as “pre tested loop” or “fixed execution loop”. Thie loop is the
modified form of a while loop.

The for loop is especially used to execute the statements for a certain number of times.
This loop simplifies the program as well as can reduce the number of statements.

Syntax: Ex: #include<stdio.h>

for( expression1 ; expression2 ; expression3) main()


{ {
Statement1 int i;
: for ( i=1;i<=5;i++)
: {
Statement printf(“%d”,i);
} }
getch();
}

Nested Loop in C
As the name already suggests, a loop inside a loop is called Nested Loop. There can be any number of loops
inside a loop. We know there are generally many looping conditions like for, while, and do-while. We can
loop different kinds of loops within each other to form nested loops. C language supports this functionality of
Nested Loops. below is the syntax of Nested Loop in C.

Department of BCA Page 27


Programming in C Vidyavahini First Grade College, Tumkur

Syntax:
Outside_loop
{
//Outside Loop Statements
Inside_loop
{
//Inside loop Statements
}
}

Flowchart:

Example
Nested loop in ‘for’ condition. This we can generally use for creating or printing a multi-dimensional array.

Code:

#include<stdio.h>
int main()
{
int i,j,x,y;
int a[10][10];
printf("Enter value for x(rows)- max of 10:");
scanf("%d", &x);
printf("Enter value for y(columns) - max of 10:");
scanf("%d",&y);
printf("Let's create a 2-Darray:");
for(i=0;i<x;i++)

Department of BCA Page 28


Programming in C Vidyavahini First Grade College, Tumkur

{
for(j=0;j<y;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Now printing the array:");
printf("\n");
for(i=0;i<x;i++)
{
for(j=0;j<y;j++)
{
printf("\t");
printf("%d",a[i][j]);
}
printf("\n");
}
return 0;
}

Output:
Enter the value for x(rows) – max of 10 : 3
Enter the value for y(columns) – max of 10 : 2
Lets’s create a 2-D array: 1

2
3
4
5
6
Now printing the array:

1 2
3 4
5 6

Department of BCA Page 29


Programming in C Vidyavahini First Grade College, Tumkur

Questions:

1. What is an Expression?
2. Write a short note on operators.
3. What is difference between = and = = ?
4. What is the difference between - - count and count - - ?
5. Explain any 3 unary operators.
6. Explain implicit and explicit type conversions.
7. What is the conditional operator explain?
8. Explain different operators in C with examples.
9. Explain logical and relational operators with an example.
10. Write a C program to find size of integer, character and real variables.
11. What is an operator ?
12. Explain type casting with an example.
13. Explain size of ( ) operator.
15. What is Assignment operator explain?
16. What is meant by looping statement? Explain a pre-tested and a post-
tested loop with asuitable program.
17. What is the purpose of if-else construct?
18. What is the purpose of break statement in a loop?
19. Which of the looping statement is always executes at least ones?
20. Explain any three forms of If statements?
21. Give the syntax of for statement
22. Explain the nested IF statement.
23. Write a program to find the sum of n natural numbers using while
statement.
24. Explain for loop statement.
25. Give the syntax of switch statement. and explain
26. Explain any 3 character handling functions.
27. Differentiate between while and do-while loops.
28. Explain various condition statements.
29. What is the use of control statements?
30. Explain general syntax of for loop with an example program.
31. Explain exit, break and goto statements.
32. Explain else-if ladder with example program.
33. How to terminate infinite loop.
34. Mention any 2 differences between while and do-while.
35. Write the general syntax of the following
a) nested-if b) switch c) for loop

Department of BCA Page 30

You might also like