Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 26th, 2010, 3:59 AM   #1
VanGogh
Newbie
 
Join Date: Apr 2010
Posts: 7
Rep Power: 0 VanGogh is on a distinguished road
Function 'printf' should have a prototype

im making my first steps in C programming,
and as i was trying to execute my first lines, it seems that i have a problem, and i couldnt find answers in the book.

i tried to run a simple calculation:



when i compiled, i got the following error:
"Function 'printf' should have a prototype" (as you can also see in the pic above).

can anyone please point me to the problem?
and please use simple language, i'm a beginner, as you probably noticed.

thanks in advance.
VG.
VanGogh is offline   Reply With Quote
Old Apr 26th, 2010, 5:19 AM   #2
Adak
Hobby Coder
 
Join Date: May 2006
Posts: 872
Rep Power: 6 Adak will become famous soon enough
Re: Function 'printf' should have a prototype

C is a language that is used on many different types of devices. Many don't have a standard display device.

So you need to tell your compiler that YOU DO have a stdio device by adding this line to the top of your program:

#include <stdio.h>

int main(void) {
  int i;

/* your code here */
  //fatal errors return 1 

  printf("\n\n\t\t\t    press enter when ready");
  i = getchar; ++i;    //holds the console window open 
                        //& stops a warning from TC
  return 0;
}

I added the above to my Turbo C editor file "noname.c", and saved it. Just cuts down on keyboarding for every new program I start.

C expects an integer to be returned from main. void main() may work, but it is never good C, in real life. Books use it because it saves words, but it is something to leave behind.

And welcome to the forum! (Please leave both ears firmly attached to your head)
Adak is offline   Reply With Quote
Old Apr 26th, 2010, 6:16 AM   #3
VanGogh
Newbie
 
Join Date: Apr 2010
Posts: 7
Rep Power: 0 VanGogh is on a distinguished road
Re: Function 'printf' should have a prototype

Hi Adak.

i'm afraid what you wrote at the code box is a bit too complicated for me at this point.

what i DID understand is that C has to be notified about the machine ability to display output, and the way to do it is by adding the line #include <stdio.h>.

so i added it, and now it looks like this:
#include <stdio.h>

main()
{
int A,B;
float C,D;

A=1000;
B=3;
C=8.5;

D=A*B*C/100;

printf("%f",D);
}

compiled it, and no errors were found, but there was 1 warning:
"Function should return a value"
absolutely no clue what it means, i'd happy if you could tell me what is it about...

2 more things, i hope it's not too much:
1. whats the diferent between main() and int main(void)?
(if its something im supposed to learn later on, then ill figure it out as ill get to it)
2. how do i create an executable file?

sorry for my ignorance, and thanks for your help.
p.s. im not an English speaker, so excuse my poor language.
VanGogh is offline   Reply With Quote
Old Apr 26th, 2010, 6:46 AM   #4
Smjprogrammer
Expert Programmer
 
Smjprogrammer's Avatar
 
Join Date: Jan 2010
Location: United States
Posts: 602
Rep Power: 3 Smjprogrammer is on a distinguished road
Re: Function 'printf' should have a prototype

I do not know anything about Turbo C++ IDE, but maybe this code will help.

c Syntax (Toggle Plain Text)
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5. int a = 1000, b = 3;
  6. float c = 8.5;
  7. printf ("%.1f\n", a * b * c / 100);
  8. }
Smjprogrammer is offline   Reply With Quote
Old Apr 26th, 2010, 7:28 AM   #5
Adak
Hobby Coder
 
Join Date: May 2006
Posts: 872
Rep Power: 6 Adak will become famous soon enough
Re: Function 'printf' should have a prototype

Quote:
1. whats the diferent between main() and int main(void)?
(if its something im supposed to learn later on, then ill figure it out as ill get to it)
2. how do i create an executable file?

sorry for my ignorance, and thanks for your help.
p.s. im not an English speaker, so excuse my poor language.
Every function includes a return type, except void functions - they return nothing (void).

In this line of code, the function scanf() will return the number of items that it has processed, and stored:

hasStored = scanf("%c %d %d", &mychar, &mynum1, &mynum2);

If hasStored equals anything less than three, then the input was not stored right, for some reason.

So hasStored is the return value of scanf(), and it can be checked to see if scanf() has worked properly.

Now main() is just another function. Yes, it will be a starting point for your programs, but still, it's just another function. It expects to return a value, to the operating system. That returned value, can be used to tell if your whole program ran normally, or had an error. (Not all errors will be detected, but some will be found).

Return of 0 is "normal", by general agreement, and anything else is an error of some kind. (not always, but usually).

With that in mind, Turbo C will give the warning "function should return a value", which is correct.

A program showing int main() should ALWAYS return an integer value. It is an error not to, in this case. A void main() function should be changed to int main(), but that warrants only a warning from Turbo C, not an error.

Your English is not a problem.
Adak is offline   Reply With Quote
Old Apr 26th, 2010, 7:33 AM   #6
Ancient Dragon
PFO God In Training

 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 3,549
Rep Power: 10 Ancient Dragon will become famous soon enough
Re: Function 'printf' should have a prototype

