Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8789123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:18:53+00:00 2026-06-13T22:18:53+00:00

#include <stdlib.h> #include <stdio.h> #include <string.h> int myatoi(const char* string) { int i =

  • 0
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int myatoi(const char* string) {
  int i = 0;
  while (*string) {
    i = (i << 3) + (i<<1) + (*string -'0');
    string++;
  }
  return i;
}

void decimal2binary(char *decimal, int *binary) {
  decimal = malloc(sizeof(char) * 32);
  long int dec = myatoi(decimal);
  long int fraction;
  long int remainder;
  long int factor = 1;
  long int fractionfactor = .1;
  long int wholenum;
  long int bin;
  long int onechecker;
  wholenum = (int) dec;
  fraction = dec - wholenum;

  while (wholenum != 0 ) {
    remainder = wholenum % 2;  // get remainder
    bin = bin + remainder * factor;  // store the binary as you get remainder
    wholenum /= 2;  // divide by 2
    factor *= 10;  // times by 10 so it goes to the next digit
  }
  long int binaryfrac = 0;
  int i;
  for (i = 0; i < 10; i++) {
    fraction *= 2;  // times by two first
    onechecker = fraction;  // onechecker is for checking if greater than one
    binaryfrac += fractionfactor * onechecker;  // store into binary as you go
    if (onechecker == 1) {
      fraction -= onechecker;  // if greater than 1 subtract the 1
    }   
    fractionfactor /= 10;
  }

  bin += binaryfrac;
  *binary = bin;
  free(decimal);
}

int main(int argc, char **argv) {   
  char *data;
  data = malloc(sizeof(char) * 32);
  int datai = 1;
  if (argc != 4) {
    printf("invalid number of arguments\n");
    return 1;
  }
  if (strcmp(argv[1], "-d")) {  
    if (strcmp(argv[3], "-b")) {
      decimal2binary(argv[2], &datai);
      printf("output is : %d" , datai);
    } else {
      printf("invalid parameter");
    }
  } else {
    printf("invalid parameter");
  }
  free(data);
  return 0;
}

In this problem, myatoi works fine and the decimal2binary algorithm is correct, but every time I run the code it gives my output as 0. I do not know why. Is it a problem with pointers? I already set the address of variable data but the output still doesn’t change.

./dec2bin "-d" "23" "-b"
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T22:18:55+00:00Added an answer on June 13, 2026 at 10:18 pm

    The line:

    long int fractionfactor = .1;
    

    will set fractionfactor to 0 because the variable is defined as an integer. Try using a float or double instead.

    Similarly,

    long int dec = myatoi(decimal);
    

    stores an integer value, so wholenum is unnecessary.


    Instead of

    i = (i << 3) + (i<<1) + (*string -'0');
    

    the code will be much more readable as

    i = i * 10 + (*string - '0');
    

    and, with today’s optimizing compilers, both versions will likely generate the same object code. In general, especially when your code isn’t working, favor readability over optimization.


    fraction *= 2;  // times by two first
    

    Comments like this, that simply translate code to English, are unnecessary unless you’re using the language in an unusual way. You can assume the reader is familiar with the language; it’s far more helpful to explain your reasoning instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include <string.h> #include <stdlib.h> #include <stdio.h> int main(void) { unsigned char *stole; unsigned char
#include<stdlib.h> #include<string.h> #include<stdio.h> int pstrcmp( char **p,char **q) { return strcmp(*p,*q) ; } int
#include <stdio.h> #include <stdlib.h> #include <string.h> const char* getfield(char* line, int num) { const
#include <stdio.h> #include <stdlib.h> #include <string.h> void main() { typedef int (FuncPtr)(); char asmFunc[]
#include <stdio.h> #include <stdlib.h> #include <string.h> void recursie(int); int main(int argc, char **argv) {
#include<stdio.h> #include<stdlib.h> #include<ctype.h> #include<string.h> struct person *create_node(char *, char *,int); void addnode(char *,char *,int,struct
My code #include <stdio.h> #include <stdlib.h> #include <ctype.h> void getData(short int *number, char *string)
#include<stdio.h> #include<string.h> #include<stdlib.h> int main() { // int char str[40],ch; FILE*fp,*fp1,*fp2; fp=fopen(ide_input,w); fp1=fopen(error_log,w); fp2=fopen(lex_output,w);
#include<iostream> #include<stdlib.h> #include<string.h> #include<stdio.h> using namespace std; union type{ int a; char b; int
#include<stdio.h> #include<stdlib.h> char* re() { char *p = hello; return p; } int main()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.