简体   繁体   中英

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?

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.

  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.

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.

The reason for not getting an error while removing \n is because it is an escape equence that denotes a newline character. Adding or removing escape sequence would not result in error until and unless other part of code is not affected. For example if you remove only \ from \n , this would result in error since your " would be escaped as \" .

For the second case, either you are not building the code again before running it or your IDE would be setting it by itself. Removing the #include line would result in error since your printf() function is declared in the stdio.h header file. Without the function declaration, calling a fucntion would result in an error.


If you are using any shell, it would be better if you write your code in a text editor and compile it using gcc in shell as:

gcc filename.c

The executable would be named by default as a.out . More about gcc can be read using man gcc .

Case 2:

Without the printf() prototype in #include<stdio.h> , many compliers use a pre-C99 standard. That assumes the function has the prototype of int printf(...) where all arguments, including the format go through the usual argument promotions with no type checking. Since code passed the expected format parameter and and int was returned, code worked. Had code been printf(5.0) , it likely would compile but crash on execution.

Removing the "\n" from the " Hello world \n" won't produce any errors, but it MAY produce a different result on execution. Depending on the operating system, it may write nothing until the next time a "\n" is sent to stdout, or until the output buffer flushes.

#include<Stdio.h> 
//this contains the function used to print which is printf(); also puts();
int main()//The the main function in code simply the one with highest importance
{//opening braces
printf("hello world\n");//this prints the word and \n enters a newline
puts("hello world");//does the exact same thing
return 0; //means if the program runs correctly then return the no zero to screen
}//closing braces
//anything that has those 2 forward slashes are comments and they aren't compiled

嗯,您的代码似乎没有任何错误,对于第二种情况,您确定要重新编译吗?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM