5

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?

mazhar islam
  • 5,561
  • 3
  • 20
  • 41
user52976
  • 59
  • 4
  • 7
    1st. throw the book. There's no chance removing the '\n' will generate an error. 2nd. It's been a while, but somewhere in the back of my mind lies an idea that `printf` can work without includes – Amit Jul 02 '15 at 18:30
  • 1
    please tell me the name of the book. – Gaurav Sehgal Jul 02 '15 at 18:31
  • 1
    "it was free from errors" - but was it free from *warnings* ? Add `-Wall -Wextra -pedantic` to your compiler flags. In the latter case you showed, without a prior decl, C will (non)helpfully assume a function returns `int` and takes variable parameters. i.e. `int printf();`. And given a big enough warning-hammer, will tell you it did so. – WhozCraig Jul 02 '15 at 18:31
  • @Amit: Not in standard C. In standard C language `printf` never "worked" without being pre-declared with a prototype. `printf` is a variadic function. Even in the very first C standard (which allowed calling undeclared functions), an attempt to call a variadic function without declaring it first triggered undefined behavior. Which means that you have to either include `` or at least manually provide a prototype for `printf`. – AnT stands with Russia Jul 02 '15 at 19:11
  • @user52976: Formally, your program already contains an error. In modern C plain `main()` is illegal. The explicit return type is required. It should be `int main()`. – AnT stands with Russia Jul 02 '15 at 19:16
  • No, no, no. It's not `-wall, -wextra, -pedantic`, it is `-Wall -Wextra -pedantic`. The `-Wxyz` means compile `With xyz`. Above that means `With all` `With extra` and `pendantic` warnings enabled. (don't ask me why there is no `-W` before `pedantic`...) – David C. Rankin Jul 02 '15 at 19:40
  • I added a downvote for the cross-posting. – Martin James Jul 02 '15 at 20:22
  • For all the pedantics .... `-pedantic` doesn't really make sense without a `-std` option. I usually use `gcc -std=c99 -pedantic -Wall -Wextra filename.c`. Not using a `-std` option is the same as using `-std=gnu89` and `pedantic` and `gnu89` make no sense together :-) – pmg Jul 02 '15 at 21:23
  • @DavidC.Rankin "(don't ask me why there is no `-W` before `pedantic`...)" but there can be! `-Wpedantic` works too. anyway, glad someone pointed out the case typos. one does not simply put any capitalisation into *n?x command-line tools. – underscore_d Aug 02 '16 at 21:44

8 Answers8

5

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.

Community
  • 1
  • 1
mazhar islam
  • 5,561
  • 3
  • 20
  • 41
  • If you remove the prototype, the call to `printf` will formally "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. – AnT stands with Russia Jul 02 '15 at 19:15
  • By default, GCC doesn't use C99, but it can be enabled with the compiler flag `-std=c99` – Kookerus Jul 02 '15 at 22:26
2
  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.

Community
  • 1
  • 1
AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • Though it might be illegal for modern standards to declare main() without a return type, there's maximal a warning with gcc, even with `-std=c99 -pedantic `, when you declare main() without the int. In classic c, a function without a return type automatically assumed int as the return type. Actually its hard to write errors into the hello_world. Removing the `;` or one or both of the `{}` or `()` will be an error. Or mistyping main or printf. – ikrabbe Jul 02 '15 at 21:34
  • @ikrabbe: GCC is known to be very permissive in that regard. It even allows skipping that `int` in C++ code... Anyway, GCC option for reporting constraint violations (i.e. errors) as actual compile errors is `-pedantic-errors`. – AnT stands with Russia Jul 02 '15 at 22:04
1

You can use this command to write hello world

#include <stdio.h>

    int main()
    {
        printf("hello world");
        return 0;
    }

This code consists of these commands:

‍‍#include <stdio.h>‍: This command is a preprocessor that calls the stdio.h file and its functions such as printf can be used Of course, you can see the exact functions in this file through this link https://cplusplus.com/reference/cstdio/

int main(): In fact, this command is read by the compiler and calls the main function, which is the default in the compiler

printf("hello world"): The printf function means (print format) and is used to print text or numbers. They usually use the variable % to display to the user, for example %d and %i for variables that are integers and %s. Used for string variables

return 0: This syntax is used to return to the function and only works in functions that are not output variables such as void, such as int and float, or string, char so return 0 should be used for int main() function

cc [file]

is enough to compile it. In [file], you only write the name and extension of the file you want to compile, for example name file hi.c so cc hi.c and run this codes in linux with ./a.out command.

cunknown
  • 29
  • 6
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.

Silvia Doomra
  • 947
  • 8
  • 16
0

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.

Rakholiya Jenish
  • 3,165
  • 18
  • 28
0

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.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
0

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.

FredK
  • 4,094
  • 1
  • 9
  • 11
0
#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