1/1/1970
typedef keyword is used to give a new name (alias) to an existing data type.(int, char, int*, struct, etc.)typedef <previous_name> <alias_name>;typedef for Inbuilt datatype
#include <stdio.h>
typedef long long ll // ll : long long
int main()
{
typedf unsigned long ul; // ul : unsigned long
ul u1, u2; // unsigned long
ll l1, l2; // long long
intPtr p = &value;
return 0;
}typedef for Structure
typdef struct Student
{
int marks;
char name[34];
}std; // std : struct Student
// Or we could use typedef after declaring struct Student
// typedef struct Student std;std s1, s2; // struct Student s1, s2
s1.id = 56;
s2. id = 89;typedef for Pointer
typedef int* intPtr // intPtr : int*
intPtr a, b; // int* a, b
int c = 89;
a = &c;
b = c;PreProcessing -> Compilation -> Assembly -> Linking
.o or .exe, printf are not resolved , Assembly Level Instructions are converted to machine code.What is a C Pre-Processor
C preprocessor comes under action before the actual compilation process.
C preprocessor is not a part of the c compiler
It is a text substitution tool
All preprocessor commands begin with a hash symbol #
#define: Used to define a macro.
#include: Include an external header file.
#undef: Undefine preprocessor macros.
#ifdef: Check if a macro is defined (defined: 1, not defined: 0).
#ifndef: Check if a macro is not defined (defined: 0, not defined: 1).
#if: If any compile-time condition is true.
#elif: Alternative of if; if not true, then it is checked.
#else: Execute if no condition is true.
#endif: Ends a conditional preprocessor directive.
#pragma: To issue some special commands to the compiler.
Include
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define and #include Preprocessor Directives: C Tutorial In Hindi#include directive causes the preprocessor to fetch the contents of some other file to be included in the present file#include some other file(s) which may in turn do the same. Most commonly the #inlcuded files have a ".h" extension, indicating that they are header files.#Includemostly header files have promises i.e. is function prototypes.
In C programming there are two common formats for #includes:
#include < headerFiIe.h > // The angle brackets say to look in the standard system directories#include " myFile.h" // The quotation marks say to look in the current directory.Disk drive full path is allowed, but discouraged since it is not portable:
#include <C:\Program Files\Harry\bhai\somefile.h > // Too specific#include <sys/file.h> // Relative and portable path to the standard locations.#define#define directive is used to "define" preprocessor "variables"#define preprocessor directive can be used to globally replace a word with a number.#define PI 3.141
#define for Debugging
#define directive can be used for debuggingifdef" block as follows:#define DEBUG
#ifdef DEBUG {print statement...}
#endif ...
#undef DEBUG
Macros using #define
#define#define SQUARE(x) x*x
float PI = 3.141
area = PI * SQUARE (radius);
Notes: Macros are resolved at, preprocessing and so faster and efficient than functions.
#Include directive
#include <stdio.h>
#include "Tutorial2.c" // contain functionDangling();
int main()
{
int *ptr = functionDangling(); // from `Tutorial2.c` file
return 0;
}#Define directive
# define PI 3.141
int main()
{
float var = PI;
printf("The value of PI is %f\n", var); // 3.141
return 0;
}#define Macros
#define SQUARE(r) r*r
int main()
{
int r = 4;
printf("The area of this circle is %f\n", PI*SQUARE(r));
}#include directive causes the preprocessor to fetch the contents of some other file to be included in the present file#includes#include some other file(s) which may in turn do the same.#include files have a ".h" extension, indicating that they are header files.#define directive is used to "define" preprocessor "variables"__DATE__: The current date as character literal in "MMM DD YYYY" format__TIME__: This contains the current time as a character literal in "HH:MM:SS" format.__FILE__: The current filename as a string literal.__LINE__: The current line number as a decimal constant.__STDC__: Define as 1(one) when the compiler complies with the ANSI standard.int main()
{
printf("FILE name is %s\n", __FILE__ ); // tutorial60.c
printf("Today's Date is %s\n", __DATE__); // Oct 06 2024
printf("Time Now is %s\n", __TIME__); // 12:16:31
printf("Line No. is %d\n", __LINE__); // 7
printf("ANSI: %d\n", __STDC__); // 1
}
int arr[10]
int a
malloc() stands for memory allocation |--------|
int *ptr ------> Heap |
|________|
garbage valuesptr = (ptr_type*) malloc(size_in_bytes)for ex, for an array If we need 3 integer space -> 3*sizeof(int);
ptr = (int*) malloc(3*sizeof(int));Void Pointer:
calloc() stands for contiguous allocationn blocks of memory with the given amount of bytes. |--------|
int *ptr ------> Heap |
|________|
0ptr = (ptr_type*) calloc(n, size_in_bytes)for ex, for an array If we need 3 integer space -> 3*sizeof(int);
ptr = (int*) malloc(3, sizeof(int));realloc() stands for reallocationrealloc() function |--------|-----|
int *ptr ------> Heap | + |
|________|_____|
ptr = (ptr_type) realloc(ptr, new_size_in_bytes)free(ptr)Note: Malloc() vs Calloc()
malloc: Allocates a block of memory but does not initialize the memory. The memory block will contain garbage values.calloc: Allocates memory and initializes all bits to zero. This means all the values in the allocated memory will initially be set to zero.<stdilib.h> file#include <stdio.h>
#include <stdlib.h>
int main(){
// Use of malloc
int *ptr;
int n;
printf("Enter the size of the array you want to create\n");
scanf("%d", &n);
ptr = (int *)malloc(n*sizeof(int));
for(int i=0; i<n; i++){
// ptr[i]=*(ptr+i):value at pointer (ptr+i),
// &ptr[i]=(ptr+i):address of pointer (ptr+i)
printf("Enter the value no %d of this array\n", i);
scanf("%d", &ptr[i]);
}
for(int i=0; i<n; i++){
print("The value at %d of this array is %d\n", i, ptr[i]);
}
}input
Enter the size of the array you want to create
3
Enter the value no 0 of this array
5
Enter the value no 1 of this array
6
Enter the value no 2 of this array
7
Output : Memory is allocated at runtime i.e. size n
The value at 0 of this array is 5
The value at 1 of this array is 6
The value at 2 of this array is 7
Value at out of bound of pointer
print("The value at ptr[3] is %d\n", ptr[3]); // 12838462834 Garbage Value#include <stdio.h>
#include <stdlib.h>
int main(){
// Use of calloc
int *ptr;
int n;
printf("Enter the size of the array you want to create\n");
scanf("%d", &n);
ptr = (int *)calloc(n,sizeof(int));
//for(int i=0; i<n; i++){
// print("Enter the value no %d of this array\n", i);
// scanf("%d", &ptr[i]);
//}
for(int i=0; i<n; i++){
print("The value at %d of this array is %d\n", i, ptr[i]);
}
}Input
Enter the size of the array you want to create
4
Output : If value not Initialised in Calloc, it is set to 0
The value at 0 of this array is 0
The value at 1 of this array is 0
The value at 2 of this array is 0
The value at 2 of this array is 0
Consider Code Connected after the Calloc i.e ptr assigne 4 byte of memory
// Use of calloc
printf("Enter the size of the new array you want to create\n");
scanf("%d", &n);
ptr = (int *)realloc(ptr,n*sizeof(int));
for(int i=0; i<n; i++){
print("Enter the new value no %d of this array\n", i);
scanf("%d", &ptr[i]);
}
for(int i=0; i<n; i++){
print("The new value at %d of this array is %d\n", i, ptr[i]);
}Input
Enter the size of the new array you want to create\n
6
Enter the new value no 0 of this array
1
Enter the new value no 1 of this array
2
Enter the new value no 2 of this array
3
Enter the new value no 3 of this array
4
Enter the new value no 4 of this array
5
Enter the new value no 5 of this array
6
Output
The new value at 0 of this array is 1
The new value at 1 of this array is 2
The new value at 2 of this array is 3
The new value at 3 of this array is 4
The new value at 4 of this array is 5
The new value at 5 of this array is 6
free(ptr); // its a good practice to use free