Saturday, June 04, 2005

Learning C

Few things that i learnt from K & R today.
1) (Page 148) quote from="K&R" A union may only be initialized with a value of the type of its first member: thus the union u described above can be initialized with integer value
The truth is that in spite of all these words by K&R, this code works
int main()

int ival;
char cval;
float fval;
} u;
printf("%c %d",u.cval,u.ival);

2) (Page 150) Bit fields *should* be only integer, i never learnt much about bit fields, but now i understand their importance.
3)sizeof is a compiler directive
4) you cant create pointers for a register variable, although i knew this, i think, its nice to jot it down here
5) Crooked examples of pointers:
char (*(*x[3])())[5]
x: an array of [3] of pointer to function returning pointer to char of array[5] (you cant get worser than this)
6)typedefs when properly used can save a lot of head scratches!!!. I rarely(when did that rare even happen?) use typedefs in my code.
7) (this one from venks)
#include <stdio.h>
#include <unistd.h>
int main()
{ while(1)
return 0;

Now i try to compile and execute:
[varun@localhost a]$ gcc test.c
[varun@localhost a]$ ./a.out
hello-errhello-errhello-errhello-err ..... i get this continuously.
[varun@localhost a]$

The question is, why the hell am i getting this? why dont i even get a string of hello-out?
Answers happily invited..


srid said...

This is not initialization, it is called assignment statement.

The question is, why the hell am i getting this? why dont i even get a string of hello-out?
Both the different streams are printed in same console and stdout is buffered before printed in console.

Varun Soundararajan said...

Yes, i get it. Its assignment, not initialization.
Does that also mean that after i fill my stdout buffer substantially (so that the buffer is filled up and it is forced to do a fflush(), i shd get hello-out, right?

Search this site