in Education by
See the following program: #include #include #include #include main(void){ printf("Array concatenateor\n-------------------------\n"); //declarations char s1[50],s2[50],s3[50]; short int n=0; //Array initialisation printf("Array 1: "); gets(s1); printf("Array 2: "); gets(s2); strcpy(s3, s1); //asure initial form of s1 in s3 strcat(s1, s2); //concatenate s1 with s2 //at this point s1 is in the concatenation form and s3 is s1's initial form printf("Arrays concatenated with STRCPY: \"%s\"\n", s1); //print concatenation, s3 ok printf("Number of characters to concatenate: "); //scan number, s3 ok scanf("%d",&n); //beyond this point s3 becomes null... peculiar printf("S3: %s\n",s3); //this is the proof of s3 being null strncat(s3,s2,n); //s3 concatenates with n chars of s2 printf("Arrays concatenated with STRNCAT(%d chars): %s\n", n, s3); //print s3 system("PAUSE"); return 0; } It is peculiar how that specific scanf erases the s3 array without even being implied. How come this happens? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Try making "n" an "int" rather than a "short int", run it, and see if that fixes it. If so, I'll explain why. As Soren points out, scanf expects to read in 32 bits of data; when the buffer is only 16 bits wide, the extra memory is shuffled somewhere it doesn't belong, essentially zeroing out memory (assuming the user enters a small enough number).

Related questions

0 votes
    Closed. This question needs details or clarity. It is not currently accepting answers. Want to improve this ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
+1 vote
    Good graphical representation ___________________ data-ink and erases as much non-data-ink as possible....
asked Oct 11, 2020 in Education by anonymous
0 votes
    I was supposed to do this exercise: Write a program in C to print a frequency chart on the screen ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    Explain the use of %i format specifier w.r.t scanf()?...
asked Jan 17, 2021 in Technology by JackTerrance
+1 vote
    Explain the use of %i format specifier w.r.t scanf() in C-Programming?...
asked Nov 9, 2020 in Technology by JackTerrance
0 votes
    I got the json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) when I tried to access to the values from a json ... f: data = json.load(f) data[account] = {} #...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    I have to copy classpath resource from one package to another. My program is: public static void main( ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    I just got Delphi 2009 and have previously read some articles about modifications that might be necessary because ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 18, 2022 in Education by JackTerrance
0 votes
    I just got Delphi 2009 and have previously read some articles about modifications that might be necessary because ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 18, 2022 in Education by JackTerrance
0 votes
    I just got Delphi 2009 and have previously read some articles about modifications that might be necessary because ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 16, 2022 in Education by JackTerrance
0 votes
    The default life of function is :- a) int b) float c) char d) void Select the correct answer from above options...
asked Dec 27, 2021 in Education by JackTerrance
0 votes
    The default life of function is :- a) int b) float c) char d) void Select the correct answer from above options...
asked Dec 26, 2021 in Education by JackTerrance
0 votes
    Write the output for the following i) char c = ‘A’ ; int m = 26 ; int n = c + m ; System.out.println(“ n = “ +n) ; Select the correct answer from above options...
asked Dec 19, 2021 in Education by JackTerrance
0 votes
    Write the output for the following i) char c = ‘A’ ; int m = 26 ; int n = c + m ; System.out.println(“ n = “ +n) ; Select the correct answer from above options...
asked Dec 18, 2021 in Education by JackTerrance
0 votes
    Rewrite the following JAVA program segment using switch case.. char code; if(code=='B'||code=='b') System.out. ... now .plz help me . Select the correct answer from above options...
asked Dec 17, 2021 in Education by JackTerrance
...