CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Definition local variable question

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
stefsun



Joined: 23 May 2007
Posts: 22

View user's profile Send private message

Definition local variable question
PostPosted: Mon Mar 03, 2008 2:47 am     Reply with quote

Definition of some local variables, the compiler show
"A numeric expression must appear here"
when they are Defined as global variable ,they are not wrong, do not they can be defined as local variables?

Code:

#include <18F4523.h>                             // PICF4523 header file
#device ADC=12                                   // 12 bits ADC
#use delay(clock=10000000)                       // for 10Mhz crystal
#fuses XT, NOWDT, NOPROTECT, NOLVP               // for debug mode

#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, PARITY=N,BITS =8,stream=MYPC)


main()
{
   for(int i=0;i< 100;i++)
   printf("good");
   
}


the complier show
Quote:
*** Error 51 "G:\Microchip\123\123.C" Line 11(5,8): A numeric expression must appear here
*** Error 12 "G:\Microchip\123\123.C" Line 11(13,14): Undefined identifier i
*** Error 12 "G:\Microchip\123\123.C" Line 11(20,21): Undefined identifier i



when the 'i' is defined as global variable, no any error
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Mon Mar 03, 2008 3:07 am     Reply with quote

This is C not C++ or a mixture of.

You need to define your vars at the top of a block.

Code:

main {
  int i;  // local to main (I know // as a remark indicator comes from C++)
 
  for (i = 0; i < 100; i++) {
    printf("good");
  }

  // Now for a block within main
  {
     int n;  // local to {} block within main (cannot be seen in main)
     n = 10;
  }
}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group