Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
This C program is not giving correct output when entered a 20 char string and asked for 1-20. It is to extract part of a given string.?

3 Answers
- husoskiLv 74 years ago
There are a few things that are wrong about that code, but the worst bit is gets(). This flat wrong, and is a "never use" part of the pre-C99 standard library, and one that has been removed from the current standard.
It never should have been part of the library in the first place. Insteadm, use:
fgets(string, sizeof string, stdin);
...and be warned that the closing \n character is going to be part of the string. Allocate at least 2 extra chars...one for the \n and one for the \0 string terminator.
Second is getch(). That doesn't exist in the standard library. There's a Unix function by that name (part of the curses library) and an MS-DOS function from the conio iibrary that's still supported under Windows. Neither of those is a C standard, and you haven't included a header to say which it is supposed to be.
Standard C requires an int result from main(). Good practice dictates returning a 0 when nothing bad happened.
If you are using Visual C++ as a C compiler under Visual Studio, you don't need that pause at the end. Just make sure your project is a Win32 Console Project and use "Start Without Debugging" (Ctrl+F5) to run it.
You make no check as to whether anything was actually read into start and num, or if the numbers are valid (0..18). Don't worry too much about this right now, but if you mistype a number, you can get segment faults, application crashes, or inexplicable output.
You can test the return from scanf() if you want. It returns an int saying how many % fields were converted and stored.
-------------------------------
The quickest way to get what you have working on good input is to expand the arrays and intialize them to zeroes with:
char string[200]={0}, temp[200]={0};
- Robert JLv 74 years ago
C strings are terminated by a null.
You need to allow extra space in the char array for the terminator and also check for that when copying characters.
And, very importantly, check the numbers entered so they cannot be "out of bounds" - at the moment it will take any number and eg. the start can be past the end of the string, copying data from a different part of memory or causing a crash.
- 4 years ago
//code in the question
#include<stdio.h>
void main()
{
int j=0, i, start, num;
char string[20], temp[20];
clrscr();
puts("enter a string");
gets(string);
puts("enter start, number of char.");
scanf("%d %d",&start,&num);
i=start-1;
while(i<(start+num-1))
{
temp[j]=string[i];
j++;
i++;
}
puts(temp);
getch();
}



