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
printf function
It is used to show output on the screen
scanf function
It is used to take input from the user
We use & with the variable name to represent "address of". This is how the syntax works:
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
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.
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:
Integer type
To store non-decimal numeric values, an integer type is used
The format specifier of an integer is "%d"
Float type
To store decimal numeric values, float type is used
The format specifier of a float is "%f"
Double type
To store a double-precision floating-point value we use double.
The format specifier of double is "%f"
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:
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
Backspace
\b adds a backspace
Form feed
Newline
Newline Character
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."
Tab
It gives a tab space
Backslash
It adds a backslash
Single quote
It adds a single quotation mark
Question mark
It adds a question mark
Octal No.
It represents the value of an octal number
Hexadecimal No.
It represents the value of a hexadecimal number
Null
The null character is usually used to terminate a string
Conditional Instructions
Conditional statements are used to perform operations based on some condition.
If Statement
If-else Statement
if else-if Statement
nested if-else
Switch Case Statement
It allows a variable to be tested for equality against a list of values (cases).
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.
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
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.
Break Statement
break keyword inside the loop is used to terminate the loop
Here is the output of the above code:
Continue Statement
continue keyword skips the rest of the current iteration of the loop and returns to the starting point of the loop
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
Function Call
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.
Parameters in Python function
Parameters are the values passed inside the parenthesis of the function while defining as well as while calling.
Ways of calling a function
- With return value and with parameters
- Without return value and with parameters
- With return value and without parameters
- 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.
Pointers
A pointer is a variable that contains the address of another variable,
Declaration
We can allocate the address of the pointing variable to the pointer variable
Dereferencing pointer variable
Arrays
An array is a collection of data items of the same type.
Declaration
Accessing element
Strings
A string is a 1-D character array terminated by a null character ('\0')
Declaration
gets() function
It allows you to enter a multi-word string.
puts() function
It is used to show string output
fgets() function
The gets()
function is considered unsafe, and it is better to use fgets()
instead.
String Functions
strlen() function
It is used to calculate the length of the string
strcpy() function
It is used to copy the content of second-string into the first string passed to it
strcat() function
It is used to concatenate two strings
strcmp() function
It is used to compare two strings
strlwr() function
It is used to convert characters of strings into lowercase
strupr() function
It is used to convert characters of strings into uppercase
strrev() function
It is used to reverse the string
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
typedef keyword
typedef function allows users to provide alternative names for the primitive and user-defined data types.
File Handling
A set of methods for handling File IO (read/write/append) in C language
FILE pointer
Opening a file
It is used to open a file in C.
fscanf() function
It is used to read the content of a file.
fprintf() function
It is used to write content into the file.
fgetc() function
It reads a character from a file opened in read mode. It returns EOF on reaching the end of the file.
fputc() function
It writes a character to a file opened in write mode
Closing a file
It closes the file.
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.
calloc() function
Stands for 'Contiguous allocation' and reserves n blocks of memory with the given amount of bytes.
free function
It is used to free the allocated memory.
realloc() function
If the allocated memory is insufficient, then we can change the size of previously allocated memory using this function for efficiency purposes
I hope the provided information covers what you need.