Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 819 Vote(s) - 3.45 Average
  • 1
  • 2
  • 3
  • 4
  • 5
assigning float into int variable causes no warning

#1
So, given the following code:

int main(void) {
int i;
i = 12.1234;
i++;
return 0;
}

I compiled the code and I expected and wanted the compiler to give me a warning, but it didn't. Is my compiler configured wrong? Is there a way to make my compiler give warning?

This is what I used

>cc -Wall test.c
Reply

#2
Since you confirmed your compiler is `gcc` then you can use the [-Wconversion](

[To see links please register here]

) flag which should provide a warning similar to this:

warning: conversion to 'int' alters 'double' constant value [-Wfloat-conversion]
i = 12.1234;
^

Converting a floating point value to *int* is perfectly valid it will discard the fractional part and as long as the value can be represented, otherwise you have undefined behavior. The C99 draft standard covers this in section `4.9` *Floating-integral conversions*:

> A prvalue of a floating point type can be converted to a prvalue of an
> integer type. The conversion truncates; that is, the fractional part
> is discarded. The behavior is undefined if the truncated value cannot
> be represented in the destination type.
Reply

#3
A float value **can be assigned** to an integer variable but an **implicit conversion** occurs when **compiler** forces a float value to be assigned as an integer.

The digits after the decimal notation in the float value get lost after assigning a float to an integer.

Edit: casting -> conversion

Thanks R..
Reply

#4
You may expect that `-Wall` does enable *ALL* warnings, but it doesn't!
There are a lot of warnings that doesn't even make sense in the wrong environment.

It is an aggregation of the following flags:

> -Waddress
> -Warray-bounds (only with -O2)
> -Wc++11-compat
> -Wchar-subscripts
> -Wenum-compare (in C/ObjC; this is on by default in C++)
> -Wimplicit-int (C and Objective-C only)
> -Wimplicit-function-declaration (C and Objective-C only)
> -Wcomment
> -Wformat
> -Wmain (only for C/ObjC and unless -ffreestanding)
> -Wmaybe-uninitialized
> -Wmissing-braces (only for C/ObjC)
> -Wnonnull
> -Wparentheses
> -Wpointer-sign
> -Wreorder
> -Wreturn-type
> -Wsequence-point
> -Wsign-compare (only in C++)
> -Wstrict-aliasing
> -Wstrict-overflow=1
> -Wswitch
> -Wtrigraphs
> -Wuninitialized
> -Wunknown-pragmas
> -Wunused-function
> -Wunused-label
> -Wunused-value
> -Wunused-variable
> -Wvolatile-register-var
as described here:

[To see links please register here]


What you need is `-Wconversion` as mentioned above. Another quite useful flag may be `-Wextra`.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through