Ternary Operator in C Language

This operator also known as conditional operator . It is similar to if-else statement.

Syntax of ? operator

There are three expressions in ? operator, Expression1, Expression2, and Expression3 .

The value of a ? expression is determined like this −

  • If Exp1 is true, then Exp2 is evaluated.
  • If Exp1 is false, then Exp3 is evaluated.

Example: write a program to find the grater between two number using ? operator.

OUTPUT

Switch Case in C Programming

“switch case ” is a case control structure.

 It contains case and default values.

“switch” statement takes a value as a expression for equality testing against a list of values/case.

Switch case statement is used in programming to select one amont multiple options.

syntax for a switch statement:-

Rules of switch statement −

In a “switch” statement we can have any number of case.

for equality testing against a list of values/case, constant-expression for a case must be the same data type as the variable in the switch expression.

When “switch” expression value is equal to a case, the statements following that case will execute until a break statement is reached/executes.

When a break statement is reached/executes, the switch statement terminates, and the control jumps to the outside of switch.

break is a optional. If there is no break in the switch case, the flow of control will jump to the next case of switch until a break is reached.

switch statement also have a default case, which appear at the end of the switch. when none of the cases is true  then default case will execute.

No break is needed in the default case. “default” case is also an optional.

Example: Write a program to take one numbers from user between 1 to 3. Write entered number in words if number between 1 to 3 otherwise print wrong number using switch.

OUTPUT

Note: In the above program, for example user enters a number 6 then output will be as follows:

Example: Write a program using switch statement but without break.  

OUTPUT

Description: Because if there is no break in the switch case, the flow of control will jump to the next case of switch until a break is reached.

Note: In the above program, for example value of char variable c = ‘ A’ then the output will be as follows:

Nested switch case

In C programming language we can define switch statement within another switch.

Syntax for a nested switch statement: −

Read More

  1. C++ program for student details
  2. Anonymous Object in C++
  3. Lowercase character to uppercase character
  4. Command Line Argument
  5. Local vs Global Object in C++
  6. Local and Nested Classes in C++
  7. Arithmetic operations using switch case
  8. Constructor in C++
  9. Know more about C++

if else statement in C programming

In the C language decision making statement is executes if the given condition is true otherwise conditional block will never execute.

In C language non-zero and non-null values are consider as true, and zero or null values are consider  false.

if statement

Syntax of an if statement −

In a if statement if the expression result is non zero true, then the block of statement of the ‘if’ statement will be executed. If the expression result is zero, then the block of statement of the ‘if’ statement will not executed and control sent to the next line of if block.

Example: Write a program to demonstrate the ‘if’ statement.

OUTPUT

In the above program if(x>y) then the condition will be false and ‘if’ block will never execute.

OUTPUT

if-else  statement

Syntax of an ‘if-else’ statement −

Example: Write a program to take two numbers from user and find the grater between them using ‘if-else’ statement.

OUTPUT

Nested If else

In a C language we can use if statement inside another if statement(s).

Syntax of an ‘nested -if’ statement −

Example: Write a program to take two numbers from user if both number are between 1 to 9 then print “Good” using nested-if statement.

OUTPUT

Nested if else statement

In a C language we can use if or if-else statement inside another if or  if-else statement(s).

Syntax of an ‘if-else’ statement −

Example: Write a program to take three numbers from user and find the grater among them using ‘nested if-else’ statement.

OUTPUT

If else ladder

some times if can be nested inside else as below

above statements can be written as

OUTPUT

Assignment Operators in C Programming

In a C language there are following assignment operators  −

Operator Description Example
= assignment operator (it assigns values of right side operands to left side operand ) a=5
+= Add assignment operator a+=5
-= Subtract assignment operator a-=5
*= Multiply  assignment operator a*=5
/= Divide assignment operator a/=5
%= Modulus assignment operator a%=5
<<= Left shift assignment operator a<<=5
>>= Right shift assignment operator a>>=5
&= Bitwise AND assignment operator a&=5
^= Bitwise exclusive OR assignment operator a^=5
|= Bitwise inclusive OR assignment operator a|=5

EXAMPLE: Use of assignment operators with arithmetic operators

Output

EXAMPLE: Use of assignment operators with bitwise operators

Output

Bitwise Operators in C Programming

In a C programming language Bitwise operator works on bits and perform bit-by-bit operation. There are following bitwise operators in C:-

Operator Description Example
& Binary AND Operator   a & b
| Binary OR Operator a| b
^ Binary XOR Operator  a^b
~ Binary One’s Complement Operator is unary ~a
<<  Binary Left Shift Operator a<<b
>>  Binary Right Shift Operator a>>b

The truth tables for &, |, and ^ is as follows –

