Thread: macro program not working?!?!?!

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    1

    Exclamation macro program not working?!?!?!

    Hey, the program below returns value '190' everytime I run it... sum1 please help... thanks!
    Code:
    #define title "\n\n\t\t\t\tBIKE ACCESSORIES STORE\n"
    #define cal(x,y)	y+=x;				
    									  	
    int main(void)
    {
    	int a[10],sum=0,n,i;
    	clrscr();
    	printf(title);
    	printf("\n\t\t*********************************************************");
    	printf("\n\t\t\tENTER THE SALARIES PAID TO THE EMPLOYEES:\n");
    	for(i=0;i<5;i++)
    	{
    		scanf("%d",&a[i]);
    	}
    	printf("\n\n\t\tTOTAL EXPENDITURE ON SALARIES:");
    	printf("%d",sizeof a );
    	for(i=0;i<sizeof(a);i++)
    	{
    		cal(i,sum);
    	}
    	printf("\n\n\t\t%d",sum);
    	getch();
    	return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why are you reading five numbers:
    Code:
    	for(i=0;i<5;i++)
    	{
    		scanf("%d",&a[i]);
    	}
    And summing 10? ... or at least you think that's what you are doing.

    What does sizeof a give you?
    What does 0 + 1 + 2 ... + (sizeof a -1) equal?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > printf("%d",sizeof a );
    > for(i=0;i<sizeof(a);i++)
    Have you noticed yet that sizeof(a) isn't giving you 10 ?

    Or why you're creating a sum of i, rather than a sum of a[i]

    Or why the first and second loops don't have the same end condition?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The OP is probably assuming that sizeof(a) will yield the number of values read into the array a during the previous loop. That is not the case.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. macro not working.
    By sblu in forum C Programming
    Replies: 2
    Last Post: 05-12-2010, 11:43 AM
  2. macro program
    By Cpro in forum C++ Programming
    Replies: 7
    Last Post: 08-04-2008, 10:23 AM
  3. Macro Program
    By daspope in forum Windows Programming
    Replies: 5
    Last Post: 04-25-2004, 04:02 AM
  4. simple macro not working???
    By COBOL2C++ in forum C++ Programming
    Replies: 9
    Last Post: 09-10-2003, 01:43 PM
  5. Passing Keys (macro program)
    By Coder87C in forum C Programming
    Replies: 3
    Last Post: 07-08-2003, 06:54 PM