Help, weird problem setting variable

This is a discussion on Help, weird problem setting variable within the C Programming forums, part of the General Programming Boards category; Im stumped... Here is a small snippet of code: Code: float W = 640; //(float)SCREEN_WIDTH; float BIG_H = 480; // ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    3

    Help, weird problem setting variable

    Im stumped...
    Here is a small snippet of code:

    Code:
     
      float W = 640;         //(float)SCREEN_WIDTH;
      float BIG_H = 480;     // (float)SCREEN_HEIGHT;
      printf("BIG_H %d %f\n", BIG_H, BIG_H);
    
      BIG_H = 480.0;
    
      printf("BIG_H %d %f\n", BIG_H, BIG_H);
    The screen output is
    BIG_H 0 0.00000

    Any ideas why?
    Last edited by sloppy_coder; 09-25-2005 at 02:14 PM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    Well there's nothing like living up to a nickname...

    Obviously, the "<< !! Posting Code? Read this First !! >>" slipped your attention.

    Oh, and to answer your question, the reason is your printf conversion characters are wrong.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    3
    Wow you're touchy. fixed.

    btw.
    a couple of lines down i have more 'sloppy' code.
    Code:
     printf ("W = %f , H = %f , W/H = %f\n", W, BIG_H, screen_ratio);
    same conversion character %f.. and the output is:
    W = 640.0000 , H = 480.00000 , W/H = 1.333333

    Why does %f work in this case?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    Perhaps reading the manual page for printf() would help.
    Basically, you've got to get the conversions consistent with the parameters or it just doesn't work.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with variable
    By AdampqmabA in forum C++ Programming
    Replies: 10
    Last Post: 09-25-2008, 09:08 AM
  2. While loop with variable problem
    By Todd88 in forum C++ Programming
    Replies: 33
    Last Post: 04-06-2008, 07:36 PM
  3. Weird printf() problem
    By stanlvw in forum C++ Programming
    Replies: 2
    Last Post: 02-23-2008, 01:29 AM
  4. problem in char variable
    By hitesh_best in forum C Programming
    Replies: 2
    Last Post: 08-11-2007, 12:01 AM
  5. weird problem with passing a value..
    By Unregistered in forum C Programming
    Replies: 11
    Last Post: 06-21-2002, 09:05 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21