Structure padding and packing

Padding aligns structure members to “natural” address boundaries – say, int members would have offsets, which are mod(4) == 0 on 32-bit platform. Padding is on by default. It inserts the following “gaps” into your first structure: Packing, on the other hand prevents compiler from doing padding – this has to be explicitly requested – under GCC it’s __attribute__((__packed__)), so the following: … Read more

Can’t understand the working of getint() in C as per K&R

The getint() function only reads digits from the input. If it gets a character that is not a digit or a + – sign at the beginning it will call ungetch() to push the character back into the input buffer so it could be read by some other function call. getint() will go on returning … Read more

Categories c Tags

getc() vs fgetc() – What are the major differences?

From the Advanced Programming in Unix Environment: … The difference between getc and fgetc is that getc can be implemented as a macro, whereas fgetc cannot be implemented as a macro. This means three things: The argument to getc should not be an expression with side effects. Since fgetc is guaranteed to be a function, we can take its address. This allows us to pass the address of fgetc as … Read more

Why is the sizeof(int) == sizeof(long)?

A long, as far as I know, is 8 bytes. Is this correct? No, this is not correct. The C and C++ specifications only state that long must be greater than or equal to 32 bits. int can be smaller, but on many platforms, in C and C++, long and int are both 32 bits. This is a very good reason … Read more

What is the difference between %g and %f in C?

See any reference manual, such as the man page: f,F The double argument is rounded and converted to decimal notation in the style [-]ddd.ddd, where the number of digits after the decimal-point character is equal to the precision specification. If the precision is missing, it is taken as 6; if the precision is explicitly zero, no … Read more

Difference between fgets and fscanf?

The function fgets read until a newline (and also stores it). fscanf with the %s specifier reads until any blank space and doesn’t store it… As a side note, you’re not specifying the size of the buffer in scanf and it’s unsafe. Try:

What exactly is meant by “de-referencing a NULL pointer”?

A NULL pointer points to memory that doesn’t exist. This may be address 0x00000000 or any other implementation-defined value (as long as it can never be a real address). Dereferencing it means trying to access whatever is pointed to by the pointer. The * operator is the dereferencing operator: This is exactly the same thing as a NullReferenceException in C#, except that pointers … Read more

Where is the header file on Linux? Why can’t I find ?

conio.h is a C header file used with old MS-DOS compilers to create text user interfaces. Compilers that target other operating systems, such as Linux-based, 32-bit Windows and OS/2, provide equivalent functionality through other header files and libraries. The #include <curses.h> will give you almost all of the functionality provided by conio.h. “ncurses” needs to be installed in the … Read more

Can I define a function inside a C structure?

No, you cannot define a function within a struct in C. You can have a function pointer in a struct though but having a function pointer is very different from a member function in C++, namely there is no implicit this pointer to the containing struct instance. Contrived example (online demo http://ideone.com/kyHlQ):

note: previous implicit declaration of ‘point_forward’ was here

The key is in this: previous implicit declaration of ‘point_forward’ was here On line 96 you have: Since the compiler hasn’t yet seen a function declaration for point_forward(m), it “implicitly defines” (ie, assumes) a function that returns an int: This conflicts with the definition later: To fix this, you can either: Put an explicit declaration somewhere before line … Read more

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