Posts

Showing posts from June, 2024

Using Generic in TypeScript about types in the Code

Image
# Using Generic in TypeScript about types \<T> in the Code ## Question 1: Here what is this <T> means? ```typescript const addID = <T>(obj: T) => {   let ID = Math.floor(Math.random() * 100);   return ({...obj, ID}); } ``` ## Answer 1: In the code snippet you've provided, `<T>` is a **generic type parameter** in TypeScript. It allows you to write a function that can work with any type of object while still maintaining that object's specific type information. When you use the function `addID`, you can pass in an object of any type, and TypeScript will understand that the returned object will be of the same type as the input, plus the additional `ID` property. Here's a breakdown of how it works: ```typescript const addID = <T>(obj: T) => {   let ID = Math.floor(Math.random() * 100);   return ({...obj, ID}); } ``` - `<T>`: Declares a generic type `T`. - `(obj: T)`: Indicates that the function takes an argument `obj` of type `T`....

Usage of Common “Header files” in C programming

  Each of these header files serves a specific purpose in a C program: stdio.h : This header file stands for "standard input-output header" and provides functions for input and output operations such as  printf()  and  scanf() . stdlib.h : It stands for "standard library" and provides functions such as memory allocation ( malloc() ,  calloc() ,  free() ), random number generation ( rand() ,  srand() ), and other utility functions. string.h : This header file provides functions for manipulating strings such as  strcpy() ,  strcat() ,  strlen() , etc. conio.h : This header file provides functions mainly for console input and output operations in DOS and Windows environments. Functions like  getch()  and  clrscr()  are included in this header file. However, note that it's not a standard C library and is often not available in many modern compilers or platforms. time.h : This header file provides functions to work with da...

Easiest way to print `int 128 bit` or`unsigned int 128 bit`

Image
  To print `__int128`, you must use `__int128_t`data type. Because there is no data type named __int128. At least I didn’t find any till now. And You must use “typecasting” every time in assigning value and outputting values. you must use format specifier `%lld` for `__int128_t` and `%llu`for `__uint128_t`. in C++, `std::cout`printing… you must use `static_cast <long long> (var_name)` Code: # include <iostream> int main () { // int 128 bit __int128_t var = ( long long ) -23542342345435 ; // unsigned int 128 bit __uint128_t var2 = ( long long ) 64324234234234 ; // printing in c++ std::cout << "int 128 bit: " << static_cast < long long >(var) << std::endl; // printing in c printf ( "int 128 bit: %lld\n" , ( long long )var); // printing in c++ std::cout << "unsinged int 128 bit: " << static_cast < long long >(var2) << std::endl; // printing in c printf ( "unsinged int 128 bi...

Popular posts from this blog

Whiteboarding Interviews

Version Control with Git: A Beginner’s Guide

Callback function in JavaScript