>>1. whats the diferent between main() and int main(void)?

There are only two acceptable ways to code that function.
(1) int main(), or int main(void)
(2) int main(int argc, char* argv[])

Many new students look at old books or old tutorials and see void main(), but that has never been allowed in the c or c++ standards. The function main() is required to return an integer.

>>2. how do i create an executable file?
Look at the menu you posted -- see that Compile option at the top???

Your best option is to not use that compiler at all because its just too damned old and uses long obsolete header files. You can get newer free compilers such as Code::Blocks or VC++ 2010 Express. And there are several others too.
__________________
PFO's FAQ is here
Forum Rules

Visit me at Ancient Dragon's Lair Includes family-oriended (G rated) arcade games. Create your own new articles, blogs, and picture albems.
Ancient Dragon is offline   Reply With Quote
Old Apr 26th, 2010, 8:30 AM   #7
VanGogh
Newbie
 
Join Date: Apr 2010
Posts: 7
Rep Power: 0 VanGogh is on a distinguished road
Re: Function 'printf' should have a prototype

Thank you all!
its been a great help.


somhow i suspected this compiler isn't exactly state of the art...

i'll start looking for a more current one.
VanGogh is offline   Reply With Quote
Old Apr 27th, 2010, 8:19 AM   #8
Dietrich
Expert Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 516
Rep Power: 8 Dietrich is on a distinguished road
Re: Function 'printf' should have a prototype

Thanks to Ancient Dragon for the info.

Very nice Code::Blocks download, installation and startup details are at:
http://www.cprogramming.com/code_blocks/

For my XP computer I Downloaded Windows binary
(this one comes with the MinGW open source C/C++ compilers):
codeblocks-8.02mingw-setup.exe

Great IDE, have to get used to using projects rather than single file names.

Here is VanGogh's little C program modified:
c Syntax (Toggle Plain Text)
  1. // a simple C program
  2. // tested with Code::Blocks 8.02 and MinGW compiler
  3.  
  4. #include <stdio.h>
  5.  
  6. int main()
  7. {
  8. int a, b;
  9. float c, d;
  10.  
  11. a = 1000;
  12. b = 3;
  13. c = 8.5;
  14.  
  15. d = a*b*c/100;
  16.  
  17. printf("%f", d); // 255.000000
  18.  
  19. getchar(); // optional console wait
  20. return 0;
  21. }
__________________
Write your bugs with C, inherit them with C++, rock 'em with Python

Last edited by Dietrich; Apr 27th, 2010 at 8:48 AM. Reason: code
Dietrich is offline   Reply With Quote
Old Apr 27th, 2010, 9:55 AM   #9
VanGogh
Newbie
 
Join Date: Apr 2010
Posts: 7
Rep Power: 0 VanGogh is on a distinguished road
Re: Function 'printf' should have a prototype

Thanks for that Dietrich.

tried Code::Blocks and got into even bigger problems with it.
then, I tried Microsoft Visual C++ 2008 (after hours of exhausting installation difficulties), and didnt know where to start (still dont).

wasted almost a whole day (seriously, a whole day...) on installing and uninstalling IDE's.
i just want to start and write some simple elementary C programs, is it supposed to be that complicated?


anyhow,
1. does Visual C++ have a built-in compiler, or should i download it separately?
2. does Visual C++ designed only for C++ programming, or is it also for C programming?

btw, i visited (more than a few times) and the link you suggested, i ran into some big issues with the Code::Blocks compiling process, couldnt find an answer even at Code::Blocks forums.

Thanks in advanced!
you're all very kind.
VanGogh is offline   Reply With Quote
Old Apr 27th, 2010, 10:37 AM   #10
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 8,367
Rep Power: 16 DaWei will become famous soon enoughDaWei will become famous soon enough
Re: Function 'printf' should have a prototype

Visual C++ has the compiler included and compiles both C and C++. I had no installation difficulties with either that or Code::Blocks. None.

You WILL have to learn to use the IDE but, at this stage, you'll probably find that preferable to using it from the command line or with makefiles.

Since almost any application will have more than one file (10s, 100s, 1000s), the concept of a project is used. Start a project. Add files (new or existing). If a single file, compile and run it. If multiple files, "build" the project. This will undertake to compile all files which have been changed since the last build. The link process is automatic, provided the compile(s) completed without error.

The IDE will know where to find the header and libraries you need for even "Hello, World." When you stray beyond the most commonly used libraries you may have to acquire more and tell the IDE where you have put them.
__________________
Contributor's Corner:
Politically Incorrect
DaWei on Pointers
Grumpy on C++ Exceptions
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
need help with function to insert text from file into array jot23 C 1 Apr 19th, 2010 3:09 PM
problem using function exquisite000 C 4 Oct 23rd, 2009 4:21 AM
Returning a value from a variable to the main function colt C 4 Jun 20th, 2009 3:26 PM
Assigning a class function as a callback function core8583 C++ 4 Jun 18th, 2008 7:20 PM
'function prototype'.... Konnor C++ 3 Sep 4th, 2005 6:19 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:56 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC