Open In App
Related Articles

C Program to Convert a Char From Upper to Lower and Vice Versa in a Single Line Efficiently

Improve Article
Save Article
Like Article

Here we will build a program to showcase an efficient way to convert a char from upper to lower and vice versa in C in a single line. Below is one example which explains how bitwise operators help in efficient programming and also convert the given alphabet from one case to another.



// C program to demonstrate converting of
// char from upper to lower and vice-versa 
// then printing it in a same line
#include <stdio.h>
// Flip the 5th position of the char using XOR operator
#define convert_case(c)    c^(1<<5) 
// Set the 5th position of the char
#define to_lower(c)        c|(1<<5) 
// clear the 5th position of the char
#define to_upper(c)        c&~(1<<5) 
int main() 
      char ch_u = 'S';
      char ch_l = 's';
      char ch = 's';
      printf("convert: %c, to_lower: %c, to_upper: %c\n"
     convert_case(ch), to_lower(ch_u), to_upper(ch_l));
    return 0;


convert: S, to_lower: s, to_upper: S


  • The difference between upper case and lower case in ASCII is 32 (‘A’ – ‘a’) 
  • So, in the lower case of the alphabet character always 5th position is set (assuming little endian here)
Last Updated : 02 Aug, 2022
Like Article
Save Article
Similar Reads