Thread: What is the value of % in my code (I am a beginer)

  1. #1
    Registered User
    Join Date
    Jul 2017
    Posts
    2

    Post What is the value of % in my code (I am a beginer)

    Hi all,
    I've just started to learn C language, I've completed several commands by myself and stuck at the code as under,
    insert
    Code:
    #include <stdlib.h>
    int main(){
    int array[]={1,2,3,4,5};
    int factorial =1;
    int i;
    for(i=0;i<5;i++)
        factorial*=array[i];
    printf("5! is %d.\n", factorial);
    return 0;
    }
    the above code works perfectly, but whenever i tried to change %d to %f it gives me an error.

    also in the code under,

    Code:
    #include <stdlib.h>
    int main(){
    int a=1;
    float b=1.2;
    double c =1.21;
    float sum;
    sum =a+b+c;
    printf("Sum of 3 values is%f",sum);
    return 0;
    }
    Whenever i tried to put %d in place of %f it gives me an error. (I've attached the error screen shot)

    kindly help me understand why i need to put %f only and not any other alphabet?


    thanks


    - Hardip
    Attached Images Attached Images What is the value of % in my code (I am a beginer)-error-jpg 

  2. #2
    Registered User
    Join Date
    Apr 2017
    Posts
    80
    C library function printf() should help.

    Basically the percent character combo gets replaced with the variable that it corresponds to. The variables follows the format (the format string is what contains the percent character combo). The first variable that follows the format is the one that applies to the first percent character combo, the second the second, etc.

    Variables have different types (ints, single characters, floats, etc.). The character which follows the percent states (to/in the printf function) which type of variable to expect, to output.
    Last edited by BpB; 07-22-2017 at 03:06 AM.

  3. #3
    Registered User
    Join Date
    May 2015
    Posts
    56
    Hello,

    %d and %f are different number formats, integer versus float.

    printf - C++ Reference

    Regards

  4. #4
    Registered User
    Join Date
    Jul 2017
    Posts
    2
    Thanks all for the prompt reply. Now i get the whole point.

    Thanks once again.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. COM Beginer
    By arjunajay in forum Windows Programming
    Replies: 2
    Last Post: 06-01-2006, 09:11 AM
  2. beginer help
    By gtsreddy in forum C Programming
    Replies: 11
    Last Post: 11-24-2005, 09:42 AM
  3. OOP beginer help
    By kernel in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2004, 11:29 AM
  4. Very NEW Beginer, Going mad
    By chrismax2 in forum C++ Programming
    Replies: 24
    Last Post: 04-08-2004, 11:28 AM
  5. Beginer Help with gui
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 04-20-2002, 06:21 PM

Tags for this Thread