-2

I am running this program with two command line arguments: 10 and test. It works as intended when I use:

printf("C = %s\n\n", &C);

but not when I use:

printf("C = %s\n\n", C);

and I can't understand why.

#include <stdio.h>
#include <string.h>

int main(int argc, char ** argv)
{

    int N = 0;
    char C = '\0';

    sscanf(argv[1], "%d", &N);
    sscanf(argv[2], "%s", &C);

    printf("N = %d\n\n", N);
    printf("C = %s\n\n", &C);

    return 0;

}
Nae
  • 14,209
  • 7
  • 52
  • 79
MarkAsh
  • 33
  • 6

2 Answers2

1

I changed to the code below and seems to be working. I changed char C = '\0' to char C[10] so C is a string and not a character and removed ampersand (&) operator from printf("C = %s\n\n", &C).

#include <stdio.h>
#include <string.h>

int main(int argc, char ** argv)
{

    int N = 0;
    char C[10];

    sscanf(argv[1], "%d", &N);
    sscanf(argv[2], "%s", C);

    printf("N = %d\n\n", N);
    printf("C = %s\n\n", C);

    return 0;

}
MarkAsh
  • 33
  • 6
0

As I see it there are several problems in your code and in order to fix it you will need to change it alot. I don't believe that someone will do this for you, and I won't as well because everybody need to learn.

Google is your best friend!

Before asking question in Stack Overflow check how to do what you want to do online.

For example, here you can find a great answer about how to properly get command line parameters.

And here you can see the difference between with or without '&' and what does it mean.

Good luck

Yonlif
  • 1,770
  • 1
  • 13
  • 31