Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis+ 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
5 Respostas
+ 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.
+ 2
May I know
Is there string type in c? Is it new feature? Shadow
+ 1
Jayakrishnaš®š³ Not that I know of, I assumed it to be a typedef for char* maybe.
+ 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