Thread: little ptoblem with chars

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    7

    little ptoblem with chars

    I wrote this simple code, to print the char wich it's binary value is assigned in the "bits" integer:
    Code:
    #include<stdio.h>
    
    int main()
    {
        int bits=01001101;
        putchar(bits);
        getchar();
    }
    according to the ascii table ,the binary value of 'M' is 01001101 but when i run the program it outputs 'A'

    when i change "bits" to 01010111 wich is the ascii of 'W' the program out puts 'I'
    etc'..
    why is that ? what did i do wrong ?

  2. #2
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    i dont know how to program computer biut try

    int bits=01001101b;

    and if that dont work convert binaryy to decimal and use that instead. (binary value is same as decimal value)
    .sect signature

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Numbers with leading zeros are interpreted as octal (base 8), not decimal, not binary.

    There is no standard way to write a binary number, though some embedded compilers do support the b suffix as ggs has shown.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. while condition question..
    By transgalactic2 in forum C Programming
    Replies: 3
    Last Post: 04-04-2009, 04:49 PM
  2. Counting how many chars in a string
    By tigs in forum C Programming
    Replies: 4
    Last Post: 08-05-2002, 12:25 AM
  3. really got stuck with unsigned chars
    By Abdi in forum C Programming
    Replies: 7
    Last Post: 06-11-2002, 12:47 PM
  4. move chars position
    By SpuRky in forum C Programming
    Replies: 3
    Last Post: 06-09-2002, 02:59 AM
  5. fancy strcpy
    By heat511 in forum C++ Programming
    Replies: 34
    Last Post: 05-01-2002, 04:29 PM