Thread: Shouldn't this give a warning?

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    127

    Shouldn't this give a warning?

    I always forget which one has stricter rules (C or C++) but shouldn't this give a warning in at least one of the languages?

    Code:
    int x = 120000;
    short y = x;
    I've tried compiling with both c and c++ using

    Code:
    g++ -c -Wall test.cpp
    gcc -c -Wall test.cpp
    Neither give a warning.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    What warning are you expecting?

    Why?

    Jim

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Warnings are compiler-specific. The language does not mandate what warnings should emitted. Nevertheless, some compilers (e.g. Visual Studio) does provide warnings for this sort of code:

    Code:
    int main()
    {
    	int x = 0;
    	float y = x;
    	int z = y;
    }
    Warning C4244 'initializing': conversion from 'float' to 'int', possible loss of data
    Warning C4244 'initializing': conversion from 'int' to 'float', possible loss of data
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Use the "-Wconversion" flag.
    Why it does not get enabled with -Wall, I have no idea.


  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    127
    Quote Originally Posted by manasij7479 View Post
    Use the "-Wconversion" flag.
    Why it does not get enabled with -Wall, I have no idea.

    Thanks. Found this write up on that flag. https://gcc.gnu.org/wiki/NewWconversion. Unfortunately, my compiler is 4.1 so it seems like I may not have this option.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by manasij7479 View Post
    Use the "-Wconversion" flag.
    Why it does not get enabled with -Wall, I have no idea.

    Interestingly, even -Wextra does not include -Wconversion
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by homer_3 View Post
    Thanks. Found this write up on that flag. https://gcc.gnu.org/wiki/NewWconversion. Unfortunately, my compiler is 4.1 so it seems like I may not have this option.
    That compiler version is very outdated. Highly recommend upgrading some way or another.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Quote Originally Posted by homer_3 View Post
    Thanks. Found this write up on that flag. https://gcc.gnu.org/wiki/NewWconversion. Unfortunately, my compiler is 4.1 so it seems like I may not have this option.
    :S why are you using an 8-years-old compiler?!

    GCC is open source. You can upgrade for free!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why it give warning message, how will it remove
    By hitlar in forum C Programming
    Replies: 2
    Last Post: 01-31-2014, 02:50 PM
  2. Free Windows C Compile that give good warning?
    By stahta01 in forum C Programming
    Replies: 2
    Last Post: 11-24-2010, 09:20 PM
  3. OpenGL using SDL --> Should I or shouldn't I?
    By Devils Child in forum C++ Programming
    Replies: 12
    Last Post: 02-01-2008, 01:18 PM
  4. I know I really shouldn't...
    By cboard_member in forum A Brief History of Cprogramming.com
    Replies: 25
    Last Post: 09-16-2006, 10:05 AM
  5. Do-ing loops , While I shouldn't be
    By Loraswish in forum C++ Programming
    Replies: 4
    Last Post: 03-15-2003, 08:33 PM