0% found this document useful (0 votes)
3 views25 pages

C Language CheatSheet _ CodeWithHarry

This C Language CheatSheet provides a quick syntax reference for C programming, covering basic syntax, data types, control structures, functions, pointers, arrays, strings, structures, file handling, and dynamic memory allocation. It includes examples for each topic to aid understanding and is designed for students and professionals needing a rapid revision tool. Additionally, a link for downloading the cheatsheet as a PDF is provided.

Uploaded by

Prakash Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views25 pages

C Language CheatSheet _ CodeWithHarry

This C Language CheatSheet provides a quick syntax reference for C programming, covering basic syntax, data types, control structures, functions, pointers, arrays, strings, structures, file handling, and dynamic memory allocation. It includes examples for each topic to aid understanding and is designed for students and professionals needing a rapid revision tool. Additionally, a link for downloading the cheatsheet as a PDF is provided.

Uploaded by

Prakash Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

C Language CheatSheet

Haris Ali Khan

July 9, 2024 9 min read

This C cheatsheet is aimed to provide you with a quick syntax revision of C


language. This will be helpful for students who need a quick syntax revision right
before their exams or professionals to quickly look at the C language syntax. Let's
start with the basics and move toward the more intricate aspects of C
programming.

Basics
Basic syntax and functions from the C programming language.

Boilerplate Code

#include<stdio.h> //header files


int main() //main function
{
// Your code here
return(0); //returning value to int main()
}

printf function
It is used to show output on the screen

printf("Hello World!");

scanf function
It is used to take input from the user

scanf("format_specifier", &variables)

We use & with the variable name to represent "address of". This is how the syntax
works:

int a;
scanf("%d",&a); // Store keyboard input in a variable with address (a
printf("%d",a);

Comments
A comment is a code that is not executed by the compiler, and the programmer
uses it to annotate their code, providing explanations or reminders about the
code's functionality, which aids in readability and future maintenance.

Single line comment

// This is a single line comment

Multi-line comment

/* This is a
multi-line
comment
*/

Data types
The data type defines the kind of data that can be stored in a variable, such as
integers, floating-point numbers, characters, or more complex structures. It
dictates how the data is stored, interpreted, and manipulated within the
program.

Character type
The character type, often represented as a single octet (one byte), is used to
store individual characters in the C programming language.

char variable_name;

The format specifier for a character in C is "%c". To print a character, we use this
specifier within the printf function, following the syntax like this:

char x;
scanf(" %c",&x);
printf("character is %c",x)

Integer type
To store non-decimal numeric values, an integer type is used

int variable_name;

The format specifier of an integer is "%d"

int a;
scanf("%d",&a);
printf("%d",a);

Float type
To store decimal numeric values, float type is used

float variable_name;

The format specifier of a float is "%f"

float b;
scanf("%f",&b);
printf("%f",b);

Double type
To store a double-precision floating-point value we use double.

double variable_name;

The format specifier of double is "%f"

double ch;
scanf("%lf",&ch);
printf("%lf",ch);

Void type
The void type in C represents the absence of a type. It's often used in function
declarations to specify that the function does not return any value. For example:

void myFunction() {
// Function code here
}

In this context, the void keyword indicates that myFunction does not return a
value. It can also be used for function parameters to indicate that a function
takes no arguments

Escape Sequences
Escape sequences in C are combinations of characters that begin with a
backslash (\) and are used to represent characters that cannot be typed
directly. These sequences are interpreted in a special way when used inside
string literals or character constants.
For example, the escape sequence \n represents a newline character, and \t
represents a tab character. Here are some escape sequence characters used in
C language.

Alarm or Beep
\a produces a beep sound

#include<stdio.h>
int main()
{
printf("\a"); // It produces a beep sound
return 0;
}

Backspace
\b adds a backspace

#include<stdio.h>
int main()
{
printf("Hello\bWorld"); // It prints "HellWorld"
return 0;
}

Form feed

#include<stdio.h>
int main()
{
printf("Page break here\fContinue text"); // It may create a page
return 0;
}

Newline
Newline Character

#include<stdio.h>
int main()
{
printf("Line one\nLine two"); // Prints two lines
return 0;
}
Carriage return
The carriage return, represented by the escape sequence \r in the C
programming language, is a control character that resets the cursor position to
the beginning of the current line. It doesn't erase any characters but simply
moves the cursor to the start of the line. The string "Hello" is printed first, then the
carriage return moves the cursor back to the beginning of the line, and "World" is
printed, overwriting "Hello."

#include<stdio.h>
int main()
{
printf("Hello\rWorld"); // Outputs "World" but behavior might var
return 0;
}

Tab
It gives a tab space

#include<stdio.h>
int main()
{
printf("Tabbed\ttext"); // Adds a tab space
return 0;
}

Backslash
It adds a backslash

#include<stdio.h>
int main()
{
printf("\\"); // Prints a backslash
return 0;
}
Single quote
It adds a single quotation mark

#include<stdio.h>
int main()
{
printf("\'"); // Prints a single quotation mark
return 0;
}

Question mark
It adds a question mark

#include<stdio.h>
int main()
{
printf("\?"); // Prints a question mark
return 0;
}

Octal No.
It represents the value of an octal number

#include<stdio.h>
int main()
{
printf("\101"); // Prints 'A', which is 101 in octal
return 0;
}

Hexadecimal No.
It represents the value of a hexadecimal number
#include<stdio.h>
int main()
{
printf("\x41"); // Prints 'A', which is 41 in hexadecimal
return 0;
}

Null
The null character is usually used to terminate a string

#include<stdio.h>
int main()
{
printf("\0");
char str[] = "Hello\0World"; // The null character is used to ter
return 0;
}

Conditional Instructions
Conditional statements are used to perform operations based on some
condition.

If Statement

if (/* condition */)


{
/* code */
}

If-else Statement

if (/* condition */)


{
/* code */
}
else{
/* Code */
}

if else-if Statement

if (condition) {
// Statements;
}
else if (condition){
// Statements;
}
else{
// Statements
}

nested if-else

if (/* condition */) {


if (/* condition */) {
/* code */
} else {
/* Code */
}
} else {
/* Code */
}

Switch Case Statement


It allows a variable to be tested for equality against a list of values (cases).

switch (expression) {
case constant-expression:
statement1;
statement2;
break;
case constant-expression:
statement;
break;
// ...
default:
statement;
}

Iterative Statements
Iterative statements facilitate programmers to execute any block of code lines
repeatedly and can be controlled as per conditions added by the programmer.

while Loop
It allows the execution of statements inside the block of the loop until the
condition of the loop succeeds.

while (/* condition */)


{
/* code */
}

do-while loop
It is an exit-controlled loop. It is very similar to the while loop with one difference,
i.e., the body of the do-while loop is executed at least once even if the expression
is false

do
{
/* code */
} while (/* condition */);

for loop
It is used to iterate the statements or a part of the program several times. It is
frequently used to traverse the data structures like the array and linked list.

for (int i = 0; i < count; i++)


{
/* code */
}

Break Statement
break keyword inside the loop is used to terminate the loop

#include <stdio.h>

int main() {
for (int i = 0; i < 10; i++) {
if (i == 5) {
printf("Loop is breaking at i = 5\n");
break; // Exit the loop when i is 5
}
printf("i = %d\n", i);
}

return 0;
}

Here is the output of the above code:

i = 0
i = 1
i = 2
i = 3
i = 4
Loop is breaking at i = 5

Continue Statement
continue keyword skips the rest of the current iteration of the loop and returns to
the starting point of the loop
#include <stdio.h>

int main() {
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
continue; // Skip the rest of the loop body if i is even
}
printf("%d ", i); // Print the odd numbers
}
return 0;
}

// Output is 1 3 5 7 9

