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

271
Visualizações
Does size_t foo = 0; need a cast?

Looking at this answer and knowing that 0 is an octal constant:

For hexadecimal [constants [and octal according to the comments]], it is the first type the value can fit in: int, unsigned int, long, unsigned long, long long, unsigned long long

Therefore, I deduce this does not need a cast:

size_t foo = 0;

However, due to a strict MISRA-C lint tool, I get back a message about an illegal implicit type conversion - MISRA-C:2004 Rule 10.1.

Is my understanding wrong, or is the tool in error?

(NB: I've changed to size_t foo = 0U; as that is a lot simpler than arguing with QA, but I'd like to satisfy my own curiosity.)

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I'm not sure what you are trying to achieve, but...

  • 0 is of type signed int
  • 0U is of type unsigned int

size_t requires type size_t - and sizes are (usually) unsigned

So for strict compliance size_t foo = 0U; although it might be more correct to use size_t foo = (size_t)0;

As an aside, discussion as to whether 0 is decimal, octal or anything else is irrelevant... it is still zero.

--

Edit to add:

Although explicit conversion between signed and unsigned was deprecated in MISRA C:2004 (although this was widely deviated), for MISRA C:2012 Rule 10.3 explicitly allows a non-negative integer constant expression of essentially signed type may be assigned to an object of essentially unsigned type if its value can be represented by that type

This is a standard-ese way of saying you don't need the U

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