4

I have to read from standard input two numbers as strings, in C. How can I print their sum as a string too? I have:

char a[10],b[10]; printf("enter the two numbers"); scanf("%s%s",&a,&b); char sum[20]; sum=? 

A little tip,please? Thank you!

1
  • snprintf(sum,........); Commented Feb 13, 2013 at 18:43

5 Answers 5

3

Just do it like in elementary school

Let's say the input was "...69 + ...63"

9 + 3 = 2 and 1 to go 6 + 6 + 1 = 3 and 1 to go ... 
Sign up to request clarification or add additional context in comments.

Comments

2

Do this with programming just as you would do it on paper. Make small steps, and make explicit every little step that we do on paper without noticing.

 00111 --------- 12345 123456789 --------- 123469134 
  • Notice different lengths. You'll need separate vars to hold that info. You'll need to find out the two lengths - either use a standard function, or write your own: strings end with a binary 0.
  • Notice how we go backwards while we add. You'll have to maintain two indices to access the two strings (arrays of chars).
  • Notice how we carry over the high digit of the addition 5+9=14. You'll need to maintain that in another variable.

Use loops. After you've added, you will need to know the total length, and move it to the left edge. Because we don't know in advance what length we'll get, you'll start writing down the answer from the right edge of your sum array. Think, how and what can you improve about this.

Comments

2

Unless it is prohibited to do so, then I would approach as the following:

  • Convert the input string(s) to numbers. I would trystrtol().

  • Add the numbers

  • Convert the sum from number to string. I would trysnprintf().

Comments

1

chars are 8-bit binary numbers, just like smaller ints. We must interpret them to give them meaning as letters.

Your computer probably uses the ASCII standard. In ASCII, the char value representing the character 0 does not actually have the numerical value 0. It's 48. Fortunately, the numbers are all consecutive, so 1 is 49, etc.

char zero = '0'; printf("%d\n", zero); char one = '1'; printf("%d\n", one); > 48 > 49 

(Note that the %d format flag in printf means "interpret as an integer".)

Since chars are numbers, we can do math with them.

char one = '1'; int one_num = one - '0'; printf("%d\n", one_num); > 1 

Now, you can implement the addition with decimal digits and carrying just like you'd do on paper.

Comments

1

Convert the strings into numbers using atoi, then add them together, and then print the sum using a print function like printf. See this

11 Comments

Better strtol() than the function you suggested. strtol() is well behaved in presence of 'stupid' input.
this is one of the problems that i need to prepare for school. and it is not allowed to convert them into integer . thanks a lot!
Oh, so you're asking us to do your homework. That's horrible. If this is for school and you can't use atoi, they probably want to teach you about ascii encodings, look into that.
@Madalina Well, you have to convert it to numeric value somewhere, unless you use the brute force approach.
@Deidara-senpai you can add digit by digit.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.