Awesome q2a theme
0 votes
15 views

Suppose we have a code like this

#include <stdio.h>
#include <stdlib.h>

int main()
{
    unsigned char a=4;
    unsigned  char b=128;
    unsigned short int c;
    c=a*b;
    printf("%hu",c);
    return 0;
}

the output it is giving is 512

but I think it should give 0 :

.a is unsigned char 
b is unsigned char 

so when 4*128=512 = 00000010 00000000

as a and b are unsigned char so a*b is unsigned char  only one byte (which is the lower byte (00000000).)will be assigned to c which is an signed int right?

what concept am I missing?

 


 

in Programming by (69 points) | 15 views
0
Multiplication allowed on characters?
0

a computer treats a charater as a number. 
so I think it is posssible.

https://onlinegdb.com/SJZFQHwOS

check this code.

0
@Shaik

Can you please explain it more
0
Multiplication operator, operands are integers by default, so they treated as 4 bytes rather 1Byte.
0
so basically when it is being multiplied both are treated as 4bytes and a space of 4B is allocated.

and after that when we are assignning the value to the LHS it is being treated as 2B right?
0
AFAIK

Please log in or register to answer this question.

Welcome to GATE CSE Doubts, where you can ask questions and receive answers from other members of the community.
Top Users Oct 2019
  1. GAITONDE

    410 Points

  2. Satbir

    317 Points

  3. Rudr Pawan

    163 Points

  4. srestha

    136 Points

  5. Mk Utkarsh

    127 Points

  6. Debapaul

    94 Points

  7. chandrikabhuyan8

    83 Points

  8. Shaik Masthan

    79 Points

  9. Verma Ashish

    77 Points

  10. !KARAN

    74 Points

Monthly Top User and those within 60% of his/her points will get a share of monthly revenue of GO subject to a minimum payout of Rs. 500. Current monthly budget for Top Users is Rs. 450.
1,680 questions
1,089 answers
4,559 comments
89,591 users