So I decided to learn C and using learn c the hard way. At any rate I tried editing one of the examples and the output isn't what I expected it would be. I call the program from the command line as e14 asd which "should" print: 'e' == 101 'a' == 97 's' == 115 'd' == 100
But, it doesn't print the 'd' line at all. The code I have is:
#include <stdio.h>
#include <ctype.h>
void print_letters(int argc, char *arg[])
{
int i = 0;
int j = 0;
for(j = 0; j < argc; j++) {
for(i = 0; arg[i] != '\0'; i++) {
char ch = arg[j][i];
printf("j is %d and i is %d\n", j, i);
if(isalpha(ch) || isblank(ch)) {
printf("'%c' == %d \n", ch, ch);
}
}
printf("\n");
}
}
int main(int argc, char *argv[])
{
print_letters(argc, argv);
return 0;
}
I'm assuming the problem has to do with the argv part but after looking around, I still have no idea what exactly is causing the 'd' not appear.
If someone could explain it to me it's be appreciated.
Thanks!
for(i = 0; arg[i] != '\0'; i++) {
should be
for(i = 0; arg[j][i] != '\0'; i++) {
// ^^^
The loop exit condition should be iterating through the characters of a single command line argument but is actually iterating over the arguments.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With