Hello world hello program?

As I start, I typed the following global hello program in Code :: Blocks -

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

Now I click "Build and Run" and "Hello world" is displayed on the output screen.

However, the book I am reading offers me to remove certain program elements in order to see what errors are occurring in the program.

I made 2 changes. Firstly, I removed \ n from the program. (The book says that without \ n there will be an error when starting the program). However, when I click Build and Run, the output screen gives me the same result as when there were no errors.

The second change I made was the removal of #include from the program. Even now, the same output is displayed on the output screen as in the absence of errors.

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

I am using the GNU GCC compiler.

EDIT: As I said, I added -wall, -wextra, -pedantic. Now when I click "Build and Run", he says that he cannot find -1-wall, -1-wextra and -1-pedantic, and the program does not start. How to fix it now?

+5
source share
7 answers

Case 1: Your book is wrong. Deleting \n will never cause any errors. \n means newline , which will print a new line after Hello World.

Case 2: Perhaps you are not creating the code again, because without enabling stdio (means standard input / output) you cannot call the printf() function if you use the newer C standards (C99, C11). Learn more about stdio.h .

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

 int printf(); 

means that it takes any number of arguments and returns int . But in C99, implicit slowdown has been removed. Therefore, most likely, your compiler does not encounter C99.

Look here , compile perfectly!

Learn more about implicit declarations in c .

EDIT: As stated in AnT's comment, by removing #include<stdio.h> , the printf call will "compile" in the C-C99 language version. However, the call will cause undefined behavior. Variadic functions (e.g. printf) must be prototyped before being called even in C89 / 90. Otherwise, the behavior is undefined.

+4
source
  • Your program already contains an error. Functions in modern C must be declared with an explicit return type. Your main() declared without a return type that has been illegal since C99.

  • There are various โ€œerrorsโ€. Some errors cause the compiler to issue a diagnostic message. Some errors simply make your program behave unpredictably at runtime (display undefined behavior). The latter may be more difficult to detect, since the โ€œunpredictableโ€ may look completely normal at first sight.

    In your case, removing #include <stdio.h> will result in a diagnostic message in the C99 compiler, but will result in simple undefined behavior in the C89 / 90 compiler. undefined behavior may still display the same screen as before.

+2
source

You see no problem deleting \ n because '\ n' is the newline character. So earlier your result was ** "Hello world

"** (new line) And now your output is " Hello World " That's why you do not see any difference.

0
source

The reason for not getting the error when deleting \n is because it is escape equence , which denotes a newline character. Adding or removing an escape sequence will not result in an error until another part of the code is affected. For example, if you remove only \ from \n , this will result in an error, since your " will be escaped as \" .

In the second case, either you do not create the code again before running it, or your IDE installs it yourself. Deleting the #include line will result in an error because your printf() function is declared in the stdio.h header file. Without a function declaration, calling the fucntion function will result in an error.


If you use any shell, it would be better to write the code in a text editor and compile it with gcc in the shell as:

 gcc filename.c 

The executable will be named a.out by default. You can read more about gcc with man gcc .

0
source

Case 2:

Without the printf() prototype in #include<stdio.h> many experts use the pre-C99 standard. This assumes that the function has an int printf(...) prototype, where all arguments, including the format, pass regular promotions that do not check the type. Since the code passed the expected format parameter and returned int , the code worked. If the code were printf(5.0) , it most likely compiled, but failed to execute.

0
source

Removing "\ n" from "Hello world \ n" will not lead to any errors, but may lead to a different result when executed. Depending on the operating system, it may not write anything until the next time that "\ n" is sent to stdout or until the output buffer is flushed.

0
source
 #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 
0
source

All Articles