C - explanation for this answer [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
int main(void)
{
float w = 8.456;
int b = 3;
printf("%d", (int)(b * w));
return 0;
}
Can't seem to understand how does this equal to 25, even though it's int * float and is displayed as an int, and what does int means in printf line... Isn't int multiply by float a 0?

b*w result is a float (=25.368) then you cast it to an int and it is truncated to 25.
NB:
If you were expecting the result to be 24, both variable should be ints.
See: c-language data type arithmetic rules

As you multiply an integer with a floating point number, the so called "usual arithmetic conversions" (UAC) will take place. According to the UAC, if one of the operands is a float and the other is an integer then both operand will be converted to float: 3.0 * 8.456 = 25.368. Later, in the printf when it is converted to an int then fractional part will be truncated that's why the result will be 25.

Related

Is there a difference between these two operations? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I would like to ask if there is a difference in the result (s16Result) between
int16_t s16X, s16Y, s16Result;
s16Result = (int16_t) (s16X - s16Y);
and
int16_t s16X, s16Y, s16Result;
s16Result = (int16_t) ((uint16_t) s16X - (uint16_t) s16Y)
with s16X and s16Y having the datatype signed integer and therefore having the range -32767...32767 . Thank you.
These statements do not generally have the same behavior as defined by the C standard. Consider when s16X has the least value of its type (e.g., perhaps INT_MIN in an implementation where the int type is 16 bits, so it could be −32767) and s16Y is 2. Then, in:
s16Result = (T_S16) (s16X - s16Y)
the expression s16X - s16Y overflows—the mathematical result of −32769 is not representable in the int type, and the C standard does not define the result.
However, in:
s16Result = (T_S16) ((T_U16) s16X - (T_U16) s16Y)
the T_U16 type is presumably an unsigned 16-bit type. In this case, s16X is converted to the 16-bit type by adding or subtracting 65536, yielding 32769. s16Y retains its value of 2. Then the subtraction yields 32767. finally, this result is converted to the T_S16 type, which keeps the value 32767.
Thus, the statement with unsigned arithmetic may have a defined value in some situations where the statement with signed arithmetic does not have a value defined by the C standard.
(The statement with unsigned arithmetic still has undefined behavior if the final result is not representable in the T_S16 type, as when the final result is a number from 32768 to 65535 rather than from 0 to 32767.)

Convert char hex to char dec in C [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I want to convert a value stored in a variable type unsigned char as a decimal in this way:
unsigned char hexValue = 0x0C;
And I want to convert hexValue into a new unsigned char in "dec" format like this:
unsigned char decValue = 0x12; //Ox0C
I tried sprintf() and strtol() but without good results.
Sometimes it can get confusing understanding the difference between value and representation. So, consider this code:
#include <stdio.h>
int main(void) {
unsigned char hexVal = 0x0C;
unsigned char dec = hexVal;
printf("\nValue expressed in decimal: %d and in hexadecimal: %x", dec,dec);
return 0;
}
See live code
It expresses the notion of ten as a hexadecimal in the first assignment statement. When hexVal is assigned to variable dec what is assigned is the value ten which is stored in a binary format. Since dec is already a variable, no need for sprintf() here. You may then use the format specifier to express the value of ten as a decimal or in another base. In this case. The value is a expressed as a decimal.

Explain commented lines in the body [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
typedef union
{
float f;
struct
{
//unsigned int mantissa : 23;
//unsigned int exponent : 8;
//unsigned int sign : 1;
} field;
} myfloat;
I came across these lines in this code. What to they mean?
The commented lines are members using bitfields. The number after the colon determines the number of bits that the member would use.
Since the struct they are contained in forms a union with a float, they are likely an attempt by somebody to inspect the components of the member f, as a single precision IEEE-754 floating point number, which uses 23 bits of mantissa, 8 bits for the exponent and 1 bit for the sign.
Those commented out lines are the names and lengths of the different sections of bits in a float. What is this code from/what is it supposed to be doing?

right shifting the double variable [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
hi I want to do the followed task:
double time;
char array[6];
for(int index=0; index<6; index++){
array[index]=(char)(time>>(8*index));
}
but the error appears: expresion must have integral or unscoped enum
From ISO/IEC 9899:1999 (a.k.a. C99 standard):
6.5.7 Bitwise shift operators
Constraints
2 Each of the operands shall have integer type.
If you want to divide time by 2 to the power of (8*index), you can either:
Use pow() from math.h, or
Create an integer variable with value 1 << (8*index), then divide time by this variable
If you want to actually do a bit shift of the binary representation of the IEEE floating point number(1) (not that I understand why you would want to do that), you can do the following:
uint64_t x = *(uint64_t *)&time;
array[index]=(char)(x>>(8*index));
(1): Assuming your implementation uses IEEE floating point
Right shifting a float or double is almost certainly not what you meant to do, as the data representation is not one that would be affected by right shift as division by a power of 2.
You cannot use right-shifting operator for double variable.

Why is an int promoted in multiplication but not division? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
It's my understanding that when multiplying, for example, a float and an int the int gets promoted to a float and so the product can have a fractional part. On the other hand, when dividing a float by an int there's no promotion and any fractional part of the quotient is discarded. What's the reason for this asymmetry?
float f1 = 13.3;
int i1 = 2;
float product = f1 * i1; // Product will be 26.6
float quotient = f1 / i1; // Quotient will be 6.0
Edit: Well, I guess I was just wrong. An int is promoted in division, too. Right? The truncation only applies to division with two ints. Right? What's the site best-practice here, should I just delete this question since it's so off base?
On the other hand, when dividing a float by an int there's no promotion and any fractional part of the quotient is discarded.
That is simply not correct. The fractional part is kept. The multiplication and division operations work in the same way in this sense.
If you run this program like:
#include <stdio.h>
int main()
{
float f1 = 13.3;
int i1 = 2;
float product = f1 * i1; // Product will be 26.6
float quotient = f1 / i1; // Quotient
printf( "product = %f\n",product );
printf( "quotient = %f\n", quotient );
}
then the output is
product = 26.600000
quotient = 6.650000
According to the C Standard (6.5.5 Multiplicative operators)
3 The usual arithmetic conversions are performed on the operands.
and (6.3.1.8 Usual arithmetic conversions)
Otherwise, if the corresponding real type of either operand is float,
the other operand is converted, without change of type domain, to a
type whose corresponding real type is float.62)

Resources