DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 963,503 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Cover image for C Language Cheatsheet
Rohan Kiratsata
Rohan Kiratsata

Posted on

C Language Cheatsheet

Hello Developers ! Today I'm starting cheatsheet Series. I'll be posting cheatsheet for programming languages and many more.

These cheatsheets are also available on my GitHub.
Improve this GitHub repository by contributing to it

Download PDF from here


C Cheatsheet

Table of content


Boilerplate Code

#include<stdio.h>
int main(){
    return 0;
}

Enter fullscreen mode Exit fullscreen mode

printf() function

It is used to print anything/show output on console

printf("Hello World");
Enter fullscreen mode Exit fullscreen mode

scanf() function

It is used to take input from user

scanf("format specifier",variable_name);
Enter fullscreen mode Exit fullscreen mode

Variables

It is data name that is used to store the data value in the memory.

Rules to declare variables:

Variable Name Valid? Why?
int Not Valid keywords are not allowed
amount$ Not Valid Dollar Sign is not allowed
your name Not Valid Space between variable name is not allowed
average_score Valid underscore can be used as space
First_name Valid -
int_type Valid Keywords can be used as combination

Basic Data Types

char - It stores single character. Size - 1 byte

char var_name = 'a';
// Another example
char var_name2 = 'x';
Enter fullscreen mode Exit fullscreen mode

int - It stores an integer value. Size - 4 bytes

int age = 18;
//Another example
int amount = 10000;
Enter fullscreen mode Exit fullscreen mode

float - It stores an floating point value with 6 digit precision. Size - 4 bytes

float radius = 5.8;
//Another example
float area = 15.2;
Enter fullscreen mode Exit fullscreen mode

double - It stores an float value with 14 digit precision. Size - 8 bytes.

double var = 12.1531452;
//Another Example
double more_var = 542.12452;
Enter fullscreen mode Exit fullscreen mode

void - Represents the absence of type.

void main(){
    //does not return anything.
}
Enter fullscreen mode Exit fullscreen mode

Format Specifiers

Format Specifier Type
%c Character
%d Integer
%f float
%lf double
%l long
%Lf long double
%lld long long
%o octal representation
%p pointer
%s string
%% prints % symbol

Escape Sequences

Escape Sequence Type
\a Produces Alarm/Beep Sound
\b Backspace
\f Form Feed
\n New Line
\t Tab Space
\v Tab Space - Vertically
\\ Backslash
\" Double Quote
\' Single Quote
\? Question Mark

Operators

Arithmetic Operators

Operators Example Meaning
+ a+b Addition or unary plus
- a-b Subtraction or unary minus
* a*b Multiplication
/ a/b Division
% a%b Modulo Division-Gives remainder

Relational Operators

Operator Example Meaning
< a < b is less than
> a > b is greater than
<= a<=b is less than or equal to
>= a>=b is greater than or equal to

Logical Operators

Operator Example Meaning
&& a && b logical AND
|| a || b logical OR
! a ! b logical NOT

Increment and Decrement Operators

variable_name++ Here ++ is a increment operator, it increments the value of variable by 1.
variable_name-- Here -- is a decrement operator and it decrements the value of variable by 1.

More examples:

int a = 1; // a value is 1
a++;       // Now Value becomes 2

int b = 5; // b value is 5
b--;       // Now, b value is 4
Enter fullscreen mode Exit fullscreen mode

Conditional Statements

If Statement
if(codition)
{
    //statements or code
}
//example
int a = 1, b = 5;
if(a < b){
    printf("A is smaller");
}
Enter fullscreen mode Exit fullscreen mode
If-Else Statements
if(condition){
    //statements
}
else{
    //statements
}
//example
int a=1; b=5;
if(a < b){
    printf("A is smaller");
}
else{
    printf("B is smaller");
}
Enter fullscreen mode Exit fullscreen mode
If Else-If Statements
if(condtion){
    //code
}
else if(another_condtion){
    //code
}
else{
    //code
}
Enter fullscreen mode Exit fullscreen mode
Switch Case Statements
switch(expression)
{
    case constant-expression:
        statement1;
        statements2;
        break;
    case constant-expression:
        another_statements;
        break;

    .....(n number of cases)
    default;
        statements;
}
Enter fullscreen mode Exit fullscreen mode

Iterative Statements

It executes the statements inside a block of loop until condition is false

While loop
while(condition){

    //code
}
//example

int a = 10;
while(a <=10){
    printf("%d",a);
}//will print 1 to 10
Enter fullscreen mode Exit fullscreen mode
Do-While Loop
do{
    // code
}
while(codition);
Enter fullscreen mode Exit fullscreen mode
For Loop
for(int i=0; i < counter; i++){
    // code
}
Enter fullscreen mode Exit fullscreen mode
Break Statement

Break keyword is used to terminate the loop.

break;
Enter fullscreen mode Exit fullscreen mode
Continue Statement

Continue keyword skips the rest of iterative code of loop and return to starting point of loop.

continue;
Enter fullscreen mode Exit fullscreen mode

Functions

Functions are used to divide the code and to avoid repetitive task. It provides reusability and readability to code.
Function Declaration

return_type function_name(data_type-parameters){
    //code
}
//  Example of function to add two numbers

int add(int a, int b){
    return a+b;
}
Enter fullscreen mode Exit fullscreen mode

Recursion

Recursion is the process of repeating items in a self-similar way.If a program allows you to call a function inside the same function, then it is called a recursive call of the function.

void myFunction(){
    myFunction();   //Function calling itself
}
Enter fullscreen mode Exit fullscreen mode
//Factorial Using Recursion
long factorial(long n){
    if(n==0){
        return 1;
    }
    return n * factorial(n -1);
}

int main(){
    int n = 5;
    printf("Factorial of %d is %l.",n,factorial(n));
    return 0;
}
//OUTPUT : Factorial of 5 is 120.
Enter fullscreen mode Exit fullscreen mode

Pointers

Pointer is a variable that contains the address of another variable.

datatype *var_name;
Enter fullscreen mode Exit fullscreen mode

Arrays

Array is an collection of data of same data-type.

Declaration

data_type array_name[array_size];
Enter fullscreen mode Exit fullscreen mode

Fetching Array Element
Array index starts from 0.

data_type variable_name = arr[index]
Enter fullscreen mode Exit fullscreen mode
//Example

int arr = {1,2,3,4,5};
printf("%d",arr[0])
//OUTPUT : 1
Enter fullscreen mode Exit fullscreen mode

Strings

It is basically 1D character array. It character of string is null character (\0)
Declaration

char string_name[size];
Enter fullscreen mode Exit fullscreen mode
Important String Functions

gets() function
It is used to take input of multi-character string

gets("string");
Enter fullscreen mode Exit fullscreen mode

puts() function
It is used to show string output

puts("string");
Enter fullscreen mode Exit fullscreen mode

strlen() function
It prints the length of string.

strlen(string_name);
Enter fullscreen mode Exit fullscreen mode

strcat() function
It is used to concatenate two strings.

strcat(string1, string2);
Enter fullscreen mode Exit fullscreen mode

strcmp() function
It is used to compare two strings. Gives output in 0/1.

strcmp(string1, string2);

Enter fullscreen mode Exit fullscreen mode

strcpy() function
It is used to copy the content of 1st string to another string.

strcpy(string1, string2);
Enter fullscreen mode Exit fullscreen mode

Structures

A structure creates a data type that can be used to group items of possibly different types into a single type.
Declaration

struct student
{
    char name[50];
    int class;
    float percentage;
    char college[50];
};      //Notice the semicolon
Enter fullscreen mode Exit fullscreen mode

Dynamic Memory Allocation

If you are aware of the size of an array, then it is easy and you can define it as an array. For example, to store a name of any person, it can go up to a maximum of 100 characters. But now let us consider a situation where you have no idea about the length of the text you need to store, for example, you want to store a detailed description about a topic. Here we need to define a pointer to character without defining how much memory is required and later. So we use Dynamic Memory Allocation.

malloc() function
Stands for 'Memory allocation' and reserves a block of memory with the given amount of bytes.

var = (casting_type*)malloc(size);
//Example
var = (int*)malloc(n * sizeof(int))
Enter fullscreen mode Exit fullscreen mode

calloc() function
Stands for β€œcontiguous allocation” method in C is used to dynamically allocate the specified number of blocks of memory of the specified type.

var = (cast_type*)calloc(n, size);
Enter fullscreen mode Exit fullscreen mode

realloc() function
If the allocated memory is insufficient, then we can change the size of previously allocated memory using this function for efficiency purposes.

var = realloc(var2,n);
Enter fullscreen mode Exit fullscreen mode

File Handling

Creating File Pointer

FILE *file
Enter fullscreen mode Exit fullscreen mode

Opening a File

file = fopen(file_name.txt,w)
Enter fullscreen mode Exit fullscreen mode

fscanf() function
Used to read file content

fscanf(FILE *stream, const char *format, ..);
Enter fullscreen mode Exit fullscreen mode

fprintf() function
Used to write the file content

fprintf(FILE *var, const char *str,..);
Enter fullscreen mode Exit fullscreen mode

Closing a File

fclose(file);
Enter fullscreen mode Exit fullscreen mode

Credits

This cheatsheet is written and managed by Rohan Kiratsata.

Something is missing? Contribute to this repo and improve.


Top comments (1)

Collapse
 
abhidadhaniya23 profile image
Abhi Dadhaniya

Great work... Thanks for this cheat sheet.. πŸ‘πŸ»πŸ’–

🌚 Friends don't let friends browse without dark mode.

Sorry, it's true.