'C makes it easy to shoot yourself in the foot. C++ makes it harder, but when you do, it blows away your whole leg.' -Bjarne Stroustrup

Reference : http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf

ed2k://|file|n1256.pdf|3788603|a728a3c5e98e681a8c389ffc46640625|

DEVEL / C

[[Langage]] Keywords :

  • if else
  • for while do
  • return
  • struct typedef enum
  • main

TYPES

  • special type “void” which is not really a type
  • lang defined basic types : char, short, int, void, long, double

On X86 architechure (running Linux or Windows ) :

# sizeof(char)=1
# sizeof(void)=1 @ gcc@[[Linux]] & 0 @ [[MSVC]]@[[Windows]]
# sizeof(short)=2
# sizeof(int)=4
# sizeof(void*)=4
# sizeof(char*)=4
# sizeof(long int)=4
# sizeof(long)=4
# sizeof(double)=8
# sizeof(long long)=8 // long long is not ansi and not supported by msvc
  • arrays and pointers , char ptr[] ~=~ char* ptr
  • structures :

struct buffer { unsigned int size_; char* pbytes_; }

  • enum

enum colors { RED=1, YELLOW, GREEN=6, BLUE };

Now RED=1, YELLOW=2, GREEN=6 and BLUE=7.

The main advantage of enum is that if you don't initialize your constants, each one would have a unique value. The first would be zero and the rest would then count upwards.

T limitmax = ((tmp=~0)==(tmp>>1)?~(tmp<<((sizeof(tmp)<<3)-1)):tmp);

ARRAYS

C & C++ only support:

int w[[]] = {1,2,3};

Java supports both :

int[[]] v = {1,2,3};
int w[[]] = {1,2,3};
//static allocation (on compile time)
static char img[[h]][[w]][[IMAGE_DEPTH]];
img[[h]][[w]][[k]] = 'a';
unsigned char* ptr = &img[[0]][[0]][[0]];
// dynamic allocation (on run
pimg = new char[[h*w*IMAGE_DEPTH]]; time)
pimg[[(ih*w*IMAGE_DEPTH)+(iw*IMAGE_DEPTH)+k]] = 'a' ;
// both are stored the same in RAM
memcmp( ( char*) pimg, ( char*) ptr, h*w*IMAGE_DEPTH) ); // =0 //same

See how array can be usefull

int f(int c[[]][[2]] ) { c[[0]][[0]] = 1; }
int main(int argc, char* argv[[]]) {
int a[[1]][[2]];
a[[0]][[0]] = 0;
f(a);
debugi( a[[0]][[0]] ); //1
}

OPERATORS

Bit MASK :

int reverseColor_ARGB_to_BGRA(int c)
{
int b= c &0xFF;
int g = (c>>8) &0xFF;
int r = (c>>16) &0xFF;
int a = (c>>24) &0xFF;
// return ( a | (r<<8) | (g<<16) | (b<<24) ); // may be unsafe
return ( a
| ( (r<<8) &0xFF00 )
| ( (g<<16) &0xFF0000 )
| ( (b<<24) &0xFF000000 )
);
}

POINTERS

<RzR> can you tell why in std libs they use : “const char *” while “char const * const” …could be used

<The_Vulture> RzR: because the other const just makes the pointer itself const, and since it's passed by value, that wouldn't make a difference to the caller

<The_Vulture> an implementation could add or remove the extra const without making any difference to the caller

<RzR> your answser is smart

http://www.goingware.com/tips/member-pointers.html

API

[[OOP]] IN C

return-type function-name(paramater-type-list); int (*aaron(char *(*)(void)) (long, int);

Defines the function aaron which returns a pointer to a function. The function aaron takes one argument: a pointer to a function which returns a character pointer and takes no arguments. The returned function returns a type int and has two parameters of type long and int.

typedef struct my_stack_s my_class_t;

struct my_stack_s {
/* method "push" with one parameter and no return value */
void (*push)(my_stack_t *this, int i);
/* method "add" with no parameters and no return value */
void (*add)(my_stack_t *this);
/* method "pop" with no parameters (except "this") and a return value */
int (*pop) (my_stack_t *this);
}; 
/* constructor */
my_class_t *new_my_stack(void);

[[printf]]

cat > main.c << EOF && make main && ./main
p="p=%c%s%c;main(){printf(p,34,p,34);}";main(){printf(p,34,p,34);}
EOF

operators

i++ vs ++i ?

++i does not create a temp object and addition, so it is better and faster

MISC

MorE

c.txt · Last modified: 2022/04/16 12:22 (external edit)
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki