Thread: char = int

  1. #1
    Registered User julianenepom's Avatar
    Join Date
    Sep 2010
    Location
    Portugal or UK
    Posts
    19

    Unhappy char = int

    Hi there, i'm new to this forum and new to programming too.

    I did this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    main()
    {
    char c;
    int a, b;
     
    a = 1;
    b = 2;
    c = char(a) + char(b);
     
     printf("%c", c);
     system ("pause > null");     
    }
    I want the system to print the values of each variable like this:
    12

    But i'm getting a "heart" symbol! O.o

    Any ideas?

    Thx
    July

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    In that case, you should print the values of those variables, e.g.,
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
        int a, b;
        a = 1;
        b = 2;
        printf("%d%d\n", a, b);
    
        system("pause > null");
        return 0;
    }
    The "heart symbol" thing is just the result of printing a character will the value of 3, but that was not what you wanted to print in the first place.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User julianenepom's Avatar
    Join Date
    Sep 2010
    Location
    Portugal or UK
    Posts
    19

    Talking Thx!

    Thank u so much! Its ok now.

    Just one thing: Is there by any chance how to store in a char variable multiples int variables?

    Thx again.
    July

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by julianenepom
    Is there by any chance how to store in a char variable multiples int variables?
    That question does not quite make sense, but... why do you ask?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User julianenepom's Avatar
    Join Date
    Sep 2010
    Location
    Portugal or UK
    Posts
    19

    Talking

    Cause I need to read 3 numbers with scanf and then show them. Order by ascending using only the If expression.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Then declare a third int variable.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User julianenepom's Avatar
    Join Date
    Sep 2010
    Location
    Portugal or UK
    Posts
    19

    Thumbs up

    I did what u said. It seems ok here. I will post when the program is ok. =)

    Thx
    July

  8. #8
    Registered User julianenepom's Avatar
    Join Date
    Sep 2010
    Location
    Portugal or UK
    Posts
    19
    Thats the way I did it, only using the (If) statement. (Which is required by the teacher).

    Can this be smaller or/and smarter?

    Thx.
    July

    Code:
    # include <stdio.h>
    # include <stdlib.h>
    
    main ()
    {
         int a, b, c;
    
         scanf("%d %d %d", &a, &b, &c);
         
         if ((a < b) && (a < c) && (b < c))
            {
               printf("%d %d %d", a, b, c);
            }
         if ((a < b) && (a < c) && (c < b))
         {
               printf("%d %d %d", a, c, b);
         }
         if ((b < a) && (b < c) && ( a < c)) 
         {
               printf("%d %d %d", b, a, c);
         }
         if ((b < a) && (b < c) && ( c < a)) 
         {
               printf("%d %d %d", b, c, a);
         }
         if ((c < a) && (c < b) && ( a < b)) 
         {
               printf("%d %d %d", c, a, b);
         }
         else
         {
               printf("%d %d %d", c, b, a);
         } 
         system ("pause > null");
         return 0;
    }

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    74
    you have consecutive ifs, so each one is going to be evaluated every time.
    You could try instead something like this:
    Code:
    if (...)
    	{}
    else if (...)
    	{}
    else if (...)
    	{}
    else
    	{}
    this way, you can also simplify your expressions.
    For example:

    Code:
    if (a < b)
    	{
    	if (a < c)	//no need to test a < b again
    		{}
    	else
    		{}
    	}

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by julianenepom View Post
    Hi there, i'm new to this forum and new to programming too.

    I did this:
    [code]
    c = char(a) + char(b);
    I'm surprised this compiled without errors.

    The reason you got a little heart insted of a number is that you incorrectly typecast the a and b variables, c was probably never assigned any value and you got the random junk in that memory location.

    Typecasts should be like this...
    Code:
    c = (char) a + (char) b;
    Also if you cant a number instead of a letter, use %d in your printfs.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by CommonTater
    I'm surprised this compiled without errors.

    The reason you got a little heart insted of a number is that you incorrectly typecast the a and b variables, c was probably never assigned any value and you got the random junk in that memory location.
    Well, I probably should have mentioned that instead of just throwing an example, but yeah: julianenepom probably compiled the code using a C++ compiler. With a C compiler, there would have been a compile error, thus the program would not even run, so c would not even be printed in the first place.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by julianenepom View Post
    Can this be smaller or/and smarter?
    Yes, and by quite a lot.

    Approach the problem with the idea of partitioning the remaining cases equally in two at each point. E.g. at the outermost level you could have:
    Code:
    if (a < b)
    {
        // more of the same
    }
    else
    {
        // more of the same
    }
    //done
    5 comparisons all up!
    It's basically like performing a binary search over the six possible outcomes.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  13. #13
    Registered User julianenepom's Avatar
    Join Date
    Sep 2010
    Location
    Portugal or UK
    Posts
    19

    Question

    Thx for the posts. Im getting familiar with C.

    I'm using Dev-C++ to compile this program.

    And about the heart symbol and things like that, im getting this:

    Code:
    int a, b;
    char c;
    a = 1;
    b = 2;
    
    c = (char) a + (char) b;
        
    printf("%d", c);
    Here I get the number 3. Not 12.

    and...

    Code:
    int a, b;
    char c;
    a = 1;
    b = 2;
    
    c = (char) a + (char) b;
        
    printf("%c", c);
    Here I get the heart symbol.

    I just dont get this. I did in VB Script and its kinda simple. =)
    Thx!
    July
    Last edited by julianenepom; 09-24-2010 at 07:22 AM.

  14. #14
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    A char in C is just a number. On a typical system, a char can hold the values -128 to 127, or 0 to 255. There is really nothing special about a char compared to other integers. So when you cast an int to a char, all you're doing is converting from one integer type (a large one) to another (a small one). Both 1 and 2 will fit into a char, so converting 1 to a char gives you 1. Same with 2. And 1 + 2 is 3.

    C doesn't do a lot of work for you that other languages will. If you want to create a string, it'll take more work. In C, you can do something like:
    Code:
    char buf[32]; /* this will hold the string */
    sprintf(buf, "%d%d", a, b); /* sprintf() is just like printf(), but it writes to an array, not the terminal */
    You are apparently under the impression that + in C concatenates strings, which it absolutely does not. There is no string type in C, in fact. Again, char is just a number. C has strings, but they are implemented as arrays of char, and to manipulate them you use library functions like strcpy(), strcat(), and so on. The language itself (as compared to the library) does not understand strings.

    C does not hold your hand anywhere near as much as a lot of other languages, and this is one of the first things you should learn if you're coming from a scripting background.

  15. #15
    Registered User julianenepom's Avatar
    Join Date
    Sep 2010
    Location
    Portugal or UK
    Posts
    19

    Thumbs up

    Oh, thank you so much!!! I will get familiar with C.

    Thx again!
    July

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  3. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  4. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  5. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04:24 PM