2.8 – Identifiers and Hungarian notation

by subbu on August 16, 2013

Identifier:

Every human being is identified with a name. Likewise a variables, an array, a functions or a pointer in a C program must be identified with a name called identifier. C language allows to select any name which may be relevant to the context. But some rules must follow while selecting an identifier.

Rules in selecting an identifier:

  • Though the maximum length of an identifier is compiler dependent, it is suggested to concise the length up to 31 characters. Make it, as short as possible to avoid typo
  • The first character must be an alphabet
  • Spaces and special symbols are not allowed, only an underscore _ operator can be used
  • Keywords can’t be used as identifier
  • Private identifiers are started with underscore _
  • System level identifiers are started with double underscore __
  • Identifiers are case sensitive, For example say Num is different from nUm

Guess which are valid and invalid

1) india
2) gross salary
3) net_profit
4) 21num
5) no.1
6) Int
7) no4
8) H-R-A
9) nUmBeR
10) float
11) _private_idenifier
12) __system_level_identifier

Show Answers

1) valid
2) Invalid (Space character is not allowed)
3) Valid
4) Invalid (First character must not be a digit)
5) Invalid (Special symbol . can’t be used)
6) Valid (because C is case sensitive keyword int is different from Int)
7) Valid
8) Invalid (Special symbol – can’t be used)
9) Valid
10) Invalid (float is a keyword)
11) Valid (can be used as a private identifier in C++)
12) Valid (can be used as a system variable)

Hungarian notation:

It is a naming convention in which variable type and scope are specified as a prefix to the variable name. It is invented by Charles Simonyi a Xerox PARC programmer. It was useful in the type less languages like BCPL, because BCPL has no data types, we can identify the variable type by looking at its prefix. Most of the people argue that it is not useful now with modern languages and IDEs. But we believe that its advantages are outweighing the disadvantages.

prefix meaning
n integer
f float
ch character
d double
l long
u unsigned
arr array
b boolean
e enum
s struct
str string
p pointer
r reference
g_ global variable
m_ member of a class
s_ static member

Examples:

int nvalue; /*integer variable */
int arrnsubjects[10]; /* an integer array */
float *pfmark;  /* float type of pointer */
unsigned int unhell;  /* unsigned integer variable */
int g_ncount;   /* global integer variable  */
char s_chalp;  /* static character type of variable */

Note: Code conventions may change organization to organization.

A simple program with Hungarian notation:

Specification:
Accept principle amount, time in months and percentage of interest as input and print the simple interest, total amount must be paid to the financier.

Program:


#include<stdio.h>
int main()
{
int ntime;
float famt,fintrest,fsi,ftotal;
printf("Amount:");
scanf("%f",&famt);
printf("Time in months:");
scanf("%d",&ntime);
printf("Rate of intrest:");
scanf("%f",&fintrest);
fsi=famt*ntime*fintrest/100;
ftotal=famt+fsi;
printf("Simple intrest %f",fsi);
printf("\nTotal amount %f",ftotal);
return 0;
}

Execution:
Amount:1000
Time in months:2
Rate of intrest:20
Simple intrest 400.000000
Total amount 1400.000000

Previous post:

Next post: