Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

389
Visualizações
How to get parameters from the command line

I've got this code written in C that makes a decimal to binary conversion. How can I make it that it gets the parameter from the command line in linux? I know i have to use something like int main(int argc, char *argv[]) but i dont know how to implement it.

#include <stdio.h>
int main() 
{
    int a[10], decimal, i, j;
    printf("\nIntroduceti numarul decimal: ");
    scanf("%d", &decimal);
    for(i = 0; decimal > 0; i++)
    {
        a[i] = decimal % 2;
        decimal = decimal / 2;
    } 
    printf("\nNumarul binar este: ");
    for(j = i - 1; j >= 0; j--)  {
        printf("%d", a[j]);
    }
    printf("\n");
    return 0;
}
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You want this:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
  if (argc < 2)
  {
    printf("missing command line argument\n");
    return 1;
  }

  int decimal = (int)strtol(argv[1], NULL, 10);

  ...
}
  • you need a minimal understanding of strings.
  • The (int) cast is for making clear that we explicitely convert the long int returned by strtol to int which may or may not be a smaller type than long int, depending on your platform.
  • Documentation for strtol.
  • Explanation about argc and argv
over 4 years ago · Santiago Trujillo Relatório

0

Answer:

#include <stdio.h>
#include<stdlib.h>
int main(int argc, char *argv[]) 
{
    int a[10], decimal, i, j;
    decimal = atoi(argv[1]); 
    for(i = 0; decimal > 0; i++)
    {
        a[i] = decimal % 2;
        decimal = decimal / 2;
    } 
    printf("\nNumarul binar este: ");
    for(j = i - 1; j >= 0; j--)  {
        printf("%d", a[j]);
    }
    printf("\n");
    return 0;
}

Compile Image Open it

Here gcc is the c-compiler, and demo.c is the file name After your file gets successfully compiled then ./a.out is the command to run that build file, and append the command-line argument with it, as shown in the image. atoi(argv1) see argv[0] is the file name, argv1 is the argument, and atoi is used to convert string to int, as we can take only string input from the command line, and argc is the total number of argument user have given.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda