Hi everyone. I am an undergraduate student of mechanical engineer and i am trying to make a program that will convert a decimal number to a binary one and a hexadecimal one using c . The problem is that i am not familiar with c at all. I am only familiar with Basic and python. Anyway, doing some research in here i have managed to find this code

#include <stdio.h>

int main(void)
   
      {
   
      int hexa = 16, deci, quotient, ctr;
   
      printf("enter decimal number: ");
  
      scanf("%d",&deci);
   
      for (ctr = 1; ctr<=deci; ctr++)
      quotient = deci / hexa;
  
      printf("Equivalent in Hexadecimal is %d",quotient);
  
      printf("\n\n Try this for Hexadecimal: %X\n", deci);
       
  
      getchar();
  
      return 0;
  
      }

for decimal to hexadecimal conversion and this

#include <stdio.h>
       
      void dec2bin(long decimal, char *binary);
       
      int main()
      {
      long decimal;
      char binary[80];
       
      printf("\n\n Enter an integer value : ");
      scanf("%ld",&decimal);
      dec2bin(decimal,binary);
      printf("\n The binary value of %ld is %s \n",decimal,binary);
       
      getchar(); // trap enter
      getchar(); // wait
      return 0;
      }
       
      //
      // accepts a decimal integer and returns a binary coded string
      //
      void dec2bin(long decimal, char *binary)
      {
      int k = 0, n = 0;
      int neg_flag = 0;
      int remain;
      int old_decimal; // for test
      char temp[80];
       
      // take care of negative input
      if (decimal < 0)
      {
      decimal = -decimal;
      neg_flag = 1;
      }
      do
      {
      old_decimal = decimal; // for test
      remain = decimal % 2;
      // whittle down the decimal number
      decimal = decimal / 2;
      // this is a test to show the action
      printf("%d/2 = %d remainder = %d\n", old_decimal, decimal, remain);
      // converts digit 0 or 1 to character '0' or '1'
      temp[k++] = remain + '0';
      } while (decimal > 0);
       
      if (neg_flag)
      temp[k++] = '-'; // add - sign
      else
      temp[k++] = ' '; // space
       
      // reverse the spelling
      while (k >= 0)
      binary[n++] = temp[--k];
       
      binary[n-1] = 0; // end with NULL
      }

for decimal to binary conversion.
Now my problem is that i want to enhance this by adding two features
a)I want the user to be able to give the integer for conversion straight from the command line. I think that this is possible by using the argc and argv but i am not sure on how to do so.
b)Secondly i want the user to be able to to write the results in a txt file using again some kind of command from the terminal.
Any help or guidance would be much appreciated
thanks in advance

This is the simplest example of argc and argv:

int main(int argc, char *argv[])
{
   /*
   Print the number of command-line arguments, if the user didn't give
   any command-line args., the value of argc will be 1 which is the
   complete name and path of the program.
   */
   printf("Number of command-line arguments: %d \n", argc);
   if(argc == 1)
   {
      printf("You didn't specify any command-line args.\n");
   }

   // Print the first element of **argv which is the program's full path and name
   printf("Full path and name of the program: %s \n", argv[0]);

   // Print command-line args. if any:
   for(int n=1; n<argc; ++n)
   {
      printf("Command-line argument #%d: %s \n", n, argv[n]);
   }

   return 0;
}

This program outputs the number of command-line args. (argc), which is 1 by default(if the user didn't specify command-line args.).
Then it prints the full path and name of the program (argv[0]).
argv is an array of arrays (multi-dimensional array), in C a string is an array of characters, so argv is an array of strings.

Hope that helps.

thanks kim00000 i 'll give it a try soon

I have no idea what your first code is doing. The loop makes no sense, neither does the output.

As for command line parameters, see this

And for your first post:
Code tags -- fantastic! Thank you!
Code -- fair attempt at formatting the code. Check this out for more complete details.
Comments in code -- wonderful to finally see! Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.