Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
December 05, 2016, 09:27:44 09:27


Login with username, password and session length


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

Posts: 289

Thank You
-Given: 246
-Receive: 396


« on: August 07, 2008, 08:11:33 20: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, 08:12:29 20:12 by LithiumOverdosE » Logged
spasbyspas
Active Member
***
Offline Offline

Posts: 169

Thank You
-Given: 663
-Receive: 364


« Reply #1 on: August 07, 2008, 08:27:47 20: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
V.I.P
Senior Member
*****
Offline Offline

Posts: 289

Thank You
-Given: 246
-Receive: 396


« Reply #2 on: August 07, 2008, 08:38:54 20: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: 663
-Receive: 364


« Reply #3 on: August 07, 2008, 08:41:51 20: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, 09:58:01 21: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
V.I.P
Senior Member
*****
Offline Offline

Posts: 289

Thank You
-Given: 246
-Receive: 396


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

@cuoiset

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

Posts: 126

Thank You
-Given: 703
-Receive: 199


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

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

Posts: 289

Thank You
-Given: 246
-Receive: 396


« Reply #7 on: August 10, 2008, 06:37:21 18: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