I am currently learning C, and so I wanted to make a program that asks the user to input a string and to output the number of characters that were entered, the code compiles fine, when I enter just 1 character it does fine, but when I enter 2 or more characters, no matter what number of character I enter, it will always say there is just one character and crashes after that. This is my code and I can't figure out what is wrong.
int main(void)
{
int siz;
char i[] = "";
printf("Enter a string.\n");
scanf("%s", i);
siz = sizeof(i)/sizeof(char);
printf("%d", siz);
getch();
return 0;
}
I am currently learning to program, so if there is a way to do it using the same scanf() function I will appreciate that since I haven't learned how to use any other function and probably won't understand how it works.
Please, FORGET that
scanf
exists. The problem you are running into, whilst caused mostly by your understandable inexperience, will continue to BITE you even when you have experience - until you stop.Here is why:
scanf
will read the input, and put the result in thechar
buffer you provided. However, it will make no check to make sure there is enough space. If it needs more space than you provided, it will overwrite other memory locations - often with disastrous consequences.A safer method uses
fgets
- this is a function that does broadly the same thing asscanf
, but it will only read in as many characters as you created space for (or: as you say you created space for).Other observation:
sizeof
can only evaluate the size known at compile time : the number of bytes taken by a primitive type (int, double, etc) or size of a fixed array (like int i[100];). It cannot be used to determine the size during the program (if the "size" is a thing that changes).Your program would look like this:
I hope this helps.
update you asked the reasonable follow-up question: "how do I know the string was too long".
See this code snippet for inspiration:
Remember that when you have space for N characters, the last character (at location
N-1
) must be a'\0'
and sincefgets
includes the'\n'
the largest string you can input is reallyN-2
characters long.This line:
is equivalent to:
The array
i
has only one element, the program crashes because of buffer overflow.I suggest you using
fgets()
to replacescanf()
like this:The length is decremented by
1
becausefgets()
would store the new line character at the end.That's because
char i[] = "";
is actually an one element array.Strings in C are stored as the text which ends with
\0
(char
of value 0). You should use bigger buffer as others said, for example:Then, when calculating length of this string you need to search for the
\0
char.After running this code
length
contains length of the specified input.The problem is here:
You are essentially creating a
char array
with a size of 1 due to setting it equal to""
;Instead, use a buffer with a larger size:
See the link below to a similar question if you want to include spaces in your input string. There is also some good input there regarding
scanf
alternatives.How do you allow spaces to be entered using scanf?
You need to allocate space where it will put the input data. In your program, you can allocate space like:
Which will be ok. But, using
malloc
is better. Check out the man pages.