C programming language cheatsheet

C programming language cheatsheet

The main() Function

    
   int main (int argc, char *argv[]){
   // code
   return 0;
   }
  • main() function is the entry point of a C program.
  • return type of the main() function is an integer (type int) and it is known as the return value of the program and a return value of 0 means the program executed successful while non-zero value means there was an error in executing the program.

Include Files

   #include <external_file>
   
   int main (int argc, char *argv[]){
   // code
   return 0;
   }
  • The purpose of these files is to tell the compiler to include external functions from the external_file.

Preprocessor directives

DirectivePurpose
#include "game.h"include the file "game.h" which is in the current working directory
#include <stdio.h>include the file "stdio.h" which is part of the compiler or system files
#define TRUE 1macro substitution which means substitute 1 for every occurrence of TRUE. Conventionally written in uppercase.
#define min(a,b) (a<b)?(a):(b)macro substitution which evaluates to the minimum of the 2 parameters
#define abs(a) (a<0)?(-(a)):(a)macro substitution which evaluates to the absolute value of the parameter
#define note /* comment */macro substitution which inserts the comment every time note appears
#undef TRUEundefines a previously defined macroname eg undefine TRUE in this case
#errorstop compiling at this point
#ifif expression for conditional compilation
#elifelse if expression for conditional compilation
#elseelse expression for conditional compilation
#endifend of conditional compilation
#ifdef macronamecompiles if macroname is defined
#ifndef macronamecompiles if macroname is undefined
#pragmagives the compiler commands

Reserved words

TermDescription
auto(optional) local variable declaration
breakbranch from a loop and or a switch construct
casea conditional choice in a switch
chardeclaration of a type character
constprefix declaration for a variable that can not be changed later
continueskip to the next itterable condition in a loop
default(optional) default case of a switch if no case is matched
doexecutable statement in a do-while loop
doubledeclaration of a double precision floating point type
elseconditional executable statement if the if part of a condition is false
enumbasic declaration of the enumeration type
externprefix declaration of a variable is defined externally
floatdeclaration of a floating point type
forexecutable statement of a for loop
gotojump within function to a specified label
ifconditional executable statement if a condition is true
longprefix declaration applying to many types
registerprefix declaration meaning for keeping a variable in a register
returnexecutable statement for returning a value and exiting a function
shortprefix declaration applying to many types
signedprefix declaration for signed numerical types
sizeofoperator from returning size of variables and types in bytes
staticprefix declaration to make local variable static
structdeclaration of a structure type
switchexecutable statement for conditional cases
typedefcreates a new type name for an existing type
uniondeclaration of variables that are in the same memory locations
unsignedprefix declaration for unsigned numerical types
voiddeclaration of a typeless variable
volatileprefix declaration meaning the variable can be changed at any time
whileexecutable statement for a while loop or do-while loop

Basic types

TypeDescription
charcharacter type, usually one byte ( a string is array of char )
intinteger type, usually 2 or 4 bytes (default)
floatfloating point type, usually 4 bytes
doublefloating point type, usually 8 bytes
voidno type
enumenumeration type

Type modifiers, prefix for basic types

ModifierDescription
signedsigned data type
unsignedunsigned data type
longbigger version of a data type
shortshorter version of a data type
constvariable can not be changed

Operators

OperatorDescription
( )for grouping statements and for function calls
[ ]for creating and indexing arrays
->selector, structure pointer
.select structure element
!relational not, complement
~bitwise not, ones complement, ~ a
++variable increment
variable decrement
unary minus (-a) and also subtraction (a – b)
+unary plus (+a) and also addition (a + b)
*indirect, the value of a pointer, *p is value at pointer p address and also multiplication (a * b)
&&b is the memory address of variable b, also bitwise "and"
(type)atypecasting the type a to another type type for example (float)i, (fun)(a,b), (int)x
/divide, a / b
%modulo or remainder, eg a % b means remainder of a / b
<<bitwise shift left
>>bitwise shift right
<relational less than evaluates to true or false
<=relational less than or equal, evaluates to true or false
>relational greater than, evaluates to true or false
>=greater than or equal, evaluates to true or false
==equal to, evaluates to true or false
!=not equal to, evaluates to true or false
^bitwise exclusive "or"
|bitwise "or"
&&relational "and", evaluates to true or false
||relational "or", evaluates to true or false
?ternary operator shorthand for if-else
=assignment operator
+=add and assign
-=subtract and assign
*=multiply and assign
/=divide and assign
%=modulo and assign
<<=bitwise shift left and assign
>>=bitwise shift right and assign
&=bitwise "and" and assign
^=bitwise exclusive or and assign
|=bitwise "or" and assign
,separator