Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 354305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:54:41+00:00 2026-05-12T11:54:41+00:00

I understand this code calculates the sum of the args of a variable, however,

  • 0

I understand this code calculates the sum of the args of a variable, however, I don’t understand how it works. It looks really abstract to me. Can someone explain how the below works?

Thanks!

#include <stdio.h>

#define sum(...) \
    _sum(sizeof((int []){ __VA_ARGS__ }) / sizeof(int), (int []){ __VA_ARGS__ })

int _sum(size_t count, int values[])
{
    int s = 0;
    while(count--) s += values[count];
    return s;
}

int main(void)
{
    printf("%i", sum(1, 2, 3));
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T11:54:42+00:00Added an answer on May 12, 2026 at 11:54 am

    With the pre-processor macro

    #define sum(...) \
        _sum(sizeof((int []){ __VA_ARGS__ }) / sizeof(int), (int []){ __VA_ARGS__ })
    

    being called with sum(1,2,3), the line is translated (a simple string substitution, replacing "__VA_ARGS__" with "1,2,3") into:

    _sum(sizeof((int []){1,2,3}) / sizeof(int), (int []){1,2,3})
    

    which is a function call to _sum() passing two things:

    • the number of integers in the array {1,2,3} which is 3 (it gets this by dividing the size of the three-integer array by the size of a single integer).
    • the pointer to the array itself (or a totally different array containing the same values, depending on how smart your compiler is).

    All the _sum() function does is add each of the integers to s (which is initially zero) until the count runs out.


    That first bullet point above bears some explanation. When you have an array of N elements defined as follows:

    tType x[22];
    

    the size of the array is sizeof(x), the size of all elements. The size of a single element of that array is sizeof(x[0]), the size of the first element, although I often prefer the sizeof(*x) variant.

    So, to count the number of elements, you simply divide the total size by the size of an element, using one of the following:

    sizeof(x) / sizeof(x[0])
    sizeof(x) / sizeof(*x)
    

    And, since you’ve asked for a detailed analysis of the code, here we go:

    // Needed for printf().
    
    #include <stdio.h>
    
    // Macro to convert sum(n1,n2,...,nN) to _sum(N,n1,n2,...,nN).
    // This calculates the length of the array by dividing its size by the size
    //   of an int and passes both the length and array through to the new
    //   function.
    // __VA_ARGS__ is replaced with the entire marcro argument list, '1,2,3' in
    //   this case.
    
    #define sum(...) \
        _sum(sizeof((int []){ __VA_ARGS__ }) / sizeof(int), (int []){ __VA_ARGS__ })
    
    // Function to take size and pointer to int array, and return sum.
    
    int _sum (size_t count, int values[]) {
        int s = 0;                // Initial sum of zero.
        while(count--)            // Until elements exhausted (count down).
            s += values[count];   // Add each array element to accumulator.
        return s;                 // Return sum.
    }
    
    int main (void) {
        printf ("%i", sum(1, 2, 3));   // Test it with 1, 2 and 3 (should print 6).
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This code works, but i dont understand why. With DeferredLoadingEnabld = false, I would
Lutz Roeder's Reflector, that is. Its obfuscated. I still don't understand this. Can somebody
I fail to understand why this code won't compile ExecutorService executor = new ScheduledThreadPoolExecutor(threads);
I'm trying to understand this bit of code: in display.php: <html> ... <body> <table>
I was trying to understand something with pointers, so I wrote this code: #include
I'm asking more about what this means to my code. I understand the concepts
I don't know much about database optimization, but I'm trying to understand this case.
If I understand this correctly: Current CPU developing companies like AMD and Intel have
While I understand this question is fairly vague since I'm not giving you all
I'm confused with how views are organized, and it is important to understand this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.