1

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?

thephysicsguy
  • 403
  • 1
  • 4
  • 15
  • 1
    The string is null terminated so only read until strlen(func) : not until sizeof(func). – cup Aug 22 '16 at 13:56

4 Answers4

4
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.

lost_in_the_source
  • 10,998
  • 9
  • 46
  • 75
  • 3
    It's not even a question of arrays decaying. In the context of the line you call out, `func` (terrible name, BTW) is declared as a pointer, not an array. And of course `sizeof` uses only the *type* of its operand, not its value. – John Bollinger Aug 22 '16 at 14:02
  • So when I do sizeof func I am getting the size of the pointer which is obviously wrong? I added int func_size to read_input()'s arguments and passed strlen of func from main and updated the fgets inputs but I still get the weird characters and the new line. – thephysicsguy Aug 22 '16 at 14:04
  • You could use `strlen` in the `read_input` function. No need for the extra argument, because the array in your case is null terminated – lost_in_the_source Aug 22 '16 at 14:05
  • ...which only leaves the trailing newline to account for (the first actual question in the OP's post). – WhozCraig Aug 22 '16 at 14:06
  • 3
    "Or, use strlen." --> This is a questionable part of your answer. How would code use `strlen()` to pass the number of elements as the argument to `read_input()`? – chux - Reinstate Monica Aug 22 '16 at 14:17
3

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

Ahmad Khan
  • 2,655
  • 19
  • 25
1

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
Mirakurun
  • 4,859
  • 5
  • 16
  • 32
0

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

Community
  • 1
  • 1
Support Ukraine
  • 42,271
  • 4
  • 38
  • 63