-1

in my C script my input is printing out gibberish and im not sure why

heres more or less what i have on it

int main (int arg, char argv[])
{
    printf(argv);
}

this prints out giberish?

tripleee
  • 175,061
  • 34
  • 275
  • 318
nidh0gg
  • 21
  • 6

1 Answers1

0

The following should yield the results you are looking for

#include <stdio.h>

int main(int argc, char **argv)
{
    // Check if there is at least 2 arguments. First argument is the executable name.
    if(argc > 1)
    {
        // Print out a string, followed by a new-line character.
        printf("%s\n", argv[1]);
    }
    // Exit successfully
    return 0;
}

Edit: After looking at your code here and some of the things I recommend to change:

The signature of your main function to int main(int argc, char **argv). Here argc is the argument count, and argv are the argument values. argv is a double-pointer. If we consider char* to be a string (sequence of characters in memory terminated by a null-character, or 0), then argv is a pointer to argc-many strings.

Secondly, to check the first program argument, consider making sure that there is in-fact an argument there. if(argc > 1) will make sure there is at least 1 argument to the program (the 0-index argument to a program is the executable path).

When you want to actually check the value of the first argument, de-reference argv to get a "string" with argv[1] //The first argument. Then you can de-reference this string to get the first character

if ( *(argv[1]) == 'f' )
{
    ....
}

If you want to check for a full string, rather than just a single character, consider using a function such as strcmp defined in <string.h>.

T-Fowl
  • 704
  • 4
  • 9
  • adding ** to argv gives me an error "/usr/include/stdio.h:362:12: /../ 'const char * _restrict_' but arugement is of type 'char ***' extern int printf (const char *_restrict_format, ...);" i searched up that i need to use double quotes instead of single quotes but in all my code i dont use any single quotes (https://www.dropbox.com/s/cxvhm5i9kek8wok/bestls.c?dl=0) – nidh0gg Mar 22 '17 at 07:51
  • 2
    Did you change it to `**argv[]`? If so, it should be either `*argv[]` or `**argv`. – T-Fowl Mar 22 '17 at 07:52
  • ok, *argv makes the error go away, however the output becomes gibberish still – nidh0gg Mar 22 '17 at 07:56
  • Probably because neither of those two things are what I suggested. Give me a second and I will update my answer with parts of your code. – T-Fowl Mar 22 '17 at 07:57