2

I am very very beginner in C programming. I am using turbo C++ 4.0, I also don't know whether it is open source or not. But I've downloaded this from internet somewhere. I've written the very simple program as following :

main()
{
printf("I see, I remember")
}

And the compiler shows the following error :
function printf should have a prototype
And the following warning : Function should return a value.

I am referring the book Programming in ANSI C by E. Balaguruswamy. In the book, this is the first sample program. And I just cannot run the very first program. So, please help.


20
  • 4
    First thing: Get rid of this prehistorical compiler and switch to a modern one. Commented Aug 9, 2016 at 16:03
  • 2
    #include <stdio.h> Commented Aug 9, 2016 at 16:03
  • 2
    Error 1: missing #include <stdio.h>. Error 2: int main(). Error 2: missing ; after printf(). Commented Aug 9, 2016 at 16:03
  • 1
    @noob gcc is allowing many different non-ANSI-C things. Commented Aug 9, 2016 at 16:06
  • 2
    @noob: That's implementation-defined. There is absolutely no reason not to write int main(), int main(void) or int main(int argc, char **argv), which will all work portably on any standard C-compiler. Commented Aug 9, 2016 at 16:07

3 Answers 3

4

First of all, Turbo C++ is a very old and discontinued development environment which does not support the latest C programming language standards. You'd be better off using MinGW or Cygwin instead.

Secondly, the code pasted here is missing a few things; either you didn't copy it correctly, or the book you're using is not well-written. Here's what the code should look like using modern C:

#include <stdio.h>

int main( void )
{
  printf( "I see, I remember\n" );
}

As of the C99 language standard, implicit function declarations are no longer allowed; the compiler will no longer assume that a function returns int if the type isn't specified.

Also, you should not leave the parameter list for main empty; in this case, we're specifying that main takes no arguments using the void keyword1.

printf writes to standard output, and standard output is usually line buffered, meaning the output won't appear on your console unless you fill the buffer or send a newline character ('\n').

Also as of C99, you don't need an explicit return from main, even though it's typed to return an integer value; by default, it will return 0 to the runtime environment when the program finishes executing.


  1. main is unique in that it can either take no arguments (int main( void )) or two arguments (int main( int argc, char **argv )) for processing command-line parameters, and implementations may provide additional versions.

Sign up to request clarification or add additional context in comments.

5 Comments

I would upvote this, but you put spaces on the inside of parentheses. When you put spaces on the inside of parentheses you make the baby Jesus cry. (And you did it inconsistently, which is even worse.)
Making the baby Jesus cry once is better than doing it twice, so +1
@zwol - made it consistent. >:-)
@zwol: Blame my eyes; I have an easier time reading code formatted this way. But I realize it's not a popular convention.
3

Since you don't have the proper #include <stdio.h> your compiler thinks that printf is a function that should be declared somewhere, hence this:

function printf should have a prototype

also, you forgot ; at the end of the line

Comments

1

You are missing a semicolon(;) here. In C, every statement ends with a semicolon. You are also missing a newline (\n) at the end of the string to be printed. Your printf statement should look like this:

printf("I see, I remember\n");

And about the warning. Warnings are not any error, but are used to show that your code can show undefined behavior.

Use int main() as function name. Here, int is the return type of a function. int before main() means it means the function returns an integer.

So, you might wanna add return 0; at the end of your program as main function expects an int to return. The return 0; statement tells that your program has successfully completed the execution.

And also you are missing a header file #include<stdio.h> from which your printf() function definition comes. If you don't include header files, your compiler thinks that printf() is a function that should be declared somewhere.

Your code should look something like this:

#include<stdio.h>
int main()
{
    printf("I see, I remember\n");
    return 0;
}

I hope this clears your doubts.

10 Comments

void main() is implementation-defined at best.
@VatsalSura: main is a special case in that you don't have to explicitly write return 0; to, well, return zero. C11 draft standard n1570: 5.1.2.2.3 Program termination 1 If the return type of the main function is a type compatible with int, a return from the initial call to the main function is equivalent to calling the exit function with the value returned by the main function as its argument; 11) reaching the } that terminates the main function returns a value of 0. This is not to say that you can't or shouldn't write return 0;, just that you don't have to.
@zwol But he is using Turbo C++ to write programs. And Turbo C++ doesn't hold the output window. So he won't be able to see the output if he doesn't write it.
@VatsalSura, it's not so much about what you know, as about what you implied. Pointing out that warnings are not errors leaves the impression that it might be ok for the OP to ignore them. That is inappropriate for a beginner.
@VatsalSura He should stop using Turbo C++ anyway, as discussed in other answers, so that's irrelevant.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.