Others have pointed out the missing space faster than I did, but there is actually much more "wrong" with your code, so if you excuse me I'll switch to tutorial mode for a minute or two.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// The maximum allowed length of an input line, including the newline.
// Longer lines result in an error message.
const size_t INPUTMAXSIZE = 32;
int main()
{
// You can change the command at will. Length is dynamic.
// Do not forget the space at the end.
char * command = "wget ";
char parameter[INPUTMAXSIZE];
char * ptr;
// Using fgets, which allows us to avoid buffer overflow.
if ( fgets( parameter, INPUTMAXSIZE, stdin ) == NULL )
{
puts( "Error while reading input." );
exit( EXIT_FAILURE );
}
// fgets stores the newline as well
if ( ( ptr = strchr( parameter, '\n' ) ) != NULL )
{
// Replace newline with terminating null
*ptr = 0;
}
else
{
// Input longer than INPUTMAXSIZE
puts( "URL too long." );
exit( EXIT_FAILURE );
}
// Allocating the buffer memory dynamically allows us to avoid
// a second magic number. Re-using 'ptr'.
if ( ( ptr = malloc( strlen( command ) + strlen( parameter ) + 1 ) ) == NULL )
{
puts( "Memory allocation failed." );
exit( EXIT_FAILURE );
}
sprintf( ptr, "%s%s", command, parameter );
printf( "system( \"%s\" ) returned %d.\n", ptr, system( ptr ) );
free( ptr );
return( EXIT_SUCCESS );
}
- Always provide code in complete, compilable form.
- Reduce the use of "magic numbers" as much as possible.
- Use constants where possible.
- Make your code stable in the face of unexpected / malformed input. Failing with error is excuseable, dumping core is not.
- Do check the return code of functions you are using that might fail.
I don't say my code above is perfect, but I think there's a lesson or two in there. I hope it helps.