0

I am doing the caesar exercise for PSET 2 and this is my code:

#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

int main(int argc, string argv[] )
{
    //Check if the program was run with one argv
    if(argc != 2)
    {
        printf("Usage: ./caesar key \n");
        return 1;
    }
    else
    {   printf("Success, %s\n"), argv[1];
        return 0;
    }
}

Running this code throws me two errors:

  • error: more '%' conversions than data arguments [-Werror,-Wformat] printf("Success, %s\n"), argv[1];
  • error: expression result unused [-Werror,-Wunused-value] printf("Success, %s\n"), argv[1];

Where did I do wrong and how should I fix this? Thanks.

2

This int printf(const char *format, ...); is the function signature of printf. The ... represents the data arguments used by the format.

Since argv[1] is the argument for %s in the format, it needs to be inside the closing parenthesis.

0

Looks like your printf() statement is trying to access the value of argv[1] which would be the second argument if array index starts with 0. Try argv[0] instead.

2
  • argv[1] exists because argc is guaranteed to be 2 if it reaches the else block. Jan 17 '20 at 11:36
  • Whoops, obviously that is correct. Should I delete my answer? Jan 17 '20 at 14:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .