Posts Tagged ‘C’

Source

a continuation to the post

const char *p,**p2;
char *c,**c2;
p=c; //no warning
c=p; //warning a const pointer to a non const pointer
c2=p2; //warning incompatible type pointer assignment
p2=c2; //warning incompatible type pointer assignments

Explanation :
p=c -> in this assignment all the attributes of RHS are retained with an extra attribute (const) added hence no warning
c=p -> in this assignment, RHS loses one of its attribute
c2=p2 / p2=c2 -> in this assignment, both c2 and p2 are the pointers to pointers and C does not carry the compatibility of pointers associatively to next level and hence incompatible pointer assignment


if( -1 < (unsigned int) (1) )
printf("ANSI C\n");
else
printf("K & R C\n");

Output of the above code is : “K & R C”
Acc. to ANSI standards, signedness of the operands is retained while acc. to K & R C, if either operands is unsigned, conversion is unsigned. hence it really becomes a compiler specific problem and hence whenever you want to obtain a consistent result out of these kind of codes, use specific type casted conversions.

 

[update]

Flexible Size Array
In a structure, you can have the last element as a size zero element. It helps to have the flexible sized array.
struct myStruct
{
int length;
int arr[0]
}myStruct_t;
myStruct_t *ms = malloc (sizeof(myStruct) + size * (sizeof(int)));
ms->length = size;

Here element arr of myStruct can have size no. of elements.
More details:
http://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html

Happy programming.

Following is the code to clear the screen on terminal.

main() {write(1, "\33[H\33[2J", 7);}

Above program does nothing but writes an escape sequence on screen. Escape sequences are used to control the display properties like graphics,control cursor movements and key reassignment. Above one is specific to clear the screen. \33[H sends the cursor to home location and \33[2J clears the text on screen. It can also be used interchangeably i.e first clear the screen and then send the cursor to home location since printing of terminal info (like root@localhost] ) takes place after executing the complete process though if you write the separate programs for both escape sequences individually and then execute them sequencially, output may differ based on order of execution.

To read about what are the escape sequences, refer to http://en.wikipedia.org/wiki/Escape_sequence

Here you can find a complete list of escape sequences. Have fun with your terminal 🙂

smallest compilable program:
main;

smallest executable program:
main();

declaration of an integer global/function variable without using keyword int:
var1;
main(var2,var3){}

another way to declare the variables without using datatype keywords:
__typeof(1) var_name; //declares a variable of integer type
__typeof(1.0) var_name; //declares a variable of double type

reading garbage input without storing it to any variable
scanf(“%*s”);

You can separate the words using multiline comments characters instead of space:
int/**/var1,var2;

Well C is a powerful language and with years, so many features have been added to the language which we don’t use in daily life programming as a developer but its always fascinating to know the hidden concepts of a programming language as a programmer. Happy Programming.