How exactly is this function an example of a char to int conversion?












6














The book The C Programming Language by Kernighan and Ritchie, second edition states on page 43 in the chapter about Type Conversions:




Another example of char to int conversion is the function lower, which maps a single character to lower case for the ASCII character set. If the character is not an upper case letter, lower returns returns it unchanged.



/* lower: convert c to lower case; ASCII only */
int lower(int c)
{
if (c >= 'A' && c <= 'Z')
return c + 'a' - 'A';
else
return c;
}



It isn't mentioned explicitly in the text so I'd like to make sure I understand it correctly: The conversion happens when you call the lower function with a variable of type char, doesn't it? Especially, the expression



c >= 'A'


has nothing to do with a conversion from int to char since a character constant like 'A'
is handled as an int internally from the start, isn't it? Edit: Or is this different (e.g. a character constant being treated as a char) for ANSI C, which the book covers?










share|improve this question





























    6














    The book The C Programming Language by Kernighan and Ritchie, second edition states on page 43 in the chapter about Type Conversions:




    Another example of char to int conversion is the function lower, which maps a single character to lower case for the ASCII character set. If the character is not an upper case letter, lower returns returns it unchanged.



    /* lower: convert c to lower case; ASCII only */
    int lower(int c)
    {
    if (c >= 'A' && c <= 'Z')
    return c + 'a' - 'A';
    else
    return c;
    }



    It isn't mentioned explicitly in the text so I'd like to make sure I understand it correctly: The conversion happens when you call the lower function with a variable of type char, doesn't it? Especially, the expression



    c >= 'A'


    has nothing to do with a conversion from int to char since a character constant like 'A'
    is handled as an int internally from the start, isn't it? Edit: Or is this different (e.g. a character constant being treated as a char) for ANSI C, which the book covers?










    share|improve this question



























      6












      6








      6







      The book The C Programming Language by Kernighan and Ritchie, second edition states on page 43 in the chapter about Type Conversions:




      Another example of char to int conversion is the function lower, which maps a single character to lower case for the ASCII character set. If the character is not an upper case letter, lower returns returns it unchanged.



      /* lower: convert c to lower case; ASCII only */
      int lower(int c)
      {
      if (c >= 'A' && c <= 'Z')
      return c + 'a' - 'A';
      else
      return c;
      }



      It isn't mentioned explicitly in the text so I'd like to make sure I understand it correctly: The conversion happens when you call the lower function with a variable of type char, doesn't it? Especially, the expression



      c >= 'A'


      has nothing to do with a conversion from int to char since a character constant like 'A'
      is handled as an int internally from the start, isn't it? Edit: Or is this different (e.g. a character constant being treated as a char) for ANSI C, which the book covers?










      share|improve this question















      The book The C Programming Language by Kernighan and Ritchie, second edition states on page 43 in the chapter about Type Conversions:




      Another example of char to int conversion is the function lower, which maps a single character to lower case for the ASCII character set. If the character is not an upper case letter, lower returns returns it unchanged.



      /* lower: convert c to lower case; ASCII only */
      int lower(int c)
      {
      if (c >= 'A' && c <= 'Z')
      return c + 'a' - 'A';
      else
      return c;
      }



      It isn't mentioned explicitly in the text so I'd like to make sure I understand it correctly: The conversion happens when you call the lower function with a variable of type char, doesn't it? Especially, the expression



      c >= 'A'


      has nothing to do with a conversion from int to char since a character constant like 'A'
      is handled as an int internally from the start, isn't it? Edit: Or is this different (e.g. a character constant being treated as a char) for ANSI C, which the book covers?







      c type-conversion kernighan-and-ritchie






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 4 hours ago

























      asked 6 hours ago









      efie

      367216




      367216
























          2 Answers
          2






          active

          oldest

          votes


















          3














          K&R C is old. Really old. Many particulars of K&R C are no longer true in up-to-date standard C.



          In stadard, up-to-date C11, there is no conversion to/from char in the function you posted:



          /* lower: convert c to lower case; ASCII only */
          int lower(int c)
          {
          if (c >= 'A' && c <= 'Z')
          return c + 'a' - 'A';
          else
          return c;
          }


          The function accepts int arguments as int c, and per 6.4.4.4 Character constants of the C standard, character literals are of type int.



          Thus the entire lower function, as posted, under C11 deals entirely with int values.



          The conversion, if any, is may be done when the function is called:



          char upperA = 'A`;

          // this will implicitly promote the upperA char
          // value to an int value
          char lowerA = lower( upperA );


          Note that this is one of the differences between C and C++. In C++, character literals are of type char, not int.






          share|improve this answer





















          • Thanks! Do you know if a character constant was of type int in ANSI C as well? Or was it of type char which would add another explanation?
            – efie
            4 hours ago



















          3














          Character constants have type int, as you expected, so you are correct that there are no promotions to int in this function.



          Any promotion that may occur would happen if a variable of type char is passed to this function, and this is most likely what the text is referring to.



          The type of character constants is int in both the current C17 standard (section 6.4.4.4p10):




          An integer character constant has type
          int




          And in the C89 / ANSI C standard (section 3.1.3.4 under Semantics):




          An integer character constant has type int




          The latter of which is what K&R Second Edition refers to.






          share|improve this answer



















          • 1




            K&R precedes the C standard. Is there reason to believe that, in their text, the type of 'a' was int and not char?
            – Eric Postpischil
            5 hours ago










          • @EricPostpischil On page 37 the book says: "A character constant is an integer, written as one character within single quotes, such as 'x'". I'm not sure if "is an integer" means "is of type int"?
            – efie
            5 hours ago








          • 2




            @efie: char, short, int, and long are all integer types, and an integer is just a value, so I do not think their reference to a character as an integer is informative.
            – Eric Postpischil
            5 hours ago










          • That makes sense, thanks. Regarding your first comment: On page two the book says: "The second edition of The C Programming Language describes C as defined by the ANSI standard."
            – efie
            5 hours ago











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53926270%2fhow-exactly-is-this-function-an-example-of-a-char-to-int-conversion%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3














          K&R C is old. Really old. Many particulars of K&R C are no longer true in up-to-date standard C.



          In stadard, up-to-date C11, there is no conversion to/from char in the function you posted:



          /* lower: convert c to lower case; ASCII only */
          int lower(int c)
          {
          if (c >= 'A' && c <= 'Z')
          return c + 'a' - 'A';
          else
          return c;
          }


          The function accepts int arguments as int c, and per 6.4.4.4 Character constants of the C standard, character literals are of type int.



          Thus the entire lower function, as posted, under C11 deals entirely with int values.



          The conversion, if any, is may be done when the function is called:



          char upperA = 'A`;

          // this will implicitly promote the upperA char
          // value to an int value
          char lowerA = lower( upperA );


          Note that this is one of the differences between C and C++. In C++, character literals are of type char, not int.






          share|improve this answer





















          • Thanks! Do you know if a character constant was of type int in ANSI C as well? Or was it of type char which would add another explanation?
            – efie
            4 hours ago
















          3














          K&R C is old. Really old. Many particulars of K&R C are no longer true in up-to-date standard C.



          In stadard, up-to-date C11, there is no conversion to/from char in the function you posted:



          /* lower: convert c to lower case; ASCII only */
          int lower(int c)
          {
          if (c >= 'A' && c <= 'Z')
          return c + 'a' - 'A';
          else
          return c;
          }


          The function accepts int arguments as int c, and per 6.4.4.4 Character constants of the C standard, character literals are of type int.



          Thus the entire lower function, as posted, under C11 deals entirely with int values.



          The conversion, if any, is may be done when the function is called:



          char upperA = 'A`;

          // this will implicitly promote the upperA char
          // value to an int value
          char lowerA = lower( upperA );


          Note that this is one of the differences between C and C++. In C++, character literals are of type char, not int.






          share|improve this answer





















          • Thanks! Do you know if a character constant was of type int in ANSI C as well? Or was it of type char which would add another explanation?
            – efie
            4 hours ago














          3












          3








          3






          K&R C is old. Really old. Many particulars of K&R C are no longer true in up-to-date standard C.



          In stadard, up-to-date C11, there is no conversion to/from char in the function you posted:



          /* lower: convert c to lower case; ASCII only */
          int lower(int c)
          {
          if (c >= 'A' && c <= 'Z')
          return c + 'a' - 'A';
          else
          return c;
          }


          The function accepts int arguments as int c, and per 6.4.4.4 Character constants of the C standard, character literals are of type int.



          Thus the entire lower function, as posted, under C11 deals entirely with int values.



          The conversion, if any, is may be done when the function is called:



          char upperA = 'A`;

          // this will implicitly promote the upperA char
          // value to an int value
          char lowerA = lower( upperA );


          Note that this is one of the differences between C and C++. In C++, character literals are of type char, not int.






          share|improve this answer












          K&R C is old. Really old. Many particulars of K&R C are no longer true in up-to-date standard C.



          In stadard, up-to-date C11, there is no conversion to/from char in the function you posted:



          /* lower: convert c to lower case; ASCII only */
          int lower(int c)
          {
          if (c >= 'A' && c <= 'Z')
          return c + 'a' - 'A';
          else
          return c;
          }


          The function accepts int arguments as int c, and per 6.4.4.4 Character constants of the C standard, character literals are of type int.



          Thus the entire lower function, as posted, under C11 deals entirely with int values.



          The conversion, if any, is may be done when the function is called:



          char upperA = 'A`;

          // this will implicitly promote the upperA char
          // value to an int value
          char lowerA = lower( upperA );


          Note that this is one of the differences between C and C++. In C++, character literals are of type char, not int.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 5 hours ago









          Andrew Henle

          19.4k21333




          19.4k21333












          • Thanks! Do you know if a character constant was of type int in ANSI C as well? Or was it of type char which would add another explanation?
            – efie
            4 hours ago


















          • Thanks! Do you know if a character constant was of type int in ANSI C as well? Or was it of type char which would add another explanation?
            – efie
            4 hours ago
















          Thanks! Do you know if a character constant was of type int in ANSI C as well? Or was it of type char which would add another explanation?
          – efie
          4 hours ago




          Thanks! Do you know if a character constant was of type int in ANSI C as well? Or was it of type char which would add another explanation?
          – efie
          4 hours ago













          3














          Character constants have type int, as you expected, so you are correct that there are no promotions to int in this function.



          Any promotion that may occur would happen if a variable of type char is passed to this function, and this is most likely what the text is referring to.



          The type of character constants is int in both the current C17 standard (section 6.4.4.4p10):




          An integer character constant has type
          int




          And in the C89 / ANSI C standard (section 3.1.3.4 under Semantics):




          An integer character constant has type int




          The latter of which is what K&R Second Edition refers to.






          share|improve this answer



















          • 1




            K&R precedes the C standard. Is there reason to believe that, in their text, the type of 'a' was int and not char?
            – Eric Postpischil
            5 hours ago










          • @EricPostpischil On page 37 the book says: "A character constant is an integer, written as one character within single quotes, such as 'x'". I'm not sure if "is an integer" means "is of type int"?
            – efie
            5 hours ago








          • 2




            @efie: char, short, int, and long are all integer types, and an integer is just a value, so I do not think their reference to a character as an integer is informative.
            – Eric Postpischil
            5 hours ago










          • That makes sense, thanks. Regarding your first comment: On page two the book says: "The second edition of The C Programming Language describes C as defined by the ANSI standard."
            – efie
            5 hours ago
















          3














          Character constants have type int, as you expected, so you are correct that there are no promotions to int in this function.



          Any promotion that may occur would happen if a variable of type char is passed to this function, and this is most likely what the text is referring to.



          The type of character constants is int in both the current C17 standard (section 6.4.4.4p10):




          An integer character constant has type
          int




          And in the C89 / ANSI C standard (section 3.1.3.4 under Semantics):




          An integer character constant has type int




          The latter of which is what K&R Second Edition refers to.






          share|improve this answer



















          • 1




            K&R precedes the C standard. Is there reason to believe that, in their text, the type of 'a' was int and not char?
            – Eric Postpischil
            5 hours ago










          • @EricPostpischil On page 37 the book says: "A character constant is an integer, written as one character within single quotes, such as 'x'". I'm not sure if "is an integer" means "is of type int"?
            – efie
            5 hours ago








          • 2




            @efie: char, short, int, and long are all integer types, and an integer is just a value, so I do not think their reference to a character as an integer is informative.
            – Eric Postpischil
            5 hours ago










          • That makes sense, thanks. Regarding your first comment: On page two the book says: "The second edition of The C Programming Language describes C as defined by the ANSI standard."
            – efie
            5 hours ago














          3












          3








          3






          Character constants have type int, as you expected, so you are correct that there are no promotions to int in this function.



          Any promotion that may occur would happen if a variable of type char is passed to this function, and this is most likely what the text is referring to.



          The type of character constants is int in both the current C17 standard (section 6.4.4.4p10):




          An integer character constant has type
          int




          And in the C89 / ANSI C standard (section 3.1.3.4 under Semantics):




          An integer character constant has type int




          The latter of which is what K&R Second Edition refers to.






          share|improve this answer














          Character constants have type int, as you expected, so you are correct that there are no promotions to int in this function.



          Any promotion that may occur would happen if a variable of type char is passed to this function, and this is most likely what the text is referring to.



          The type of character constants is int in both the current C17 standard (section 6.4.4.4p10):




          An integer character constant has type
          int




          And in the C89 / ANSI C standard (section 3.1.3.4 under Semantics):




          An integer character constant has type int




          The latter of which is what K&R Second Edition refers to.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 4 hours ago

























          answered 5 hours ago









          dbush

          91.9k12100131




          91.9k12100131








          • 1




            K&R precedes the C standard. Is there reason to believe that, in their text, the type of 'a' was int and not char?
            – Eric Postpischil
            5 hours ago










          • @EricPostpischil On page 37 the book says: "A character constant is an integer, written as one character within single quotes, such as 'x'". I'm not sure if "is an integer" means "is of type int"?
            – efie
            5 hours ago








          • 2




            @efie: char, short, int, and long are all integer types, and an integer is just a value, so I do not think their reference to a character as an integer is informative.
            – Eric Postpischil
            5 hours ago










          • That makes sense, thanks. Regarding your first comment: On page two the book says: "The second edition of The C Programming Language describes C as defined by the ANSI standard."
            – efie
            5 hours ago














          • 1




            K&R precedes the C standard. Is there reason to believe that, in their text, the type of 'a' was int and not char?
            – Eric Postpischil
            5 hours ago










          • @EricPostpischil On page 37 the book says: "A character constant is an integer, written as one character within single quotes, such as 'x'". I'm not sure if "is an integer" means "is of type int"?
            – efie
            5 hours ago








          • 2




            @efie: char, short, int, and long are all integer types, and an integer is just a value, so I do not think their reference to a character as an integer is informative.
            – Eric Postpischil
            5 hours ago










          • That makes sense, thanks. Regarding your first comment: On page two the book says: "The second edition of The C Programming Language describes C as defined by the ANSI standard."
            – efie
            5 hours ago








          1




          1




          K&R precedes the C standard. Is there reason to believe that, in their text, the type of 'a' was int and not char?
          – Eric Postpischil
          5 hours ago




          K&R precedes the C standard. Is there reason to believe that, in their text, the type of 'a' was int and not char?
          – Eric Postpischil
          5 hours ago












          @EricPostpischil On page 37 the book says: "A character constant is an integer, written as one character within single quotes, such as 'x'". I'm not sure if "is an integer" means "is of type int"?
          – efie
          5 hours ago






          @EricPostpischil On page 37 the book says: "A character constant is an integer, written as one character within single quotes, such as 'x'". I'm not sure if "is an integer" means "is of type int"?
          – efie
          5 hours ago






          2




          2




          @efie: char, short, int, and long are all integer types, and an integer is just a value, so I do not think their reference to a character as an integer is informative.
          – Eric Postpischil
          5 hours ago




          @efie: char, short, int, and long are all integer types, and an integer is just a value, so I do not think their reference to a character as an integer is informative.
          – Eric Postpischil
          5 hours ago












          That makes sense, thanks. Regarding your first comment: On page two the book says: "The second edition of The C Programming Language describes C as defined by the ANSI standard."
          – efie
          5 hours ago




          That makes sense, thanks. Regarding your first comment: On page two the book says: "The second edition of The C Programming Language describes C as defined by the ANSI standard."
          – efie
          5 hours ago


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53926270%2fhow-exactly-is-this-function-an-example-of-a-char-to-int-conversion%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          TypeError: fit_transform() missing 1 required positional argument: 'X'