Is it possible to initialize char value into int value?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bthakurwar
    New Member
    • Oct 2012
    • 2

    Is it possible to initialize char value into int value?

    Code:
    #include<stdio.h>
    int main()
    {
    	int j=printf("Hello.");
    	return 0;
    	exit(10);
    }
    In the above program is it possible to initialize int j with char value "Hello."?
    Last edited by bthakurwar; Oct 13 '12, 08:29 AM. Reason: to give more explanation
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Yes it is. But there are a few considerations. One is that "Hello" is not an integer value. Instead it is an array of char. You would have to construct your intger value using your own code. If you use "12345" you could call atoi() in ctypes.h. This works because each character is an ASCII integer which is convertible to a binary interger. You just add up the converted values and there you are.

    Remember a char is an integer just like an int. It's just smaller in size. The only reason it is called "char" is that it is supposed to be large enough to hold one character of the implementation set. In our case this is ASCII so your char is 8 bits.

    Further, the library functions thst use char or char* arguments assume ASCII characters or a C-style string od ASCII characters.

    Comment

    • whodgson
      Contributor
      • Jan 2007
      • 542

      #3
      In C "Hello" is string.
      You could visualize it as an array which ends with NULL ('\0')
      int array[6]={H,e,l,l,o,\0} ;
      If you cycled through that array with a for loop, you could develop a running total of the values of each character (excluding NULL) with something like:
      Code:
      int total=0,j=0,i=0;
      for(i=0;i<5;i++)
         total=total+array[i];
      j=total;

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        Please clarify your intent.
        As mentioned earlier, "Hello" is a string -- it does not have a char value. However, each individual character in the string (including the terminating null) has a character value.
        Do you want j to be:
        1. The char value of the first letter of the string ('H')?
        2. The sum of the char values of all letters in the string (with or without the terminating null)?
        3. A count of the number of characters in the string (with or without the terminating null)?
        4. Something else?


        By the way, the C Standard does not mandate any particular character encoding. That is, you cannot be completely certain what numeric value your compiler will use to encode a particular character. That said, ASCII encoding is very common.

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          A char is just an integer.

          The C standard says it has to be large enough to hold one symbol of the implementation character set. For the United States implementaton of ANSI C, the encoding is ASCII.

          There are implementations of C based on other encodings.

          So while you can add up the char values in a C string, the result is just some number.

          Comment

          • vikramjit
            New Member
            • Nov 2012
            • 2

            #6
            int j = "HELLO" is forbidden since you are assigning string value to an integer type.

            but
            int j = 'a' is valid.

            Since character has integral values associated with them, hence 'j' will have ascii value corresponding to 'a' which is 97.

            For your knowledge... you can assign integral values to character type as well

            For example: char a = 97 is legal.
            After this 'a' will have the character whose ascii value is 97.

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              @vikramjit:

              int j = "HELLO" is forbidden since you are assigning string value to an integer type.


              Not quite true. The name of an array is always the address of element 0. So the above is forbidden because you cannot assign an address (the address of the H) to an int.

              To get the value of the H you would need to code:

              Code:
              int j = *"Hello";

              Comment

              Working...