Sunday, December 6, 2020

, ,

Example Program for memset function in c

 memset() function in C use to take a memory location and taken a VOID* pointer and this function copies the first n byte in memory location.

After update the memory location it return that location with the help of pointer.

Example Program for memset function in c
memset c

 

Syntax :  -

void* memset(void* mem_loc, int c, size_t n);

mem_loc is a memory location

c is an unsigned character in that upper syntax


Header File for memset function in c

Here this function deal with the characters so we have to use header file in the program

Now the complete import for memset function

#include <string.h>
 
void* memset(void* mem_loc, int c, size_t n);

Program Example for memset c

#include <stdio.h>
#include <string.h>
 
int main() {
    char a[] = {"Hello from functioninc"};
 
    printf("a = %s\n", a);
     
    printf("Filling the first 5 characters a with 'P' using memset\n");
     
    memset(a, 'P', 5 * sizeof(char));
     
    printf("After memset, a = %s\n", a);
 
    return 0;
}

Then the 5 character of the given string is filled by the 'P'

a = Hello from Functioninc
Filling the first 5 characters with 'P' using memset function in c
After memset c, a = PPPPP from Functioninc

Conclusion of memset c

Here we learn about the memset function in c and how to allocate the memory location.

Friday, December 4, 2020

, ,

Understand the extern in c with extern c example

  •  extern keyword in C is used when we have multiple source file and we want to shear the variable among those files.
  • Or when we prefixing a global variable with the extern keyword in C that's mean we are telling to the compiler that the variable is already defined in other file.
  • That's means don't allocate any memory for the same variable twice.

What is use of extern in c

extern can be use in two type :-

  • A variable declaration statement 

  • A variable definition statement 

declaration statement for extern

int a;
 
char* ch;

With the help of extern in c we can tell to the compiler that these variables already exist somewhere so don't allocate any memory for these variables.

There is an another way to defining a variable,where you can allocate storage for the given variable. 

// Defining a
int a = 11;
 
// Defining ch, even if it is NULL
char* ch = NULL;

let's check the use of extern in c. You can use this extern keyword only for declaring a global variable.

// Allowed. Variable declaration
extern int a;
 
// Not allowed. extern with variable definition
extern int b = 5;

Let's know about the use of extern in c with example

Assume we have to files 

  •     file1x.c, file1x.h
  •     file2x.c file2x.h.

And a main.c which is an drive program.

Here we have a global variable which is called int file1_var defined in file1x.c.

Here we use extern in extern c example for sharing the same variable for file2x.c.

Here the extern c example : -

// file1.h
// Contains function prototypes for functions of file1.c
 
int addition_1(int a, int b);


// file1.c
// Contains function definition for addition_1()
#include "file1.h"
#include <stdio.h>
 
int file1_var = 100;
 
int addition_1(int a, int b) {
    file1_var = file1_var + 100;
    printf("Inside file1.c addition(). file1_var = %d\n", file1_var);
    return a + b;
}


As you can see that two files share a variable file1_var with the help of extern keyword and this variable will be update when the addition() functions gets called.

Now let's write the main.c program with both the header files file1x.h and file2x.h



#include "file1.h"
#include "file2.h"
#include <stdio.h>
 
// We can also use the file1_var reference here!
extern int file1_var;
 
// We must have only one main() reference
// Since this is our driver program, the main function must be only here
int main() {
    int res1 = addition_1(10, 20);
    int res2 = addition_2(30, 40);
    printf("file1_var = %d\n", file1_var);
    return 0;
}

Output extern c example

Output extern c example
Output extern c example


Thursday, December 3, 2020

, , , ,

strtok Fucntion in c with example Program for strtok

  •  strtok function in c programming will help you to give the token of given input.
  • This kind of function (strtok in c ) is very useful which reduce the length of c program.
strtok in c
 strtok

 

Use of the strtok function in c

strtok is an inbuilt function which is part of the <string.h> c header file.

Let's see first : - 

#include <string.h>
 
char* strtok(char* str, const char* delim);
  • This function take an input str and also a delimiter character delim.
  • Then strtok() in c split the input string into the tokens which is based on the delimited character.

Return Value of strtok in c

  • This function return a single string.
  • strtok() in c call this function continuously until we get NULL input sting.

Example for strtok in c

#include <stdio.h>
#include <string.h>
 
int main() {
    // Our input string
    char input_string[] = "Hello from function in c!";
 
    // Our output token list
    char token_list[20][20]; 
 
    // We call strtok(input, delim) to get our first token
    // Notice the double quotes on delim! It is still a char* single character string!
    char* token = strtok(input_string, " ");
 
    int num_tokens = 0; // Index to token list. We will append to the list
 
    while (token != NULL) {
        // Keep getting tokens until we receive NULL from strtok()
        strcpy(token_list[num_tokens], token); // Copy to token list
        num_tokens++;
        token = strtok(NULL, " "); // Get the next token. Notice that input=NULL now!
    }
 
    // Print the list of tokens
    printf("Token List:\n");
    for (int i=0; i < num_tokens; i++) {
        printf("%s\n", token_list[i]);
    }
 
    return 0;
}


We use the strtok(NULL, " ") function in c and use the loop for until the get NULL.

Output strtok in c

Token List:
Hello
from
Function
in
c

Tuesday, December 1, 2020

,

