0

enter image description here

Please explain the difference between an identifier and variable? And what went wrong in this program ? I have declared an variable name then also it says undeclared variable and calls name an identifier ??

0

There is a problem with the scope of the variable. You declared name as a string, that's OK, but what's the scope of this variable? It has a local scope, i.e. it exists only inside the brackets {} of do-while loop. You can google for more info on scope of variables in programming languages(especially C). At lines 11, 13, 15, 18, and 20 you try to access name which actually does not exist, so compiler throws an error message.

Declare the name at the beginning and then do all the stuff you want later. BTW Declaring all the variables in the beginning of a function is considered a good practice.

Two other problems I see are :

  1. name == 'NULL' should be name == NULL, to test whether the variable name is a NULL value. Single quotes check for characters.

  2. The thing you are trying to accomplish with the do-while loop, GetString() does that implicitly for you, So you don't need a loop there.

Good Luck.


PS : Variables and Identifiers are same, both of them help us uniquely recognize an object.

3
  • so if i just press enter instead of typing a string, will it re-promt for a string, without a do-while loops ? – Prateek Pande Jan 17 '15 at 16:24
  • Yes, it will, imho you should have played with such functions for a bit more time. – sinister Jan 17 '15 at 16:26
  • @PrateekPande a variable, by concept, is a location in memory. A variable name is an identifier for a variable just like your name is an identifier for you. – Kareem Jan 18 '15 at 1:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .