Thread: 1L and casting

  1. #1
    Registered User
    Join Date
    Jun 2017
    Posts
    88

    1L and casting

    Quote Originally Posted by whiteflags
    Code:
    fseek(ifp, -1L * sizeof(Person), SEEK_CUR);
    is this:
    Code:
    unsigned int a = 5;
    printf("%d\n", 1L * a);
    the same as:
    Code:
    unsigned int a = 5;
    printf("%d\n", (long)a);
    Is there a difference between 1L and a cast? I realize that the reason it is used in the fseek above is that sizeof returns an unsigned int.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    It depends on the types of the operands. C types have the following precedence for automatic conversion in arithmetic operations:
    Code:
    long double > double > float > unsigned long > long > unsigned int > int
    So yes, for your example, (long)*(unsigned int) equals (long).
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    And be careful, both of those printf() statements are incorrect. Remember that the format specifier must match the variable type, in this case you're trying to print a long as an int.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Also I intended for the number to be negative because I wanted fseek() to move backwards. sizeof is never a negative number of bytes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advantages of c++ type casting over c type casting
    By kaibalya2008 in forum C++ Programming
    Replies: 10
    Last Post: 05-05-2009, 11:09 AM
  2. casting?
    By audinue in forum C Programming
    Replies: 26
    Last Post: 07-21-2008, 06:21 AM
  3. casting
    By EirikO in forum C Programming
    Replies: 4
    Last Post: 07-12-2007, 12:00 PM
  4. Casting in C
    By yescha in forum C Programming
    Replies: 2
    Last Post: 12-04-2005, 04:42 PM
  5. Casting
    By laasunde in forum C++ Programming
    Replies: 3
    Last Post: 08-11-2003, 02:10 AM

Tags for this Thread