# sbit definitions with SDCC - Error: “initializer element is not constant”

I am facing this error when try to compile a C program using the SDCC compiler:

The part of programme that is causing error.

include(reg51.h)
define cmdport P3
define dataport P2
define q 100

(following  3 line are causing error.)
sbit at  cmdport^0  rs ;  //register select pin
sbit at  cmdport^1  rw ;  // read write pin
sbit at  cmdport^6  e ;  //enable pin

• take the time to format your code, it is not easily readable right now. – Kortuk Mar 13 '11 at 11:54
• That isn't valid C code. I'm not surprised that you get errors. – Leon Heller Mar 13 '11 at 14:37
• I have write a small part of code and if you are talking about # before define then i did it and if bout sbit declaration then in sdcc we define <br>sbit rs = cmdport^0; as<br>sbit at cmdport^0 rs ; – Zain Mar 13 '11 at 15:58
• @Leon Heller: while this technically isn't ANSI C code, the original poster clearly stated he is using SDCC. The __sbit __at language extension is clearly documented in the SDCC manual. – davidcary Mar 14 '11 at 15:17
• would you mind if I edit your original question so that it's valid C? You have a good question here about sbit with SDCC, but it's being overshadowed by confusion around the code – Toby Jaffey Mar 15 '11 at 1:28

## 2 Answers

Your code is not valid C. It should look like this:

#include <reg51.h>
#define cmdport P3
#define dataport P2
#define q 100


To create sbit definitions for the individual bits in P3 (address 0xB0), you want:

__sbit __at (0xB0) rs; // P3.0
__sbit __at (0xB1) rw; // P3.1
__sbit __at (0xB6) e;  // P3.6


Though, if you look in sdcc/includes/mcs51/8051.h you'll find that there are already generic definitions for P3_0 and so on, allowing you to write:

#define rs P3_0
#define rw P3_1
#define e  P3_6


I'd also recommend that you pick more descriptive names for your bit definitions than rs, rw and e as these may clash with variable names.

• i was including like you but i could not add # in question.I also tried __ before sbit and at as they were warned by sdcc as depriciated.i just copy your code nd now my code is working fine.but still i can,t figure out the cause f problem.please let me to know. – Zain Mar 14 '11 at 16:46
• @Zain Try the SDCC mailing list. That's a much better forum for detailed back and forth discussion – Toby Jaffey Mar 14 '11 at 17:27

My guess is that this is an assembly file - you need to run it through an assembler rather than the C compiler.

Many C compilers (especially gcc based ones) will recognise a file with a particular suffix (.s for example) and do the right thing for you - other compilers wont - if you're using a make file that was written for gcc with a different compiler you might find that it makes this mistake