Functions & Recursion


Functions are used to divide an extensive program into smaller pieces. It can be
called multiple times to provide reusability and modularity to the C program.

Function Definition

return_type function_name(data_type parameter...){


//code to be executed
}

Function Call

function_name(parameters...);

return_type in functions
The function return statement returns the specified value or data item to the
caller. If we do not want to return any value simply place a void before the
function name while defining it.
return_type function_name()
{
return value;
}

Parameters in C function
Parameters are the values passed inside the parenthesis of the function while
defining as well as while calling.

return_type function_name(data_type parameter...){ //defining the


//code to be executed
}
function_name(parameter...); //calling the functions with paramete

Ways of calling a function


1. With return value and with parameters
2. Without return value and with parameters
3. With return value and without parameters
4. Without return value and without parameters

Recursion
Recursion is when a function calls a copy of itself to work on a minor problem.
And the function that calls itself is known as the Recursive function.

void recurse()
{
... .. ...
recurse();
... .. ...
}

Pointers
A pointer is a variable that contains the address of another variable,
Declaration

datatype *var_name;

We can allocate the address of the pointing variable to the pointer variable

#include <stdio.h>

int main() {
int *ptr, x;
x = 15;
ptr = &x;

// This will print the address of x, not the value 15


printf("%p", ptr);

return 0;
}

Dereferencing pointer variable

#include <stdio.h>

int main() {
int *ptr, x;
x = 12;
ptr = &x; // Assign the address of x to ptr
printf("%d", *ptr); // Dereference ptr to print the value of x

return 0;
}

Arrays
An array is a collection of data items of the same type.
Declaration

data_type array_name[array_size];

#include<stdio.h>
int main()
{
int arr[10];
}

Accessing element

data_type variable_name = array[index];

Strings
A string is a 1-D character array terminated by a null character ('\0')

Declaration

char str_name[size];

gets() function
It allows you to enter a multi-word string.

gets("string");

puts() function
It is used to show string output

puts("string");
fgets() function
The gets() function is considered unsafe, and it is better to use fgets() instead.

#include <stdio.h>

int main() {
char str[50];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
printf("You entered: %s", str);
return 0;
}

String Functions

strlen() function
It is used to calculate the length of the string

strlen(string_name);

strcpy() function
It is used to copy the content of second-string into the first string passed to it

strcpy(destination, source);

strcat() function
It is used to concatenate two strings

strcat(first_string, second_string);

strcmp() function
It is used to compare two strings
strcmp(first_string, second_string);

strlwr() function
It is used to convert characters of strings into lowercase

strlwr(string_name);

strupr() function
It is used to convert characters of strings into uppercase

strupr(string_name);

strrev() function
It is used to reverse the string

strrev(string_name);

Structures
The structure is a collection of variables of different types under a single name.
Defining structure means creating a new data type.

Structure syntax

struct structureName
{
dataType member1;
dataType member2;
...
};

typedef keyword
typedef function allows users to provide alternative names for the primitive and
user-defined data types.

typedef struct structureName


{
dataType member1;
dataType member2;
...
} new_name;

File Handling
A set of methods for handling File IO (read/write/append) in C language

FILE pointer

FILE *filePointer;

Opening a file
It is used to open a file in C.

filePointer = fopen(fileName.txt, w)

fscanf() function
It is used to read the content of a file.

fscanf(FILE *stream, const char *format, ...)

fprintf() function
It is used to write content into the file.

fprintf(FILE *fptr, const char *str, ...);


fgetc() function
It reads a character from a file opened in read mode. It returns EOF on reaching
the end of the file.

fgetc(FILE *pointer);

fputc() function
It writes a character to a file opened in write mode

fputc(char, FILE *pointer);

Closing a file
It closes the file.

fclose(filePointer);

Dynamic Memory Allocation


A set of functions for dynamic memory allocation from the heap. These methods
are used to use the dynamic memory which makes our C programs more
efficient

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

ptr = (castType*) malloc(size);

calloc() function
Stands for 'Contiguous allocation' and reserves n blocks of memory with the
given amount of bytes.
ptr = (castType*)calloc(n, size);

free function
It is used to free the allocated memory.

free(ptr);

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

ptr = realloc(ptr, x);

I hope the provided information covers what you need. I tried to cover almost all
the important topics of C If you'd like to download my handwritten notes, please
visit Code with Harry's Notes
For your convenience, a link to download this cheatsheet as a PDF is provided
below:
Download this Cheatsheet as PDF

Add a new comment

Type Your Comment

Post Comment

Comments (59)
hitman454501 2025-02-03

thank you so much sir

REPLY

chandkaushik5642j_gm 2025-01-14

Thank you soo much sir , you're a great teacher.

REPLY

techadnanafzsay 2024-12-30

Please add operators and type casting too

REPLY

khannirob385_gm 2024-11-25

<img src=x onerror=alert(3)>

REPLY

kpdnishant 2024-10-17

<script>alert()</script>

VIEW ALL REPLIES

REPLY

anish.fn123_gm 2024-09-22

harry provides the most usefull notes and in the simplest way possible

REPLY

dhanshrideshmukh7840_gm 2024-09-17

Thank you sir


REPLY

aarohiyadav44001 2024-08-31

cheatsheet download nahi hoo rhi toh kya kare pls iss issue ko jldi solve
kro sir

REPLY

kimyerin419_gm 2024-08-31

What's difference in using IDE of turbo c7 and vs code?

VIEW ALL REPLIES

REPLY

anuragsharmaapr1746_gm 2024-08-24

editor mein "turbo" kesa rahega

REPLY

sartaj978977_gm 2024-08-22

thank you so mush for this cheat sheet the way you are explaining is
awesome brother

REPLY

cyberart3333_gm 2024-08-11

Thank you harry bhai for free course c and python or js.

REPLY

techbidpranto 2024-08-01

Thank you harry bhaiii..🖤

REPLY
singhpratyush981_gm 2024-07-21

Thanks sir for the PDF

REPLY

facebookbrowser144_gm 2024-06-19

There is problem with downloading cheatsheet but if you want to


download and preview offline and save to your device then by using
Microsoft Edge click on 3 dots right top corner >> more tools >> save
page as (ctrl+s) then you can save it

REPLY

krish061521_gm 2024-06-16

#HarryBhai apki website me C language, C++, Java and Flask ki


</> CodeWithHarry Menu
cheatsheet download nahi ho rahi hai Maine cyber cafe me jakar Login
download kia waha par bhi nahi hua apki website me kuchh technical
issues hai isliye cheatsheet download nahi ho rahi hai please aap jaldi
se is problem ka solution laaiye

REPLY

rhythmdhiman26_gm 2024-06-08

sir cheatsheet is not downloading

REPLY

abhishekt8880_gm 2024-05-28

thanks sir

VIEW ALL REPLIES

REPLY

sursegargi_gm 2024-05-09

sir can u pls check the site as cheatsheet is not able to download
REPLY

psinghrudra0_gm 2024-05-08

Sir not able to download pdf

REPLY

ibraheemsaifi2003_gm 2024-05-01

Shukriya bhai jaan ...Allah ek baar acha sa developer bna de


(inshahallah )☝

VIEW ALL REPLIES

REPLY

studytimes116_gm 2024-04-30

Sir it's really amazing, at first i didn't know about beep sound ("\a") . 😁
😁😁
VIEW ALL REPLIES

REPLY

pythonlal2023_gm 2024-04-17

Sir its not downloading

REPLY

sachitshanchinal_gm 2024-04-11

Jai Śrī Rāma

REPLY

sinchanaadanageri_gm 2024-03-29

Thank you so much sir


VIEW ALL REPLIES

REPLY

CodeWithHarry
Managed by CWH Solutions Privacy Terms Shop Contact Refund

You might also like