(e-mail address removed) a écrit :
I have written a program to concatanae two strings, and should be
returned to the main program. Iam enclosing the code,
please give me ur critics. Thanks,
First of all, I'd suggest
- A better trimming of your compiler:
Compiling: main.c
main.c:2: warning: return type defaults to `int'
main.c: In function `main_':
main.c:4: error: implicit declaration of function `printf'
main.c:4: warning: nested extern declaration of `printf'
<internal>:0: warning: redundant redeclaration of 'printf'
main.c:5: error: implicit declaration of function `scanf'
main.c:5: warning: nested extern declaration of `scanf'
<internal>:0: warning: redundant redeclaration of 'scanf'
main.c:8: error: implicit declaration of function `string_concatenate'
main.c:8: warning: nested extern declaration of `string_concatenate'
main.c: At top level:
main.c:11: warning: return type defaults to `int'
main.c:11: warning: no previous prototype for 'count_characters'
main.c:18: warning: return type defaults to `int'
main.c:18: warning: no previous prototype for 'string_concatenate'
main.c: In function `string_concatenate':
main.c:26: warning: nested extern declaration of `printf'
<internal>:0: warning: redundant redeclaration of 'printf'
main.c:27:2: warning: no newline at end of file
Process terminated with status 1 (0 minutes, 0 seconds)
3 errors, 10 warnings
- A better presentation (indentation) Use spaces instead of tabs.
You code commented. Please ask for details if you need it.
/* -ed- missing headers added */
#include <stdio.h>
/* -ed-
- code rearranged according to the
"define before use"
state-of-the-art rule
- static added to the non exported functions according to the
"scope shall be reduced to minimum"
state-of-the-art rule
*/
/* -ed-
count_characters(char *x)
- explicit return type added (mandatory in C99)
- this functions only reads the string. It may accept the address of
a read-only string. 'const' qualifier added.
*/
static int count_characters(char const *x)
{
int i = 0, count = 0;
/* -ed
for (i = 0;*(x + i) != '\0';i++)
*(x + i) is a complicated way of writing x
*/
for (i = 0; x != '\0'; i++)
{
/* -ed- { } added for maintenance facilities... */
count++;
}
/* -ed- do you really need count ? How is it different from i ? */
return count;
}
/* -ed-
Ok, I understand that you are learning C and that you are building your
own tools. But note that strlen() is part of the standard C library and
that is grossly achieves what you wanted here.
*/
static void string_concatenate(char *s1, char *s2)
{
int i, j;
char s3[50];
/* -ed-
how to be sure that 50 bytes is enough ?
Additionally, you never test the size in the function.
This is a serious potential bug.
*/
/* -ed-
for (i = 0;i < count_characters(s1);i++)
This is nuts. You recompute the length at each turn.
It is obvioulsy completely inefficient.
*/
int const len1 = count_characters(s1);
for (i = 0;i < len1;i++)
{
s3 = s1;
}
{
int const len2 = count_characters(s2);
for (j = 0;j < count_characters(s2);j++)
{
s3[i + j] = s2[j];
}
}
/* -ed- Ok you didn't forgot the final 0. Good point. */
s3[i + j] = s2[j];
/* -ed- debug : ,' added. Also, \n added for reasons exposed further */
printf("'%s'\n", s3);
}
int main()
{
char s1[20], s2[20];
/* -ed-
printf("enter first string");
Warning. To be sure that the text comes out, you must terminante the
string with a \n or force the output with fflush(stdout).
Also, for a better presentation, I suggest to add ": "
*/
printf("enter first string: ");
fflush(stdout);
/* -ed-
scanf("%s", s1);
scanf() is a hard-to-use-it-right function.
Even advanced programmers avoid it like plague...
The way you use it is dangerous. It behaves like gets(), hence there
is no input limit control.
For a quick-and-dirty code you can use it as follow, but it's
ugly and
hard to maintain... Also, the returned value should be tested,
and there
are issues with string containing blanks and eventually with pending
characters, I'm not sure... (I don't use scanf())
A better solution to get a line is to use fgets(), it's not trivial,
but at least, it can be used safely.
*/
scanf("%19s", s1);
printf("enter second string: ");
fflush(stdout);
scanf("%19s", s2);
/* -ed-
string_concatenate(&s1, &s2);
Wrong type. According to the prototype, you want to pass the
address of
a char (here, the address of the 1st element of the array),
hence &s1[0], that is also written s1 + 0 or more simply s1.
Ditto for s2.
*/
string_concatenate(s1, s2);
/* explicit return in main() added (mandatory in C90) */
return 0;
}