0

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??

2
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by Aia
0

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]

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.