+ 2

C question: why there's an "i" here ?

​int​ ​main​(​int​ argc, string argv[ ]) ​{ ​    ​int​ trackdigit = ​0​; ​    ​if​ (argc == ​2​) ​    { ​        ​for​ (​int​ i = ​0​, n = ​strlen​(argv[​1​]); i < n; i++) ​        { ​            ​if​ (​isdigit​(argv[​1​][i])) /////// here ​            { ​                trackdigit +=​1​; ​            } And another question: why when im doing normal if else statement and trying to check if argv[1] is digit without adding "i" it doesn't work and give me segfault

17th Feb 2022, 4:51 PM
Abdallah Ashraf
5 ответов
+ 5
Command line arguments are strings, but isdigit() is only defined for single characters (which a string is composed of), hence the loop and the additional index. The expression argv[1][i] refers to the (i + 1)th character in the second command line argument, so the loop counts how many digits are present in that string.
17th Feb 2022, 5:13 PM
Shadow
Shadow - avatar
+ 2
May I know Is there string type in c? Is it new feature? Shadow
17th Feb 2022, 6:12 PM
Jayakrishna 🇮🇳
+ 1
Jayakrishna🇮🇳 Not that I know of, I assumed it to be a typedef for char* maybe.
17th Feb 2022, 6:28 PM
Shadow
Shadow - avatar
+ 1
Jayakrishna, The below thread might have answer to your doubt. The code in that thread includes cs50 header, perhaps 'string' was a custom type introduced in that header. https://www.sololearn.com/Discuss/2984614/?ref=app
17th Feb 2022, 9:23 PM
Ipang
+ 1
edit: Oh. Thats useful. Ipang , Shadow thanks you both for the clarification.. I guess, for OP also..
18th Feb 2022, 9:40 AM
Jayakrishna 🇮🇳