C Cheatsheet
C Cheatsheet
Basics
scanf("%d", &x); -- read value into the variable x from input stream
printf("%d",x); -- printf value to the output stream
gets(str); -- reads a line from input stream into a variable
scanf("%[^\n]",s); --reads a line from input using scanf only
Sample C program
#include <stdio.h>
int main() {
printf("Hello World!!");
return 0;
}
Data types
Types Data-type
Enumeration enum
Types Data-type
Void void
-2,147,4
used to store 83,648
int whole to 4 bytes %d
numbers 2,147,48
3,647
used to store
0 to
unsigned non-negative
4,294,96 4 bytes %u
int whole
7,295
numbers
used to store
unsigned non-negative 0 to
2 bytes %hu
short int whole 65535
numbers
-2,147,4
used to store 83,648
long int whole to 4 bytes %ld
numbers 2,147,48
3,647
used to store
0 to
unsigned non-negative
4,294,96 4 bytes %lu
long int whole
7,295
numbers
used to store
unsigned
non-negative 0 to
long long 4 bytes %llu
whole (2^64)
int
numbers
used to store 6 to 7
oat fractional decimal 4 bytes %f
numbers digits
used to store 15
double fractional decimal 8 bytes %lf
numbers digits
Arrays:
data-type array-name[size];
Pointers:
datatype *pointername;
Structures:
struct structure_name {
member definition;
member definition;
...
member definition;
} [one or more structure variables];
Variables
Syntax:
Example:
Literals
Literals are the constant values assigned to the constant variables.
Literal Example
Escape sequences
\n New line
\r Carriage Return
? Question mark
\t Horizontal tab
Escape sequence Description
\v Vertical tab
\f Form feed
\ Backslash
\0 Null character
\b Back space
\a Alarm or Beep
Arrays
Array in C can be de ned as a method of clubbing multiple entities of similar type
into a larger group.
data-type array-name[size];
Example
data-type array-name[size][size];
Example
int a[2][3] = {
{1,2,3},
{4,5,6}
};
Operators
Operator type Description
Ternary Operators ?:
Keywords(reserved words)
Keywords are words that have special meaning to the C compiler. These words help
us to use the functionality of C language.
There are 32 keywords in C language.
Identi ers
Identi ers are user de ned names for variables, functions and arrays.
Rules:
Strings
Strings are an array of characters ended with null character. Enclosed in double
quotes.
Declaration
char str[]="onecompiler";
strcat(first_strin
strcat() It is used to concatenate two strings
g, second_string)
strcmp(first_strin
strcmp() It is used to compare two strings
g, second_string)
Constants
Constants are the xed values. They can be declared in two ways as shown below:
Special characters
{} : speci es start and end of code blocks
[] : used for arrays
() : used for functions
, : used to seperate variables, constants etc
* : used for pointers
# : used as a macro processor.
Conditional Statements
1. If
if(conditional-expression)
{
//code
}
2. If-else
if(conditional-expression)
{
//code
} else {
//code
}
3. If-else-if ladder
if(conditional-expression-1)
{
//code
} else if(conditional-expression-2) {
//code
} else if(conditional-expression-3) {
//code
}
....
else {
//code
}
4. Switch
switch(conditional-expression){
case value1:
//code
break; //optional
case value2:
//code
break; //optional
...
default:
//code to be executed when all the above cases are not matched;
}
Loops
1. For
2. While
while (condition){
//code
}
3. Do-While
do {
//code
} while (condition);
Functions
Function is a sub-routine which contains set of statements.
// declaring a function
return_type function_name(parameters);
// defining a function
return_type function_name(parameters){
//code
}
// calling a function
function_name (parameters)
Pointers
Pointer is a variable which holds the memory information(address) of another
variable of same data type.
datatype *pointername;
Example
int a[10];
ptr = a; // since a is the address of the first element in array (a[0])
ptr++; // pointer points to next array element (a[1])
Types of Pointers
Structures
Structure is a user-de ned data type where it allows you to combine data of
different data types.
struct structure_name {
member definition;
member definition;
...
member definition;
} [one or more structure variables];
Unions
Union is a user-de ned datatype similar to structs which allows to store different
data types in the same memory location. In Unnions, one member can contain a
value at any given time.
union union_name {
member definition;
member definition;
...
member definition;
} [one or more union variables];
File handling
File operations like create, update, read, and deleting les which are stored on the
local le system can be performed in C.
Mode Description
ptr =
(castType*
Stands for 'Memory allocation' and reserves a block
malloc() )
of memory with the given amount of bytes.
malloc(siz
e)
ptr =
Stands for 'Contiguous allocation' and reserves n (castType*
calloc()
blocks of memory with the given amount of bytes. )calloc(n,
size)
Header Files
Mathematical Functions
All functions de ned under <math.h> header le.
Function Description
Randomize Functions
All functions de ned under <stdlib.h> header le. These randomized functions are
pseudo-random (time-based random functions).
use of f ush()
*used to clear the buffer
*used to accept the next string