the small program dosen't execute

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aravind12345
    New Member
    • Jan 2014
    • 22

    #1

    the small program dosen't execute

    the problem is that the program dosen't execute and there are no errors and warnings. ( look at this link and you will
    know what i mean http://www.youtube.com/watch?v=lialD...ature=youtu.be ) please help me
    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    float s=0,t=0,d=0;
    printf("please enter a distence in kliometers");
    scanf("%f",d);
    printf("now enter the time taken in hours to travle that distence entered before");
    scanf("%f",t);
    s = d/t;
    printf("the speed is %f",s);
    getch();
    clrscr();
    }
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    main returns int, pretty much always. At best returning void is a compiler extension and not portable. Get out of the habit of using conio.h it is not a standard header and most modern systems do not have it.

    Correcting the return value of main (and adding a return statement) and removing lines 2, 12 and 13 all of which are not required for your program to function I get the following 2 warnings (line numbers corrected to match your posted source)

    test.c:7:1: warning: format '%f' expects type 'float *', but argument 2 has type 'double'

    test.c:9:1: warning: format '%f' expects type 'float *', but argument 2 has type 'double'

    Which hi-lights your error, you have missed the & off the parameters in scanf so instead of pointers you are passing uninitialised double (note that it is double due to the automatic promotion rules for undefined parameters in C) values and scanf then treats them as pointers and writes to some random place in memory causes (for me) a crash.

    Comment

    Working...