简体   繁体   中英

c programming print ascii value of char*

I'm trying to print the ascii value (ascii int numbers) of a char* for example A as 65 as on the ascii table .

Here is the code I have.

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

int main(int argc, char *argv[])
{
    int i = 0;

    printf(" Hello World \n");
    printf("argv[0] is %s! \n",argv[0]);
    printf("argv[1] is %s! \n",argv[1]);
    printf("argv[2] is %s! \n",argv[2]);
    printf("argv[3] is %s! \n",argv[3]);

    if( argc == 4 )
    {
        printf("The first argument supplied is %s\n", argv[1]);
        printf("The second argument supplied is %s\n", argv[2]);
        printf("The third argument supplied is %s\n", argv[3]);
    }
    else if( argc > 4 )
    {
        printf("Too many arguments supplied.\n");
        exit( 1 );
    }
    else
    {
        printf("Not enough arguments supplied. \n");
        exit( 1 );
    }

    for(i = 1; i < 100; i++)
    {
        printf("i is %d argv[i] %d\n", i, argv[i]);
    }

    return(0);
}

I want these values to be entered from the command so this is what I put in.

./a.out A B C

When I compile it I get this warning.

warning: format '%d' expects type 'int', but argument 3 has type 'char *'

If I cast the argv[i] to (int) I get this message.

warning: cast from pointer to integer of different size

If I cast the argv[i] to (int *) I get this message.

warning: format '%d' expects type 'int', but argument 3 has type 'int *'

    printf("i is %d argv[i] %d\n", i, argv[i]);

is the problem causing line. Using incorrect format specifier is undefined behaviour in C.

argv is of type char** but %d format specifier expects an int . So to print the ascii values of arguments, you'll need a nested loop over argv .

    for(j=1;j<argc; j++) {
       for(i=0;argv[j][i];i++) {
          printf("i is %d argv[i] %d\n", j, argv[j][i]);
       }
    }

Note that your loop condition is not correct. You only have strlen(argv[j]) characters for each argument. So you can't arbitrarily print for 100 chars.

Move these printf statements

printf("argv[0] is %s! \n",argv[0]);
printf("argv[1] is %s! \n",argv[1]);
printf("argv[2] is %s! \n",argv[2]);
printf("argv[3] is %s! \n",argv[3]);

below the arguments checking so that you would only print them after arguments checking. Otherwise, you might end up accessing arguments that are not there if fewer arguments are passed.

  • Point 1: Don't use argv[n] directly until you have checked for argc > n . This also includes unchecked loops like

     for(i = 1; i < 100; i++) 

    you'll end up accessing invalid memory after i gets equal to argc .

  • Point 2: argv[n] is a null-terminated array or string (for valid cases), by default. You need %s format specifier to print that.

    That is, a command line A as the first argument to a.out is represented as "A" as argv[1] inside the program. It is not a char anymore, it is a string .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM