Bus Error in very simple C code. Any thoughts?

Discussion in 'Mac Programming' started by kdum8, Apr 28, 2009.

  1. kdum8 macrumors 6502a

    kdum8

    Joined:
    Sep 8, 2006
    Location:
    Tokyo, Japan
    #1
    Hi,

    I am still a C beginner so I may have missed something in this code, but I keep getting bus error when I enter a value. I have searched through the code carefully and can't find where any errors could be. Any thoughts?

    Code:
    #include <stdio.h>
    
    /*	This code illustrates the modulus operator. It takes the user inputted number of seconds
    	and converts them into hours, minutes, and seconds. Notice the "%" command on lines 22 & 23
    	which gives the remainder when the first operand is divided by the second operand. */
    	
    
    	/* Firstly define the constants to be used in the code */
    	
    	#define SECS_PER_MIN 60
    	#define SECS_PER_HOUR 3600
    	
    	unsigned seconds, minutes, hours, secs_left, mins_left;
    
    main()
    {
    	printf("Enter the number of seconds (< 65000): ");
    	scanf("%d, &seconds");
    	
    	hours = seconds/SECS_PER_HOUR;
    	minutes = seconds/SECS_PER_MIN;
    	mins_left = minutes % SECS_PER_MIN;
    	secs_left = seconds % SECS_PER_MIN;
    	
    	printf("%u seconds is equal to ", seconds);
    	printf("%u h, %u m, and %u s\n", hours, mins_left, secs_left);
    	
    	return 0;
    }
    
    
    I can't see any error in the code unless I am missing something. I am running this on a MBP using OS 10.5.6 in the terminal. I compiled using gcc.

    Any help much appreciated.

    :eek:
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    Code:
    scanf("%d, &seconds");
    should be
    Code:
    scanf("%d", &seconds);
     
  3. kdum8 thread starter macrumors 6502a

    kdum8

    Joined:
    Sep 8, 2006
    Location:
    Tokyo, Japan
    #3
    So it should. I feel really embarrassed now :eek:

    Thanks for your help. :)
     

Share This Page