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

169
Visualizações
A beginner's hello world C program?

As I beginner, I typed the following hello world program on Code::Blocks -

#include<stdio.h>
main()
    {
        printf("Hello world \n");
    }

Now, I click on 'Build and Run', and the output screen shows 'Hello world'.

However, the book I am reading from, suggests me to remove certain elements of the program to see what errors occur in the program.

I made 2 changes. First, I removed \n from the program. (The book tells me that without \n, there will be an error running the program) However, when I click on 'Build and Run', the output screen gives me the same output it did when it was without any errors.

The second change I made was removing #include from the program. Even now, the output screen shows the same output it did when it was free from errors.

Why is this happening? Please tell me how to fix this?

The compiler I am using is GNU GCC compiler.

EDIT: As suggested, I added -wall, -wextra, -pedantic. Now, when I click on 'Build and Run', it says cannot find -1-wall, -1-wextra and -1-pedantic and the program doesn't run. How to fix this now?

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

0

Case 1: your book is wrong. Removing \n will never raise any error. \n means newline which will print a new line after Hello World.

Case 2: May be you are not building the code again, because without including the stdio (means standard input/output) you may not invoke printf() function if you use newer C standards (C99, C11). Read more about stdio.h.

Note that, in pre C99 standard if you remove the prototype (#include <stdio.h>) C will automatically provide an implicit declaration for a function. Which will look like this:

int printf();

means, it will take any number of arguments and return int. But in C99 implicit deceleration were removed. So most probably your compiler does not confront C99.

Take a look here, compile fine!

Read more about implicit declarations in c.

EDIT: As AnT mentioned in the comment, removing #include<stdio.h>, the call to printf will "compile" in pre-C99 version of language. However, the call will produce undefined behavior. Variadic functions (like printf) have to be declared with prototype before the call even in C89/90. Otherwise, the behavior is undefined.

over 4 years ago · Santiago Trujillo Relatório

0

  1. Your program already contains an error. Functions in modern C have to be declared with an explicitly specified return type. Your main() is declared without a return type, which has been illegal since C99.

  2. There are different kinds of "errors". Some errors cause compiler to issue a diagnostic message. Some errors simply make your program behave unpredictably at run time (exhibit undefined behavior). The latter might be harder to detect, since "unpredictable" might look perfectly fine on the first sight.

    In your case removing #include <stdio.h> will trigger a diagnostic message in C99-compiliant compiler, but will lead to mere undefined behavior in C89/90 compiler. Undefined behavior might still produce the same screen output as before.

over 4 years ago · Santiago Trujillo Relatório

0

You are not seeing any issue by removing \n because '\n' is a new line character. So previously your output was **"Hello World

"** (newline) And now your output is "Hello World" That's why you don't see any difference.

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