Recently, I compiled the following program, and it ran accurately. But when I manipulated the code in the program, even if I compiled without problem that it doesn't run properly.
PROGRAM 1: Here is the unmodified code, and it's running properly;
Code:
#include <stdio.h>
#include <stdlib.h>
void copy1(char* const, const char* const);
void copy2(char* , const char*);
int main()
{
char string1[40];
char *string2 = "Bendimi cigner asarim!"; // 22 literal character
char string3[40];
char string4[] = "Hangi yamyam bana cilgin gibi bakicakmis, sasarim!"; // 50 literal character
copy1(string1,string2);
printf("%s\n",string1);
copy2(string3,string4);
printf("%s\n",string3);
return 0;
}
void copy1(char *const s1, const char *const s2)
{
int i;
for (i=0; (s1[i]=s2[i]) != '\0'; i++) {
;
}
}
void copy2(char *s3, const char *s4)
{
for( ; (*s3=*s4) != '\0'; s3++,s4++) {
;
}
}
PROGRAM 2: Here is the modified code, and it's running properly;
Code:
#include <stdio.h>
#include <stdlib.h>
void copy1(char* const, const char* const);
void copy2(char* , const char*);
int main()
{
char string1[40];
char string2[] = "Bendimi cigner asarim!"; // LOOK AT NOTATION IN THIS LINE
char string3[40];
char *string4 = "Hangi yamyam bana cilgin gibi bakicakmis, sasarim!"; // AND THIS LINE...
copy1(string1,string2);
printf("%s\n",string1);
copy2(string3,string4);
printf("%s\n",string3);
return 0;
}
void copy1(char *const s1, const char *const s2)
{
int i;
for (i=0; (s1[i]=s2[i]) != '\0'; i++) {
;
}
}
void copy2(char *s3, const char *s4)
{
for( ; (*s3=*s4) != '\0'; s3++,s4++) {
;
}
}
PROGRAM 3: Here is the modified code, and it's running properly;
Code:
#include <stdio.h>
#include <stdlib.h>
void copy1(char* const, const char* const);
void copy2(char* , const char*);
int main()
{
char string1[10]; // LOOK AT SIZE
char *string2 = "Bendimi cigner asarim!";
char string3[10];// AND THIS...
char string4[] = "Hangi yamyam bana cilgin gibi bakicakmis, sasarim!";
copy1(string1,string2);
printf("%s\n",string1);
copy2(string3,string4);
printf("%s\n",string3);
return 0;
}
void copy1(char *const s1, const char *const s2)
{
int i;
for (i=0; (s1[i]=s2[i]) != '\0'; i++) {
;
}
}
void copy2(char *s3, const char *s4)
{
for( ; (*s3=*s4) != '\0'; s3++,s4++) {
;
}
}
Program 4: Here is the modified program, and it is not running properly!
Code:
#include <stdio.h>
#include <stdlib.h>
void copy1(char* const, const char* const);
void copy2(char* , const char*);
int main()
{
char string1[10]; // LOOK AT SIZE
char string2[] = "Bendimi cigner asarim!"; // LOOK AT NOTATION
char string3[10];// SIZE...
char *string4 = "Hangi yamyam bana cilgin gibi bakicakmis, sasarim!"; // LOOK AT NOTATION
copy1(string1,string2);
printf("%s\n",string1);
copy2(string3,string4);
printf("%s\n",string3);
return 0;
}
void copy1(char *const s1, const char *const s2)
{
int i;
for (i=0; (s1[i]=s2[i]) != '\0'; i++) {
;
}
}
void copy2(char *s3, const char *s4)
{
for( ; (*s3=*s4) != '\0'; s3++,s4++) {
;
}
}
Questions:
1. string2 has 22 literal characters and string4 has 50 literal characters, doesn't they?
2. How string4 which has 50 literal characters can copy entirely to string3 in the PROGRAM 1 when string3 can take 40 literal characters as maximum? Why do the compiler give an error about this issue, and run properly?
3. How string2 which has 22 literal characters can copy entirely to string1 int he PROGRAM 3 when string 1 can take 10 literal characters as maximum? Why do the compiler give an error about this issue, and run properly?
4. Seeing that the compiler runs the codes(in PROGRAM 1,2,3) properly, ignoring the sizes of strings,well, why the PROGRAM 4 do not run properly? Why the compiler give a warning or an error for PROGRAM 4, although it can run even if it execute the problematic?
Note: PROGRAM 1, PROGRAM 2 and PROGRAM 3 is output in this way:
PROGRAM 4 is output in this way: