Thread: Problem With Typedef Struct

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    27

    Problem With Typedef Struct

    I have declared a two variable structure that has 2 long long integers.
    After declaring a variable "pair", of the structure Pair, I am able to
    write a value to one of the variables, "first", but not to the second
    variable, "snd".
    "first", though a long long was happy to receive the value of an int
    variable.
    "snd", though a long long will not receive any value at all, neither direct,
    "pair.snd = 10;" nor "pair.snd = Length;"
    The output for "pair.snd" is always zero.

    I really have missed something important
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,532
    What compiler are you using?

    A decent properly configured compiler should be emitting a few warnings that need to be fixed, since using the incorrect format specifier for the type produces undefined behaviour.

    In future please place the code, inside code tags, within the post.

    Code:
    #include<stdio.h>
    typedef struct
    {
        long long first;
        long long snd;
    } Pair;
    
    int main()
    {
        Pair pair;
        long long Length; int N = 26;
        Length = (N * (N + 1)) / 2; //351
        printf("MAIN1: %d - %ld\n", N,Length); // 26 - 351
    
        pair.first = N;
        pair.snd = Length;
    //
        printf("MAIN2: %ld - %ld\n", pair.first, pair.snd); // 26 - 0
        return 0;
    }

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    27
    Thanks for the reply
    The compiler is Code::Blocks, 13.12 (Windows 10).
    Have you answered the question or are you waiting for an answer to the complier question?
    You have said something about the format specifiers, %d and %ld. Are these the problem?
    Thanks and wait to hear again

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    928
    The format spec for long long is "%lld". You have "%ld", which is for long.

    If long long is actually bigger than long on your machine and your machine is little endian, then a 0 output for a relatively small input (e.g., 351, possibly up to 2**32 - 1 or even higher) is to be expected. Only the high-order bytes are being printed, and they are all zeroes.

    What is the output of this on your machine?:
    Code:
    printf("%d : %d\n", (int)sizeof(long), (int)sizeof(long long));
    Last edited by john.c; 03-19-2020 at 09:10 AM.
    If you want the truth to stand clear before you, never be for or against. - Sent-ts'an

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,532
    You have said something about the format specifiers, %d and %ld. Are these the problem?
    Most likely. As I said using the wrong specifier for the type produces undefined behaviour, a very bad place to be since anything can, and usually will happen.

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    27
    Thanks to both of you and here is additional information

    The code: "printf("MAIN2: %ld - %lld\n", pair.first, pair.snd);"
    outputs: "MAIN2: 26 - 1507533520896"

    The code: "printf("MAIN2: %ld - %ld\n", pair.first, pair.snd);"
    outputs: "MAIN2: 26 - 0"

    According to Wikipedia (C data types - Wikipedia) the long long data type uses “%lli” as the format specifier. So I tried it
    The code: "printf("MAIN2: %ld - %lli\n", pair.first, pair.snd);"
    outputs: "MAIN2: 26 – 1507533520896”
    Notice how the output is tha same as the %lld format specifier.

    The code "printf("%d : %d\n", (int)sizeof(long), (int)sizeof(long long));"
    outputs: "4 : 8"

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,532
    Notice how the output is tha same as the %lld format specifier.
    That is because "%i" and "%d" are both for integer types, the difference being that the "%i" recognizes a leading zero as Octal.

    What version of the compiler are you using. Are you using a 32 or 64 bit compiler?


    Did you try just using a long int, instead of the long long. Perhaps your compiler doesn't properly support long long ints.

    Edit: Your program prints what you expect on my machine, 64 bit, gcc version 9.2, using C11 standard.
    Last edited by jimblumberg; 03-19-2020 at 10:18 AM.

  8. #8
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    299
    Just to correct you, Code::Blocks will be the IDE i.e. Integrated Development Environment that you're using. Your compiler is most likely going to be GNU GCC *some version*.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook, The Wizardry Compiled

  9. #9
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    715
    BOTH format specifiers must be %lld, as in printf("MAIN2: %lld - %lld\n", pair.first, pair.snd);, rather than printf("MAIN2: %ld - %lld\n", pair.first, pair.snd);.

  10. #10
    Registered User
    Join Date
    Nov 2001
    Posts
    27
    Update and resolution

    The compiler is in fact GNU GCC. Code::Blocks is 32 bit on a 64 bit Windows 10.

    Meanwhile, Christop has solved it. When the first format specifier is inaccurately %ld and the second one is correctly %lld, “pair.first” outputs correct, and pair.snd outputs incorrect. Making both %lld, as they are both long long, means the output for both is correct.

    Thanks to all

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. typedef struct vs struct declaration error in code
    By shaswat in forum C Programming
    Replies: 3
    Last Post: 07-25-2016, 06:45 AM
  2. typedef struct problem
    By jarrix in forum C Programming
    Replies: 2
    Last Post: 01-29-2011, 04:22 PM
  3. differences between struct and typedef struct
    By stanlvw in forum C Programming
    Replies: 1
    Last Post: 07-22-2008, 03:28 PM
  4. Problem with typedef struct
    By cprogrammer_18 in forum C Programming
    Replies: 2
    Last Post: 02-03-2006, 06:01 AM
  5. another 'typedef struct' problem ?
    By thjoon in forum C Programming
    Replies: 3
    Last Post: 11-18-2002, 08:26 AM

Tags for this Thread