a b a & b a | b a ^ b
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1

Assume A = 60 and B = 13 in binary format, they will be as follows −

               A in binary     = 0011 1100         (60)                     

              B in binary     = 0000 1101          (13)

Bitwise AND (&)

                      A      = 0011 1100         (60)                

          &         B      = 0000 1101          (13)

———————————————————

                                0000 1100           (12)

Bitwise OR (|)

                      A      = 0011 1100         (60)                

          |          B      = 0000 1101          (13)

———————————————————

                                0011 1101           (61)

Bitwise XOR (|)

                      A      = 0011 1100         (60)                

          ^         B      = 0000 1101          (13)

———————————————————

                                0011 0001           (49)

Binary Left Shift Operator

Left shift operator shift all the bits in a left direction to specified number of times.

i=16 //0001 0000

Example:

Binary Right Shift Operator

Left shift operator shift all the bits in a left direction to specified number of times.

int i = 16;                    // 0001 0000

i = i>>2;                      // 0000 0100  (shift right 2 bits, last 2 bits will be lost, and 2 zero add in front)

Example

Output

Exapmle:  Write a program to demonstrate Bitwise operator.

OUTPUT

Logical Operators in C Programming

There are three logical operators in C language. Assume variable A and B holds 1 and 0 respectively then –

Operator Description Example
&&  AND operator (the condition becomes true if both the operands are non-zero) (A && B) is false.
||  OR Operator (the condition becomes true if any of the two operands is non-zero) (A || B) is true.
!  NOT Operator (this operator reverse the logical state of operand. For example: If condition is true, then NOT operator will make it false) !(A && B) is true.

Example: Check both numbers are greater the 10 or not

Output

Example: Check both numbers are greater the 10 or not. Also check any one number is greater then 10 or not

Output

Relational Operators in C Programming

C Language has a following relational operators. For example: Assume variable A holds 100 and variable B holds 200 then −

Operator Description Example
== Equals to (A == B) is not true.
!= Not equal (A != B) is true.
Greater than (A > B) is not true.
Less than (A < B) is true.
>= Greater than or equals to (A >= B) is not true.
<= Less than or equals to (A <= B) is true.
Example: Write a C Program to find the largest of two numbers.

Output

Arithmetic Operators in C Programming

C language have a following arithmetic operators. Assume variable i holds 100 and variable j holds 200 then –

Operator Description Example
+ Adds two operands. i + j = 300
Subtracts second operand from the first. i − j = -100
* Multiplies two operands. i * j = 20000
/ Divide. j / i = 2
% Modulus Operator returns the remainder of integer division. j % i = 0
++ Increment operator increases the value of variable by one. i++ = 101
Decrement operator decreases the value of variable by one. i– = 99

Example : Write a program to demonstrate the various arithmetic operators.

OUTPUT

Increment and Decrement Operator

  1. Increment operator      ++
  2. Decrement operator    —

In a C language, Increment operator (++)  increases the value of variable by one and Decrement operator  (–) decreases the value of variable by one.

There are two form of increment  and decrement operator:

  1. Prefix form
  2. Postfix form

Prefix Form : In the prefix expression operator appears in the expression before the operands.

Example : ++A 

In the prefix form first the value of operand is increment or decrement than the value of operand is used in expression.

Example

Postfix Form: In the postfix expression operator appears in the expression after the operands.

Example : A++

In the postfix form first the value of operand is used in expression than value of operand is incremented or decremented.

Example: Write a program to demonstrate the increment and decrement operator. 

OUTPUT

Operators in a C Programming

An operator is a symbol. It is used to tells the compiler to perform specific mathematical or logical functions. C language have a following types of built-in operators:−

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators

Basics of C Programming

Tokens

In a C language, token is either a keyword, a symbol ,an identifier, a constant or a string literal.

A C program consists of various tokens for example, the following C program consists of five tokens −

The individual tokens are −

Comments

Comments are the text in C program and they are ignored by the compiler during compilation and execution.

They start with /* and terminate with the characters */ as shown below −

Anything written within Comment will never execute. You can’t write comments within comments.

Identifiers

In a C language, identifier is a name given to any variable, function, or any other user-defined item.

C language identifier starts with a letter A to Z, a to z, digits (0 to 9) or an underscore ‘_’ followed by zero.

C does not allow punctuation characters such as @, $, and % within identifiers. Here are some examples of acceptable identifiers −

Keywords

In a C language keywords are the reserved words whose meaning already explained in a compiler.

These reserved words can’t be used as a variables or identifier names. There are 32 Keywords in C

auto else long switch
break enum register typedef
case extern return union
char float short unsigned
const for signed void
continue goto sizeof volatile
default if static while
do int struct double