Related Articles

Related Articles

Assigning multiple characters in an int in C language
  • Difficulty Level : Hard
  • Last Updated : 30 Apr, 2018

Consider the following C program.

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
int main(void)
{
    int a = 'd';
  
    printf("%d\n", a);
    /*OUTPUT - 100 (ASCII Code for character d)*/
  
    int b = 'dd';
    printf("%d", b);
    /*OUTPUT - 25700 (Explanation in detail given below)*/
  
    return 0;
}

chevron_right


Output :

100
25700

We can easily guess that the output for ‘d’ is 100 as 100 is ASCII value of character ‘d’.

Let us consider below line

int a = 'dd' 

(%d, a) prints 25700 as output
01100100 01100100 (Binary of 100 100)
Assuming int is of 2 bytes, starting byte is occupied by first character ‘d’ and second byte by second character ‘d’. Therefore overall binary involves 0110010001100100 i.e 2^14 + 2^13 + 2^10 + 2^6 + 2^5 + 2^2 = 25700.

Now guess the output of following code.

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
int main(void)
{
    int b = 'de';
    printf("%d", b);
    return 0;
}

chevron_right


Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :