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);
}









share|improve this question
























  • What errors are you getting? And what do you want to print? char?, sequence of chars? 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 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






  • 2




    This code is not valid in any standard (C89 does not allow // comments)
    – M.M
    Nov 11 at 8:57















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);
}









share|improve this question
























  • What errors are you getting? And what do you want to print? char?, sequence of chars? 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 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






  • 2




    This code is not valid in any standard (C89 does not allow // comments)
    – M.M
    Nov 11 at 8:57













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);
}









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 of chars? 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 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






  • 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 chars? 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 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






  • 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 chars? string?
– wdc
Nov 11 at 8:41






What errors are you getting? And what do you want to print? char?, sequence of chars? 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












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, 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.







share|improve this answer



















  • 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




















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);





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    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

























    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, 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.







    share|improve this answer



















    • 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

















    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, 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.







    share|improve this answer



















    • 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















    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, 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.







    share|improve this answer














    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, 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.








    share|improve this answer














    share|improve this answer



    share|improve this answer








    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, 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
















    • 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










    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














    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);





    share|improve this answer

























      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);





      share|improve this answer























        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);





        share|improve this answer












        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);






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 8:47









        Kamil Cuk

        7,6471222




        7,6471222






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Bressuire

            Vorschmack

            Quarantine