For In depth Knowledge Click here.
Basic syntax and functions from the C programming language.
#include<stdio.h>
int main(){
return(0);
}
It is used to show output on the screen
printf("Hello World!");
Here's a table containing commonly used types in C programming for quick access.
Type | Size (bytes) | Format Specifier |
---|---|---|
int |
at least 2, usually 4 | %d , %i |
char |
1 | %c |
float |
4 | %f |
double |
8 | %lf |
short int |
2 usually | %hd |
unsigned int |
at least 2, usually 4 | %u |
long int |
at least 4, usually 8 | %ld , %li |
long long int |
at least 8 | %lld , %lli |
unsigned long int |
at least 4 | %lu |
unsigned long long int |
at least 8 | %llu |
signed char |
1 | %c |
unsigned char |
1 | %c |
long double |
at least 10, usually 12 or 16 | %Lf |
It is used to take input from the user
scanf("placeholder", variables);
A comment is the code that is not executed by the compiler, and the programmer uses it to keep track of the code.
// It's a single line comment
/*
It's a
multi-line
comment
*/
The data type is the type of data
Typically a single octet(one byte). It is an integer type
char variable_name;
The most natural size of integer for the machine
int variable_name;
A single-precision floating-point value
float variable_name;
A double-precision floating-point value
double variable_name;
Represents the absence of the type
void
It is a sequence of characters starting with a backslash, and it doesn't represent itself when used inside string literal.
It produces a beep sound
\a
It adds a backspace
\b
\f
Newline Character
\n
\r
It gives a tab space
\t
It adds a backslash
\\
It adds a single quotation mark
\'
It adds a question mark
\?
It represents the value of an octal number
\nnn
It represents the value of a hexadecimal number
\xhh
The null character is usually used to terminate a string
\0
Conditional statements are used to perform operations based on some condition.
if (/* condition */){
/* code */
}
if (/* condition */){
/* code */
}
else{
/* Code */
}
if (condition) {
// Statements;
}
else if (condition){
// Statements;
}
else{
// Statements
}
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 facilitate programmers to execute any block of code lines repeatedly and can be controlled as per conditions added by the programmer.
It allows execution of statement inside the block of the loop until the condition of loop succeeds.
while (/* condition */){
/* code */
}
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 */);
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 keyword inside the loop is used to terminate the loop
break;
continue keyword skips the rest of the current iteration of the loop and returns to the starting point of the loop
continue;
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.
return_type function_name(data_type parameter...){
//code to be executed
}
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();
... .. ...
}
Pointer is a variable that contains the address of another variable,
datatype *var_name;
An array is a collection of data items of the same type.
data_type array_name[array_size];
int variable_name = array[index];
A string is a 1-D character array terminated by a null character ('\0')
char str_name[size];
It allows you to enter multi-word string
gets("string");
It is used to show string output
puts("string");
It is used to calculate the length of the string
<string.h> is used to perform string functions
strlen(string_name);
It is used to copy the content of second-string into the first string passed to it
strcpy(destination, source);
It is used to concatenate two strings
strcat(first_string, second_string);
It is used to compare two strings
strcmp(first_string, second_string);
There are some pre defined macors and there type are :
- __Date__ : Thee current date as a chracter in : "MM : DD : YY" format.
- __TIME__ : The current time as a character in "HH : MM : SS" format
- __FILE__ : This contains the current file name.
- __LINE__ : This contains current line number.
#include <stdio.h>
int main()
{
printf("Current time: %s",__TIME__);
printf("Current Date: %s",__DATE__);
printf("Current file name: %s",__FILE__);
printf("Current line number: %s",__LINE__);
return 0;
}
A macro is a fragment of code that is given a name. You can define a macro in C using the #define preprocessor directive.
Here's an example.
#define PI 3.1415 // Value of PI
#include <stdio.h>
#define PI 3.1415
int main()
{
float radius, area;
printf("Enter the radius: ");
scanf("%f", &radius);
// Notice, the use of PI
area = PI*radius*radius;
printf("Area=%.2f",area);
return 0;
}
The structure is a collection of variables of different types under a single name. Defining structure means creating a new data type.
struct structureName {
dataType member1;
dataType member2;
...
};
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;
A set of methods for handling File IO (read/write/append) in C language
FILE *filePointer;
Mode | Meaning of Mode | During Inexistence of file |
---|---|---|
r |
Open for reading. | If the file does not exist, fopen() returns NULL. |
rb |
Open for reading in binary mode. | If the file does not exist, fopen() returns NULL. |
w |
Open for writing. | If the file exists, its contents are overwritten. If the file does not exist, it will be created. |
wb |
Open for writing in binary mode. | If the file exists, its contents are overwritten. If the file does not exist, it will be created. |
a |
Open for append. Data is added to the end of the file. |
If the file does not exist, it will be created. |
ab |
Open for append in binary mode. Data is added to the end of the file. |
If the file does not exist, it will be created. |
r+ |
Open for both reading and writing. | If the file does not exist, fopen() returns NULL. |
rb+ |
Open for both reading and writing in binary mode. | If the file does not exist, fopen() returns NULL. |
w+ |
Open for both reading and writing. | If the file exists, its contents are overwritten. If the file does not exist, it will be created. |
wb+ |
Open for both reading and writing in binary mode. | If the file exists, its contents are overwritten. If the file does not exist, it will be created. |
a+ |
Open for both reading and appending. | If the file does not exist, it will be created. |
ab+ |
Open for both reading and appending in binary mode. | If the file does not exist, it will be created. |
It is used to open file in C.
filePointer = fopen(fileName.txt, w)
It is used to read the content of file.
fscanf(FILE *stream, const char *format, ...)
It is used to write content into the file.
fprintf(FILE *fptr, const char *str, ...);
It reads a character from a file opened in read mode. It returns EOF on reaching the end of file.
fgetc(FILE *pointer);
It writes a character to a file opened in write mode
fputc(char, FILE *pointer);
It closes the file.
fclose(filePointer);
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
<stdlib.h> header file is used in memory allocation functions
Stands for 'Memory allocation' and reserves a block of memory with the given amount of bytes.
// ptr = (int*)malloc(number of blocks * size of each block in byte)
ptr = (castType*) malloc(size);
Example:
ptr = (int *)malloc(n*sizeof(int));
Stands for 'Contiguous allocation' and reserves n blocks of memory with the given amount of bytes.
// ptr = (int*)malloc(number of blocks, size of each block in byte)
ptr = (castType*)calloc(n, size);
It is used to free the allocated memory.
free(ptr);
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);
Exit function is used to exit with program
#include <stdio.h>
int main()
{
int a = 2;
int b = 3;
int value = a+b;
printf("The ans is : %d\n",value);
exit(0);
retunr 0;
}