printf function


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers printf function
# 1  
Old 11-14-2011
printf function

Code:
[QUOTE]#include<stdio.h>

int counter;
int fibonacci(int n)
{
    counter += 1;
    if ( n <= 2 )
        return 1;
    else
        return fibonacci(n-1) + fibonacci(n-2);
}

int main(void)
{
    int i;
    int sum ;
    for( i = 1 ; i<= 10; i++)
    {
        counter = 0;
        sum = fibonacci(i);
        printf ( "fibonacci(%d)= %d, counter= %d\n",i,sum,counter );
    }
    return 0;
}

---------- Post updated at 12:29 AM ---------- Previous update was at 12:22 AM ----------

why the counter will be zero every times in the for loop when i modify the
code " printf ( "fibonacci(%d)= %d, counter= %d\n",i,sum,counter ) "
to "printf ( "fibonacci(%d)= %d, counter= %d\n",i,fibonacci(i),counter )" !

that is i want to call the fibonacci routing in the printf function!

---------- Post updated at 12:30 AM ---------- Previous update was at 12:29 AM ----------

fibonacci(1)= 1, counter= 1
fibonacci(2)= 1, counter= 1
fibonacci(3)= 2, counter= 3
fibonacci(4)= 3, counter= 5
fibonacci(5)= 5, counter= 9
fibonacci(6)= 8, counter= 15
fibonacci(7)= 13, counter= 25
fibonacci(8)= 21, counter= 41
fibonacci(9)= 34, counter= 67
fibonacci(10)= 55, counter= 109

---------- Post updated at 12:32 AM ---------- Previous update was at 12:30 AM ----------

when the fibonacii routing called in the printf function
the result of running is
fibonacci(1)= 1, counter= 0
fibonacci(2)= 1, counter= 0
fibonacci(3)= 2, counter= 0
fibonacci(4)= 3, counter= 0
fibonacci(5)= 5, counter= 0
fibonacci(6)= 8, counter= 0
fibonacci(7)= 13, counter= 0
fibonacci(8)= 21, counter= 0
fibonacci(9)= 34, counter= 0
fibonacci(10)= 55, counter= 0
# 2  
Old 11-14-2011
counter does not have scope in the fibonacci() function, it only is visible when the code runs
printf() if main().
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Function - Make your function return an exit status

Hi All, Good Day, seeking for your assistance on how to not perform my 2nd, 3rd,4th etc.. function if my 1st function is in else condition. #Body function1() { if then echo "exist" else echo "not exist" } #if not exist in function1 my all other function will not proceed.... (4 Replies)
Discussion started by: meister29
4 Replies

2. Shell Programming and Scripting

Will files, creaetd in one function of the same script will be recognized in another function?

Dear All. I have a script, which process files one by one. In the script I have two functions. one sftp files to different server the other from existing file create file with different name. My question is: Will sftp function recognize files names , which are created in another... (1 Reply)
Discussion started by: digioleg54
1 Replies

3. Programming

How access a specific memory portion through printf() function????

Hi friends, Hope everyone is doing well. Please have a look at this simple program, you will figure out what I want. #include <stdio.h> int main() { printf("Enter an integer!\n"); scanf( "%d", 134511890 ); // Valid address on my computer printf( "%d\n", ???? ); return 0; } ... (3 Replies)
Discussion started by: gabam
3 Replies

4. Programming

How to step in one function after the function be executed in gdb?

In gdb, I can call one function with command "call", but how can I step in the function? I don't want to restart the program, but the function had been executed, gdb will execute next statement, and I don't know how to recall the function. (4 Replies)
Discussion started by: 915086731
4 Replies

5. Programming

Internals of the printf function?

hey all, im a new programmer. i was wondering how you would go about writing the printf function yourself? it is my understanding that when you call printf you are calling an already written function and just providing an argument? if this is the case, is it possible to write that function... (8 Replies)
Discussion started by: Christian.B
8 Replies

6. Shell Programming and Scripting

SHELL SCRIPT Function Calling Another Function Please Help...

This is my function which is creating three variables based on counter & writing these variable to database by calling another function writeRecord but only one record is getting wrote in DB.... Please advise ASAP...:confused: function InsertFtg { FTGSTR="" echo "Saurabh is GREAT $#" let... (2 Replies)
Discussion started by: omkar.sonawane
2 Replies

7. Shell Programming and Scripting

Return a value from called function to the calling function

I have two scripts. script1.sh looks -------------------------------- #!/bin/bash display() { echo "Welcome to Unix" } display ----------------------------- Script2.sh #!/bin/bash sh script1.sh //simply calling script1.sh ------------------------------ (1 Reply)
Discussion started by: mvictorvijayan
1 Replies

8. Programming

why printf() function don't go work?

I use FreeBSD,and use signal,like follows: signal(SIGHUP,sig_hup); signal(SIGIO,sig_io); when I run call following code,it can run,but I find a puzzled question,it should print some information,such as printf("execute main()") will print execute main(),but in fact,printf fuction print... (2 Replies)
Discussion started by: konvalo
2 Replies

9. Shell Programming and Scripting

Passing global variable to a function which is called by another function

Hi , I have three funcions f1, f2 and f3 . f1 calls f2 and f2 calls f3 . I have a global variable "period" which i want to pass to f3 . Can i pass the variable directly in the definition of f3 ? Pls help . sars (4 Replies)
Discussion started by: sars
4 Replies

10. Shell Programming and Scripting

How to print a % within a printf() function using awk

Here is the code I'm using { printf("%11d %4.2f\% %4.2f\%\n", $1,$2,$3); } I want the output to look something like 1235415234 12.24% 52.46% Instead it looks something like 319203842 42.27\%4.2f\% How do I just print a "%" without awk or printf thinking I'm trying to do... (1 Reply)
Discussion started by: Awanka
1 Replies
Login or Register to Ask a Question