Reputation: 405
I am writing a simple mathematical function reader in C and compiling with gcc-5. When I run the following code
#include <stdio.h>
#include <stdlib.h>
int read_input(char* func)
{
printf("Please enter the function: ");
if (fgets(func, sizeof func, stdin)) {
printf("The function is %s.\n",func);
}
return 0;
}
int main ()
{
char func[64];
read_input(func);
for(int i = 0; i < sizeof(func); i++) {
printf("Char %d in func is %c\n", i, func[i]);
}
return 0;
}
I get
Char 0 in func is h
Char 1 in func is e
Char 2 in func is l
Char 3 in func is l
Char 4 in func is o
Char 5 in func is
Char 6 in func is
Char 7 in func is
Char 8 in func is
Char 9 in func is
Char 10 in func is
Char 11 in func is
Char 12 in func is
Char 13 in func is
Char 14 in func is
Char 15 in func is
Char 16 in func is
Char 17 in func is
Char 18 in func is
Char 19 in func is
Char 20 in func is
Char 21 in func is
Char 22 in func is
Char 23 in func is
Char 24 in func is �
Char 25 in func is
Char 26 in func is @
Char 27 in func is
Char 28 in func is
Char 29 in func is
Char 30 in func is
Char 31 in func is
Char 32 in func is
Char 33 in func is
Char 34 in func is
Char 35 in func is
Char 36 in func is
Char 37 in func is
Char 38 in func is
Char 39 in func is
Char 40 in func is
Char 41 in func is
Char 42 in func is
Char 43 in func is
Char 44 in func is
Char 45 in func is
Char 46 in func is
Char 47 in func is
Char 48 in func is �
Char 49 in func is
Char 50 in func is @
Char 51 in func is
Char 52 in func is
Char 53 in func is
Char 54 in func is
Char 55 in func is
Char 56 in func is
Char 57 in func is
Char 58 in func is @
Char 59 in func is
Char 60 in func is
Char 61 in func is
Char 62 in func is
Char 63 in func is
Why am I getting a new line character and how I can I not get it in my fgets? Also what are all those weird characters that I am getting?
Upvotes: 1
Views: 1591
Reputation: 44274
The problem is that sizeof func
is different inside the function and in main. In the function it is:
sizeof(char*) // Will probably give 8 (or 4)
In main it is:
sizerof(char[64]) // Will give 64
So you only allow input of 8 (or 4) characters but you print 64.
You need to do:
1) Add size as a function argument and use sizeof func
when calling the function
2) Change the for-loop to use strlen(func)
instead of sizeof func
so what you only print out valid characters.
To get rid of a newline in the end of func
see https://stackoverflow.com/a/28462221/4386427
Upvotes: 0
Reputation: 2693
You're using sizeof
to determine the size of the array, and it will not return the no of characters that were entered, change the code that you're using to print the characters to this:
int size = strlen(func);
for(int i = 0; i < size; i++) {
printf("Char %d in func is %c\n", i, func[i]);
}
And you should pass the size of the array as argument to the function that reads input i.e.
void read_input(char* func, int size)
{
printf("Please enter the function: ");
if (fgets(func, size, stdin)) {
printf("The function is %s.\n",func);
}
}
As you can see, I've also changed the return type of the function, as it is useless to make the function return an int
when there is no need. Instead use void
Upvotes: 3
Reputation: 5079
The problem lies in sizeof func
.
That would be the size of a pointer.
Proper use would be strlen(func)
.
My output with strlen:
Please enter the function: hello
The function is hello.
Char 0 in func is h
Char 1 in func is e
Char 2 in func is l
Char 3 in func is l
Char 4 in func is o
Upvotes: 1
Reputation: 11237
if (fgets(func, sizeof func, stdin))
The problem is that arrays, when passed to functions, decay into a pointer to their first element. You are passing a char
array, which then decays into a pointer to char
. Thus, with the sizeof
statement, you are simply getting the size of a pointer on the current machine. The solution to this is to pass the number of elements as the argument to read_input
, and use that as the second argument of fgets
. Or, use strlen
.
Upvotes: 4