C (Harry)

1/1/1970

C (Harry)

Goto Statement In C: C Tutorial In Hindi #17

Structures In C: C Tutorial In Hindi #37

#38 Typedef In C: C Tutorial In Hindi

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;

#42 Static Variables In C: C Tutorial In Hindi

#58 C Pre-processor Introduction & Working: C Tutorial In Hindi

PreProcessing -> Compilation -> Assembly -> Linking
  1. Pre-Processing : Removal of comments, Expansion of macros, Expansion of include files
  2. Compilation : Assembly level instructions are generated
  3. Assembly : make .o or .exe, printf are not resolved , Assembly Level Instructions are converted to machine code.
  4. Links the function implementations

What is a C Pre-Processor

Include

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#59 #define and #include Preprocessor Directives: C Tutorial In Hindi

#Include

mostly header files have promises i.e. is function prototypes.

In C programming there are two common formats for #includes:

Disk drive full path is allowed, but discouraged since it is not portable:

#define

#define PI 3.141

#define for Debugging

#define DEBUG
#ifdef DEBUG {print statement...}
#endif ...
#undef DEBUG

Macros using #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));
}

#60 Predefined Macros & Other Pre-processor Directives: C Tutorial In Hindi

Predefined Macros in C

  1. __DATE__: The current date as character literal in "MMM DD YYYY" format
    1. __TIME__: This contains the current time as a character literal in "HH:MM:SS" format.
    1. __FILE__: The current filename as a string literal.
    1. __LINE__: The current line number as a decimal constant.
    1. __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 
}

#47 Dynamic Memory Allocation Malloc Calloc Realloc & Free(): C Tutorial In Hindi

Recap

  1. Code
  2. Static/global variables
  3. Stack
  4. Heap

Stack

int arr[10] int a

Global and Static Variable

Functions for Dynamic Memory Allocation in C

  1. malloc
  2. calloc
  3. realloc
  4. free

C MALLOC()

              |--------|
int *ptr ------> Heap  |
              |________|

for ex, for an array If we need 3 integer space -> 3*sizeof(int);

ptr = (int*) malloc(3*sizeof(int));

Void Pointer:

C CALLOC()

              |--------|
int *ptr ------> Heap  |
              |________|

for ex, for an array If we need 3 integer space -> 3*sizeof(int);

ptr = (int*) malloc(3, sizeof(int));

C REALLOC()

              |--------|-----|
int *ptr ------> Heap  |  +  |
              |________|_____|  

C FREE()

Note: Malloc() vs Calloc()

Lets Code

Malloc

#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

Calloc

#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

Realloc

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

free(ptr);  // its a good practice to use free