17.02.2015 Views

CCS C Compiler Manual PCB / PCM / PCH

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Here is a function that calculates and returns the sum of all variables:<br />

int Sum(int count, ...)<br />

{<br />

//a pointer to the argument list<br />

va_list al;<br />

int x, sum=0;<br />

//start the argument list<br />

//count is the first variable before the ellipsis<br />

va_start(al, count);<br />

while(count--) {<br />

//get an int from the list<br />

x = var_arg(al, int);<br />

sum += x;<br />

}<br />

//stop using the list<br />

va_end(al);<br />

return(sum);<br />

}<br />

Some examples of using this new function:<br />

x=Sum(5, 10, 20, 30, 40, 50);<br />

y=Sum(3, a, b, c);<br />

40

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!