Description
C programming Cheatsheet...
Syntax (Boilerplate Code) :
#include
int main()
{
return(0);
}
printf() : printf() function for used to show the output.
printf("lapmos.com");
scanf() : scanf() function for used to take the input from users.
scanf("format_specifier", &variable);
Comments : A comment is not executed by the compiler. The comments is used for the increase the readability. In c programming language two types of comments.
1. Single Line comment
// This is single Line Comment
2. Multi Line comment
/* This is
Multiple line
comment
*/
Data type : Datetype is the type of data to declare the variable.
Integer Type
int variable_name;
Character Type
char var;
Float Type
float var_name;
Double Type
double variable_name;
Void Type
void
Escape Sequences : It is a sequence of characters starting with a backslash, and it doesn't represent itself when used inside string literal.
Beep / Alarm : It produce the beep sound.
\a
backspace : Add backspace
\t
New Linke : Add New line
\n
Carriage return
\r
Tab : Gives the tab space
\t
Backslash : Add backslash
\\
Single Quote : Add single quotation mark.
\'
Question Mark : Add question mark
\?
Octal Number : It represents the value of an octal number
\nnn
Hexadecimal Number : It represents the value of an Hexadecimal number.
\xhh
Null : The null character is usually used to terminate a string.
\0
Conditional Instructions
Conditional Statement is used to perform the logical opertion in program.
if statement
if(/* condition */){
/* statement; */
}
if-else statement
if(/* condition */){
/* statement*/
}
else{
/* statement */
}
if-else-if-else statement
if(/*condition*/){
/* statement */
}
else if(/* condition */){
/* statement */
}
else
{
/* statement */
}
Switch Statement
switch(/*expression*/){
case 1 :
/*statement*/
break;
case 2 :
/* statement */
break;
....
default :
/* default statement */
}
Loops :
Loops is used for , the line of code execute repatedly then programmer used loops.
for loop
for(/*initialization*/ ; /*condition*/; /*increament/decrement*/)
{
/* code*/
}
while loop
while(/*condition*/){
/*code*/;
}
do while loop
do{
/* code */
}while(/*condition*/);
break statement : break keyword inside the loop is used to terminate the loop.
break;
continue statement : continue keyword skips the rest of the current iteration of the loop and returns to the starting point of the loop.
continue;
Function and Recursion : Function are used to divide the program in pieces and used function many times in our program.
Function Defination
return_type name_function(data_type parameter ...){
// Code of the function
}
Recursion : Recursion is when a function calls itself to solve the problem. Which function is calls itself called the recursive function.
void fun(){
... ... ..
fun();
... ... ..
}
Pointers : Pointer is the variable which contains the address of the another variable.
data_type = *variable_name;
Array : Array is the collection of the same datatypes.
data_type array_name[array_size];
Access Element
arr[index_no];
String :
A string is a 1-D character Array terminated by a null character('\0')
Declaration
char string_name[string_size];
gets() function : It allows to enter the multi-word string.
gets("lapmos.com");
puts() function : It used to show the string.
puts("lapmos.com");
strlen() function : It is used for to calculate the length of string.
strlen(string_name);
strcpy() function : It is used for copy the string to another one.
strcpy(destination, source);
strcat() function : It is used for concatenate two string.
strcat(first_string, second_string);
strcmp() function : It is used for campare two string.
strcmp(first_string, second_string);
Structure :
The Structure is a collection of different types of variable in a single. Define structure means creating a new data type.
Syntax :
struct structure_name
{
datatype member1;
datatype member2;
... .... ...
};
typedef keyword : typedef function allows user to provide alternative names for the primitive and user-defined data type.
typedef struct structure_name
{
datatype member1;
datatype member2;
... ... ...
}name;
Or
struct structure_name
{
datatype member1;
datatype member2;
... ... ...
};
typedef structure_name new_name;
File Handling
File handling in C enables us to create, update, read, and delete the files stored on the local file system through our C program.
FILE pointer
FILE *filepointer;
Opening a file : It is used to open file in C.
filePointer = fopen(filename.txt,w);
fscanf() function : It is used to read the content of 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.
fgetc(FILE *pointer);
fputc() function : It writes a character to file opened in write mode.
fputc(char, FILE,*pointer);
Closing a File : It close the file.
fclose(filePointer);
Dynamic Memory Allocation
The concept of dynamic memory allocation in c language enables the C programmer to allocate memory at runtime.
malloc() function :
ptr = (TypeCast*)malloc(size());
calloc() function :
ptr = (TypeCase*)calloc(n,size);
free function :
free(ptr);
realloc() function :
ptr = realloc(ptr, x);
Please Do comment and gives your Suggestion.
Thanks......