C standard
library
reference
(for Linux)
by Qazjap11
Published with CC-BY-SA 3.0 License
C functions and statements for Linux
Written by Qazjap11
Operators
Arithmetic operators:
var = something - put something into var
example: var = 5; // 5
+ add
example: var = 5 + 5; // 10
- substact
example: var = 5 - 5; // 0
* multiply
example: var = 5 * 5; // 25
/ divide
example: var = 5 / 5; // 1
% reminder of division
example: var = 5 / 3; // 2
Bitwise operators:
x >> n - shift bits in x n times right
example: var = var >> 1; // 5=00000101 => 2=00000010
x << n - shift bits in x n times left
example: var = var << 1; // 5=00000101 => 10=00001010
& AND operation
example: var = 3 & 5;//00000011 & 00000101 = 00000001
| OR inclusive operation
example: var = 3 | 5;//00000011 | 00000101 = 00000111
^ OR exclusive operation
example: var = 3 ^ 5;//00000011 ^ 00000101 = 00000110
~ reverse bits
example: var = ~var; // 5=00000101 => 250=11111010
Compound assignment operators:
+= add to itself
example: var += 3; // 8
-= subtract from itself
example: var -= 3; // 2
*= multiply itself by
example: var *= 3; // 15
/= divide itself by
example: var /= 5; // 1
%= set as reminder of devision itself by
example: var %= 3; // 2
a++ add one after making operation
example: printf("%d",var++); // prints 5 => 6
a-- subtract one after making operation
example: printf("%d",var--); // prints 5 => 4
++a add one before making operation
example: printf("%d",++var); // prints 6 => 6
--a subtract one before making operation
example: printf("%d",--var); // prints 4 => 4
Compound assignment bitwise operators:
x =>> n - shift bits in x n times right & assign to x
example: var =>> 1; // 5=00000101 => 2=00000010
x =<< n - shift bits in x n times left & assign to x
example: var =<< 1; // 5=00000101 => 10=00001010
&= AND operation to itself
example: var &= 3; //00000011 & 00000101 = 00000001
|= OR inclusive operation to itself
example: var |= 3; //00000011 | 00000101 = 00000111
^= OR exclusive operation to itself
example: var ^= 3; //00000011 ^ 00000101 = 00000110
Logical & Comparative operators:
== if equals to
example: 5 == 5 // true
!= if not equals to
example: 5 != 4 // true
> if bigger than
example: 5 > 3 // true
< if smaller than
example: 5 < 6 // true
>= if bigger or equals to
example: 5 >= 2 // true
<= if smaller or equals to
example: 5 <= 5 // true
= if can associate (not if equals!)
example: var = 5 // true
&& and
example: 5 == 5 && 5 < 6 // true
|| or
example: 5 == 4 || 5 < 6 // true
! not
example: !(5 == 4) // true
C statements & flow control
Statements:
In c almost all statements end with ';':
statement1; statement2;
Compound statements don't.
There is no importance to new lines, spaces or tabs.
Conditions:
if(condition is true)
{
something;
}else if(other condition is true){
something else;
}else{
other thing;
}
example:
if(var == 5)
{
printf("Var is 5\n");
}else if(var > 5){
printf("Var is bigger than 5\n");
}else{
printf("Var is smaller than 5\n");
}
While loop:
while(condition)
{
something;
}
example:
while(i<4)
{
printf("%d\n",i);
}
Do-While loop:
do{
something;
}while(condition);
runs at least one time.
example:
do{ printf("%d\n",i);
}while(i<4);
For loop:
for(initialize statement; condition; post loop
statement)
{
do something;
}
example:
for(i=0; i<5; i++)
{
printf("%d\n",i);
}
All above are optional, but ';' is required. For
example: for(;;) is an endless loop (without stop
condition).
Switch:
switch (var)
{
case something:
{
....
break;
}
case something else:
{
...
break;
}
default:
{
... break;
}
}
The {} after each case are not obligated, but they
form the code. Notice that the variables in the {}
are local to the block!
example:
switch(x)
{
case 1:
{
printf("X is 1\n");
break;
}
case 2:
{
printf("X is 2\n");
break;
}
default:
{
printf("X is neither 1 nor 2\n");
break
}
}
Structs
struct name {
int var1;
char var2;
...
}; // without instances
struct name xxx; // declare instance
struct name {
int var1;
char var2;
...
} first, second; // without initialization
struct name {
int var1;
char var2;
...
} first = {13,'c',...}; // with initialization
first.var1 = 44; // access struct members
printf("%c\n",first.var2); // access struct members
first = second; // assign first struct's members to
second
struct parent {
int var;
};
struct child {
struct parent p;
} c;
c.p.var = 4; // complex structs
struct dot {
int x;
int y;
} dots[3] = {1,2,2,3,3,4};
dots[0].x = 5; // struct arrays
struct name {
int var;
} x;
struct name *ptr = &x; // pointer
(*ptr).var = 4; // first access method
ptr->var = 4; // second access method
x.var = 4; // third access method
struct dot {
int x;
int y;
} dots[3] = {1,2,2,3,3,4};
struct dot *ptr = dots; // dots is pointer to dots[0]
ptr++; // go to dots[1], increase by dot struct size
typedef struct {
int x;
int y;
} dot; // define dot struct as dot variable
dot bottom,top; // using it
Unions:
Unions hold one value over various variables:
union name {
int i;
double z;
char c;
} instance;
instance.c = 1; // 1 is in all types
printf("%d\n",instance.i); // 1
printf("%lf\n",instance.z); // 1
Size of a union is the size of it's biggest member
Variables
Variable
Size X86
Size X64
int*
4 bytes
4 bytes
long
4 bytes
8 bytes
short
2 bytes
2 bytes
float
4 bytes
4 bytes
double
8 bytes
8 bytes
char
1 byte
1 byte
unsigned
4 bytes
4 bytes
unsigned long
4 bytes
8 bytes
* int's size is different in different compilers, tested in gcc
Printf/Scanf
Flags:
- align to left (if there isn't, to right)
+ always show number's sign
0 fill empty spaces in field width with zeros
Field width:
%2d - 2 numbers
%.2f - 2 fractional numbers
%2.2f - 2 numbers and 2 fractional numbers
%*d - width specified in variable
Formated vars:
%d,%i - decimal integers
%o - octal integers
%u - decimal unsigned
%x,%X - small and BIG hexadecimal values
%f - float
%lf - double
%Lf - long double
%c - char
%s - string
%% - % sign
%[set] - scan if these chars
%[^set] - scan till these chars
%n - scan number of chars entered
Add New Comment