Example program for map() function in python

 map() function in Python applies a given function to every item of an iterable and give a list of results.

Syntax:-

map(function, iterable, ...)

Parameter For map() function in python 

map() function use two Parameter these are follow : - 

  •  function is a first Parameter map() function passes every item of iterable to the given function.
  • iterables are second which is going to to be mapped.

 Return value : - map() function in python return the list of result.

Example of map() function in python

def calculateSquare(n):
    return n*n


numbers = (1, 2, 3, 4)
result = map(calculateSquare, numbers)
print(result)

# converting map object to set
numbersSquare = set(result)
print(numbersSquare)

OUTPUT : -

map() function output
 map() function output

Well Python lambda Function commonly used with map () python function.



Monday, November 30, 2020

, , ,

date difference function in sql with example program

  •  datediff sql function will helps to calculate the difference between two dates in week,year,months etc.
  • datediff sql function accept the three arguments start_date, end_date and date_part.
  •  date_part is a part of the date like year months and week that's compare between the start_date and end_date.
  • start_date and end_date are two dates which is going to be compare.They contain the value in the form of  type DATE, DATETIME, DATETIMEOFFSET, DATETIME2, SMALLATETIME, or TIME.

Table for date_part for datediff sql function

sql server datediff
sql server datediff

Return value of sql server datediff function

SQL DATEDIFF() function will return the integer value to indicate the  difference between the start_date and end_date and specified by the date_part.

sql server datediff function return the error if range of the integer return value is (-2,147,483,648 to +2,147,483,647).

 Example for  datediff SQL function  for SQL Server

  •  let see the differences between two date value 

Now use the DATEDIFF() function in SQL to compare two dates dates in various date parts:

DECLARE 
    @start_dt DATETIME2= '2019-12-31 23:59:59.9999999', 
    @end_dt DATETIME2= '2020-01-01 00:00:00.0000000';

SELECT 
    DATEDIFF(year, @start_dt, @end_dt) diff_in_year, 
    DATEDIFF(quarter, @start_dt, @end_dt) diff_in_quarter, 
    DATEDIFF(month, @start_dt, @end_dt) diff_in_month, 
    DATEDIFF(dayofyear, @start_dt, @end_dt) diff_in_dayofyear, 
    DATEDIFF(day, @start_dt, @end_dt) diff_in_day, 
    DATEDIFF(week, @start_dt, @end_dt) diff_in_week, 
    DATEDIFF(hour, @start_dt, @end_dt) diff_in_hour, 
    DATEDIFF(minute, @start_dt, @end_dt) diff_in_minute, 
    DATEDIFF(second, @start_dt, @end_dt) diff_in_second, 
    DATEDIFF(millisecond, @start_dt, @end_dt) diff_in_millisecond;

OUTPUT

DATEDIFF() function in SQL
DATEDIFF() function in SQL

Sunday, November 29, 2020

, ,

meshgrid MATLAB Function With It's example surface plot of a function.

meshgrid matlab Function:-

Helps to generate X and Y matrices for three-dimensional plots.

Syntax For meshgrid: -

[X,Y] = meshgrid(x,y)
[X,Y] = meshgrid(x)
[X,Y,Z] = meshgrid(x,y,z)

Description for meshgrid matlab Function

  • meshgrid function will help to trans transforms the given domain which is specified by the  x and y into arrays X and Y and these array can be used to check the two variables and three-dimensional mesh/surface plots.
  •  Output rows of array X will be copies of x vector; same as the columns  output array Y will be copies of y vector
  •  [X,Y] = meshgrid(x) is like [X,Y] = meshgrid(x,x).
  •  [X,Y,Z] = meshgrid(x,y,z) helps to produces the three-dimensional arrays which is used to check the desired functions of three variables and three-dimensional volumetric plots.

 Important Note for meshgrid matlab Function

  •  The meshgrid function in MATLAB is same as the ndgrid function but the point that should be remembered that is "in meshgrid function the order of  first two input and output arguments is switched"

     [X,Y,Z] = meshgrid(x,y,z)

gives the same result as

    [Y,X,Z] = ndgrid(y,x,z)

  •   meshgrid function in MATLAB is surely suited for two- or three-dimensional Cartesian space.
  •  ndgrid fucntion in MATLAB is suited for multidimensional problems that are not spatially based.

 Example for meshgrid matlab Function

    [X,Y] = meshgrid(1:3,10:14)

    X =

         1     2     3
         1     2     3
         1     2     3
         1     2     3
         1     2     3

    Y =

        10    10    10
        11    11    11
        12    12    12
        13    13    13
        14    14    14

use meshgrid to create a surface plot of a function.

    [X,Y] = meshgrid(-2:.2:2, -2:.2:2);                                
    Z = X .* exp(-X.^2 - Y.^2);                                        
    surf(X,Y,Z)


meshgrid to create a surface plot of a function.
meshgrid to create a surface plot of a function.

Friday, November 27, 2020

,

ord()Function in python with it's example

 ord function in python is used to return an integer representing of Unicode character.

Syntax:-

ord(ch)

Parameters For ord function in python

This function take just only one perameter and that known as ch (a Unicode character)

Return value

The ord() function in python returns an integer representing the Unicode character.

Example for ord()Function in python

print(ord('5'))    # 53
print(ord('A'))    # 65
print(ord('$'))    # 36

OUTPUT

ord()Function
ord()Function in python