Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
March 28, 2024, 11:08:25 23:08


Login with username, password and session length


Pages: [1]
Print
Author Topic: Why does this statement produce compiler error?  (Read 3226 times)
0 Members and 1 Guest are viewing this topic.
LithiumOverdosE
Senior Member
****
Offline Offline

Posts: 350

Thank You
-Given: 374
-Receive: 568


« on: August 07, 2008, 07:11:33 19:11 »

I'm not sure why but this simple code snippet at the line where "char count;" is declared produce following error in CCS: "A numeric expression must appear here."

Code:
#define LCD_ENABLE_PIN PIN_D0
#define LCD_RS_PIN PIN_D1
#define LCD_RW_PIN PIN_D2
#define LCD_TYPE 2
#include <lcd.c>

void main()
{
   set_tris_b(0b00001111);
   
   lcd_init();

   lcd_putc("\fTest...\n");
   lcd_putc("Another one...");
   
   delay_ms(2000);
   
   lcd_putc("\f2 Seconds Later\n");
   lcd_putc("LED high");
   
   output_high(pin_b0);
   
   delay_ms(3000);
   
   lcd_putc("\fN3 Seconds Later\n");
   lcd_putc("LED low");
   

   char count;

         
       for (count = 0; count < 3; count++)
         {
            output_b(0b00000000);
            delay_ms(500);
            output_b(0b11111111);
            delay_ms(500);
         }
         
       
       output_b(0b00000000);
       lcd_putc("\f");
       
   

}


Any ideas? I mean it's just the most standard declaration one could think of so I'm a bit perplexed. Huh
« Last Edit: August 07, 2008, 07:12:29 19:12 by LithiumOverdosE » Logged
spasbyspas
Active Member
***
Offline Offline

Posts: 169

Thank You
-Given: 718
-Receive: 364


« Reply #1 on: August 07, 2008, 07:27:47 19:27 »

Ok,  Man! This "char count;", must be move to under "void main()" function line exemple:
void main()
{
   char count;
   set_tris_b(0b00001111);

   ...



}
Logged
LithiumOverdosE
Senior Member
****
Offline Offline

Posts: 350

Thank You
-Given: 374
-Receive: 568


« Reply #2 on: August 07, 2008, 07:38:54 19:38 »

@spasbyspas

Thx m8!  Smiley That helped. I wondered why is that so and I realized that C doesn't allow variable declaration except at the very beginning of the function body. C++ on the other hand allows for variable declarations at any point in the function.

I guess I'm so very used to C++ that I really ought to check out ANSI C standard more often.  Roll Eyes
Logged
spasbyspas
Active Member
***
Offline Offline

Posts: 169

Thank You
-Given: 718
-Receive: 364


« Reply #3 on: August 07, 2008, 07:41:51 19:41 »

Yes, yes in C++ is diffrent Wink!
Best regarts!
SpasBySpas
Logged
cluoiset
Newbie
*
Offline Offline

Posts: 9

Thank You
-Given: 0
-Receive: 1


« Reply #4 on: August 07, 2008, 08:58:01 20:58 »

Hi!

I'm just wondering. Why do you have to put it in the main function?
Isn't it better to put it before the main?
Does the compiler produce a different code?

Cluoiset
Logged
LithiumOverdosE
Senior Member
****
Offline Offline

Posts: 350

Thank You
-Given: 374
-Receive: 568


« Reply #5 on: August 09, 2008, 08:41:48 20:41 »

@cuoiset

You mean something like a global variable? I don't really understand your question.
Logged
orange
Active Member
***
Offline Offline

Posts: 126

Thank You
-Given: 702
-Receive: 204


« Reply #6 on: August 10, 2008, 09:45:02 09:45 »

I think you didn't initialize the value of count. Try
char count = 0;
Logged
LithiumOverdosE
Senior Member
****
Offline Offline

Posts: 350

Thank You
-Given: 374
-Receive: 568


« Reply #7 on: August 10, 2008, 05:37:21 17:37 »

@orange

Thx for the reply but as I said ANSI C only allows for variables to be declared at the beginning of the function body while you can initialize variable at any further point in the function. C++ however allows for variable declaration at any point in the function body.
Logged
Pages: [1]
Print
Jump to:  


DISCLAIMER
WE DONT HOST ANY ILLEGAL FILES ON THE SERVER
USE CONTACT US TO REPORT ILLEGAL FILES
ADMINISTRATORS CANNOT BE HELD RESPONSIBLE FOR USERS POSTS AND LINKS

... Copyright © 2003-2999 Sonsivri.to ...
Powered by SMF 1.1.18 | SMF © 2006-2009, Simple Machines LLC | HarzeM Dilber MC