Pin Loon's Wiki
  • Welcome to my technical notes
  • About Me
  • Linux
    • Basic commands
    • Controller Area Network (CAN)
    • Disk
    • Ethernet
    • Systemd Service
    • SSH
  • Microcontroller / Single Board Computer
    • Debugger / Compiler
    • Raspberry Pi 4
  • Application Platform
    • Docker
    • Docker Compose
  • Application Notes
    • Dataype Overflow
    • Macros
    • Wrong Casting
    • Variadic Function
  • Git
    • Create SSH Keys
    • Git submodule
  • VPN
    • Wireguard Setup on Azure
Powered by GitBook
On this page
  1. Application Notes

Variadic Function

Template

#include <stdio.h>
#include <stdarg.h>

int variadic_addition (int count,...)
{
  va_list args;
  int i, sum;

  va_start (args, count);         /* Save arguments in list. */

  sum = 0;
  for (i = 0; i < count; i++)
    sum += va_arg (args, int);    /* Get the next argument value. */

  va_end (args);                  /* Stop traversal. */
  return sum;
}


int main(){

  // call 1: 4 arguments
  printf("Sum: %d\n", variadic_addition(3, 10, 20, 30));

  //call 2: 6 arguments
  printf("Sum: %d\n", variadic_addition(5, 10, 20, 30, 40, 50));

  return 0;

}
PreviousWrong CastingNextCreate SSH Keys

Last updated 3 years ago