Datatypes sizes varies in visual studio 2005 problem confusing

This is a discussion on Datatypes sizes varies in visual studio 2005 problem confusing within the C Programming forums, part of the General Programming Boards category; hi, i have started learning c for 1 week and i am using VS 2005. from tutorials i learned that ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    2

    Datatypes sizes varies in visual studio 2005 problem confusing

    hi,
    i have started learning c for 1 week and i am using VS 2005.
    from tutorials i learned that size of char is 1 byte
    i am using 32bit system
    but i tried this program:
    Code:
    # include<stdio.h>
    
    
    1. void main() { char a,b;
    a=0xfffffffffffffffe; //now a is holding 8bytes thst is sizeof a double datatype b=0x1; b=a+0x1; printf("size of varaible A:%d\n",sizeof(a)); printf(value in a is:%x\n value in b is:%x); return; }
    output was
    size of varaible A:1
    value in a is:fffffffe
    value in b is:ffffffff

    my question are:
    1)when i added one more f in "a" variable a=0xfffffffffffffffef then when i compiled it showed me the error constant too big(bcoz its more than 32 bit) then why it didnot show earlier this erro message when i assigned value(0xfffffffffffffffe) to char a which greater than it can hold ie 0xff(1 byte size maximum valu that char can hold)
    2)
    even if "A" has holded the whole 8 bytes in it then why did take only last 4 bytes for calculation?
    Or VS 2005 itself expands the datasize from char to float or double ?
    Please help

  2. #2
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    First: get into the habit of compiling with all warnings turned on.

    Second: the function main should return a value of type int: say so with
    Code:
    int main() { /* ... */ }
    Third: the range of values a variable of type can hold varies from computer to computer. In some it it -128 to 127; in some others it is 0 to 255, in yet some (rarer) others it is 0 to 512 or 0 to 65535, etc ... Your computer, very very likely, has the first range, and assigning a value outside the range is a big no no. What happens for strictly positive ranges is that the assigned value is calculated modulo the (maximum value + 1); and for other ranges is Undefined Behaviour. Anyway: writing code that assigns a value with more bytes than the variable can hold does not make the variable large: the excess bytes are ignored.

    Fourth: the values you specify in the second printf() are automatically converted to values of type int; but you asked for values of type unsigned int (with the "%x"). The type of the value provided and the type requested do not match: another big no no.

    To sum up: if you lie to the compiler, it will get its revenge.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    2
    Quote Originally Posted by qny View Post
    First: get into the habit of compiling with all warnings turned on.

    Second: the function main should return a value of type int: say so with
    Code:
    int main() { /* ... */ }
    Third: the range of values a variable of type can hold varies from computer to computer. In some it it -128 to 127; in some others it is 0 to 255, in yet some (rarer) others it is 0 to 512 or 0 to 65535, etc ... Your computer, very very likely, has the first range, and assigning a value outside the range is a big no no. What happens for strictly positive ranges is that the assigned value is calculated modulo the (maximum value + 1); and for other ranges is Undefined Behaviour. Anyway: writing code that assigns a value with more bytes than the variable can hold does not make the variable large: the excess bytes are ignored.

    Fourth: the values you specify in the second printf() are automatically converted to values of type int; but you asked for values of type unsigned int (with the "%x"). The type of the value provided and the type requested do not match: another big no no.

    To sum up: if you lie to the compiler, it will get its revenge.
    Thank you for points i will follw them from now own.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem buidling in Visual Studio 2005 (new to C++)
    By ashley in forum C++ Programming
    Replies: 3
    Last Post: 12-17-2007, 02:14 AM
  2. Problem with Visual Studio 2005
    By Duskan in forum C++ Programming
    Replies: 2
    Last Post: 08-25-2007, 02:04 PM
  3. Visual Studio 2005 (C++)
    By SkyRaign in forum C++ Programming
    Replies: 4
    Last Post: 10-22-2006, 08:52 PM
  4. Visual studio 2005
    By imanidiot in forum C Programming
    Replies: 6
    Last Post: 04-13-2006, 09:04 AM
  5. Visual C++ 2005 linking and file sizes
    By Rune Hunter in forum C++ Programming
    Replies: 2
    Last Post: 11-12-2005, 09:41 PM

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