what is identifier in C programming ?

Thread Starter

vead

Joined Nov 24, 2011
629
Hello
I know c programing has identifiers so I am trying to understand which is identifiers in below program.
Code:
#include<p89v51rd2.h>
#define LED P2_0
#define LED P2_1
main ( )
{

}
I think define, LED, P2_0, and P2_1. these are the identifier in program because identifier start with a letter A to Z or a to z or underscores, and digits (0 to 9).
what is use of identifier in C programming ?
 

Papabravo

Joined Feb 24, 2006
22,082
Before the compiler ever sees the source text of a program, the C preprocessor performs string substitution as determined by the #define statements. In this case you have a program with a logical error. You are asking the preprocessor to substitute both P2_0 and P2_1 for the string LED. This cannot possibly succeed. You need to construct a more meaningful example.
 

Thread Starter

vead

Joined Nov 24, 2011
629
Before the compiler ever sees the source text of a program, the C preprocessor performs string substitution as determined by the #define statements. In this case you have a program with a logical error. You are asking the preprocessor to substitute both P2_0 and P2_1 for the string LED. This cannot possibly succeed. You need to construct a more meaningful example.
here is program for motor. I don't understand which are identifiers
Code:
sbit L293D_A = P2^0;
sbit L293D_B = P2^1;
sbit L293D_E = P2^2;

// Function Prototypes
void rotate_f(void); //Forward run funtion
void rotate_b(void); //Backward run function
void breaks(void); //Motor stop function
void delay(void); //Some delay

void main()
{
while (1) { //Infinite loop
rotate_f(); //Run forward
delay(); //Some delay
breaks(); //Stop
delay(); //Some delay
rotate_b(); //Run Backwards
delay(); //Some delay
breaks(); //Stop
delay(); //Some delay
} //Do this infinitely
}

void rotate_f()
{
L293D_A = 1; //Make positive of motor 1
L293D_B = 0; //Make negative of motor 0
L293D_E = 1; //Enable L293D
}
void rotate_b()
{
L293D_A = 0; //Make positive of motor 0
L293D_B = 1; //Make negative of motor 1
L293D_E = 1; //Enable L293D
}
void breaks()
{
L293D_A = 0; //Make positive of motor 0
L293D_B = 0; //Make negative of motor 0
L293D_E = 0; //Disable L293D
}
//Some delay...
void delay()
{
unsigned char i,j,k;
for(i=0;i<0x20;i++)
for(j=0;j<255;j++)
for(k=0;k<255;k++);
}
 

Papabravo

Joined Feb 24, 2006
22,082
All of the keywords in the language are identifiers in the sense that they follow the syntax of an identifier. When you have a declaration like:

unsigned char i,j,k ;​

The identifiers which are not keywords are i, j, and k. Function names like delay, breaks, and rotate_b are also identifiers, and they are not keywords. Are you starting to get the picture?
 

Thread Starter

vead

Joined Nov 24, 2011
629
All of the keywords in the language are identifiers in the sense that they follow the syntax of an identifier. When you have a declaration like:

unsigned char i,j,k ;​

The identifiers which are not keywords are i, j, and k. Function names like delay, breaks, and rotate_b are also identifiers, and they are not keywords. Are you starting to get the picture?
unsigned char i,j,k ; here unsigned char is keyword and i,j,k are the identifiers. also void is identifier. keywords are reserved words. what is define ? I think it should be identifier
 

WBahn

Joined Mar 31, 2012
32,839
Have you even bothered to attempt to find the answer on your own?

http://bfy.tw/B7Pe

Whether a keyword is considered an identifier or not depends on who wrote the language spec. It's a distinction without much meaning since it merely affects how other parts of the language spec have to be worded. Usually, as in C, identifiers and keywords are disjoint sets. By the spec, if the compiler has the option of translating a token into either a keyword or an identifier, it is required to choose the keyword.

Per the language specification: An identifier can denote an object; a function; a tag or a member of a structure, union, or enumeration; a typedef name; a label name; a macro name; or a macro parameter.

define (as in #define) is not anything (in this regard). It is a preprocessor directive and does not survive to the compilation stage. The same is true for everything else on the #define line since that entire line is never seen by the compiler. The concept of an identifier applies to what the compiler sees.
 

WBahn

Joined Mar 31, 2012
32,839
It appears I need to walk back part of what I said -- the language spec includes macro names and parameters in its concept of what an identifier is. It's a bit confusing (probably meaning that I just haven't mapped it out sufficiently) because they make a distinction about when preprocessing tokens are converted to tokens, at which point they potentially become identifiers. So there's some fine print that applies.
 
Top