/* Collection of code snippets by Arne Vajhøj */
/* (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) */
#include <stdio.h>

struct hdr
{
   char data1present;
   char data2present;
   char data3present;
};

struct data1
{
    short v1;
};

struct data2
{
    short v2;
};

struct data3
{
    short v3;
};

void print(char *buf, int n)
{
    int i;
    for(i=0;i<n;i++) printf(" %d",(unsigned int)buf[i]);
    printf("\n");
}

void packandprint(int v1, int v2, int v3)
{
    char buf[sizeof(struct hdr) + sizeof(struct data1) + sizeof(struct data2) + sizeof(struct data3)];
    char *p;
    struct hdr *phdr;
    struct data1 *pdata1;
    struct data2 *pdata2;
    struct data3 *pdata3;
    p = buf;
    phdr = (struct hdr *)p;
    phdr->data1present = v1 > 0 ? 1 : 0;
    phdr->data2present = v2 > 0 ? 1 : 0;
    phdr->data3present = v3 > 0 ? 1 : 0;
    p += sizeof(struct hdr);
    if(v1 > 0)
    {
        pdata1 = (struct data1 *)p;
        pdata1->v1 = v1;
        p += sizeof(struct data1);
    }
    if(v2 > 0)
    {
        pdata2 = (struct data2 *)p;
        pdata2->v2 = v2;
        p += sizeof(struct data2);
    }
    if(v3 > 0)
    {
        pdata3 = (struct data3 *)p;
        pdata3->v3 = v3;
        p += sizeof(struct data3);
    }
    print(buf, p-buf);
}

int main()
{
    packandprint(1, -1, 7);
    return 0;
}
