Home>

The following program is an excerpt from page 206 of "C language to learn while solving".
This program returns the array index if the character c contains the character c, and returns -1 if it does not.
I have three questions. Comments are provided at the relevant points in the question.

1. Int is specified as the return value of the dummy argument. Why is the caller describing ch[0] as an actual argument?
Is ch[0] char type?

2. "ABCDEFGHIJKLMNOPQRSTUVWXYZ" is the subscript 0-25
"abcdefghijklmnopqrstuvwxyz" is the subscript 26~51
However, since it is delimited by double quotation marks, isn't the subscript 27~52?

#include<stdio.h>
/*--- find character c from string str and return the index of the element at the top --*/
int str_char(const char str[], int c) // Question 1
{
    int i;
    for (i = 0;str[i] !='\0';i++)
        if (str[i] == c)
            return (i);
    return (-1);
}
int main(void)
{
    int no;
    char ch[10];
    printf("Please enter English characters:");
    scanf("%s", ch);
    no = str_char("ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                  "abcdefghijklmnopqrstuvwxyz", ch[0]);// Question 2
    if (no >= 0&&no<= 25)
        printf("It is %dth uppercase letter.\n", no + 1);
    else if (no >= 26&&no<= 51)
        printf("It is %dth lowercase letter.\n", no-25);
    else
        printf("It is not a letter\n");
    return (0);
}
c
  • Answer # 1

    >1. Int is specified as the return value of the dummy argument. Why is the caller describing ch[0] as an actual argument?
    It's the second argument, not the return value.

    int str_char(const char str[], int c) // Question 1

    Certainly, I feel like using char c instead of int c.

    >, but since they are delimited by double quotation marks, aren't the subscripts 27 to 52?
    In the C language, if it is followed by double quotes, it will be recognized as a concatenated string.

    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    "abcdefghijklmnopqrstuvwxyz"

    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"

  • Answer # 2

    The char type is said to be a character type, but it is actually an 8-bit integer type.
    Numerical types are automatically converted to the larger one that can be represented.
    If you pass a char type, which is an 8-bit integer, as an int type argument, it is automatically expanded to an int type.

    In C,""Line breaks cannot be made within the range enclosed by.
    Instead, write nothing in between,"ABC" "abc"If you write
    Automatically"ABCabc"Since there is a function that is regarded as a character string,
    It is treated as a series.
    So in the meantime'\0'Does not enter.


    Why is it good to use char type but int? Regarding that,
    C character literals,'A'Toka is actually an int type.
    printf("char=%d\n",sizeof(char));
    printf("\'A\' =%d\n",sizeof('A'));
    printf("int =%d\n",sizeof(int));
    You can see by running.
    While char type is 1 byte,'A'Has 4 bytes like int.
    That's what the C language does.

  • Answer # 3

    Question 1printf("%d \n", ch[0]);
    Question 2printf("i:%d c:%d \n", i, c);

    It may be obvious when you debug or output the contents of the variable that is being processed.
    The content of ch[0] is an integer value, for example, lowercase'a' is character code 97
    If you enter the lowercase letter a, argument 97 is passed,
    str[i] == cAnd the character code 97 of the lowercase letter a == argument 97 holds,
    i=26 is passed, 25 is subtracted, and 1 is output.

    The result shows that the subscripts of "ABC...""abc" start from A=0 and a=26, so they are continuous.
    Array is0originSo 27th = 26.
    Considering that, it seems that a is -25 so that it becomes the first.

    Please enter English: a
    97
    i:26 c:97
    It is the first lowercase letter.
    #include<stdio.h>/*--- find character c from string str and return the index of the element at the top --*/
    int str_char(const char str[],
     int c) // Question 1
    {
      int i;
      for (i = 0;str[i] !='\0';i++)
        if (str[i] == c) {
          printf("i:%d c:%d \n", i, c);
          return (i);
        }
      return (-1);
    }
    int main(void) {
      int no;
      char ch[10];
      printf("Please enter English characters:");
      scanf("%s", ch);printf("%d \n", ch[0]);
      no = str_char("ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                    "abcdefghijklmnopqrstuvwxyz",
                    ch[0]);// Question 2
      if (no >= 0&&no<= 25)
        printf("It is %dth uppercase letter.\n", no + 1);
      else if (no >= 26&&no<= 51)
        printf("It is %dth lowercase letter.\n", no-25);
      else
        printf("It is not a letter\n");
      getchar();
      return (0);
    }

    For reference, this is a sample that returns the same result.
    Since the character code is known, the same result can be output only by calculation without preparing a character string in the array and determining the subscript in the loop.

    #include<stdio.h>int main(void) {
      char ch[4];
      printf("Please enter English characters:");
      scanf("%s", ch);
      if (ch[0] >= 65&&ch[0]<= 90)
        printf("It is the %dth uppercase letter.\n", ch[0]-64);
      else if (ch[0] >= 97&&ch[0]<= 122)
        printf("It is %dth lowercase letter.\n", ch[0]-96);
      else
        printf("It is not a letter\n");
      getchar();
      return (0);
    }