C Programming Tutorial - ITDeveloper

ITDeveloper : C Programming

C Programming

Step by Step Tutorials


Share with a Friend

Sizeof & Ternary Operators In C

Sizeof Operator In C

 

sizeof is one of the mostly used operator in the C. It is a compile-time unary operator which can be used to compute the size of its operand. The result of sizeof is of the unsigned integral type which is usually denoted by size_t. sizeof can be applied to any data type, including primitive types such as integer and floating-point types, pointer types, or compound datatypes such as Structure, union, etc.

 

Syntax:

sizeof(Expression);

where ‘Expression‘ can be a data type or a variable of any type.

Return: It returns the size size of the given expression.

 

Usage of sizeof() operator 

sizeof() operator is used in different ways according to the operand type. 

1. Operand as Data Type: When sizeof() is used with the data types such as char, int, float, etc. it simply returns the amount of memory allocated to that data types. 

Example:

 

// C Program To demonstrate
// sizeof operator
#include <stdio.h>
int main()
{
    printf("%lu\n", sizeof(char));
    printf("%lu\n", sizeof(int));
    printf("%lu\n", sizeof(float));
    printf("%lu", sizeof(double));
    return 0;
}
Output
1
4
4
8

2. Operand as an expression: When sizeof() is used with the expression, it returns the size of the expression. 

Example:

 

// C Program To demonstrate
// operand as expression
#include <stdio.h>
int main()
{
    int a = 0;
    double d = 10.21;
    printf("%lu", sizeof(a + d));
    return 0;
}
 
Output
8

As we know from the first case size of int and double is 4 and 8 respectively, a is int variable while d is a double variable. The final result will be double, Hence the output of our program is 8 bytes.

 

Type of operator

sizeof() is a compile-time operator. compile time refers to the time at which the source code is converted to a binary code. It doesn’t execute (run) the code inside ().

Example:

C

// C Program to illustrate
// that the 'sizeof' operator
// is a 'compile time operator'
#include <stdio.h>
int main( void )
{
     int y;
     int x = 11;
 
     // value of x doesn't change
     y = sizeof (x++);
 
    // prints 4 and 11
     printf ( "%i %i" , y, x);
 
     return (0);
}
Output
4 11

If we try to increment the value of x, it remains the same. This is because x is incremented inside the parentheses and sizeof() is a compile-time operator.  

Need of sizeof 

1. To find out the number of elements in an array: Sizeof can be used to calculate the number of elements of the array automatically. 

Example:

C

// C Program
// demonstrate the method
// to find the number of elements
// in an array
#include <stdio.h>
int main()
{
    int arr[] = { 1, 2, 3, 4, 7, 98, 0, 12, 35, 99, 14 };
    printf("Number of elements:%lu ",
            sizeof(arr) / sizeof(arr[0]));
    return 0;
}
Output
Number of elements:11 

Ternary (Conditional) Operator In C

The conditional operator in C is kind of similar to the if-else statement as it follows the same algorithm as of if-else statement but the conditional operator takes less space and helps to write the if-else statements in the shortest way possible. It is also known as the ternary operator in C as it operates on three operands.

 

Syntax of Conditional/Ternary Operator in C

The conditional operator can be in the form

variable = Expression1 ? Expression2 : Expression3;

Or the syntax can also be in this form

variable = (condition) ? Expression2 : Expression3;

Or syntax can also be in this form

 

(condition) ? (variable = Expression2) : (variable = Expression3);


It can be visualized into an if-else statement as: 

if(Expression1)
{
    variable = Expression2;
}
else
{
    variable = Expression3;
}

Since the Conditional Operator ‘?:’ takes three operands to work, hence they are also called ternary operators.

Note: The ternary operator have third most lowest precedence, so we need to use the expressions such that we can avoid errors due to improper operator precedence management.

Working of Conditional/Ternary Operator in C

The working of the conditional operator in C is as follows:

  • Step 1: Expression1 is the condition to be evaluated.
  • Step 2A: If the condition(Expression1) is True then Expression2 will be executed.
  • Step 2B: If the condition(Expression1) is false then Expression3 will be executed.
  • Step 3: Results will be returned.

Examples of C Ternary Operator

Example 1: C Program to Store the greatest of the two Numbers using the ternary operator

 

// C program to find largest among two
// numbers using ternary operator
  
#include <stdio.h>
  
int main()
{
     int m = 5, n = 4;
     (m > n) ? printf ( "m is greater than n that is %d > %d" ,
     m, n)
             : printf ( "n is greater than m that is %d > %d" ,
                      n, m);
 
     return 0;
}
Output
m is greater than n that is 5 > 4

Example 2: C Program to check whether a year is a leap year using ternary operator

 

// C program to check whether a year is leap year or not
// using ternary operator
#include <stdio.h>
int main()
{
int yr = 1900;
(yr%4==0) ? (yr%100!=0? printf ( "The year %d is a leap year" ,yr)
: (yr%400==0 ? printf ( "The year %d is a leap year" ,yr)
: printf ( "The year %d is not a leap year" ,yr)))
: printf ( "The year %d is not a leap year" ,yr);
return 0;
}

Output
The year 1900 is not a leap year