Function to produce sum with int and char

This is a discussion on Function to produce sum with int and char within the C Programming forums, part of the General Programming Boards category; My teacher for my class is asking the following question: Write a simple C program that defines a function (other ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    29

    Question Function to produce sum with int and char

    My teacher for my class is asking the following question:

    Write a simple C program that defines a function (other than main) that takes two parameters, an int and a char, and that returns their sum as an int. Be sure to declare the variables, and assign them values using assignment statements. In the main function, your sample program should call your function, passing int and char variables and assigning the return value to another int variable.

    How do you add an int and a char together to yield an int sum? I'm confused by this. Help appreciated.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int x;
    char y;
    int z;
    
    int sumfunc(int x, int y){
         z= x + y;
         return z;
         }
         
    int main(void){
       
        printf("Please type an integer.\n");
        scanf("%i", &x);
        printf("Please type a single digit number.\n");
        scanf("%c", &y);
        printf("The sum of the two numbers is %i\n", sumfunc(x,y));
        
        
        system("pause");    
        return 0;
    }
    Obviously this doesn't work because using char doesn't make any sense.
    Last edited by quintenmater; 11-27-2010 at 10:45 AM.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    There's a couple of ways. chars are really just small ints and you can treat it as such. The other way would be to convert the character to its int equivalent by subtracting '0' from it.
    Code:
    int sumfunc(int x, char y)
    {
      z = x + y;
      return z;
    }
    Of course, using %c in the scanf() confuses things and will not give you the expected output.

    Your other option is to subtract '0' from y after reading it in and storing that result in something like and int.
    Code:
    scanf("%c", &y);
    int yInt = y - '0';
    Then you could pass yInt to your function. This works because the digits are laid our sequentially in the ASCII table. '0' is 48 decimal in the ASCII table. '1' is 49, '2' is 50, etc. Subtracting any single digit by '0' (48) will give you the actual number (e.g. '2' - '0' is 50 - 48 is 2).
    Last edited by itsme86; 11-26-2010 at 07:52 PM.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok, first thing, move the declaration of z into the top of the sum function. you should also move the declarations of x and y into your main function. There's no reason for them to be global.

    Yes you can sum an int and a char... A char is really just an 8 bit integer.

    You can add any two numbers where the product will fit into the lvalue's (left of the equal sign) type.

    The problem is that using scanf("%c", &y); y will be equivalent to the ascii value of the character you type, so if you type 0 you're gonna get 48... not 0 from scanf. If you use scanf("%d",y); it will be the true value... you should be able to enter any number from -128 to +127.

    When in doubt, make a simple test proggy like this...
    Code:
    int x = 0;
    int y = 50;
    char z = 100;
    
    x =  y + z;
    
    printf( "x = %d \n", x);

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    char is not required to be 8 bits. bits in char >= 8 as per standard.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    Quote Originally Posted by CommonTater View Post
    ...
    Yes you can sum an int and a char... A char is really just an 8 bit integer.
    ...
    Make that one byte. A char is guaranteed to be 1 byte, though a byte may not necessarily be 8 bits.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    29
    Thanks, guys. Got it working with the following code and also understand!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int x;
    char y;
    int z;
    
    int sumfunc(int x, char y){
         z= x + y;
         return z;
         }
         
    int main(void){
       
        printf("Please type an integer.\n");
        scanf("%i", &x);
        printf("Please type another integer.\n");
        scanf("%d", &y);
        printf("The sum of the two numbers is %i\n", sumfunc(x,y));
        
        
        system("pause");    
        return 0;
    }

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    29
    Oh, when I move the declarations out of global, the sum function doesn't work!

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    Change
    z= x + y;
    to
    return x + y;
    Plain and simple.

    Or make z local:
    int z = x + y;
    return z;

    Plain and simple. Why do you need globals?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quintenmater View Post
    Oh, when I move the declarations out of global, the sum function doesn't work!
    Try it like this...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int sumfunc(int x, char y){
        int z;
         z= x + y;
         return z;
         }
         
    int main(void){
       int x;
       char y;
    
        printf("Please type an integer.\n");
        scanf("%i", &x);
        printf("Please type another integer.\n");
        scanf("%d", &y);
        printf("The sum of the two numbers is %i\n", sumfunc(x,y));
        
        
        system("pause");    
        return 0;
    }

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    In my house
    Posts
    32
    the atoi function in the stdio header file can return an int from a char then you can just add it up. You could try something like this:

    Code:
    int sumfunc(int x, char y){
         z = x + atoi(y) /* returns the int value of y) */
         return z;
         }
    you can read up on it here: atoi - C++ Reference

  11. #11
    Registered User
    Join Date
    Nov 2010
    Location
    xian china
    Posts
    31
    Quote Originally Posted by Elysia View Post
    Change
    z= x + y;
    to
    return x + y;
    Plain and simple.

    Or make z local:
    int z = x + y;
    return z;

    Plain and simple. Why do you need globals?
    z can't be local, it is dangerous to return a local varible.

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,540
    > z can't be local, it is dangerous to return a local varible.
    Wow - talk about being dumb on your first post.

    The rule is "POINTER TO LOCAL" which is dangerous. Returning the value stored in a local is perfectly safe, because the return makes a copy.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  13. #13
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    the atoi function in the stdio header file can return an int from a char then you can just add it up. You could try something like this:
    What ?? Think before posting. It seems like you didn't even read the link you posted.

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    Quote Originally Posted by patrink View Post
    the atoi function in the stdio header file can return an int from a char then you can just add it up. You could try something like this:

    Code:
    int sumfunc(int x, char y){
         z = x + atoi(y) /* returns the int value of y) */
         return z;
         }
    you can read up on it here: atoi - C++ Reference
    atoi wants a string, not a char. Tsk.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Nov 2010
    Location
    xian china
    Posts
    31
    Quote Originally Posted by Salem View Post
    > z can't be local, it is dangerous to return a local varible.
    Wow - talk about being dumb on your first post.

    The rule is "POINTER TO LOCAL" which is dangerous. Returning the value stored in a local is perfectly safe, because the return makes a copy.
    got it...

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to combine these working parts??
    By transgalactic2 in forum C Programming
    Replies: 0
    Last Post: 02-01-2009, 07:19 AM
  2. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  3. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21