switch case: error: case label does not reduce to an integer constant

switch labels must be constant expressions, they have to be evaluated at compile time. If you want to branch on run-time values, you must use an if. A const-qualified variable is not a constant expression, it is merely a value you are not allowed to modify. The form of integer constant expressions is detailed in … Read more

Categories C Tags

C Unknown type name ‘my_structure’

Because of how you’ve ordered your includes, the compiler sees void some_func(my_structure *x); before it sees typedef struct abcd { int a; } my_structure;. Let’s walk this through. Assuming my_struct.h is processed first, we get the following sequence of events: UTILSH is defined MAINH is defined Because UTILSH is already defined, we don’t process my_struct.h … Read more

How to repeat a char using printf?

Short answer – yes, long answer: not how you want it. You can use the %* form of printf, which accepts a variable width. And, if you use ‘0’ as your value to print, combined with the right-aligned text that’s zero padded on the left.. produces: With my tongue firmly planted in my cheek, I … Read more

Is there a standard sign function (signum, sgn) in C/C++?

I want a function that returns -1 for negative numbers and +1 for positive numbers. http://en.wikipedia.org/wiki/Sign_function It’s easy enough to write my own, but it seems like something that ought to be in a standard library somewhere. Edit: Specifically, I was looking for a function working on floats.

C fopen vs open

First, there is no particularly good reason to use fdopen if fopen is an option and open is the other possible choice. You shouldn’t have used open to open the file in the first place if you want a FILE *. So including fdopen in that list is incorrect and confusing because it isn’t very … Read more

C char array initialization

This is not how you initialize an array, but for: The first declaration: char buf[10] = “”; is equivalent to char buf[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; The second declaration: char buf[10] = ” “; is equivalent to char buf[10] = {‘ ‘, 0, 0, 0, 0, 0, 0, … Read more

Implementation of strtok() function

Internal implementation of strtok has already been discussed here: How does strtok() split the string into tokens in C? In your type of implementation ( calling it your type because it is quite different from the actual one), you have not allocated any memory dynamically to the local variable ‘W’. So when you return it, … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)