Bool

This is a discussion on Bool within the C Programming forums, part of the General Programming Boards category; What would be the format specifier while printing the value of a bool variable in C99? I am using MinGW ...

  1. #1
    Anirban Ghosh
    Join Date
    Jan 2006
    Posts
    278

    Bool

    What would be the format specifier while printing the value of a bool variable in C99? I am using MinGW compiler in Code Blocks.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Code:
    printf("%s", bool_variable?"true":"false");

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,765
    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.

  4. #4
    Anirban Ghosh
    Join Date
    Jan 2006
    Posts
    278
    Thanks a lot...
    And what would be the statement for reading a Boolean variable in C99?

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,752
    Well, C99 would support int to bool conversion I would think.
    You could also be clever like

    bool toggle = strcmp( maybe, "true" ) == 0;

  6. #6
    Anirban Ghosh
    Join Date
    Jan 2006
    Posts
    278
    Actually I am inputting an adjacency matrix, so user can input only 0 and 1. I have defined the matrix as bool. So any idea please?

  7. #7
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,752
    I don't see a problem. If you assign 1 or 0 to a bool, then it becomes true or false respectively. Read the number and then assign to a bool.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Read it in with %d.

  9. #9
    Anirban Ghosh
    Join Date
    Jan 2006
    Posts
    278
    Code:
    scanf("%d",&G[row][col]);
    I am getting the following warning!

    G:\PROJECT MATERIAL\Program (C)\Graph Isomorphism\main.c||In function `main':|
    G:\PROJECT MATERIAL\Program (C)\Graph Isomorphism\main.c|61|warning: int format, _Bool arg (arg 2)|
    ||=== Build finished: 0 errors, 1 warnings ===|

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    Read to a temporairy int then copy it into the array.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  11. #11
    Anirban Ghosh
    Join Date
    Jan 2006
    Posts
    278
    @King Mir

    Thanks it worked! Simply but witty suggestion!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing params between managed c++ and unmanaged c++
    By cechen in forum C++ Programming
    Replies: 11
    Last Post: 02-03-2009, 08:46 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 10:42 PM
  3. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 08:05 AM
  4. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 05:25 AM
  5. Need Help With an RPG Style Game
    By JayDog in forum Game Programming
    Replies: 6
    Last Post: 03-30-2003, 08:43 PM

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