C: scanf to array

if (array[0]=1) should be if (array[0]==1). The same with else if (array[0]=2). Note that the expression of the assignment returns the assigned value, in this case if (array[0]=1) will be always true, that’s why the code below the if-statement will be always executed if you don’t change the = to ==. = is the assignment … Read more

Reaching EOF with fgets

I’m writing a function that perform some authentications actions. I have a file with all the user_id:password:flag couples structured like this: Users.txt user_123:a1b2:0 user_124:a2b1:1 user_125:a2b2:2 This is the code: How can I manage the EOF reaching? I saw that when EOF is reached fgets doesn’t edits anymore the usr_psw_line but neither returns a NULL pointer. … Read more

warning: return makes pointer from integer without a cast but returns integer as desired

It’s not obvious what you’re trying to accomplish here, but I’ll assume you’re trying to do some pointer arithmetic with x, and would like x to be an integer for this arithmetic but a void pointer on return. Without getting into why this does or doesn’t make sense, you can eliminate the warning by explicitly … Read more

Return a `struct` from a function in C

You can return a structure from a function (or use the = operator) without any problems. It’s a well-defined part of the language. The only problem with struct b = a is that you didn’t provide a complete type. struct MyObj b = a will work just fine. You can pass structures to functions as … Read more

Math constant PI value in C

Calculating PI value is one of the complex problem and wikipedia talks about the approximations done for it and says it’s difficult to calculate PI accurately. How does C calculate PI? Does it calculate it every time or is it using a less accurate fixed value?

How does the fscanf function work?

You have to focus in the double loop you have there: EOF stands for End Of File, so the first loop will continue until the end of the file is found, i.e. until the file pointer reaches the end of the file. Now remember that your file has data like this: Steve Stevenson 2 9 … Read more

execvp: bad address error

The code seems to miss to NULL-terminate arrays[k]. Make the last entry in arrays[k] carry NULL. Update: This should be of even more straight forward just: as the members of the exec*()-family of functions return on error only.