Results 1 to 3 of 3

Thread: Problem understanding function in C

  1. #1
    Join Date
    Apr 2009
    Posts
    22

    Problem understanding function in C

    hello,
    I have problem with a function declaration, in following program the main section initializes k to 0. The function f is known as k <4. As increments each time, this function will be performed 4 times. My only problem is that I have a printf ( "% ld", i) at the beginning of the loop and the PC gives me the value 0 so the loop should never execute (j <= i)!. Yet it runs 4 times.

    Code:
    # include <stdio.h> 
    # include <stdlib.h> 
    float f (int i) 
    ( 
    
    
    int j = 1, k = 1; 
    
    while (j <= i) 
    ( 
    
    k = k * j; 
    j + +; 
    ) 
    return k; 
    ) 
    int main (int argc, char * argv) 
    (Int i; 
    int k = 0; 
    do 
    ( 
    printf ( "f (% i) =% f \ n", k, f (k)); 
    k + +; 
    ) 
    while (k <4); 
    
    
    system ( "PAUSE"); 
    return 0; 
    )

  2. #2
    Join Date
    May 2008
    Posts
    2,012

    Re: Problem understanding function in C

    Code:
     printf ( "f (% i) =% f \ n", k, f (k));
    or it Should be:
    Code:
    printf ( "f (% Id) =% Id \ n", i, k, f (k));

  3. #3
    Join Date
    May 2008
    Posts
    2,389

    Re: Problem understanding function in C

    The function f is known as k <4. As increments each time, this function will be performed 4 times. My only problem is that I have a printf ( "% ld", i) at the beginning of the loop and the PC gives me the value 0 so the loop should never execute (j <= i)!. Yet it runs 4 times.

    your function does not return a decimal number in integer that should be used, especially as you refer k, which is an integer variable. For your function by itself, at first sight of the factorial function.

Similar Threads

  1. Help Understanding IP
    By guaro in forum Networking & Security
    Replies: 2
    Last Post: 07-07-2010, 11:04 AM
  2. Need help understanding AMD CPU's and compatibility
    By Doshi1 in forum Monitor & Video Cards
    Replies: 3
    Last Post: 04-06-2010, 01:31 PM
  3. Umask understanding
    By Edwards in forum Operating Systems
    Replies: 4
    Last Post: 24-04-2010, 03:27 AM
  4. Problem with wildcard function
    By ASHER in forum Software Development
    Replies: 4
    Last Post: 16-02-2010, 02:57 PM
  5. Problem in a function pointer
    By Rilex in forum Software Development
    Replies: 3
    Last Post: 23-05-2009, 10:21 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,717,385,315.31680 seconds with 16 queries