#(C lang) %d vs %f

25 messages · Page 1 of 1 (latest)

silent bane
#

please tell the work of %d and %f
i m little bit confused in functions of %d and %f bcz
code run properly when i tried in college in turbo compiler
vs
code run properly when i tried in home in vs code

difference in use of %f and %d in both code

//tried in home in vs code 

#include<stdio.h>
#include<conio.h>
int main() {

    int age , sum ;

    printf("enter age") ;
    scanf("%d" , &age ) ;

    printf("age %d" , age ) ;

    return 0 ;
} 
//tried in clg in turbo 

#include<stdio.h>
#include<conio.h>
int main() {

    int age , sum ;

    printf("enter age") ;
    scanf("%f" , &age ) ;

    printf("age %d" , age ) ;

    return 0 ;
}```

please reply fast, i have 3 days holiday  and want to complete c ASAP
waxen sandalBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

wild void
#

"%d" is used to tell scanf f to read integers (int)
"%f" is used to tell scanf to read real decimal number (float/double)

silent bane
#

?

wild void
#

;compile

1234
432.463
#include<stdio.h>

int main() {

    int age , sum ;
    float height;

    printf("enter age\n") ;
    scanf("%d" , &age ) ;
    printf("age %d\n" , age ) ;

    printf("enter height\n") ;
    scanf("%f" , &height ) ;
    printf("height %f\n" , height ) ;

    return 0 ;
}
lean ingotBOT
#
Program Output
enter age
age 1234
enter height
height 432.463013
wild void
#

also it is adviced to use %lf and double, instead of %f and float
More precision

#

(lf stands for long float)

silent bane
#

bcz 6 is also 6.0 somewhere

wild void
#

6 is an int and uses %d
6.0 is a double and uses %lf

silent bane
silent bane
#

then what i have to use ?

#

%char

wild void
#

%s

#

/cref printf

silent bane
clear pythonBOT
silent bane
# clear python

um little bit confusing
umm leave it I will learn it with my lessons ig then I will understand it more accurately

silent bane
#

!solved

waxen sandalBOT
#

Thank you and let us know if you have any more questions!