On Oct 29, 9:42*am, "FredK" <fred.nos...@dec.com> wrote:
> "Chad" <cdal...@gmail.com> wrote in message
>
> news:4f0b9ee4-9957-4430-95e3-...
> On Oct 26, 7:08 pm, Keith Thompson <ks...@mib.org> wrote:
>
> > Chad <cdal...@gmail.com> writes:
>
> [snip]
>
>
>
> > Wouldn't it be more convenient to have the function return the result
> > rather than assigning it via a pointer parameter?
>
> |A lot of the engineers at Lawrence Berkeley National Laboratory use
> |this method. I guess in my case, it's monkey see, monkey do. I don't
> |know if there is a formal computer science name to this technique or
> |if this is just some kind of strange Berkeley thing.
>
> It is not an uncommon convention in some programming environments that the
> return from all procedure calls is a status code (or none if there is no
> meaningful status).
And in retrospect, I guess I would use fgets() instead of scanf().
Maybe something like the following...
[cdalten@localhost oakland]$ more getin.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 10
int main(void)
{
char *buf;
char first[MAX];
char second[MAX];
printf("Input the first string:");
fflush(stdout);
if (fgets(first, MAX, stdin) != NULL) {
if (strlen(first) + 1 >= MAX) {
fprintf(stderr, "The first string is too long\n");
exit(EXIT_FAILURE);
} else if ((buf = strchr(first, '\n')) != NULL) {
*buf = '\0';
printf ("The first string is: %s\n", first);
}
}
printf("Input the second string:");
fflush(stdout);
if (fgets(second, MAX, stdin) != NULL) {
if (strlen(second) + 1 >= MAX) {
fprintf(stderr, "The second string is too long\n");
exit(EXIT_FAILURE);
} else if ((buf = strchr(second, '\n')) != NULL) {
*buf = '\0';
printf ("The second string is: %s\n", second);
}
}
exit(EXIT_SUCCESS);
}
[cdalten@localhost oakland]$ gcc -Wall -Wextra getin.c -o getin
[cdalten@localhost oakland]$ ./getin
Input the first string:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
The first string is too long
[cdalten@localhost oakland]$ ./getin
Input the first string:a
The first string is: a
Input the second string:b
The second string is: b
[cdalten@localhost oakland]$ ./getin
Input the first string:a
The first string is: a
Input the second
string:bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
The second string is too long
[cdalten@localhost oakland]$
|