”;
This C language cheat sheet gives a quick overview of C language concepts starting from the basics to the advanced level. This cheat sheet is very useful for students, developers, and those who are preparing for an interview. Go through this cheat sheet to learn all basic and advanced concepts of C programming language.
Basis Structure of C Program
The basic structure of a C program gives you an idea about the basic statements that you need to use to write a program in C language. The following is the basic structure of a C program −
// Preprocessor directive/header file inclusion section #include <stdio.h> // Global declaration section // the main() function int main() { // Variable declarations section int x, y; // other code statements section // Return o return 0; } // Other user-defined function definition section
#include <stdio.h>
#include is a preprocessor directive that includes the header file in the C program. The stdio.h is a header file where all input and output-related functions are defined.
main() Function
The main() function is an entry point of a C program, the program”s executions start from the main() function.
The below is the syntax of the main() function −
int main() { return 0; }
Comments
There are two types of comments in C language. Single-line and multi-line comments. Comments are ignored by the compilers.
Single-line Comments
Use // to write a single-line comment.
// This is a single-line comment
Multi-line Comments
Use /* and */ before and after the text to write multi-line comments in C language.
/* This is line 1 This is line 2 .. */
Printing (printf() Function)
The printf() function is a library function to print the formatted text on the console output. Whenever you want to print anything, use the printf().
Example
printf("Hello world");
User Input (scanf() Function)
The scanf() function is used to take various types of inputs from the user.
Here is the syntax of the scanf() function −
scanf("format_specifier", &variable_name);
Format Specifiers
The following is the list of C format specifiers that are used in printf() and scanf() functions to print/input specific type of values.
Format Specifier | Type |
---|---|
%c | Character |
%d | Signed integer |
%e or %E | Scientific notation of floats |
%f | Float values |
%g or %G | Similar as %e or %E |
%hi | Signed integer (short) |
%hu | Unsigned Integer (short) |
%i | Unsigned integer |
%l or %ld or %li | Long |
%lf | Double |
%Lf | Long double |
%lu | Unsigned int or unsigned long |
%lli or %lld | Long long |
%llu | Unsigned long long |
%o | Octal representation |
%p | Pointer |
%s | String |
%u | Unsigned int |
%x or %X | Hexadecimal representation |
Example
#include <stdio.h> int main(){ int age = 18; float percent = 67.75; printf("Age: %d nPercent: %f", age, percent); return 0; }
Output
Age: 18 Percent: 67.750000
Data Types
The data types specify the type and size of the data to be stored in a variable. Data types are categorized in 3 sections −
- Basic Data Types
- Derived Data Types
- User-defined Data Types
Basic Data Types
The basic data types are the built-in data types in C language and they are also used to create derived data types.
Data Type | Name | Description |
---|---|---|
int | Integer | Represents integer Value |
char | Character | Represents a single character |
float | Float | Represents float value |
Derived Data Types
The derived data types are derived from the basic data types. The derived data types are −
- Array
- Pointer
User-defined Data Types
The user-defined data types are created by the programmer to handle data of different type and based on the requirements. The user-defined data types are −
- Structures
- Unions
- Enumerations
Basic Input & Output
For basic input and output in C language, we use printf() and scanf() functions.
The printf() function is used to print the formatted text on the console.
printf("Hello world");
The scanf() function is used to take input from the user.
scanf("%d", &x); // Integer input scanf("%f", &y); // float input scanf("%c", &z); // Character Input scanf("%s", name); // String input
Example of Basic Input and Output
#include <stdio.h> int main() { int num; printf("Input any integer number: "); scanf("%d", &num); printf("The input is: %dn", num); return 0; }
Output
Input any integer number: The input is: 0
Identifiers
C identifiers are user-defined names for variables, constants, functions, etc. The following are the rules for defining identifiers −
- Keywords can”t be used as identifiers.
- Only alphabets, underscore symbol (_), and digits are allowed in the identifier.
- The identifier must start either with an alphabet or an underscore.
- The same identifier can”t be used as the name of two entities.
- Identifiers should be meaningful and descriptive.
Examples of Valid Identifiers
age, _name, person1, roll_no
Keywords
C keywords are the reversed words in the C compiler, which are used for specific purposes and must not be used as an identifier.
The following are the keywords in C language −
auto | double | int | struct |
break | else | long | switch |
case | enum | register | typedef |
char | extern | return | union |
continue | for | signed | void |
do | if | static | while |
default | goto | sizeof | volatile |
const | float | short | unsigned |
Variables
C variables are the name given to a storage area that our programs can use to access and manipulate the data.
Syntax of Declaring a Variable
data_type variable_name;
Escape Sequences
Escape sequences are the special characters followed by the escape (backward slash ). Escape sequences have special meanings and are used for printing those characters that cannot be printed normally.
Here is the list of escape sequences in C language −
Escape sequence | Meaning |
---|---|
\ | character |
” | ” character |
“ | ” character |
? | ? character |
a | Alert or bell |
b | Backspace |
f | Form feed |
n | Newline |
r | Carriage return |
t | Horizontal tab |
v | Vertical tab |
ooo | Octal number of one to three digits |
xhh . . . | Hexadecimal number of one or more digits |
Operators
Operators are the special symbols that are used to perform specific mathematical or logical operations.
Below are the operators used in C language −
Operators | Symbols | Description |
---|---|---|
Assignment Operators | =, +=, -=, <<= | Performs assignment operations i.e., assigning values to variables. |
Arithmetic Operators | +, -, *, /, % | Performs arithmetic operations. |
Relational Operators | <, >, <=, >=, ==, != | Performs comparisons on two operands. |
Logical Operators | &&, ||, ! | Performs logical operations such as logical AND, OR, and NOT. |
Bitwise Operators | &, ^, |, <<, >>, ~ | Performs bitwise operations. |
Ternary Operator | ? : | Performs conditional operation for decision-making. |
Miscellaneous Operators | , sizeof, &, *, ⇒, . | Used for performing various other operations. |
Example of Operators
result = num1 + num2; if(result >=10){ printf("Greater than 10."); }
Conditional Statements
C language provides the following conditional statements −
- if Statement
- if-else Statement
- if-else-if Statement
- Nested if-else Statement
- Switch Statement
- Ternary Operator
if Statement
An if statement consists of a Boolean expression followed by one or more statements.
The syntax of if statement is −
if(boolean_expression) { /* statement(s) will execute if the boolean expression is true */ }
if-else statement
An if-else statement can be followed by an optional else statement, which executes when the Boolean expression is false.
The syntax of the if statement is −
if (Boolean expr){ Expression; . . . } else{ Expression; . . . }
if-else-if Statement
The if-else-if statement is also known as the ladder if-else. It is used to check multiple conditions when a condition is not true.
The syntax of if-else-if statement −
if(condition1){ } else if(condition2){ } … else{ }
Nested if Statements
By using the nested if statements, you can use one if or else-if statement inside another if or else-if statement(s).
The syntax of nested if statements −
if (expr1){ if (expr2){ block to be executed when expr1 and expr2 are true } else{ block to be executed when expr1 is true but expr2 is false } }
Switch Statement
A switch statement allows a variable to be tested for equality against a list of values.
The syntax of the switch statement is −
switch (Expression){ // if expr equals Value1 case Value1: Statement1; Statement2; break; // if expr equals Value2 case Value2: Statement1; Statement2; break; . . // if expr is other than the specific values above default: Statement1; Statement2; }
Ternary Operator
The ternary operator (?:) is also known as the conditional operator. It can be used as a replacement for an if-else statement.
The syntax of the ternary operator is −
(condition) ? true_block: false_block;
Loops
C loops are used to execute blocks of one or more statements respectively a specified number of times, or till a certain condition is reached. The following are the loop statements in C language −
- while Loop
- do…while Loop
- for Loop
while Loop
The while loop is an entry-control loop where the condition is checked before executing the loop body.
The syntax of the while loop is −
while(test_condition){ // Statement(s); }
do…while Loop
The do…while loop is an exit control loop where the body of the loop executes before checking the condition.
The syntax of do…while loop is −
do{ // Statement(s); }while(test_condition);
for Loop
The for loop is also an entry-controlled loop where the elements (initialization, test condition, and increment) are placed together to form a for loop inside the parenthesis with the for keyword.
The syntax of the for loop is −
for(initialization ; test condition ; increment){ // Statement (s); }
Jump Statements
Jump statements are used to transfer the flow of the program from one place to another. The following are the jump statements in C language −
- goto Statement
- break Statement
- continue Statement
goto Statement
The goto statement transfers the program”s control to a specific label. You need to define a label followed by the colon (:). The goto statement can transfer the program”s flow up or down.
The syntax of the goto statement is −
label_name:
//Statement(s) if(test_condition){ goto label_name; }
break Statement
The break statement can be used with loops and switch statements. The break statement terminates the loop execution and transfers the program”s control outside of the loop body.
The syntax of the break statement is −
while(condition1){ . . . . . . if(condition2) break; . . . . . . }
continue Statement
The continue statement is used to skip the execution of the rest of the statement within the loop in the current iteration and transfer it to the next loop iteration.
The syntax of the continue statement is −
while (expr){ . . . . . . if (condition) continue; . . . }
User-defined Functions
The user-defined function is defined by the user to perform specific tasks to achieve the code reusability and modularity.
Example of user-defined function
#include <stdio.h> // Function declaration int add(int, int); // Function definition int add(int a, int b) { return (a + b); } int main() { int num1 = 10, num2 = 10; int res_add; // Calling the function res_add = add(num1, num2); // Printing the results printf("Addition is : %dn", res_add); return 0; }
Output
Addition is : 20
Arrays
An array is a collection of data items of similar data type which are stored at a contiguous memory location. The data item may be primary data types (int, float, char), or user-defined types such as struct or pointers can be stored in an array.
C Arrays can be of two types −
- One-dimensional (1D) Array − A one-dimensional array is a single list of data items of the same data type.
- Multi-dimensional Arrays − A multi-dimensional array such as a two-dimensional array is an array of arrays.
Syntax of Arrays
The following is the syntax of declarations of different types of arrays −
type array_name [size1]; // One-dimensional array type array_name [size1][size2]; // Two-dimensional arrays type array_name [size1][size2][size3]; // Three-dimensional arrays
Example of One-dimensional Array
#include <stdio.h> int main(){ int numbers[5] = {10, 20, 30, 40, 50}; int i; // loop counter // Printing array elements printf("The array elements are : "); for (i = 0; i < 5; i++) { printf("%d ", numbers[i]); } return 0; }
Output
The array elements are : 10 20 30 40 50
Example of Two-dimensional Arrays
#include <stdio.h> int main () { /* an array with 5 rows and 2 columns*/ int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}}; int i, j; /* output each array element''s value */ for ( i = 0; i < 5; i++ ) { for ( j = 0; j < 2; j++ ) { printf("a[%d][%d] = %dn", i,j, a[i][j] ); } } return 0; }
Output
a[0][0] = 0 a[0][1] = 0 a[1][0] = 1 a[1][1] = 2 a[2][0] = 2 a[2][1] = 4 a[3][0] = 3 a[3][1] = 6 a[4][0] = 4 a[4][1] = 8
Strings
C string is a sequence of characters i.e., an array of char data type, terminated by “null character” represented by ”