[SOLVED]Question on creating variables in C

Need help with C, C++, perl, python, etc?

[SOLVED]Question on creating variables in C

Postby Grishan Pradhan » 2018-10-02 14:23

I read in my textbook that while creating variables the length should not be normally more than eight characters(though ANSI standard recognizes the length of 31 characters), since only the first eight characters are treated as significant by many compilers.

The question is Will there be any problem or not if there are two variabled having first eight character same (for eg:- average_length and average_width) when only first eight characters are significant?
Last edited by Grishan Pradhan on 2018-10-03 01:47, edited 2 times in total.
Grishan Pradhan
 
Posts: 21
Joined: 2018-09-30 12:46

Re: Question on creating variables in C

Postby GarryRicketson » 2018-10-02 15:17

Why not just try it and see, ?
User avatar
GarryRicketson
 
Posts: 5193
Joined: 2015-01-20 22:16
Location: Durango, Mexico

Re: Question on creating variables in C

Postby Grishan Pradhan » 2018-10-02 15:30

GarryRicketson wrote:Why not just try it and see, ?


I wish I could but I am just starting. I am at THEORY part.
Grishan Pradhan
 
Posts: 21
Joined: 2018-09-30 12:46

Re: Question on creating variables in C

Postby bw123 » 2018-10-02 15:53

What textbook are you using? It's been 30 yrs since I did "Intro to C Programming" cs 101 and I don't recall that. Maybe things have changed, but at the time I recall my instructor wanted us to use naming for variable that included abbreviated data type in the name, using short tags on the front, like sz_variable1 or sh_variable, ln_var2, or ptr_somepointer. The names were often pretty long.

I like longer descriptive names, but I guess there are many styles. I think it's important to stay consistent.
User avatar
bw123
 
Posts: 3579
Joined: 2011-05-09 06:02
Location: TN_USA

Re: Question on creating variables in C

Postby Grishan Pradhan » 2018-10-02 16:16

bw123 wrote:What textbook are you using? It's been 30 yrs since I did "Intro to C Programming" cs 101 and I don't recall that. Maybe things have changed, but at the time I recall my instructor wanted us to use naming for variable that included abbreviated data type in the name, using short tags on the front, like sz_variable1 or sh_variable, ln_var2, or ptr_somepointer. The names were often pretty long.

I like longer descriptive names, but I guess there are many styles. I think it's important to stay consistent.



It's a textbook from a native man(Lecturer) and this question is on the book. And I am asking beacuse the answer is not on the book.
Grishan Pradhan
 
Posts: 21
Joined: 2018-09-30 12:46

Re: Question on creating variables in C

Postby bw123 » 2018-10-02 16:23

I think you should obey your instructor to get the best grade? Afterwards, obey the project leader when they tell you to do things differently than the instructor? Develop your own style for your own projects, just stay consistent within each type of project.
User avatar
bw123
 
Posts: 3579
Joined: 2011-05-09 06:02
Location: TN_USA

Re: Question on creating variables in C

Postby GarryRicketson » 2018-10-02 16:56

The question is Will there be any problem or not if there are two variabled having first eight character same (for eg:- average_length and average_width) when only first eight characters are significant?

My question is , Is this a home work assignment ? and you are supposed to figure the answer out on your own ? I suspect it is, I know the answer, but you need to figure it out on your own.
It's a textbook from a native man(Lecturer) and this question is on the book. And I am asking beacuse the answer is not on the book.

The answer might not be in the book, and something else to consider, a "Lecturer", can lecture, and talk about all sorts of theories they dream up, but unless the person puts the "theory" to a real life test, it is just a meaning less theory, and has no effect on what happens in real life, or on a real compiler, Maybe the question is intended to make you do some real research, and try some real code, then you can answer the question, Yes it will cause problems, or No it won't, and back up your answer, with real code, a real example.
It would then no longer be a theory.
A example , but with qbasic, many argue you should not use 'goto', and in theory it can cause problems, So, should you use 'goto' or not, ? The answer is , to try to avoid it's use, but at times it is needed, 'goto' can be used, but mis used it can cause problems.
Go figure. Use some logic.
User avatar
GarryRicketson
 
Posts: 5193
Joined: 2015-01-20 22:16
Location: Durango, Mexico

Re: Question on creating variables in C

Postby Grishan Pradhan » 2018-10-02 17:46

GarryRicketson wrote:
The question is Will there be any problem or not if there are two variabled having first eight character same (for eg:- average_length and average_width) when only first eight characters are significant?

My question is , Is this a home work assignment ? and you are supposed to figure the answer out on your own ? I suspect it is, I know the answer, but you need to figure it out on your own.
It's a textbook from a native man(Lecturer) and this question is on the book. And I am asking beacuse the answer is not on the book.

The answer might not be in the book, and something else to consider, a "Lecturer", can lecture, and talk about all sorts of theories they dream up, but unless the person puts the "theory" to a real life test, it is just a meaning less theory, and has no effect on what happens in real life, or on a real compiler, Maybe the question is intended to make you do some real research, and try some real code, then you can answer the question, Yes it will cause problems, or No it won't, and back up your answer, with real code, a real example.
It would then no longer be a theory.
A example , but with qbasic, many argue you should not use 'goto', and in theory it can cause problems, So, should you use 'goto' or not, ? The answer is , to try to avoid it's use, but at times it is needed, 'goto' can be used, but mis used it can cause problems.
Go figure. Use some logic.



I found this book online which has the same thing written in it.
BOOK(By: E Balagurusamy):https://books.google.com.np/books?id=3fZa9af1KtYC&pg=PA29&lpg=PA29&dq=in+c+only+the+first+eight+characters+are+treated+as+significant?&source=bl&ots=yF75cuc38-&sig=uMFGMypK3T4a6vDXKszMfOHLyjM&hl=en&sa=X&ved=2ahUKEwj9w57srejdAhXLKY8KHW-ZBnUQ6AEwBHoECAUQAQ#v=onepage&q&f=false

Page no. 29 Point No. 2
Now it is clear that not only "Lecturer" gave his theory on the book. It is also written in the book by someone who happens to be an recognised engineer.

I found the solution too.
Page No. 30 1st paragraph.
Grishan Pradhan
 
Posts: 21
Joined: 2018-09-30 12:46

Re: Question on creating variables in C

Postby bw123 » 2018-10-02 18:27

I think I might use two variables named average_length and average_width, or I might use two functions named get_average_length() and get_average_width() and I would expect all compilers or preprocessors to differentiate between the similar identifiers. I never heard of one that could not, but I wouldn't argue the point.

I can't read the google book, but it loks to be from the early 199.s, was it only 8 character identifiers for variables that would have this problem, or functions and other identifiers like macros? Would it have something to do with a compiler flag for optimized code, or smaller name mangling to save space/memory somehow?
User avatar
bw123
 
Posts: 3579
Joined: 2011-05-09 06:02
Location: TN_USA

Re: Question on creating variables in C

Postby GarryRicketson » 2018-10-02 20:36

Well, congratulations, then you probably notice , they key word is "IF",
If the compiler only recognizes the first 8 characters, THEN, the 2 names:
Code: Select all
average_height
average_weight

mean the same thing to the compiler.
You can use abbreviations, like "ht" for height, or "avg" for average. Or, maybe use a different compiler, one that does recognize longer names.
I read in my textbook that while creating variables the length should not be normally more than eight characters(though ANSI standard recognizes the length of 31 characters), since only the first eight characters are treated as significant by many compilers.

"many compilers" is not the same as all compilers, which is why I said you would need to try it, and see if your compiler accepted it. OR if you looked at the manual for the particular compiler, it might say on that. But yes, the Logic is, you do need to obey and restrictions or limitations the compiler has. So IF it can not recognize a variable name longer then 8 characters. then you must use something shorter or a abbreviation.


bw123 »I can't read the google book, but it loks to be from the early 199.s, was it only 8 character identifiers for variables that would have this problem, or functions and other identifiers like macros?

I remember as well , when Dos would not accept filenames over 8 characters , and yes this was the way it was, back in the early and mid 90's , even late 90's. I don't know when this book was written though.
===== edit =======
Just a thought, more "theory", but I am inclined to agree with the author, or if it is the OP asking :
The question is Will there be any problem or not if there are two variabled having first eight character same (for eg:- average_length and average_width) when only first eight characters are significant?

If one was/is writing source code, it would be best practice to take into consideration that you don't know what compiler might be used by someone else trying to compile the code, so yes there could be problems for those using a compiler that only recognizes 8 characters, in the variables, best practice would be to avoid that.
A long time ago, I got really aggravated , trying to compile code written by windows users, even though it was supposed to be usable and compile able for Linux, the problem was the way windows programmers mix uppercase and lower case, I suppose that is another topic though. But, that is something else that should be avoided when writing code, and naming files,libraries, etc.
User avatar
GarryRicketson
 
Posts: 5193
Joined: 2015-01-20 22:16
Location: Durango, Mexico

Re: Question on creating variables in C

Postby Grishan Pradhan » 2018-10-03 01:58

GarryRicketson wrote:Well, congratulations, then you probably notice , they key word is "IF",
If the compiler only recognizes the first 8 characters, THEN, the 2 names:
Code: Select all
average_height
average_weight

mean the same thing to the compiler.
You can use abbreviations, like "ht" for height, or "avg" for average. Or, maybe use a different compiler, one that does recognize longer names.
I read in my textbook that while creating variables the length should not be normally more than eight characters(though ANSI standard recognizes the length of 31 characters), since only the first eight characters are treated as significant by many compilers.

"many compilers" is not the same as all compilers, which is why I said you would need to try it, and see if your compiler accepted it. OR if you looked at the manual for the particular compiler, it might say on that. But yes, the Logic is, you do need to obey and restrictions or limitations the compiler has. So IF it can not recognize a variable name longer then 8 characters. then you must use something shorter or a abbreviation.


bw123 »I can't read the google book, but it loks to be from the early 199.s, was it only 8 character identifiers for variables that would have this problem, or functions and other identifiers like macros?

I remember as well , when Dos would not accept filenames over 8 characters , and yes this was the way it was, back in the early and mid 90's , even late 90's. I don't know when this book was written though.
===== edit =======
Just a thought, more "theory", but I am inclined to agree with the author, or if it is the OP asking :
The question is Will there be any problem or not if there are two variabled having first eight character same (for eg:- average_length and average_width) when only first eight characters are significant?

If one was/is writing source code, it would be best practice to take into consideration that you don't know what compiler might be used by someone else trying to compile the code, so yes there could be problems for those using a compiler that only recognizes 8 characters, in the variables, best practice would be to avoid that.
A long time ago, I got really aggravated , trying to compile code written by windows users, even though it was supposed to be usable and compile able for Linux, the problem was the way windows programmers mix uppercase and lower case, I suppose that is another topic though. But, that is something else that should be avoided when writing code, and naming files,libraries, etc.


Thank You. :D :D
Grishan Pradhan
 
Posts: 21
Joined: 2018-09-30 12:46


Return to Programming

Who is online

Users browsing this forum: No registered users and 3 guests

fashionable