Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><em>All</em> C functions should have prototypes. They're not actually mandatory, but there's no good reason not to use them (unless you're stuck with a pre-ANSI compiler that doesn't support them). (But see the bottom of this answer.)</p> <p>If you want a function that takes a variable number of arguments, that prototype should end with <code>, ...</code>, and the function itself should use the <code>&lt;stdarg.h&gt;</code> mechanism to process its arguments. (This requires at least one argument with a defined type; that argument is used as an anchor for the following arguments.) It's documented <a href="http://man7.org/linux/man-pages/man3/stdarg.3.html" rel="nofollow">here</a> and elsewhere.</p> <p>As I was typing this, you updated your question with "<strong>NOTE: No libraries (such as (...) )should be used</strong>". <code>&lt;stdarg.h&gt;</code> is one of the handful headers that's required for all conforming C implementations, including freestanding (embedded) ones -- because it doesn't define any functions, just types and macros. Your C implementation should support it. If it doesn't, then it's not a conforming C implementation, and you'll need to tell us exactly what compiler you're using and/or read its documentation to find out how it handles variadic functions, or an equivalent.</p> <p>If you really can't use <code>, ...</code> and <code>&lt;stdarg.h&gt;</code>, (or perhaps the older <code>&lt;varargs.h&gt;</code>), then you can define your function with a fixed number of arguments, enough for all uses, then have callers pass extra null pointers.</p> <p><strong>EDIT:</strong></p> <p>This is an update based on new information in comments and chat.</p> <p>The OP has a homework assignment to implement <code>printf</code> for some TI microcontroller, for some reason <em>not</em> using either the <code>, ...</code> notation or <code>&lt;stdarg.h&gt;</code>. The compiler in question apparently implements C89/C90, so it does support both features; this is an arbitrary restriction.</p> <p>This information should have been in the question, which is why I'm downvoting it until the OP updates it.</p> <p>There is no portable way to achieve this -- which is exactly why <code>, ...</code> is part of the standard language, and <code>&lt;stdarg.h&gt;</code> is part of the standard library.</p> <p>Probably the best approach would be to write a program that uses <code>, ...</code> and <code>&lt;stdarg.h&gt;</code>, then invoke the compiler so it shows just the output of the preprocessor (resolving the various <code>va_*</code> macros and the <code>va_list</code> type), and then imitate that. <em>And</em> you'd have to assume, or verify using the compiler documentation, that the calling convention for variadic and non-variadic functions is compatible. In other words, find out what this particular implementation does, and reinvent a similar wheel.</p> <p>(I hope that the point of the homework assignment is to demonstrate how much better the standard techniques are.)</p> <p><strong>UPDATE 2:</strong></p> <p>I wrote above that all C functions should have prototypes. This may actually be a rare exception to this rule. At least one of these calls:</p> <pre><code>printf("Hello\n"); printf("x = %d\n", 42); </code></pre> <p>must produce a diagnostic from a conforming compiler unless either <code>printf</code> is declared with <code>, ...</code> (which is forbidden by the homework assignment), or there is no visible prototype for <code>printf</code>. If there's no prototype, then at least one of the calls will have undefined behavior (behavior that's not defined by the C standard, though it may be defined by a particular compiler).</p> <p>In effect, to meet the homework requirements, you'll have to pretend that you're using a pre-ANSI C compiler.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload