C Tutorial

C Tutorial C Language Environment Setup Execution flow of C program C printf and Scanf C Data type C Token Variable in C Operators in C Comments in C Escape Sequence in C C – Storage Classes C Decision control statement Loop Statement in C Break, continue and goto statement in C Type Casting in C Function in C Recursion in C String in C C Array Pointer in C Dynamic memory allocation C –Structure Nested Structure in C Union in C File Handling in C C pre-processor Static Function In C Sizeof In C Selection Sort In C Scope Of Variables In C Runtime Vs Compile Time In C Random Access Lseek In C Queue Implementation In C Pseudo Code In C Prototype In C Pointer To Pointer In C Pointer Arithmetic In C Passing Array To Function In C Null Character In C Merge Sort In C Macros In C Library Functions In C Memory Leak In C Int In C Goto And Labels In C Fibonacci Series In C Fflush In C Derived Data Types In C Data Types In C Const Vs Volatile In C Character Set In C Character Class Tests In C Calloc In C C Pointers Arrays In C Include In C Clrscr In C C Vs Java String Literals In C Types Of Pointers In C Variables In C Volatile In C Why C Is A Middle Level Language Infix To Postfix Program In C Ceil function in C LCM of two numbers in C Quick sort in C Static in C function pointer as argument in C Top Array Keywords in C Add two numbers using the function in C Armstrong program in C using function Array, Declaring Arrays and Array Initialization Limitations of Inline Function in C Merge and Merge sort with example in C Do-While Loop in C For Loop in C While-Loop in C Difference between while and do-while loop in C Array Of Structures in C Data Structures And Algorithms in C Types Of Structures In C How to Avoid Structure Padding in C Use of Structure in C Do WHILE LOOP in C Programming Examples For Loop in C Programming Examples Entry Control Loop in C Exit control loop in C Infinite loop in C Nested loop in C pow() function in C String Handling functions in C Prime Number code in C Factorial Program in C using For Loop Factorial Program in C Using While Loop Fibonacci Series in C Using For Loop Fibonacci series in C using while loop Prime Number Program in C using for Loop While Loop in C programming examples Built-in functions in C Assert() Function C vs Java Strings Call Back Function in Embedded C Else If Ladder fgets() function Ftell() Function getc() function getch() function gets() function Heap Sort Nested if-else statement Pi() Function Positioning of file Write() function abs() function in C Attributes in C C program to find factorial of a number using Recursion Ferror() in c fopen() function in C Fibonacci series program in C using Recursion Formatted Input and output function in C Snake Game in C User Defined Functions in C Beep() function in C Cbrt() function in C Hook() function in C Isalnum() function in C C Program to find the Roots of a Quadratic Equation C Switch Statements Difference between rand() and srand() function in C Difference between while and for loop in C Doubly Linked list in C Example of Iteration in C How to use atoi() function in C How to use floor() function in C How to use sine() function in C How to use Typedef Struct in C Integer Promotions in C C Program to Find Largest Number Using Dynamic Memory Allocation C Program to Find the Largest Number using Ternary Operator C/C++ Program to Find the Size of int, float, double and char Find the Largest Three Distinct Elements in an Array using C/C++ Loop Questions in C Modulus on Negative Numbers in C Multiplication table program in C using For loop Nested Loops in C Programming Examples C Program for Mean and Median of an Unsorted Array Results of Comparison Operations in C and C++ Reverse a Stack using Recursion in C Simple hash() function in C strcat() Function in C Sum of N numbers in C using For loop Use of free() function in C Write a program that produces different results in C and C++

Integer Promotions in C

As we know that some of the data types such as char, short int, Enum takes a smaller number of bytes than compared to int. When an operation is applied to these char, Enum and short int data type, these are directly further encouraged to unsigned int. Such type of promotion is known as integer promotion.

Example: When any operation is applied on short int, char, Enum like arithmetic as we know these takes lesser number of bytes, so this is firstly converted into an int or unsigned int. If int represents all original type values, then it is converted into int else the value is converted into unsigned int.

Code:

#include<stdio.h>
        int main ()
        {
             char a=20, b=30, c=10;
             char d=(a*b)/c;
             printf (“%d”, d);
             return 0;
         }

Output:

60

As we know that the signed char takes values from -128 to 127 and from the above given expression that is “(a*b)/c” appears to happen arithmetic flow but the result is 600 which is greater than 128 so, integer promotion happens in this case, and we get appropriate result without overflow.

Example:

#include<stdio.h>
            int main ()
            {
                   Char a = 0xfb;
                   unsigned char b = 0xfb;
printf (“a = %c”, a);
printf (“\nb = %c”, b);
                   if(a==b)
                         printf(“\nsame”);
                   else
printf (“\nNot Same”);
                   return 0;
             }

Output:

Not Same

From the above code the results of ‘a’ and ‘b’ is “Not Same” because at the time of comparison they both are converted into int , but ‘a’ is assigned to signed char so 0xfb becomes -5 and ‘b’ is assigned to unsigned char and it takes the value 251 as they have different values , so the result is “Not Same”.

Note:
1. Unsigned and signed char rank should always equal to char rank.
2. There should be equal ranks for unsigned char and any singed int type.
3. Extended int rank must always be smaller than standard int rank.

Arithmetic Conversions

If an operation consists of two operands, it is known as binary. When a binary operation is applied the two operands must have same type, if it is different, C impose implicit conversion to operand one and its type to other.

  • If long double is the type of each of the two operands then without any changes of domain, the next operand is also converted into long double type.
  • If double is the type of each of the two operands then without any changes of domain, the next operand is also converted into double type.
  • If float is the type of each of the two operands then without any changes of domain, the next operand is also converted into float type.

Else when integer promotions are applied on both operands then the rules are:

  • If two of them have same type then changes are not needed.
  • If both operands contain signed or unsigned int then the operand with smaller rank must be changed into operand with the higher rank.
  • If the operand is of unsigned type and rank is higher or equals to another type then that operand which is singed int must be changed to operand with type of unsigned int.

Example:

int main ()
                            unsigned char x =0;
                            unsigned char y = 1;
printf (“%u\n”, x-y);
printf (“%d\n”, x-y);
}

 As int is higher than unsigned char so integer promotion applies on the code therefore, int (x) – int (y) = int (-1) and the value of unsigned (-1) is 42949697295.

Output:

  424967295
      -1

From the above code changing one to an unsigned int

int main ()
                          unsigned int x=0;
unsigned char y=1;
                           printf (“%u\n”, x-y);
                           printf (“%d\n”, x-y);
}

As x is unsigned int, integer promotions apply to y then we obtain (unsigned int) x – (int) y, we can see the data types are still not same arithmetic conversions applies here, therefore we obtain:

(unsigned int) x – (unsigned int) y = 4294967295

Output:

4294967295
    -1

Likewise, the upcoming code results to the same output:

int main ()
                   unsigned char x =0;
                    unsigned int y=1;
                    printf (“%u\n”, x-y);
printf (“%d\n”, x-y);
             }

By changing either of them into unsigned, we obtain the following code:

int main () {
   unsigned int x =0;
                     unsigned int y=1;
printf (“%u\n”, x-y);
printf (“%d\n”, x-y);
                }

As each of them are unsigned int, integer promotions do not apply, and the output is:

4294967295
-1

We can set the code in the following way:

int main ()
              unsigned char x =0;
              unsigned char y=1;
              printf (“%u\n”, x-y);
printf (“%d\n”, x-y);
              unsigned char z = x-y;4
               printf (“%u\n”, z);
}

Output:  

4294967295
   -1
   255



ADVERTISEMENT
ADVERTISEMENT