Thread: Some C questions

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    43

    Some C questions

    Hello there. I was reading this book (C premire) and came up with this

    Code:
    The ranking of types, from highest to lowest, is long double, double, float, unsigned long long, long long, unsigned long, long, unsigned int, and int. One possible exception is when long and int are the same size, in which case unsigned int outranks long. The short and char types don't appear in this list because they would have been already promoted to int or perhaps unsigned int.
    Remembering in one of the threads where user "cas" mentioned this :

    Code:
    255 / -1 as an unsigned char:
    1111 1111
    Converted to int:
    0000 0000 0000 0000 0000 0000 1111 1111
    
    The above is only -1 if the least significant 8 bits are used.  As an int (signed or unsigned) it's clearly 255.
    
    4294967295 / -1 as an unsigned int:
    1111 1111 1111 1111 1111 1111 1111 1111
    Converted to int:
    1111 1111 1111 1111 1111 1111 1111 1111
    
    This is ambiguous as an int: unsigned, it's 4294967295.  Signed, it's -1.
    Therefore having this code :

    Code:
    signed char x = -5;
    int y=5;
    printf("%d",x+y);
    x should be converted to int

    5 in the binary form can be written as a char by 0000 0101
    => x=-5 is equal to 1111 1011 (2's compliment)

    after converting to an int y will be represented by

    0000 0000 0000 0000 0000 0000 1111 1011
    + 0000 0000 0000 0000 0000 0000 0000 0101 (adding 5)

    0000 0000 0000 0000 0000 0001 0000 0000

    which is 256.

    But how come it is printing 0, which is the logical answer that i should get ?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Because x is converted to an int too, and that gives you 1111 1111 1111 1111 1111 1111 1111 1011.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    43
    Because x is converted to an int too, and that gives you 1111 1111 1111 1111 1111 1111 1111 1011.
    x is being converted to an int after having 2's complement

    0000 0000 0000 0000 0000 0000 1111 1011

    or i made a mistake .. is it changed to an int before taking it's 2's compliment ?


    And another Question :

    Can anyone advice me some tutorials or papers about API and windows programming in C and about how should i start socket programming in C and under windows ?
    Last edited by BlaX; 07-16-2009 at 11:14 AM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    "Convert to an int" does not mean "have zeroes put in front of it". "Convert to an int" means "build an int that has the same value as". The int that has the same value as -5 is 1111 1111 1111 1111 1111 1111 1111 1011.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Simply put, a signed integer can hold the range of a signed char, so its value is preserved once promoted. And +5 + (-5) = 0.
    And please don't put any text in code tags. If anything, use quotes.
    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.

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    43
    As for ...

    Can anyone advice me some tutorials or papers about API and windows programming in C and about how should i start socket programming in C and under windows ?

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by BlaX View Post
    Can anyone advice me some tutorials or papers about API and windows programming in C and about how should i start socket programming in C and under windows ?
    Not a windows user myself but pretty sure the word you want for google here is "winsock", maybe it is even called "The Winsock API".

    A lot of people recommend this:

    Beej's Guide to Network Programming

    which the keyword here is "network" and not "socket", but since they refer to the same level of the OSI model it's the same deal.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. Several Questions, main one is about protected memory
    By Tron 9000 in forum C Programming
    Replies: 3
    Last Post: 06-02-2005, 07:42 AM
  4. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 09:44 AM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM