473,386 Members | 1,943 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Strcpy versus Strncpy in arrays

SK
Hi
I appreciate all of the feedback I have recieved. I am enjoying
working on my program and I hope that it can be appreciated. I have my
program compiling now and I am continuing to work out the bugs. I have
two problems that I cannot seem to resolve:

1. I want to have someone enter in as many employees as they want
to..and for each employee entered I would like to save the data from
that entry to print out at the end of the program. I continue to run
into a problem with the loop. I actually did not intend to use arrays
however I cannot seem to figure outany other way to approach the
problem.

2.This second problem is probably due to using arrays
intheprogram,however that being said I still want to figure this
out....I am trying to connect two strings together, concatenation. I
understandthat strncpy refers to n number of characters versus strcpy
is the actual character but the difficulty I am running into has to do
with the syntax. It is evident that I am somehow not correctly dealing
with putting things into memory and then extracting them. Any
thoughts??

//Specs to be added later

//C Libraries
#include <stdio.h>
#include <math.h>
#include <string.h>
//Global Constants
# define FULLNAME 20
# define EMPLOYEES 1000
//Global Defined Constant
const float OT = 1.5;

//Global Variable Declaratives
FILE*inp;

//Global Constants
# define FULLNAME 20
# define EMPLOYEES 1000

char fn[FULLNAME];
char ln[FULLNAME];
char department[20];
char again;
int count_EMP;
int number_EMP;

float wage;
float OTwage;
float hours;
float RegHr;
float RegHrPay;
float OTHrPay;
float OTHr;
float GrossPay;


int main(void)
{
/*Define the structure.*/
struct EMP_WeeklyPay
{
char first_name[FULLNAME];
char last_name[FULLNAME];
float RegHr;
float wage;
float OTHr;
float OTHrPay;
float GrossPay;
};
/*Rename the structure syntax.*/
typedef struct EMP_WeeklyPay EWP;
/*Create an array of structures.*/
EWP emp[EMPLOYEES];

/*Counters*/
int n, numemp;
int count = 0;

printf("\n\nMountain Pacific Corporation\n");
printf("Department Salary Program\n\n");
printf("Please enter the name of the department: ");
scanf("%s", department);
/*Loop to read in employee wage data*/
count_EMP = 0;
count_EMP++;

for (n = 0; n < EMPLOYEES; ++n){
do{
printf("\nEnter employee # %d: ", count_EMP);
scanf("%s %s", &fn, &ln);

printf("\nPlease enter the hourly wage for the employee:
");
scanf("%f", &wage);

printf("\nPlease enter the number of hours worked this"
" week: ");
scanf("%f", &hours);

printf("\nThank you. Process another employee?");
scanf("%s", &again);

}while(again == 'Y' || again == 'y');

/*Read in the input*/

numemp = scanf("%11s%11s%f%f%f%f%f", &fn, &ln, &RegHr,
&wage, &OTHr, &OTHrPay, &GrossPay);

/*Check if user is done*/


if(again != 'Y' && again !='y');
printf("End of processing\n\n\n");
/*Process the input*/
if(numemp == 6)
{

if (RegHr > 40)
{
OTHr = hours - 40;
OTHrPay = OT * OTHr * wage;
RegHrPay = 40.0 * wage;
}

else
{
RegHrPay = hours * wage;
OTHrPay = 0.0;

}

GrossPay = RegHrPay + OTHrPay;

strncpy(emp[n].first_name, fn, FULLNAME);
emp[n].first_name[FULLNAME] = '\0';

strncpy(emp[n].last_name, ln, FULLNAME-1);
emp[n].last_name[FULLNAME-1] = '\0';

emp[n].RegHr = RegHr;
emp[n].wage = wage;
emp[n].OTHr = OTHr;
emp[n].OTHrPay = OTHrPay;
emp[n].GrossPay = GrossPay;
++count;
}

/*Print Table*/

printf("\n\nMountain Pacific Corporation\n");
printf("Department Salary Program\n\n");

printf("Employee Reg Hrs "
"Overtime Hrs Gross\n");

printf("-----------------------------------------"
"-------------------------\n\n");


for(n=0; n < count; ++n) {
printf("%-35s%-17s%12f%10f%12f%10f%%5f",
emp[n].first_name,
emp[n].last_name, emp[n].RegHr,
emp[n].wage, emp[n].OTHr, emp[n].OTHrPay,
emp[n].GrossPay);
}
}
}

Nov 15 '05 #1
53 4223
SK wrote:
2.This second problem is probably due to using arrays
intheprogram,however that being said I still want to figure this
out....I am trying to connect two strings together, concatenation. I
understandthat strncpy refers to n number of characters versus strcpy
is the actual character but the difficulty I am running into has to do
with the syntax. It is evident that I am somehow not correctly dealing
with putting things into memory and then extracting them. Any
thoughts??


Stop working on your program here and start another one just for testing
out how strcpy and strncpy work. Also, read the documentation of both
functions. If you have problems with some very specific thing, post a
minimal example - your growing main project doesn't qualify as minimal
example.

Lastly, in order to handle arbitrary strings, you will need
malloc/realloc/free for the memory handling and strlen, strcpy and strcat
for the string operations. The *n* variants are usually only necessary if
you operate on fixed size arrays, for dynamic strings you better resize
them beforehand.

Uli

Nov 15 '05 #2
On 13 Nov 2005 12:04:08 -0800, in comp.lang.c , "SK"
<ba******@aol.com> wrote:

//Global Defined Constant
const float OT = 1.5;
You really don't want to use float - use double.
scanf("%s", department);
As I said before, scanf is a bad function to use for this. Consider
what happens if someone enters 22 characters.
for (n = 0; n < EMPLOYEES; ++n){
usual to do n++
do{
printf("\nEnter employee # %d: ", count_EMP);
scanf("%s %s", &fn, &ln);
fn and ln are not defined. Assuming they're supposed to be strings,
you don't need the ampersands.
scanf("%s", &again);

}while(again == 'Y' || again == 'y');
again is a string, not a single char so you need "Y" and "y". Or
better yet, declare again as a character and use the %c format.

/*Read in the input*/
you never asked the user to enter any...
numemp = scanf("%11s%11s%f%f%f%f%f", &fn, &ln, &RegHr,
&wage, &OTHr, &OTHrPay, &GrossPay);


again, fn and ln don't seem to be defined.
You also seem to have some big problems with loops.

Take a step back. Write down _in english_ (or your native language), a
description of what the programme has to do. Consider carefully how
each piece of data needs to be obtained and stored.

Then, once you have a design, start writing code.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 15 '05 #3
Mark McIntyre wrote:
fn and ln are not defined.


He's got a bunch of globals, and those are two of them.
scanf("%s", &again);

}while(again == 'Y' || again == 'y');


again is a string, not a single char so you need "Y" and "y". Or
better yet, declare again as a character and use the %c format.


'again' was declared as a char. another global

--
pete
Nov 15 '05 #4
Mark McIntyre wrote:
On 13 Nov 2005 12:04:08 -0800, in comp.lang.c , "SK"
<ba******@aol.com> wrote:

for (n = 0; n < EMPLOYEES; ++n){


usual to do n++

Ummm, what? Usual for whom? K&R uses the prefix notation. It's so
common as to be completely unremarkable.

Brian
Nov 15 '05 #5
On 14 Nov 2005 19:40:25 GMT, in comp.lang.c , "Default User"
<de***********@yahoo.com> wrote:
Mark McIntyre wrote:
On 13 Nov 2005 12:04:08 -0800, in comp.lang.c , "SK"
<ba******@aol.com> wrote:

> for (n = 0; n < EMPLOYEES; ++n){


usual to do n++

Ummm, what? Usual for whom?


Not for whom, for what.

int array[10];

for(i=0;i<10;++i)
array[i] = i;

In this context particularly, I find pre-increment especially
counterintuitive.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 15 '05 #6
On Sun, 13 Nov 2005 23:57:55 GMT, in comp.lang.c , pete
<pf*****@mindspring.com> wrote:
Mark McIntyre wrote:
fn and ln are not defined.


He's got a bunch of globals, and those are two of them.


not in the version I was commenting on - I checked.
again is a string, not a single char so you need "Y" and "y". Or
better yet, declare again as a character and use the %c format.


'again' was declared as a char. another global


Not in the verison I was commenting on - it was a character array.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 15 '05 #7
Mark McIntyre <ma**********@spamcop.net> writes:
On 14 Nov 2005 19:40:25 GMT, in comp.lang.c , "Default User"
<de***********@yahoo.com> wrote:
Mark McIntyre wrote:
On 13 Nov 2005 12:04:08 -0800, in comp.lang.c , "SK"
<ba******@aol.com> wrote:

> for (n = 0; n < EMPLOYEES; ++n){

usual to do n++

Ummm, what? Usual for whom?


Not for whom, for what.

int array[10];

for(i=0;i<10;++i)
array[i] = i;

In this context particularly, I find pre-increment especially
counterintuitive.


I agree with "Default User". I suppose I have a slight preference for
postincrement, but I don't find either one counterintuitive or
jarring, and my guess is that most other C programmers feel the same
way. And of course they're completely equivalent in that context (as
they are in statement context). I hardly even notice which one is
used; I think I mentally translate both to "increment i" if the result
isn't used.

But then we all have our little quirks, like my own strong distaste
for "42 == x" rather than "x == 42" (and yes, I know the arguments).
So I won't criticize you for disliking ++i, but you should be aware
that your dislike is (probably) a minority viewpoint.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #8
On 2005-11-14, Mark McIntyre <ma**********@spamcop.net> wrote:
On 14 Nov 2005 19:40:25 GMT, in comp.lang.c , "Default User"
<de***********@yahoo.com> wrote:
Mark McIntyre wrote:
On 13 Nov 2005 12:04:08 -0800, in comp.lang.c , "SK"
<ba******@aol.com> wrote:

> for (n = 0; n < EMPLOYEES; ++n){

usual to do n++

Ummm, what? Usual for whom?


Not for whom, for what.

int array[10];

for(i=0;i<10;++i) array[i] = i;

In this context particularly, I find pre-increment especially
counterintuitive.


Why? The final result [i.e. what ++i evaluates to] is what is tested
for being less than 10. I tihnk the real issue is that it's not what
you're used to.
Nov 15 '05 #9
Keith Thompson wrote:
Mark McIntyre <ma**********@spamcop.net> writes:
On 14 Nov 2005 19:40:25 GMT, in comp.lang.c , "Default User"
<de***********@yahoo.com> wrote:
Mark McIntyre wrote:

On 13 Nov 2005 12:04:08 -0800, in comp.lang.c , "SK"
<ba******@aol.com> wrote:

> for (n = 0; n < EMPLOYEES; ++n){

usual to do n++
Ummm, what? Usual for whom?
Not for whom, for what.

int array[10];

for(i=0;i<10;++i)
array[i] = i;

In this context particularly, I find pre-increment especially
counterintuitive.


I agree with "Default User". I suppose I have a slight preference for
postincrement, but I don't find either one counterintuitive or
jarring, and my guess is that most other C programmers feel the same
way. And of course they're completely equivalent in that context (as
they are in statement context). I hardly even notice which one is
used; I think I mentally translate both to "increment i" if the result
isn't used.


Right. I personally use i++ most of the time, but find ++i so
unremarkable that I, well, don't remark upon it. I would expect that
most compilers would render the same machine code for either in that
context.

As I said, K&R 2 uses preincrement. Someone pointed that out to me some
time back, which surprised me. I literally had never noticed that they
didn't use postincrement, and would have sworn that they did.
But then we all have our little quirks, like my own strong distaste
for "42 == x" rather than "x == 42" (and yes, I know the arguments).
I agree with you. I don't like it, so I don't use it.
So I won't criticize you for disliking ++i, but you should be aware
that your dislike is (probably) a minority viewpoint.


I don't even know that preference for i++ is a minority opinion, but
most likely CARING would be a minority opinion.


Brian

Nov 15 '05 #10
On Mon, 14 Nov 2005 21:04:25 GMT, in comp.lang.c , Keith Thompson
<ks***@mib.org> wrote:

I agree with "Default User". I suppose I have a slight preference for
postincrement, but I don't find either one counterintuitive or
jarring,
A heck of a lot of learners do however, IME. YMMV etc. You know and I
know that the ++ is evaluated, but the original value of i used in the
loop body. My experience of trainees is that they find that odd.
and my guess is that most other C programmers feel the same
way.
And my guess is that most are relatively agnostic. Or think its a daft
debate.
So I won't criticize you for disliking ++i, but you should be aware
that your dislike is (probably) a minority viewpoint.


I disagree that it is, but its largely irrelevant. :-)
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 15 '05 #11
On Mon, 14 Nov 2005 21:38:11 +0000 (UTC), in comp.lang.c , Jordan Abel
<jm****@purdue.edu> wrote:
Why? The final result [i.e. what ++i evaluates to] is what is tested
for being less than 10. I tihnk the real issue is that it's not what
you're used to.


Because unless you know the C grammar, it can read as
"for i equals zero to ten, pre-increment i, and do some stuff. "

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 15 '05 #12
On 2005-11-14, Mark McIntyre <ma**********@spamcop.net> wrote:
On Mon, 14 Nov 2005 21:38:11 +0000 (UTC), in comp.lang.c , Jordan Abel
<jm****@purdue.edu> wrote:
Why? The final result [i.e. what ++i evaluates to] is what is tested
for being less than 10. I tihnk the real issue is that it's not what
you're used to.


Because unless you know the C grammar, it can read as
"for i equals zero to ten, pre-increment i, and do some stuff. "


How is that any less clear than using i++ ?
Nov 15 '05 #13
"Mark McIntyre" <ma**********@spamcop.net> wrote in message
news:9t********************************@4ax.com...
On Mon, 14 Nov 2005 21:04:25 GMT, in comp.lang.c , Keith Thompson
<ks***@mib.org> wrote:
I agree with "Default User". I suppose I have a slight preference forpostincrement, but I don't find either one counterintuitive or
jarring,

Except for for() loops.

A heck of a lot of learners do however, IME. YMMV etc. You know and I
know that the ++ is evaluated, but the original value of i used in the
loop body. My experience of trainees is that they find that odd.


Who gives a fuck about trainees?! ;-)

That said, we are professional _writers_ here. We should write in such a
way that others understand. The _hackers_ newsgroup is over there --->
and my guess is that most other C programmers feel the same
way.


And my guess is that most are relatively agnostic. Or think its a daft
debate.


I read that as "draft debate" and thought there was a new standard
coming out... ;-)
So I won't criticize you for disliking ++i, but you should be aware
that your dislike is (probably) a minority viewpoint.


I disagree that it is, but its largely irrelevant. :-)

I like ++i as a stand-alone statement, but i++ in my loops. For
instance:
for (i=0; i < 10; i++)
++j;
This reads (to me) as "for i equals zero, and while i is less than 10,
do the loop and then increment i; [the loop]increment j".

--
Mabden
Nov 15 '05 #14
On 2005-11-14, Mark McIntyre <ma**********@spamcop.net> wrote:
On Mon, 14 Nov 2005 21:04:25 GMT, in comp.lang.c , Keith Thompson
<ks***@mib.org> wrote:

I agree with "Default User". I suppose I have a slight preference for
postincrement, but I don't find either one counterintuitive or
jarring,


A heck of a lot of learners do however, IME. YMMV etc. You know and I
know that the ++ is evaluated, but the original value of i used in the
loop body. My experience of trainees is that they find that odd.


Do what? the ++ is evaluated at the end of the loop body, and the
variable's new value is used both for the comparison and for the next
instance of the loop body. Where after the increment is the "original
value" used?

for(A;B;C) { D; }

becomes [barring continue and break]

A; while(B) {
D;
C;
}

for(i=0;i<3;i++) { printf("%d",i); }

[u] i=0; [0] i<3 prt i++ [1] i<3 prt i++ [2] i<3 prt i++ [3] !(i<3) done

output: 0123
Nov 15 '05 #15
Jordan Abel wrote:

On 2005-11-14, Mark McIntyre <ma**********@spamcop.net> wrote:
On Mon, 14 Nov 2005 21:04:25 GMT, in comp.lang.c , Keith Thompson
<ks***@mib.org> wrote:

I agree with "Default User".
I suppose I have a slight preference for
postincrement, but I don't find either one counterintuitive or
jarring,


A heck of a lot of learners do however,
IME. YMMV etc. You know and I
know that the ++ is evaluated,
but the original value of i used in the
loop body.

for(i=0;i<3;i++) { printf("%d",i); }


That means exactly the same thing as:
for(i=0;i<3;++i) { printf("%d",i); }

I have no idea of what Mark McIntyre thinks he's trying to say.

--
pete
Nov 15 '05 #16
On Tue, 15 Nov 2005 00:08:33 +0000 (UTC), in comp.lang.c , Jordan Abel
<jm****@purdue.edu> wrote:
On 2005-11-14, Mark McIntyre <ma**********@spamcop.net> wrote:
On Mon, 14 Nov 2005 21:38:11 +0000 (UTC), in comp.lang.c , Jordan Abel
<jm****@purdue.edu> wrote:
Why? The final result [i.e. what ++i evaluates to] is what is tested
for being less than 10. I tihnk the real issue is that it's not what
you're used to.


Because unless you know the C grammar, it can read as
"for i equals zero to ten, pre-increment i, and do some stuff. "


How is that any less clear than using i++ ?


Because that reads
"for i equals zero to ten, and do some stuff and increment i
afterwards. "
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 15 '05 #17
On Tue, 15 Nov 2005 05:48:36 +0000 (UTC), in comp.lang.c , Jordan Abel
<jm****@purdue.edu> wrote:
On 2005-11-14, Mark McIntyre <ma**********@spamcop.net> wrote:
On Mon, 14 Nov 2005 21:04:25 GMT, in comp.lang.c , Keith Thompson
<ks***@mib.org> wrote:

I agree with "Default User". I suppose I have a slight preference for
postincrement, but I don't find either one counterintuitive or
jarring,


A heck of a lot of learners do however, IME. YMMV etc. You know and I
know that the ++ is evaluated, but the original value of i used in the
loop body. My experience of trainees is that they find that odd.


Do what? the ++ is evaluated at the end of the loop body, and the
variable's new value is used both for the comparison and for the next
instance of the loop body. Where after the increment is the "original
value" used?


Inside the body of the loop. Are you hard of reading?
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 15 '05 #18
On Tue, 15 Nov 2005 12:23:12 GMT, in comp.lang.c , pete
<pf*****@mindspring.com> wrote:

This is after the typographical location of the statement "++i"
I have no idea of what Mark McIntyre thinks he's trying to say.


Luckily I do, but the folks who've commented on this thread are so
incredibly wrapped up in the "well its obvious it doesn't mean that"
attitude, that I fail to see any point trying to explain again.

Its like trying to explain why one finds the rules of baseball
inexplicable. People who *know* the rules think you're an idiot
because *obviously* the rules are simple. I imagine I'm much the same
when some yank complains that cricket is complicated.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 15 '05 #19
Mark McIntyre wrote:
A heck of a lot of learners do however, IME. YMMV etc. You know and I
know that the ++ is evaluated, but the original value of i used in the
loop body. My experience of trainees is that they find that odd.


In a for statement, expr-3 is evaluated as a void exprssion.

It is evaluated for side effects only and
the value of expr-3 is never used.

(++i) and (i++) have the same side effect.
There is no difference between using either form,
as expr-3 in a for statement.

--
pete
Nov 15 '05 #20
Mark McIntyre wrote:

On Tue, 15 Nov 2005 12:23:12 GMT, in comp.lang.c , pete
<pf*****@mindspring.com> wrote:

This is after the typographical location of the statement "++i"
I have no idea of what Mark McIntyre thinks he's trying to say.


Luckily I do,


No you don't.
You're talking about the value of expr-3 in a for loop.

--
pete
Nov 15 '05 #21
Mark McIntyre wrote:
Jordan Abel wrote:
Mark McIntyre wrote:
Because unless you know the C grammar, it can read as
"for i equals zero to ten, pre-increment i, and do some stuff. "


How is that any less clear than using i++ ?


Because that reads
"for i equals zero to ten, and do some stuff and increment i
afterwards. "


And what about: for (i = 0; i < 10; i += 2)
"for i equals zero to ten, do some stuff, and add 2 to i at
the same time" ?

If anyone does read it that way, then they have a serious
mis-understanding of for loops, and it would be best to
immediately correct them. One way of doing this is to
expose them to pre-increment and post-increment, and have
them observe that they both work the same way.

It would also be good to break them of the idea that "i++"
has anything to do with "afterwards", because it doesn't.

Nov 15 '05 #22
pete <pf*****@mindspring.com> writes:
Mark McIntyre wrote:
A heck of a lot of learners do however, IME. YMMV etc. You know and I
know that the ++ is evaluated, but the original value of i used in the
loop body. My experience of trainees is that they find that odd.


In a for statement, expr-3 is evaluated as a void exprssion.

It is evaluated for side effects only and
the value of expr-3 is never used.

(++i) and (i++) have the same side effect.
There is no difference between using either form,
as expr-3 in a for statement.


I think Mark knows that; he's just asserting that one form is less
intuitive than the other. I disagree, but since it's a matter of
perception there's not a whole lot of point in arguing about it.

I suggest that anyone who's seriously confused by
for (i = 0; i < 10; ++i) { ... }
isn't really going to understand
for (i = 0; i < 10; i++) { ... }
either. Anyone who thinks there's a real difference between them
should clear up that misconception before worrying about which one is
clearer.

C code should be written clearly, but it shouldn't cater to readers
who don't know the language.

In a similar vein, I know perfectly well that (x==42) and (42==x) are
semantically equivalent, but I still have a strong stylistic
preference for the former. If I wrongly thought there was a semantic
difference, my opinion that one is clearer than the other would be
worth very little. (I present this as an example; I'm not trying to
resurrect that argument.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #23
SK wrote:
for (n = 0; n < EMPLOYEES; ++n){
do{
printf("\nEnter employee # %d: ", count_EMP);
You have not ended this printf output with a "\n". Thus, its content
might not be output on some systems until the next occurrence of a \n
which happens in your next printf.
scanf("%s %s", &fn, &ln);
This is a "buffer overflow". It means that a user can input data
sufficient to cause a write memory beyond the bounds which you've
declared -- this has undefined behavior in the C language.

The standard half assed solution here is to do a fgets() to just read
the raw line, then an sscanf() to parse out the parameters. However,
that suffers the (lesser) problem of buffer truncation. If you are
intent on using the primitive C library, then I would recommend you look
at my article on user input here:

http://www.pobox.com/~qed/userInput.html

You can just use the getf.zip sources directly and then use
getstralloc() macro to fetch the user input correctly, then use sscanf
(after possibly using malloc to allocate space for your inputs).

But if you find all this just a little too painful, you can instead use
"The Better String Library" (http://bstring.sf.net/). You would just do
a bgets(), then a bsplit() to get all the names from the input.
printf("\nPlease enter the hourly wage for the employee: ");
scanf("%f", &wage);

printf("\nPlease enter the number of hours worked this"
" week: ");
scanf("%f", &hours);
Those, of course, are *not* buffer overflows because of the naturally
compressing nature of these modes of usage of scanf.
printf("\nThank you. Process another employee?");
scanf("%s", &again);
You only want to read one character here. So first of all you are
buffer overflowing again, but you are also probably using the wrong
scanf mode. You mean "%c" here, not "%s".
}while(again == 'Y' || again == 'y');
Wait -- you've written a loop here, but you are not storing your data
into incremental storage of any kind. You need to move this do { ... }
while() to the outer loop.
/*Read in the input*/

numemp = scanf("%11s%11s%f%f%f%f%f", &fn, &ln, &RegHr,
&wage, &OTHr, &OTHrPay, &GrossPay);
So is this redundant, or what are you trying to do here? In any event,
this has the buffer truncation problem. If you enter a name with more
than 11 characters, then the spill over characters will just bleed into
the successive parameters.
/*Check if user is done*/

if(again != 'Y' && again !='y');
printf("End of processing\n\n\n");
/*Process the input*/
if(numemp == 6)
{

if (RegHr > 40)
{
OTHr = hours - 40;
OTHrPay = OT * OTHr * wage;
Ok, here you are overwriting OTHr and OTHrPay with these computations,
after you were trying to read them as input. So you are essentially
just losing your input. So perhaps your second scanf is completely
superfluous and should just be removed. (You still need to move the do
{ ... } while to the outer loop.)
RegHrPay = 40.0 * wage;
}

else
{
RegHrPay = hours * wage;
OTHrPay = 0.0;

}

GrossPay = RegHrPay + OTHrPay;

strncpy(emp[n].first_name, fn, FULLNAME);
strncpy has a well known design flaw, where a maximal truncation doesn't
add a '\0' on the end. In general, I would avoid strncpy() in all
cases. There is another extension out there called "strlcpy" which does
not suffer this problem, but still leaves you with the buffer truncation
weakness.

"The Better String Library" makes this whole thing a non-issue. You
would just use bstrcpy(), and there would not be any truncation or
overflow issues.
emp[n].first_name[FULLNAME] = '\0';


This is a bounds overrun error. An array declared of length n can be
legally indexed from 0 to n-1 inclusive. So FULLNAME is 1 more than the
limit you can index with for this declared array. That said, it will
likely function, but end up setting a '\0' into the first character of
the next element (in this case .last_name, thus causing people's last
names to be empty).

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/
Nov 15 '05 #24
On Tue, 15 Nov 2005 22:39:20 GMT, in comp.lang.c , pete
<pf*****@mindspring.com> wrote:
Mark McIntyre wrote:

On Tue, 15 Nov 2005 12:23:12 GMT, in comp.lang.c , pete
<pf*****@mindspring.com> wrote:

This is after the typographical location of the statement "++i"
>I have no idea of what Mark McIntyre thinks he's trying to say.
Luckily I do,


No you don't.


I know what I'm trying to say, and, frankly, its stupid to say that
don't. The only thing you can say for certain is that you don't or
won't understand me. Thats my fault, I'm obviously not explaining
clearly enough.
You're talking about the value of expr-3 in a for loop.


No.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 15 '05 #25
On Tue, 15 Nov 2005 22:37:06 GMT, in comp.lang.c , pete
<pf*****@mindspring.com> wrote:
In a for statement, expr-3 is evaluated as a void exprssion.


You're making my point. To those who know the rules of baseball, the
rules of baseball are obvious.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 15 '05 #26
Keith Thompson wrote:

pete <pf*****@mindspring.com> writes:
Mark McIntyre wrote:
A heck of a lot of learners do however, IME. YMMV etc.
You know and I know that the ++ is evaluated,
but the original value of i used in the loop body.
In a for statement, expr-3 is evaluated as a void exprssion.
I think Mark knows that;


It looks to me like he's talking about the significance
of the value of expr-3, and there is no significance.

There is no other difference between (++i) and (i++)
to discuss.

--
pete
Nov 15 '05 #27
On 2005-11-15, Mark McIntyre <ma**********@spamcop.net> wrote:
On Tue, 15 Nov 2005 05:48:36 +0000 (UTC), in comp.lang.c , Jordan Abel
<jm****@purdue.edu> wrote:
On 2005-11-14, Mark McIntyre <ma**********@spamcop.net> wrote:
On Mon, 14 Nov 2005 21:04:25 GMT, in comp.lang.c , Keith Thompson
<ks***@mib.org> wrote:
I agree with "Default User". I suppose I have a slight preference for
postincrement, but I don't find either one counterintuitive or
jarring,

A heck of a lot of learners do however, IME. YMMV etc. You know and I
know that the ++ is evaluated, but the original value of i used in the
loop body. My experience of trainees is that they find that odd.


Do what? the ++ is evaluated at the end of the loop body, and the
variable's new value is used both for the comparison and for the next
instance of the loop body. Where after the increment is the "original
value" used?


Inside the body of the loop. Are you hard of reading?


No, the new value is used. The increment happens at the end of the
loop, and then the new value is used for the next execution of the
body. At no time after it is incremented is the previous value used.
Nov 15 '05 #28
pete wrote:
Keith Thompson wrote:
pete <pf*****@mindspring.com> writes:
Mark McIntyre wrote:
> A heck of a lot of learners do however, IME. YMMV etc.
> You know and I know that the ++ is evaluated,
> but the original value of i used in the loop body. In a for statement, expr-3 is evaluated as a void exprssion.

I think Mark knows that;


It looks to me like he's talking about the significance
of the value of expr-3, and there is no significance.

There is no other difference between (++i) and (i++)
to discuss.


The real point is that there is so much more of that code worth
analyzing than that. Its a "style before substance" thing that most of
the posters in this thread seem obsessed with and a great demonstration
of "group think".

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

Nov 15 '05 #29
Jordan Abel wrote:
On 2005-11-15, Mark McIntyre <ma**********@spamcop.net> wrote:
On Tue, 15 Nov 2005 05:48:36 +0000 (UTC), in comp.lang.c , Jordan Abel
<jm****@purdue.edu> wrote:
On 2005-11-14, Mark McIntyre <ma**********@spamcop.net> wrote:
On Mon, 14 Nov 2005 21:04:25 GMT, in comp.lang.c , Keith Thompson
<ks***@mib.org> wrote:

> I agree with "Default User". I suppose I have a slight preference for
> postincrement, but I don't find either one counterintuitive or
> jarring,
A heck of a lot of learners do however, IME. YMMV etc. You know and I
know that the ++ is evaluated, but the original value of i used in the
loop body. My experience of trainees is that they find that odd.
Do what? the ++ is evaluated at the end of the loop body, and the
variable's new value is used both for the comparison and for the next
instance of the loop body. Where after the increment is the "original
value" used?

Inside the body of the loop. Are you hard of reading?


No, the new value is used. The increment happens at the end of the
loop, and then the new value is used for the next execution of the
body. At no time after it is incremented is the previous value used.


I believe that Mark is talking about how people might miss-read the
code, not how it actually works. I'm sure Mark knows how it actually works.

Beginner known that i++ returns the value from before the increment.
Therefore beginner reads the third statement as meaning that the old
value of i is used in the body of the loop.
Therefore beginner has got it wrong.

In the above none of us are the beginner because we all know better.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 16 '05 #30
On 2005-11-16, Flash Gordon <sp**@flash-gordon.me.uk> wrote:
Jordan Abel wrote:
On 2005-11-15, Mark McIntyre <ma**********@spamcop.net> wrote:
On Tue, 15 Nov 2005 05:48:36 +0000 (UTC), in comp.lang.c , Jordan Abel
<jm****@purdue.edu> wrote:

On 2005-11-14, Mark McIntyre <ma**********@spamcop.net> wrote:
> On Mon, 14 Nov 2005 21:04:25 GMT, in comp.lang.c , Keith Thompson
> <ks***@mib.org> wrote:
>
>> I agree with "Default User". I suppose I have a slight preference for
>> postincrement, but I don't find either one counterintuitive or
>> jarring,
> A heck of a lot of learners do however, IME. YMMV etc. You know and I
> know that the ++ is evaluated, but the original value of i used in the
> loop body. My experience of trainees is that they find that odd.
Do what? the ++ is evaluated at the end of the loop body, and the
variable's new value is used both for the comparison and for the next
instance of the loop body. Where after the increment is the "original
value" used?
Inside the body of the loop. Are you hard of reading?
No, the new value is used. The increment happens at the end of the
loop, and then the new value is used for the next execution of the
body. At no time after it is incremented is the previous value used.


I believe that Mark is talking about how people might miss-read the
code, not how it actually works. I'm sure Mark knows how it actually works.


I read him as claiming that it works this way and that using ++i
obfuscates how it uses the previous value.

Beginner known that i++ returns the value from before the increment.
Therefore beginner reads the third statement as meaning that the old
value of i is used in the body of the loop.
Therefore beginner has got it wrong.
How can this be used to claim that ++i is less clear, when ++i would in
that case lead to the correct conclusion?
In the above none of us are the beginner because we all know better.

Nov 16 '05 #31
On Tue, 15 Nov 2005 23:40:07 GMT, in comp.lang.c , pete
<pf*****@mindspring.com> wrote:
It looks to me like he's talking about the significance
of the value of expr-3,


I'm not talking about any sort of significance.

Seemingly you're unable to discern the difference between perception
and knowledge.

Lets try a simpler example: the ancients thought frogs 'grew' from
mud, because after heavy rain, frogs were to be seen arising from the
puddles. We knowledgable people know this to be false, but we can
perhaps see why a people with less knowledge might be misled.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 17 '05 #32
On Wed, 16 Nov 2005 08:46:00 +0000, in comp.lang.c , Flash Gordon
<sp**@flash-gordon.me.uk> wrote:
I believe that Mark is talking about how people might miss-read the
code, not how it actually works. I'm sure Mark knows how it actually works.
At last. Somebody who is actually bothering to think ,as opposed to
merely restate segments of the Standard.
In the above none of us are the beginner because we all know better.


Precisely.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 17 '05 #33
Mark McIntyre <ma**********@spamcop.net> writes:
On Tue, 15 Nov 2005 23:40:07 GMT, in comp.lang.c , pete
<pf*****@mindspring.com> wrote:
It looks to me like he's talking about the significance
of the value of expr-3,


I'm not talking about any sort of significance.

Seemingly you're unable to discern the difference between perception
and knowledge.

Lets try a simpler example: the ancients thought frogs 'grew' from
mud, because after heavy rain, frogs were to be seen arising from the
puddles. We knowledgable people know this to be false, but we can
perhaps see why a people with less knowledge might be misled.


Ok, but we don't carefully remove frogs from mud to avoid confusing
any ancients who might see them.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 17 '05 #34
Mark McIntyre <ma**********@spamcop.net> writes:
On Wed, 16 Nov 2005 08:46:00 +0000, in comp.lang.c , Flash Gordon
<sp**@flash-gordon.me.uk> wrote:
I believe that Mark is talking about how people might miss-read the
code, not how it actually works. I'm sure Mark knows how it actually works.


At last. Somebody who is actually bothering to think ,as opposed to
merely restate segments of the Standard.
In the above none of us are the beginner because we all know better.


Precisely.


Mark, please don't assume that we're all idiots because we
misunderstand or disagree with what you're saying.

So, given these two lines of code:

for (i = 0; i < 10; i++) { ... }
for (i = 0; i < 10; ++i) { ... }

we're all in agreement that they're semantically identical (because
expr-3, either "i++ or "++i", is evaluated only for its side effects,
with the result being discarded).

The point on which we disagree is your claim that one form is more
intuitive than the other. I think a lot of people have misconstrued
what you've said because, frankly, it doesn't seem to us to make a
whole lot of sense.

The most likely explanation for thinking that i++ is better than ++i
in this context would be a mistaken belief that there's a semantic
difference. I understand that you do understand the semantics, and
that that's not the reason for your preference, but you haven't been
very quick to state this explicitly.

If you happen to have a personal preference here, that's fine. In
this particular case, though, you need to be aware that not only will
most others not share your preference, most of us won't even
understand the basis for it, and it's not because we're not bothering
to think.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 17 '05 #35
On 2005-11-17, Keith Thompson <ks***@mib.org> wrote:
Mark McIntyre <ma**********@spamcop.net> writes:
On Wed, 16 Nov 2005 08:46:00 +0000, in comp.lang.c , Flash Gordon
<sp**@flash-gordon.me.uk> wrote:
I believe that Mark is talking about how people might miss-read the
code, not how it actually works. I'm sure Mark knows how it actually works.


At last. Somebody who is actually bothering to think ,as opposed to
merely restate segments of the Standard.
In the above none of us are the beginner because we all know better.


Precisely.


Mark, please don't assume that we're all idiots because we
misunderstand or disagree with what you're saying.

So, given these two lines of code:

for (i = 0; i < 10; i++) { ... }
for (i = 0; i < 10; ++i) { ... }

we're all in agreement that they're semantically identical (because
expr-3, either "i++ or "++i", is evaluated only for its side effects,
with the result being discarded).

The point on which we disagree is your claim that one form is more
intuitive than the other. I think a lot of people have misconstrued
what you've said because, frankly, it doesn't seem to us to make a
whole lot of sense.

The most likely explanation for thinking that i++ is better than ++i
in this context would be a mistaken belief that there's a semantic
difference. I understand that you do understand the semantics, and
that that's not the reason for your preference, but you haven't been
very quick to state this explicitly.

If you happen to have a personal preference here, that's fine. In
this particular case, though, you need to be aware that not only will
most others not share your preference, most of us won't even
understand the basis for it, and it's not because we're not bothering
to think.


I think that it fundamentally comes down to it being what he learned
first. It's convention, plain and simple. I'll usually use i++ instead
of ++i too, because it's what I learned.
Nov 17 '05 #36
Jordan Abel wrote:
I think that it fundamentally comes down to it being what he learned
first. It's convention, plain and simple. I'll usually use i++ instead
of ++i too, because it's what I learned.


I actually switched my preference from i++ to ++i,
for expressions evaluated as void.

I consider the standard description of postfix increment
to be more complicated, than that of prefix increment.
Not much more complicated, just a little.

The point being, that I would need some reason
to use the conceptually more complicated increment.
A void expression evaluation provides no such reason.

When I see a postincremented void evaluted expression
in somebody else's code, I tend not to comment on it.
If I rewrite somebody else's posted code and post it,
there's a good chance that
I'll change postincrented void evaluated expressions
to preincremented, because then it's my code.

My preference may also be influenced by K&R writing one way
and Schildt writing the other.

I originally learned C by Schildt,
in three weeks!!!
and again over the course of years
in an ongoing process here on clc.

--
pete
Nov 17 '05 #37
pete <pf*****@mindspring.com> writes:
Jordan Abel wrote:
I think that it fundamentally comes down to it being what he learned
first. It's convention, plain and simple. I'll usually use i++ instead
of ++i too, because it's what I learned.


I actually switched my preference from i++ to ++i,
for expressions evaluated as void.

I consider the standard description of postfix increment
to be more complicated, than that of prefix increment.
Not much more complicated, just a little.

The point being, that I would need some reason
to use the conceptually more complicated increment.
A void expression evaluation provides no such reason.


Fair enough.

Personally, I don't even think about the distinction between i++ and
++i if it appears in a void context, and I really have no preference
one way or the other. If the result is used, I have to stop and think
about what's going on, but I actually think both forms are used more
often in void context than otherwise.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 17 '05 #38
On Thu, 17 Nov 2005 00:50:03 GMT, in comp.lang.c , Keith Thompson
<ks***@mib.org> wrote:
Mark, please don't assume that we're all idiots because we
misunderstand or disagree with what you're saying.
I don't. I have however been forced to the conclusion that several of
you are suffering from a bad case of "the other guy must be an idiot
because he doesn't understand the simple rules of
reverse-polish-flombat swarfling with the added bermudan option, but
disallowing cue bidding"
The point on which we disagree is your claim that one form is more
intuitive than the other. I think a lot of people have misconstrued
what you've said because, frankly, it doesn't seem to us to make a
whole lot of sense.


See above.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 19 '05 #39
On Thu, 17 Nov 2005 02:13:14 +0000 (UTC), in comp.lang.c , Jordan Abel
<jm****@purdue.edu> wrote:
I think that it fundamentally comes down to it being what he learned
first.


I learned both at the same time.

As it happens, I generally use ++var when I want to preincrement a
variable, and var++ when I want to postincrement it.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 19 '05 #40
Mark McIntyre <ma**********@spamcop.net> writes:
On Thu, 17 Nov 2005 02:13:14 +0000 (UTC), in comp.lang.c , Jordan Abel
<jm****@purdue.edu> wrote:
I think that it fundamentally comes down to it being what he learned
first.


I learned both at the same time.

As it happens, I generally use ++var when I want to preincrement a
variable, and var++ when I want to postincrement it.


But that's not the whole story. And if you just want to increment a
variable, in a context where there's no difference between
preincrement and postincrement, you seem to have a strong preference
for one of them (I don't remeber which). That's what we found
confusing.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 19 '05 #41
On Sat, 19 Nov 2005 18:48:01 GMT, in comp.lang.c , Keith Thompson
<ks***@mib.org> wrote:
you seem to have a strong preference
for one of them (I don't remeber which).
Not at all - I do however have a strong preference for clarity, and in
my experience, learners often have misunderstandings that can be
avoided by steering clear of potentially confusing constructs. For
instance, its often said that
if( 4 == somevariable )
is safer than
if(somevariable == 4)
because it avoids the possible mistake of the single equals. It is
however quite counterintuitive at first glance. Thus, while I do
believe the former is safer, I rarely recommend it.
That's what we found confusing.


I strongly suspect that what you found confusing was that to you,
these rules are *obvious* and self-explanatory, you couldn't
understand why anyone would find them otherwise, and therefore my
remark that the construct could confuse was itself baffling to you.

Sometimes one needs to step back from ones knowledge.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 19 '05 #42
Mark McIntyre wrote:
On Sat, 19 Nov 2005 18:48:01 GMT, in comp.lang.c , Keith Thompson
<ks***@mib.org> wrote:

you seem to have a strong preference
for one of them (I don't remeber which).

Not at all - I do however have a strong preference for clarity, and in
my experience, learners often have misunderstandings that can be
avoided by steering clear of potentially confusing constructs. For
instance, its often said that
if( 4 == somevariable )
is safer than
if(somevariable == 4)
because it avoids the possible mistake of the single equals. It is
however quite counterintuitive at first glance. Thus, while I do
believe the former is safer, I rarely recommend it.

That's what we found confusing.

I strongly suspect that what you found confusing was that to you,
these rules are *obvious* and self-explanatory, you couldn't
understand why anyone would find them otherwise, and therefore my
remark that the construct could confuse was itself baffling to you.

Sometimes one needs to step back from ones knowledge.


I write C as I say it. 'If var is 4' is written..

if (var == 4)

I never confuse == and = in these expressions. If I did, either the
compiler will tell me or the test results of the program will.

for (i = 0; i < N; ++i)

Again I say it "increment i" and therefore '++i'.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 19 '05 #43
Joe Wright wrote:
<snip>
I write C as I say it. 'If var is 4' is written..

if (var == 4)

I never confuse == and = in these expressions. If I did, either the
compiler will tell me or the test results of the program will.

for (i = 0; i < N; ++i)

Again I say it "increment i" and therefore '++i'.

And you never confuse ++i and i++ in these expressions, because the
compiler or the results will tell you...

Oh, and how do you pronounce "i |= 0x80", by the way? It's either going
to be a circumscription or something that's not English. I "say" this as
"i or-is oh ex eighty", but I don't assign any significance to this --
I'm just reading the symbols. If I were going from English to code, I'd
probably say "set the eighth/high/sign bit of i", depending on i.

"Increment i" does not have the unique translation "++i". Saying that
this is "the way you say it" doesn't mean anything more than "I just
prefer ++i". This "I write it as I say it" argument just muddies the
waters; it's no more or less arbitrary than anything.

The reason most of us don't write (4 == var) regardless of safety is
because it's just not written that way outside C. Open any textbook on
mathematics, sit in any class where equations are written down, and
you'll find that equalities are never written with a constant on the
left hand side and a variable on the right. That, in turn, is a result
of left-to-right writing in English, and the way equations are typically
solved. How we say it has little to do with it.

I've said "increment i" to "i = i + 1", not "i becomes equal to the old
value of i plus one" or suchlike. Of course I'd never write "increment
i" as "i = i + 1" in C, but whether I'll use ++i or i++ depends on the
expression.

If either will do, I'll use ++i, because this is what I'm used to from
(irony) C++. Of course I have no trouble reading any of these
expressions in an abstract sense, and nobody should. And if I can avoid
it, I won't try to express them in English either. English sucks for
writing programs in, though it's excellent at pseudocode.

S.
Nov 19 '05 #44
On 2005-11-19, Skarmander <in*****@dontmailme.com> wrote:
Oh, and how do you pronounce "i |= 0x80", by the way? It's either going
My german 'expression' for "i |= 0x80" is "i oder-gleich Null-x-Achtzig"
which would translate to (how is "=" pronounced in english?)
"i or-assign zero-x-eighty".
to be a circumscription or something that's not English. I "say" this as
"i or-is oh ex eighty", but I don't assign any significance to this --
Ah, I was almost right. :-)
"Increment i" does not have the unique translation "++i". Saying that
this is "the way you say it" doesn't mean anything more than "I just
prefer ++i". This "I write it as I say it" argument just muddies the
waters; it's no more or less arbitrary than anything.
It depends on the perspective.
I've said "increment i" to "i = i + 1", not "i becomes equal to the old
value of i plus one" or suchlike. Of course I'd never write "increment
i" as "i = i + 1" in C, but whether I'll use ++i or i++ depends on the
expression.


Sure it should. There's quite a difference between "a[++i] = 42" and
"a[i++] = 42". Obviously not in terms of "i", but in terms of "a[]".

But you all know that.

Markus
Nov 20 '05 #45
Markus Becker wrote:
On 2005-11-19, Skarmander <in*****@dontmailme.com> wrote:
Oh, and how do you pronounce "i |= 0x80", by the way? It's either going


My german 'expression' for "i |= 0x80" is "i oder-gleich Null-x-Achtzig"
which would translate to (how is "=" pronounced in english?)
"i or-assign zero-x-eighty".
to be a circumscription or something that's not English. I "say" this as
"i or-is oh ex eighty", but I don't assign any significance to this --


Ah, I was almost right. :-)


Make no mistake, "assign" is actually better than "is" or "gleich",
because it's *not* equality. But we're used to this by now. The horror
of languages that use := or similar for assignment! Think of all the
wasted keystrokes! :-)

S.
Nov 20 '05 #46
Joe Wright wrote:

I write C as I say it. 'If var is 4' is written..

if (var == 4)


How do you write:
p is a pointer to function taking an int
and returning a pointer to void
?

Nov 20 '05 #47
Old Wolf wrote:
Joe Wright wrote:
I write C as I say it. 'If var is 4' is written..

if (var == 4)

How do you write:
p is a pointer to function taking an int
and returning a pointer to void
?


Can I call you by your first name? Old? Maybe..

void *(*p)(int);

Close? What was the Wolf's point?

My original quip was that 'if (4 == var)' is counter intuitive and is
not used here.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 21 '05 #48
On Sun, 13 Nov 2005 23:57:55 GMT, pete <pf*****@mindspring.com> wrote:
Mark McIntyre wrote:

scanf("%s", &again);

}while(again == 'Y' || again == 'y');


again is a string, not a single char so you need "Y" and "y". Or
better yet, declare again as a character and use the %c format.


'again' was declared as a char. another global


So %s, which will (normally) store _at least_ 1 data character plus a
null terminator character, is UB. Use %c, and perhaps " %c" to skip
leading whitespace. Better yet, don't use scanf at all: FAQs 12.12-20
at usual places and http://www.eskimo.com/~scs/C-faq/top.html

But if it _were_ a string, then just substituting again == "Y" is
wrong in a different way; FAQ 8.2, or use again[0] == 'Y'.

- David.Thompson1 at worldnet.att.net
Nov 21 '05 #49
"Skarmander" <in*****@dontmailme.com> wrote in message
news:43***********************@news.xs4all.nl...
Joe Wright wrote:
<snip>
I write C as I say it. 'If var is 4' is written..

if (var == 4)

I never confuse == and = in these expressions. If I did, either the
compiler will tell me or the test results of the program will.
I have started using the "if (NO_ERROR == result)" syntax even tho I am
thoroughly anal in my code (at least, people tell me I'm a "complete
asshole", so I guess they mean my code is good...)
for (i = 0; i < N; ++i)

Again I say it "increment i" and therefore '++i'.

But in a for() loop, you want to have the idea that i will be plus-one
in the next loop, so i++ really states the idea better. In the case of
just incrementing a variable, I use "++j;" because I read it as
"increment j" as opposed to "j++;" which I read as "do nothing, then
increment j" which seems silly.

for (theRecord = Nothing; theRecord < Gold; theRecord++)
++sales;
Oh, and how do you pronounce "i |= 0x80"
Set high bit of i.
I'm just reading the symbols. If I were going from English to code, I'd probably say "set the eighth/high/sign bit of i", depending on i.
Sixteenth bit, I'm sure you meant to say...
"Increment i" does not have the unique translation "++i". Saying that
this is "the way you say it" doesn't mean anything more than "I just
prefer ++i". This "I write it as I say it" argument just muddies the
waters; it's no more or less arbitrary than anything.
Agreed. But I like it. "It's only Rock-n-Roll, but I like it..."
The reason most of us don't write (4 == var) regardless of safety is
because it's just not written that way outside C. Open any textbook on mathematics, sit in any class where equations are written down, and
you'll find that equalities are never written with a constant on the
left hand side and a variable on the right. That, in turn, is a result
of left-to-right writing in English, and the way equations are typically solved. How we say it has little to do with it.
But it IS a Good Idea. I have begun to do it, and I find it really isn't
the completely inane construct I thought it was. Of course, it scans
better when the constants are #define'd. It's not:
if (4 == ever)...

it's more like:
if (FILE_LOCKED == result) file_locked (fptr);
if (FILE_PROTECTED == result) file_prot (fptr);

So you see, the info important to the programmer is right up front, AND
the compiler will balk at an error. It's a two-fer. Worth embracing.
Think on it.

I've said "increment i" to "i = i + 1", not "i becomes equal to the old value of i plus one" or suchlike. Of course I'd never write "increment
i" as "i = i + 1" in C, but whether I'll use ++i or i++ depends on the
expression.

If either will do, I'll use ++i, because this is what I'm used to from
(irony) C++.
Double-plus good.
Of course I have no trouble reading any of these
expressions in an abstract sense, and nobody should.


Nobody should read any of these expressions in an abstract sense? Maybe
you're right... ;-)

--
Mabden
Nov 23 '05 #50

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Paul Sheer | last post by:
I need to automatically search and replace all fixed size buffer strcpy's with strncpy's (or better yet, strlcpy's) as a security and stability audit. The code base is large and it is not feasable...
9
by: Pascal Damian | last post by:
I read somewhere that strcpy() is safer when dealing with malloc()-ed strings. Is that true? (Of course I know that both are unsafe). -- Pascal
81
by: Matt | last post by:
I have 2 questions: 1. strlen returns an unsigned (size_t) quantity. Why is an unsigned value more approprate than a signed value? Why is unsighned value less appropriate? 2. Would there...
302
by: Lee | last post by:
Hi Whenever I use the gets() function, the gnu c compiler gives a warning that it is dangerous to use gets(). Is this due to the possibility of array overflow? Is it correct that the program...
38
by: edu.mvk | last post by:
Hi I am using strcpy() in my code for copying a string to another string. i am using static char arrays. for the first time it is exected correctly but the second time the control reaches...
4
by: chikito.chikito | last post by:
1. Can someone tell me the difference between these two functions: void strcpy(char *s1, const char *s2) { while(*s1++ = *s2++) ; } //function prototype of strcpy follows char...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.