For a school project I am doing we are required to take character input through the command line and convert the character to its ascii value. I would think, therefore, that the line of code:
printf("%d",(int)argv[1]);
would print out 97(ascii for a) if i were to type 'a' as the first command parameter. However, my program is printing out 109, the ascii code for 'm'. Why is this happenning??

Recommended Answers

All 2 Replies

argv[1] is a string even if you enter a character.
Find the length of the string and use a loop to go through each character.

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

[Edit] That example will display the first character of the string.
printf( "%d\n", (int) argv[1] ); is displaying the possible interpretation of the address of argv[1]

argv[1] i

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

[Edit] That example will display the first character of the string.

Correction to the above edit. argv[1] in a loop will display whatever element the index i is.
I meant argv[1][0] is the first character of the string.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.