0

I'm new to C and feel as if I'm missing some formatting syntax but I can't figure out what exactly. Upon compiling the program with

gcc -o test test.c myprintf.c

I get the following error

expected â;â, â,â or â)â before âsâ

I'm sorry if this is some simple overlook on my part, but the format seems to be correct to me. Also this is part of a homework assignment however I tested every function I was responsible for creating separately and they work just fine. I'm omitting the functions I was responsible for writing, if you believe they are needed I will edit them in as it appears to not even access them. The error occurs directly between my first and section function (the blank line between printint and printstring.

Simplest test file possible — test.c

#include <stdio.h>

extern void myprintf(const char *, ...);

int main()
{
  myprintf("Nothing much\n");

  return 0;
}

myprintf.c

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


 void printint(int i){
  int temp;
  /*some while loop that shrinks i while outputting the correct char*/

  int ascii = i + '0';
  putchar(ascii);
}

void printstring(char[] s){
  int i = 0;
  /*some while loop that moves through a string using putchar to output the                 corresponding character*/
}

void printhex(int k){
  /*long complicated program involving a series of loops that change a    decimal into a hexidecimal, obviously without using any built in printf or     modifiers*/
}

/*This is the code provided that uses my 3 functions. I assume it all to be correct*/

void myprintf(const char *fmt, ...) {
  const char *p;
  va_list argp;
  int i;
  char *s;

  va_start(argp, fmt);

  for (p = fmt; *p != '\0'; p++) {
    if (*p != '%') {
      putchar(*p);
      continue;
    }
    switch (*++p) {
    case 'c':
      i = va_arg(argp, int);
      putchar(i);
      break;

    case 'd':
      i = va_arg(argp, int);
      printint(i);
      break;

    case 's':
      s = va_arg(argp, char *);
      printstring(s);
      break;

    case 'x':
      i = va_arg(argp, int);
      printhex(i);
      break;

    case '%':
      putchar('%');
      break;
    }
  }
  va_end(argp);
}
2
  • At some point, you should look into why you get the â characters in the error message. They're a nuisance if nothing else. It might be a UTF-8 vs 8859-x code set problem. Commented Jan 28, 2015 at 21:51
  • In the compiler error message, it should contain a line number indicating which line the error was on. Commented Jan 28, 2015 at 22:13

2 Answers 2

3

The following line is incorrect:

void printstring(char[] s){

Change that to:

void printstring(char s[]){
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! That worked perfect. Sorry that was such a simple problem, i looked over it a hundred times and didn't notice i declared that parameter wrong.
No worries. Most of us have been through such mistakes :)
1

Your line:

void printstring(char[] s){

should be:

void printstring(char s[]){

The square brackets for the array come after the variable name, just as they would if you indexed it with s[i] in the code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.