I fight with IDE all the time because I like spaces and blank lines but no line waste. I stand off operands, operators most places. I like to NOT bury the lead on if or while of call returns, either:
printf("Enter the value of x: ");
while ( 1 != scanf( "%d", &x )){
fprintf( stderr, "Invalid x input, must be a number!\n" );
}
printf("Enter the value of y: ");
while ( 1 != scanf( "%d", &x )){
fprintf( stderr, "Invalid y input, must be a number!\n" );
}
This reduces eyestrain for all, makes errors easier to see, improves grades and the boss's opinion, conserves vertical white space. I stack one liners but stand off all blocks. I do the {} always in case I want to add lines, like debug printouts. I say 'else' rarely, preferring to exit(), return, break, continue or use the ternary operator, in parentheses for operator precedence: ( <boolean_value> ? <true_value> : <false_value> ). I try to use switch whenever possible for more than 2 outcomes, as it is a wonderful place to hang comments, like SQL CASE, with no extra indent for cases!
int retries = 0, ret ;
char buf[4096];
do {
switch ( ret = read( fd, buf, sizeof( buf ))){
case -1: // report error
perror( "read(fd)" );
exit( 1 );
case 0: // nominal EOF; sockets, pipes return 0 for 0 length messages.
if ( ++retries > 100 ){
return 0 ; // assume normal EOF behavior for some function
}
continue ;
default: // we read something
retries = 0 ;
break;
}
} while ( ! ret );
// process the read() data
Of course, opinions vary! :D
int area = x*y;
is undefined behaviour becauseint x,y;
has no initialisation of the values.int area = x*y;
you aren't hooking up wires into some async logic gates circuitry, this actually translates into sequentially running CPU instructions, and takes whatever happened to be inx
andy
at that moment, multiplies it, and loads the result intoarea
. And thats exactly what ends up being printed.