In C: How to print function argument
up vote
0
down vote
favorite
I want to print function argument in the printf command. Please help to suggest.
void printline(char ch, int len);
value(float, float, int);
main()
{
double amount;
printline('=', 30);
amount = value(500, 0.12, 5); // I want to print argument of function value. please help
printf("The total amount is: %f n", amount);
//printf("%ft%ft%dt%f n", 500, 0.12, 5, amount);
printline('=', 30);
_getch();
}
void printline(char ch, int len)
{
int i;
for (i = 0; i < len; i++)
printf("%c", ch);
printf("n");
}
value(float p, float r, int n)
{
int year;
float sum;
sum = p;
year = 1;
while (year <= 5)
{
sum = sum * (1 + r);
year = year + 1;
}
return(sum);
}
c
add a comment |
up vote
0
down vote
favorite
I want to print function argument in the printf command. Please help to suggest.
void printline(char ch, int len);
value(float, float, int);
main()
{
double amount;
printline('=', 30);
amount = value(500, 0.12, 5); // I want to print argument of function value. please help
printf("The total amount is: %f n", amount);
//printf("%ft%ft%dt%f n", 500, 0.12, 5, amount);
printline('=', 30);
_getch();
}
void printline(char ch, int len)
{
int i;
for (i = 0; i < len; i++)
printf("%c", ch);
printf("n");
}
value(float p, float r, int n)
{
int year;
float sum;
sum = p;
year = 1;
while (year <= 5)
{
sum = sum * (1 + r);
year = year + 1;
}
return(sum);
}
c
What errors are you getting? And what do you want to print?char
?, sequence ofchar
s?string
?
– wdc
Nov 11 at 8:41
@wdc, I am not getting any error. But it just prints like below: ============================== -867922223097294223219899068414767047150339070896408162281702553844529119294822519725532256957678500245617959157159789058525573856385606984030656440815664727951563690288278971226895615479699005629460285898096640.000000 0.000000 0 0.000000 ==============================
– patel3010
Nov 11 at 8:51
6
Please code in 21st Century C. Implicitint
in the function declaration and definition ofvalue()
is obsolete since C99. Ditto with the implicitint
return type formain()
.
– Jonathan Leffler
Nov 11 at 8:54
Also, please edit the output into the question where you can format it. Also, please read about how to create an MCVE (Minimal, Complete, and Verifiable example).
– Jonathan Leffler
Nov 11 at 8:55
2
This code is not valid in any standard (C89 does not allow//
comments)
– M.M
Nov 11 at 8:57
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I want to print function argument in the printf command. Please help to suggest.
void printline(char ch, int len);
value(float, float, int);
main()
{
double amount;
printline('=', 30);
amount = value(500, 0.12, 5); // I want to print argument of function value. please help
printf("The total amount is: %f n", amount);
//printf("%ft%ft%dt%f n", 500, 0.12, 5, amount);
printline('=', 30);
_getch();
}
void printline(char ch, int len)
{
int i;
for (i = 0; i < len; i++)
printf("%c", ch);
printf("n");
}
value(float p, float r, int n)
{
int year;
float sum;
sum = p;
year = 1;
while (year <= 5)
{
sum = sum * (1 + r);
year = year + 1;
}
return(sum);
}
c
I want to print function argument in the printf command. Please help to suggest.
void printline(char ch, int len);
value(float, float, int);
main()
{
double amount;
printline('=', 30);
amount = value(500, 0.12, 5); // I want to print argument of function value. please help
printf("The total amount is: %f n", amount);
//printf("%ft%ft%dt%f n", 500, 0.12, 5, amount);
printline('=', 30);
_getch();
}
void printline(char ch, int len)
{
int i;
for (i = 0; i < len; i++)
printf("%c", ch);
printf("n");
}
value(float p, float r, int n)
{
int year;
float sum;
sum = p;
year = 1;
while (year <= 5)
{
sum = sum * (1 + r);
year = year + 1;
}
return(sum);
}
c
c
edited Nov 11 at 8:53
Jonathan Leffler
555k886621014
555k886621014
asked Nov 11 at 8:37
patel3010
1
1
What errors are you getting? And what do you want to print?char
?, sequence ofchar
s?string
?
– wdc
Nov 11 at 8:41
@wdc, I am not getting any error. But it just prints like below: ============================== -867922223097294223219899068414767047150339070896408162281702553844529119294822519725532256957678500245617959157159789058525573856385606984030656440815664727951563690288278971226895615479699005629460285898096640.000000 0.000000 0 0.000000 ==============================
– patel3010
Nov 11 at 8:51
6
Please code in 21st Century C. Implicitint
in the function declaration and definition ofvalue()
is obsolete since C99. Ditto with the implicitint
return type formain()
.
– Jonathan Leffler
Nov 11 at 8:54
Also, please edit the output into the question where you can format it. Also, please read about how to create an MCVE (Minimal, Complete, and Verifiable example).
– Jonathan Leffler
Nov 11 at 8:55
2
This code is not valid in any standard (C89 does not allow//
comments)
– M.M
Nov 11 at 8:57
add a comment |
What errors are you getting? And what do you want to print?char
?, sequence ofchar
s?string
?
– wdc
Nov 11 at 8:41
@wdc, I am not getting any error. But it just prints like below: ============================== -867922223097294223219899068414767047150339070896408162281702553844529119294822519725532256957678500245617959157159789058525573856385606984030656440815664727951563690288278971226895615479699005629460285898096640.000000 0.000000 0 0.000000 ==============================
– patel3010
Nov 11 at 8:51
6
Please code in 21st Century C. Implicitint
in the function declaration and definition ofvalue()
is obsolete since C99. Ditto with the implicitint
return type formain()
.
– Jonathan Leffler
Nov 11 at 8:54
Also, please edit the output into the question where you can format it. Also, please read about how to create an MCVE (Minimal, Complete, and Verifiable example).
– Jonathan Leffler
Nov 11 at 8:55
2
This code is not valid in any standard (C89 does not allow//
comments)
– M.M
Nov 11 at 8:57
What errors are you getting? And what do you want to print?
char
?, sequence of char
s? string
?– wdc
Nov 11 at 8:41
What errors are you getting? And what do you want to print?
char
?, sequence of char
s? string
?– wdc
Nov 11 at 8:41
@wdc, I am not getting any error. But it just prints like below: ============================== -867922223097294223219899068414767047150339070896408162281702553844529119294822519725532256957678500245617959157159789058525573856385606984030656440815664727951563690288278971226895615479699005629460285898096640.000000 0.000000 0 0.000000 ==============================
– patel3010
Nov 11 at 8:51
@wdc, I am not getting any error. But it just prints like below: ============================== -867922223097294223219899068414767047150339070896408162281702553844529119294822519725532256957678500245617959157159789058525573856385606984030656440815664727951563690288278971226895615479699005629460285898096640.000000 0.000000 0 0.000000 ==============================
– patel3010
Nov 11 at 8:51
6
6
Please code in 21st Century C. Implicit
int
in the function declaration and definition of value()
is obsolete since C99. Ditto with the implicit int
return type for main()
.– Jonathan Leffler
Nov 11 at 8:54
Please code in 21st Century C. Implicit
int
in the function declaration and definition of value()
is obsolete since C99. Ditto with the implicit int
return type for main()
.– Jonathan Leffler
Nov 11 at 8:54
Also, please edit the output into the question where you can format it. Also, please read about how to create an MCVE (Minimal, Complete, and Verifiable example).
– Jonathan Leffler
Nov 11 at 8:55
Also, please edit the output into the question where you can format it. Also, please read about how to create an MCVE (Minimal, Complete, and Verifiable example).
– Jonathan Leffler
Nov 11 at 8:55
2
2
This code is not valid in any standard (C89 does not allow
//
comments)– M.M
Nov 11 at 8:57
This code is not valid in any standard (C89 does not allow
//
comments)– M.M
Nov 11 at 8:57
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
In your printline
function you only have one character as an argument. So there is no purpose in iterating through it. If you want to print a string
or array of characters you want to use char *
or char
and iterate through it. So your function printline
could look like this:
void printline(char *ch, int len)
{
int i;
for (i = 0; i<len; i++)
printf("%c", ch[i]);
printf("n");
}
just make sure that len
isn't bigger then the length of *ch
.
Even better solution, where you don't have to worry about the value of len
is to print the characters one by one until you come across the character indicating the end of an array.
void printline(char *ch)
{
int i;
for (i = 0; ch[i] != ''; ++i)
printf("%c", ch[i]);
printf("n");
}
Or even better
If the string is null terminated, use
printf("%s", ch)
. If the string is not null terminated but the length is supplied, useprintf("%.*s", len, ch)
. In both cases, there's no loop in the user code; the loop is buried inside theprintf()
function. Further, since there's a newline printed after the loops, useprintf("%sn", ch)
orprintf("%.*sn", len, ch)
and skip the extraprintf()
after the loop.
1
If the string is null terminated, useprintf("%s", ch)
. If the string is not null terminated but the length is supplied, useprintf("%.*s", len, ch)
. In both cases, there's no loop in the user code; the loop is buried inside theprintf()
function. Further, since there's a newline printed after the loops, useprintf("%sn", ch)
orprintf("%.*sn", len, ch)
and skip the extraprintf()
after the loop.
– Jonathan Leffler
Nov 11 at 8:57
add a comment |
up vote
0
down vote
value(500, 0.12, 5); // I want to print argument of function value. please help
You can add a printf to the beginning of the function:
value(float p, float r, int n)
{
printf("%s(%f, %f, %d)n", __func__, f, r, n);
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
In your printline
function you only have one character as an argument. So there is no purpose in iterating through it. If you want to print a string
or array of characters you want to use char *
or char
and iterate through it. So your function printline
could look like this:
void printline(char *ch, int len)
{
int i;
for (i = 0; i<len; i++)
printf("%c", ch[i]);
printf("n");
}
just make sure that len
isn't bigger then the length of *ch
.
Even better solution, where you don't have to worry about the value of len
is to print the characters one by one until you come across the character indicating the end of an array.
void printline(char *ch)
{
int i;
for (i = 0; ch[i] != ''; ++i)
printf("%c", ch[i]);
printf("n");
}
Or even better
If the string is null terminated, use
printf("%s", ch)
. If the string is not null terminated but the length is supplied, useprintf("%.*s", len, ch)
. In both cases, there's no loop in the user code; the loop is buried inside theprintf()
function. Further, since there's a newline printed after the loops, useprintf("%sn", ch)
orprintf("%.*sn", len, ch)
and skip the extraprintf()
after the loop.
1
If the string is null terminated, useprintf("%s", ch)
. If the string is not null terminated but the length is supplied, useprintf("%.*s", len, ch)
. In both cases, there's no loop in the user code; the loop is buried inside theprintf()
function. Further, since there's a newline printed after the loops, useprintf("%sn", ch)
orprintf("%.*sn", len, ch)
and skip the extraprintf()
after the loop.
– Jonathan Leffler
Nov 11 at 8:57
add a comment |
up vote
1
down vote
In your printline
function you only have one character as an argument. So there is no purpose in iterating through it. If you want to print a string
or array of characters you want to use char *
or char
and iterate through it. So your function printline
could look like this:
void printline(char *ch, int len)
{
int i;
for (i = 0; i<len; i++)
printf("%c", ch[i]);
printf("n");
}
just make sure that len
isn't bigger then the length of *ch
.
Even better solution, where you don't have to worry about the value of len
is to print the characters one by one until you come across the character indicating the end of an array.
void printline(char *ch)
{
int i;
for (i = 0; ch[i] != ''; ++i)
printf("%c", ch[i]);
printf("n");
}
Or even better
If the string is null terminated, use
printf("%s", ch)
. If the string is not null terminated but the length is supplied, useprintf("%.*s", len, ch)
. In both cases, there's no loop in the user code; the loop is buried inside theprintf()
function. Further, since there's a newline printed after the loops, useprintf("%sn", ch)
orprintf("%.*sn", len, ch)
and skip the extraprintf()
after the loop.
1
If the string is null terminated, useprintf("%s", ch)
. If the string is not null terminated but the length is supplied, useprintf("%.*s", len, ch)
. In both cases, there's no loop in the user code; the loop is buried inside theprintf()
function. Further, since there's a newline printed after the loops, useprintf("%sn", ch)
orprintf("%.*sn", len, ch)
and skip the extraprintf()
after the loop.
– Jonathan Leffler
Nov 11 at 8:57
add a comment |
up vote
1
down vote
up vote
1
down vote
In your printline
function you only have one character as an argument. So there is no purpose in iterating through it. If you want to print a string
or array of characters you want to use char *
or char
and iterate through it. So your function printline
could look like this:
void printline(char *ch, int len)
{
int i;
for (i = 0; i<len; i++)
printf("%c", ch[i]);
printf("n");
}
just make sure that len
isn't bigger then the length of *ch
.
Even better solution, where you don't have to worry about the value of len
is to print the characters one by one until you come across the character indicating the end of an array.
void printline(char *ch)
{
int i;
for (i = 0; ch[i] != ''; ++i)
printf("%c", ch[i]);
printf("n");
}
Or even better
If the string is null terminated, use
printf("%s", ch)
. If the string is not null terminated but the length is supplied, useprintf("%.*s", len, ch)
. In both cases, there's no loop in the user code; the loop is buried inside theprintf()
function. Further, since there's a newline printed after the loops, useprintf("%sn", ch)
orprintf("%.*sn", len, ch)
and skip the extraprintf()
after the loop.
In your printline
function you only have one character as an argument. So there is no purpose in iterating through it. If you want to print a string
or array of characters you want to use char *
or char
and iterate through it. So your function printline
could look like this:
void printline(char *ch, int len)
{
int i;
for (i = 0; i<len; i++)
printf("%c", ch[i]);
printf("n");
}
just make sure that len
isn't bigger then the length of *ch
.
Even better solution, where you don't have to worry about the value of len
is to print the characters one by one until you come across the character indicating the end of an array.
void printline(char *ch)
{
int i;
for (i = 0; ch[i] != ''; ++i)
printf("%c", ch[i]);
printf("n");
}
Or even better
If the string is null terminated, use
printf("%s", ch)
. If the string is not null terminated but the length is supplied, useprintf("%.*s", len, ch)
. In both cases, there's no loop in the user code; the loop is buried inside theprintf()
function. Further, since there's a newline printed after the loops, useprintf("%sn", ch)
orprintf("%.*sn", len, ch)
and skip the extraprintf()
after the loop.
edited Nov 11 at 9:10
Jonathan Leffler
555k886621014
555k886621014
answered Nov 11 at 8:48
wdc
1,0041719
1,0041719
1
If the string is null terminated, useprintf("%s", ch)
. If the string is not null terminated but the length is supplied, useprintf("%.*s", len, ch)
. In both cases, there's no loop in the user code; the loop is buried inside theprintf()
function. Further, since there's a newline printed after the loops, useprintf("%sn", ch)
orprintf("%.*sn", len, ch)
and skip the extraprintf()
after the loop.
– Jonathan Leffler
Nov 11 at 8:57
add a comment |
1
If the string is null terminated, useprintf("%s", ch)
. If the string is not null terminated but the length is supplied, useprintf("%.*s", len, ch)
. In both cases, there's no loop in the user code; the loop is buried inside theprintf()
function. Further, since there's a newline printed after the loops, useprintf("%sn", ch)
orprintf("%.*sn", len, ch)
and skip the extraprintf()
after the loop.
– Jonathan Leffler
Nov 11 at 8:57
1
1
If the string is null terminated, use
printf("%s", ch)
. If the string is not null terminated but the length is supplied, use printf("%.*s", len, ch)
. In both cases, there's no loop in the user code; the loop is buried inside the printf()
function. Further, since there's a newline printed after the loops, use printf("%sn", ch)
or printf("%.*sn", len, ch)
and skip the extra printf()
after the loop.– Jonathan Leffler
Nov 11 at 8:57
If the string is null terminated, use
printf("%s", ch)
. If the string is not null terminated but the length is supplied, use printf("%.*s", len, ch)
. In both cases, there's no loop in the user code; the loop is buried inside the printf()
function. Further, since there's a newline printed after the loops, use printf("%sn", ch)
or printf("%.*sn", len, ch)
and skip the extra printf()
after the loop.– Jonathan Leffler
Nov 11 at 8:57
add a comment |
up vote
0
down vote
value(500, 0.12, 5); // I want to print argument of function value. please help
You can add a printf to the beginning of the function:
value(float p, float r, int n)
{
printf("%s(%f, %f, %d)n", __func__, f, r, n);
add a comment |
up vote
0
down vote
value(500, 0.12, 5); // I want to print argument of function value. please help
You can add a printf to the beginning of the function:
value(float p, float r, int n)
{
printf("%s(%f, %f, %d)n", __func__, f, r, n);
add a comment |
up vote
0
down vote
up vote
0
down vote
value(500, 0.12, 5); // I want to print argument of function value. please help
You can add a printf to the beginning of the function:
value(float p, float r, int n)
{
printf("%s(%f, %f, %d)n", __func__, f, r, n);
value(500, 0.12, 5); // I want to print argument of function value. please help
You can add a printf to the beginning of the function:
value(float p, float r, int n)
{
printf("%s(%f, %f, %d)n", __func__, f, r, n);
answered Nov 11 at 8:47
Kamil Cuk
7,6471222
7,6471222
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247075%2fin-c-how-to-print-function-argument%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What errors are you getting? And what do you want to print?
char
?, sequence ofchar
s?string
?– wdc
Nov 11 at 8:41
@wdc, I am not getting any error. But it just prints like below: ============================== -867922223097294223219899068414767047150339070896408162281702553844529119294822519725532256957678500245617959157159789058525573856385606984030656440815664727951563690288278971226895615479699005629460285898096640.000000 0.000000 0 0.000000 ==============================
– patel3010
Nov 11 at 8:51
6
Please code in 21st Century C. Implicit
int
in the function declaration and definition ofvalue()
is obsolete since C99. Ditto with the implicitint
return type formain()
.– Jonathan Leffler
Nov 11 at 8:54
Also, please edit the output into the question where you can format it. Also, please read about how to create an MCVE (Minimal, Complete, and Verifiable example).
– Jonathan Leffler
Nov 11 at 8:55
2
This code is not valid in any standard (C89 does not allow
//
comments)– M.M
Nov 11 at 8:57