Thread: Embarassing, but necessary question

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    111

    Embarassing, but necessary question

    Sorry, this is basic, so I refuse to not understand it:

    Code:
    main() {
    	int number = 5;
    	printf("%i devided by two is %d\n", tall, devider(tall));	
    
    }
    
    double devider(int x) {
    	return x/2;
    }
    This gives the warning:
    Code:
    test.c:7: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘double’
    I don't understand this. I'm sending an in value to a function which returns a double. And the printf has %d for the returned double value.
    Still the compiler insists that %d expects an int variable?

    .

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    %d isn't for double - printf - C++ Reference

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    111
    Even more embarassing. Thanks :-)

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    You have a good compiler. What compiler is it?
    It's good habit to pay attention to compiler warnings.

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    where do you declare tall?


    His compiler is actually quite poor if that is the only warning he gets when he tried to compile that. He should get:

    Code:
    silly.c: In function ‘main’:
    silly.c:3: warning: incompatible implicit declaration of built-in function ‘printf’
    silly.c:3: error: ‘tall’ undeclared (first use in this function)
    silly.c:3: error: (Each undeclared identifier is reported only once
    silly.c:3: error: for each function it appears in.)
    silly.c: At top level:
    silly.c:7: error: conflicting types for ‘devider’
    silly.c:3: note: previous implicit declaration of ‘devider’ was here
    Even assuming he includes the necessary headers, it certainly should not compile without tall being declared.
    Last edited by KBriggs; 08-05-2010 at 09:18 AM.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Where is main's return type?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    178
    Indeed, %d is same as %i for integer integral values. Check your textbook to see what symbol you need to print a float or double value.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question bout my work
    By SirTalksAlots in forum C Programming
    Replies: 4
    Last Post: 07-18-2010, 03:23 PM
  2. A question about a question
    By hausburn in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2010, 05:24 AM
  3. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM