Thread: Adding two octal numbers from char[] C language

  1. #1
    Registered User
    Join Date
    Apr 2020
    Posts
    4

    Adding two octal numbers from char[] C language

    Hi, I'm on the end of my project and I have to add two octal numbers. I have got them in the char array and I'm stuck. Can you give some advice how to do this?
    I tried add 0 as a frist element (adding in octal, so need 0) of arrays and use atoi, but that gives me integers without this 0.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You have two numeric strings in octal and want to add them so as to produce a numeric string in octal? I'd suggest using strtol, and then finish off with sprintf (or if available, snprintf).
    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
    Join Date
    Apr 2020
    Posts
    4
    Hey maybe a little correction, arrays looks like num1="12345677", num2"12345677"

  4. #4
    Registered User
    Join Date
    Apr 2020
    Posts
    4
    "You have two numeric strings in octal and want to add them so as to produce a numeric string in octal?". Yeah, excatly.
    Okey, I will try, thanks a lot.

  5. #5
    Registered User
    Join Date
    Apr 2020
    Posts
    4
    Quote Originally Posted by laserlight View Post
    You have two numeric strings in octal and want to add them so as to produce a numeric string in octal? I'd suggest using strtol, and then finish off with sprintf (or if available, snprintf).
    THAT'S GOOD. )) Thanks man!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Searching octal numbers in string (Text).
    By bran12 in forum C Programming
    Replies: 1
    Last Post: 11-22-2015, 08:04 AM
  2. Replies: 2
    Last Post: 05-08-2015, 07:47 PM
  3. octal numbers
    By firefly in forum C++ Programming
    Replies: 4
    Last Post: 07-13-2005, 06:24 AM
  4. adding numbers
    By Allen_Buchannon in forum C++ Programming
    Replies: 1
    Last Post: 03-29-2004, 05:43 PM
  5. Converting an octal code to Char
    By winsonlee in forum C Programming
    Replies: 3
    Last Post: 03-20-2004, 05:54 PM

Tags for this Thread