C Cheat Sheet
Overview
The C language is the basic programming language in coding. It is good to start with C language for beginners as this language deals with the very basic concepts of programming that a student needs to learn. This concept will work as a prerequisite for learning more languages. As a human being, it is not always okay to memorize each and every point of any language. So we need a cheat sheet that will help us quickly memorize the basic concepts of all the topics in C language. This C cheat sheet starts with the fundamental concepts and goes to the end of the concepts of the C language.
C Cheat Sheet
The best way to learn to program is to start with a fundamental language like C. Nearly every other popular language today borrows from C. Whether you’re curious about programming, need to pass a college course, or want to start your own app business, learning C is the right place to begin. This cheat sheet will help you to revise all the basics of the C language in a single place.
C Language Basics
In this section, we will learn the very basic concepts of the C language.
- Boilerplate code
The Boilerplate code is defined as that particular section of code that can be repeated in many places and there is almost no change seen in the code. Boilerplate code provides us with the idea of code reusability. Example-
- Printf function
The printf() function in the C language is used for printing any output on the console. This function is an inbuilt library function and it is found in the header file stdio.h. The syntax of the printf() function is as follows:
Example:
Output:
In the above example, we entered the text that we want to print as output and added it to the printf() function inside the parenthesis in double inverted commas and terminated the line with a ;
- Scanf function
The scanf() function in the C language is used or gets some input from the user. This function reads the input provided by the user by an input device such as the keyboard. The syntax for scanf() function in C is as follows:
Example:
Output:
Comments in C
We can do comments in C language for explaining the code for a clear understanding. The comments in the code are not readable by the compiler. If there is any comment in the code, the compiler does not read this as a program to be executed. It simply ignores the text that comes under the comment. There are two types of comments in C.
- Single line comment
Single-line comments are used for commenting out just a single line, for Example-
- Multi Line Comment
Multiline comments are used for commenting on two or more than two lines of text, for example-
Data types in C
A data type is defined as an entity that is used to specify the type of data that a variable stores. These data are stored in a variable of a program like an integer, float, character, etc. The various basically four types of data types available in the C language that are - Basic data type(int, char, float, double, etc), Derived data type(array, pointer, etc), Enumeration data type(enum), User-defined data type(structure). Broadly these data types are described below.
- Character types
The character data type is denoted by char in C. This data type is used for storing a single character. The memory consumed by char is a single byte of memory. The syntax for char is as follows:
- Integer type
The integer variable in C is denoted by int. This data type is used for storing the integer value. The syntax for int is as follows:
- Double type
The double data type in C is denoted by double. This data type is used for storing the decimal values with double precision. The syntax for this data type is as follows:
- Float type
The float data type in C is denoted by double. This data type is used for storing the decimal values with single precision. The syntax for this data type is as follows:
- Void type
The void data type is used in case if there is not any specified data type. The syntax for this data type is as follows:
Escape Sequences in C
The escape sequence is a continuous sequence of character values that does not represent itself upon using it. The escape sequence can be used inside the string literals. It is made up of two or more two characters that start with a backslash (\).
- Alarm/beep
This escape sequence is used for producing a sound that sounds like a beep. The syntax is as follows:
- Backspace
This escape sequence is used for adding a backslash. The syntax is as follows:
- Form feed
This escape sequence is used for a form feed. The syntax is as follows:
- Newline
This escape sequence is used for adding a new line character. The syntax is as follows:
- Carriage return
This escape sequence is used for returning the character. The syntax is as follows:
- Tab
This escape sequence is used for providing a tab space. The syntax is as follows:
- Backslash
This escape sequence is used for adding a backslash. The syntax is as follows:
- Single quote
This escape sequence is used for adding a single quotation mark. The syntax is as follows:
- Question mark
This escape sequence is used for adding a question mark. The syntax is as follows:
- Octal number
This escape sequence is used for representing the value of an octal number. The syntax is as follows:
Example:
Output:
- Hexadecimal number
This escape sequence is used for representing the value for a given hexadecimal number. The syntax is as follows:
- Null
This escape sequence is used for terminating a string. The syntax is as follows:
Conditional Instructions in C
The conditional statements in the C language are used to perform some specific decisions on the basis of the evaluation of conditions given by the user in the program. The conditional statements get executed sequentially if there are no conditions matching. Suppose, there are some conditions for some particular blocks of code, then the execution of the program will change as per the outcome of the conditions in each block. This process is known as Decision Making in C. Given below are the various types of conditional statements with examples.
- If statement
The If conditional statement is used for checking some conditions given by the user and then performing according to the output of the condition for executing some operations. This conditional operator is generally used in situations where there are different conditions for each statement.
Syntax and example:
Example:
Output:
- If-else statement
The if-else statement can be used for doing two operations on a given condition. T his operator is used in the condition when there is a statement that satisfies the condition then the next statement will execute otherwise the code written in the else operation will get executed.
Syntax and example of this operator are given below:
Example:
Output:
- If else-if statement
The if else if statement is also known as the if-else-if ladder statement. This conditional operator is used when there are nested conditions in the program. Many statements are analyzed on many conditions and then the output comes. First, the if condition is checked if it is true then the statement gets executed. If this statement is not true then the execution gets to the next else-if and again here does according to the normal if-else. If the condition is still not satisfied then execution gets out of the else-if and goes to the else statement.
Syntax and example of if-else-if are given below:
Example:
Output:
- Switch case statement
The switch case statement is used for evaluating a given expression and then it executes the related statement with it. This statement is used for performing different conditions. There are two important keywords that are:
- Break: It is used for stopping the execution of the program that is inside a particular block.
- Default: If there is no case matched with the condition, then this statement is used to specify that set of codes.
The syntax for the switch case is as follows:
Iterative statements in C
The iterative statements in C language are used for the execution of any set of codes again and again as per the user's wants. The repetition of that code in a program is controlled and managed by the programmer.
- While loop
This loop in the C language is used for the execution of a statement that is inside the block of the loop. The loop continues to execute until the condition matches. The syntax for while loop is as follows:
- Do-while loop
This loop is almost similar to the while loop but the only difference is that the code inside the block gets executed at least once even if the condition is false.
The syntax of this loop is as follows:
- For loop
The for loop is used for repeating the same part of the program many times as per the user's choice. This loop is generally used for traversing the data structure such as array and linked list.
The syntax is as follows:
- Break statement
The break statement is used for breaking the loop. This statement helps to come out from the switch cases when the condition matches with the statement.
The syntax is as follows:
- Continue statement
The continue statement is used for forcing the termination of a particular set of codes and skipping all the code in between.
The syntax is as follows:
C Functions and Recursions
In this section, we will see how function and recursion work in the C language.
- Definition of a Function
A function is defined as the particular set of code that is used for performing some specific task inside the code. It is a set of codes that gets executed only when they are called in the program. There are some data known as parameters that can be passed in the function. Functions prevent us to write some code again and again in the code. We just need to create a function and we can use it many times throughout the program. The two types of function in C are as follows:
- Library functions: The functions that are already defined in the header file are known as library functions. Example- scanf(), printf(), etc.
- User-defined function: These functions are created by the user. The user defined the working if that function in the code.
The syntax for defining a function is as follows:
- Recursion
When the function calls a function itself, this is known as Recursion. Using recursion, a large complex problem can be split into many simpler problems. In this way, the solution of complex problem becomes easy for solving.
The syntax for recursion is as follows:
Pointers
In simple terms, a pointer is a variable that stores the memory address of another variable (or object) as its value. A pointer aims to point to a data type which may be int, character, double, etc. But a pointer always points to a data type of the same type. Pointers are created by using the * operator.
The syntax for declaring a pointer is as follows:
There are majorly four types of pointers in C++:
- Null Pointer: As the name suggests, a null pointer is a type of pointer which represents nothing.
- Void Pointer: A void Pointer is a type of pointer which does not point to any data type. We can say that it is a general-purpose pointer.
- Wild Pointer: Wild pointers are similar to normal pointers but one thing that makes them different is they are only declared but not initialized. Since they are only initialized they point to unallocated memory.
- Dangling Pointer: A dangling pointer is a type of pointer that comes when we deallocate the object but forgets to modify the value of the pointer.
Arrays
The *Array in C *is defined as a kind of storage that is used for storing multiple values of the same type in a single variable. We can create an array by defining its data type and then the number of values we want to store in it. The indexing of the array starts from 0. If we want to fetch the first element from an array, then we need to specify the statement as first element[0].
For example-
Example:
In the above example, the value at index 0 which is the first value of the array will be printed as the output.
Strings
A string is used for storing a sequence of characters. It is similar to characters but a string always terminates with a null character \0.
We can declare a string as follows:
- gets() function
This function allows us to enter a multi-word string.
The syntax for this function is as follows:
- puts() function
This function is used for showing the string as output.
The syntax for this function is as follows:
- String function strlen()
This function is used for calculating the length of the given string.
The syntax for this function is as follows:
- strcpy() function
This function is used for copying the string. The content of a string is copied to another string.
The syntax for this function is as follows:
- strcat() function
This function helps us to combine two or more strings together and provide them as a single string value. Suppose we have two strings, the first one being Micro and the second one - soft and we want to combine these two strings as a single string, so we use String Concatenation and the result will be: Microsoft.
The syntax for this function is as follows:
- strcmp() function
This function in the C language is used for the comparison of two strings.
The syntax for this function is as follows:
Structures in C
Structures in C language are defined as the process of grouping the related variables in a single place. It is a collection of different variables that comes under a single name. The term defining structure, means that the creation of a new data type is taking place. For defining a structure, we must include the keyword struct in the code.
The syntax and example for structure in c are as follows:
Example:
File handling in C
The term File Handling refers to the process of storing data in a file. The storing and manipulation of the data in the file are done using a C program. We can also extract the data from the file using the C program whenever we need to fetch these data. There are many operations we can perform in Files in C language that are as follows:
- we can create a file
- we can open an existing file
- we can read data from the file that is already existing
- we can write data into the existing file
- we can move and copy the data from one place to another place in that file
- we can close the file
The syntax for creating a file is as follows:
-
Different types of actions that we can perform on a file using the C program are as follows:
-
Opening a file: The syntax for this function is as follows:
- fscanf() function: This function is used to read the content from a file.
The syntax for this function is as follows:
- fprintf() function: This function is used for writing some new content into the file.
The syntax for this function is as follows:
-
fgetc() function: This function is used for reading a character from a file that is being opened in the reading mode. And after reaching the last of the file, the function returns an EOF.
-
The syntax for this function is as follows:
- fputc() function: This function is used for writing some characters into the file when the file is in write mode.
The syntax for this function is as follows:
- closing a file: This function is used for closing that particular file.
The syntax for this function is as follows:
Dynamic Memory Allocation in C
Dynamic Memory allocation in c is defined as the process by which a user can change the size of an array during the runtime of a program. As we know, the C language is a structured language and it has some fixed rules and conditions for executing a program. The array size can be also changed by certain rules. These are done by the concept of Dynamic Memory Allocation in C. The various methods of Dynamic Memory Allocation are as follows:
- malloc() function: It stands for "Memory allocation" and it is used for reserving a particular block of memory that is bigger in size. The return value of the malloc() method is void and this return value can be cast into any type of pointer. This function does not get memory at the time of initialization.
The syntax for the malloc() function is as follows:
- calloc() function: It stands for "Contiguous allocation" and it reserves n number of memory blocks. There are two parameters of the calloc() method in C.
The syntax for the calloc() function is as follows:
- free() function: The free() method is used for the de-allocation of memory blocks dynamically. The memory that has been allocated by the malloc() and calloc() methods needs to be de-allocated dynamically. For this, the free() function is used. The free() method saves the memory by freeing it.
The syntax for the free() method is as follows:
- realloc() function: It stands for Re-allocation and it is used for allocating the memory again. If the allocated memory becomes insufficient, then we can modify the size of the allocated memory by using this method.
The syntax for the realloc() method is as follows:
Conclusion
- The C language is the basic programming language in coding. This language was developed by Dennis Ritchie at AT & T's Bell Laboratory in 1972.
- The basics of the C program include methods like the scanf() and the printf() that are used for getting the input from the user and printing the output respectively in the console.
- Single line comments can be done by using // and, multiple line comments can be done by using the /* at the start and */ at the end.
- Various types of data types in the c language are char, int, double, float, double, and void
- Various conditional statements in C language are if, if-else, and if-else-if.
- The switch case statement is used for evaluating a given expression and then it executes the related statement with it.
- The iterative statements in c language are done by using the while, do-while, and for loops.
- Function is some fixed set of codes that are sued to perform a particular task in a program and it can be used many times. Recursion is the process of function calling the function itself.