ASHWHIT
12-31-2011, 01:20 PM
Hey Everybody,
I am new to all this and learning C from 'The C Programming Language' by Brian Kernighan which I picked up on Amazon. I am really enjoying it and often find myself loosing track of time.
I have had some experience with programming but nothing too serious, only the odd experiment with PHP to create some simple websites.
I wondered if someone could explain this for me, a beginner to understand.
#include <stdio.h>
int power(int m, int n);
/* test power function */
main()
{
int i;
for (i = 0; i < 10; ++i)
printf("%d %d %d\n", i, power(2,i), power(-3,i));
return 0;
}
/* power: raise base to n-th power; n >= 0 */
int power(int base, int n)
{
int i, p;
p = 1;
for (i = 1; i <= n; ++i)
p = p * base;
return p;
}
This is a extract from the book, could someone please explain this following line of code:
printf("%d %d %d\n", i, power(2,i), power(-3,i));
I understand that I am using the 'printf()' function to display the result of 'power()'. However, I don't under the stand the use of '%d' and how this effects the result. Is this like introducing integer results which 'printf()' can use within the function?
I have also seen variations of the above, such as '%6d', '%6.2' etc..
I tried a quick google search, but didn't really fully understand what it was they were trying to describe.
Thanks in Advance,
Ashley
I am new to all this and learning C from 'The C Programming Language' by Brian Kernighan which I picked up on Amazon. I am really enjoying it and often find myself loosing track of time.
I have had some experience with programming but nothing too serious, only the odd experiment with PHP to create some simple websites.
I wondered if someone could explain this for me, a beginner to understand.
#include <stdio.h>
int power(int m, int n);
/* test power function */
main()
{
int i;
for (i = 0; i < 10; ++i)
printf("%d %d %d\n", i, power(2,i), power(-3,i));
return 0;
}
/* power: raise base to n-th power; n >= 0 */
int power(int base, int n)
{
int i, p;
p = 1;
for (i = 1; i <= n; ++i)
p = p * base;
return p;
}
This is a extract from the book, could someone please explain this following line of code:
printf("%d %d %d\n", i, power(2,i), power(-3,i));
I understand that I am using the 'printf()' function to display the result of 'power()'. However, I don't under the stand the use of '%d' and how this effects the result. Is this like introducing integer results which 'printf()' can use within the function?
I have also seen variations of the above, such as '%6d', '%6.2' etc..
I tried a quick google search, but didn't really fully understand what it was they were trying to describe.
Thanks in Advance,
Ashley