When I run my program, no errors but returns to prompt, why?

Discussion in 'Mac Programming' started by cybrscot, Jan 18, 2011.

  1. cybrscot macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #1
    Hello, when I compile the following code in my gcc compiler I don't get any errors, but it returns to the prompt, why? Is that what we call a "crash". I originally got some errors, then fixed them, now it isn't showing the user the printf statement and allowing the user to input (scanf), it's just returning to the prompt. The following code is an example from my textbook, I have copied it exactly. Terminal looks Like this....

    Scott-Deans-MacBook-Pro:~ scottdean$ gcc ~/documents/legaldate.c
    Scott-Deans-MacBook-Pro:~ scottdean$ gcc ~/documents/legaldate.c
    Scott-Deans-MacBook-Pro:~ scottdean$


    Code:
    /*prints a date in legal form */
    
    #include <stdio.h>
    
    main ()
    {
    
    	int month, day, year ;
    	
    	printf ("Enter date (mm/dd/yy): ") ;
    	scanf ("%d /%d /%d", &month, &day, &year) ;
    	
    	printf ("Dated this %d", day) ;
    	switch (day) {
    		case 1: case 21: case 31:
    			printf ("st") ; break ;
    		case 2: case 22:
    			printf ("nd") ; break ;
    		case 3: case 23:
    			printf ("rd") ; break ;
    		default: printf ("th") ; break ;
    	}
    	printf("day of") ;
    	
    	switch (month) {
    		case 1: printf ("January") ;	break;
    		case 2: printf ("February") ;	break;
    		case 3: printf ("March") ;		break;
    		case 4: printf ("April") ; 		break;
    		case 5: printf ("May") ;		break;
    		case 6: printf ("June") ;		break;
    		case 7: printf ("July") ;		break;
    		case 8: printf ("August") ;		break;
    		case 9: printf ("September") ;	break;
    		case 10: printf ("October") ;	break;
    		case 11: printf ("November") ;	break;
    		case 12: printf ("December") ;	break;
    	}
    	
    	printf (", 19%.2d.\n", year) ;
    	return 0 ;
    	}
    	
    	
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    The compiler simply compiles the code producing an executable binary. It makes no attempt to execute that binary: you have to do that. So no, this is not a crash.
     
  3. cybrscot thread starter macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #3
    Nevermind, I'm sorry I just found my stupid mistake.. I haven't written any code for 5 days and I forgot to do the ./a.out step. My bad!!

    Thanks for reminding me!!
     
  4. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #4
    :) They only do what you tell them to do.

    For future reference, it's also helpful to keep the Terminal output in a CODE block, especially for formatted output like your code seems to generate.

    B
     
  5. cybrscot thread starter macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #5
    Will do, thanks! learning!!:confused:
     

Share This Page