printf("%s", r1);
This is where your segfault is coming from.
The %s conversion specifier expects its corresponding argument to have type char * and to be the address of the first element of a zero-terminated array of char. r1 is the result of adding the values in a and c, which is not going to be a valid address.
C is not Python or Javascript - values are not magically converted between numbers and strings based on context. If you want to work with numeric values from the command line, you must first manually convert the string representations of those values to their corresponding types.
For example, if your command line is ./foo 1, then argv[1] contains the string "1" (represented as the sequence {'1', 0}). The value of *argv[1] is not 1, it's the encoding value of the character '1', which, assuming ASCII, is 49. You need to use a library function like atoi() or strtol() to convert the string "1" to the integer value 1.
Assuming your input is something like
./foo 1 + 2
and you want it to print 3, then you need to do something like the following:
#include <stdlib.h>
#include <stdio.h>
int main( int argc, char **argv )
{
if ( argc < 4 )
{
fprintf( stderr, "USAGE: %s <val> <op> <val>\n", argv[0] );
return EXIT_FAILURE;
}
/**
* strtol() allows you to detect and reject non-numeric input,
* but I'm omitting that here for brevity.
*/
int lhs = (int) strtol( argv[1], NULL, 0 );
int rhs = (int) strtol( argv[3], NULL, 0 );
if ( *argv[2] == '+' )
{
int r1 = lhs + rhs;
printf( "%d\n", r1 );
}
return EXIT_SUCCESS;
}
Things to remember:
In C, a string is a sequence of character values including a zero-valued terminator - the string "Hello" is represented as the sequence {'H', 'e', 'l', 'l', 'o', 0 }. That trailing 0 is necessary for the sequence to be a string.
Strings are stored in arrays of character type (char for encodings like ASCII, EBCDIC, and UTF-8, wchar_t for "wide" encodings like UTF-16).
Except when it is the operand of the sizeof or unary & operators, or is a string literal used to initialize a character array in a declaration, an expression of type "N-element array of T" will be converted ("decay") to an expression of type "pointer to T", and the value of the expression will be the address of the first element of the array.
argcwhich must be the number of command line arguments you are providing+ 1.printf("%s", r1);shoudl have generated a compiler warning (-Wformat) telling you that you are passing acharto a function that expects achar*.