Posts

What is the scope of a Variable? Explain the difference between a global and local variables with example programs.

 What is the scope of a Variable? Explain the difference between global and local variables with example programs. A scope in any programming is a region of the program where a defined variable can have its existence and beyond that variable, it cannot be accessed. There are three places where variables can be declared in C programming language − Inside a function or a block which is called  local  variables. Outside of all functions which are called  global  variables. In the definition of function parameters which are called  formal  parameters. Let us understand what are  local  and  global  variables, and  formal  parameters. Local Variables Variables that are declared inside a function or block are called local variables. They can be used only by statements that are inside that function or block of code. Local variables are not known to functions outside their own. The following example shows how local variables are u...

What is an array ? Write a C program to add two matrices of 3 x 3 using arrays.

Image
 What is an Array? Write a C program to add two matrices of 3 x 3 using arrays. Introduction to Arrays An array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together. This makes it easier to calculate the position of each element by simply adding an offset to a base value, i.e., the memory location of the first element of the array (generally denoted by the name of the array). For simplicity, we can think of an array a fleet of stairs where on each step is placed a value (let’s say one of your friends). Here, you can identify the location of any of your friends by simply knowing the count of the step they are on. Remember: “Location of next index depends on the data type we use”. The above image can be looked at as a top-level view of a staircase where you are at the base of the staircase. Each element can be uniquely identified by their index in the array (in a similar way as you could identify your friends by...

Explain the use of malloc function in C programming.

 Explain the use of malloc function in C programming? C malloc() method “malloc”  or  “memory allocation”  method in C is used to dynamically allocate a single large block of memory with the specified size. It returns a pointer of type void which can be cast into a pointer of any form. It initializes each block with a default garbage value. Syntax: ptr = (cast-type*) malloc(byte-size) For Example: ptr = (int*) malloc(100 * sizeof(int)); Since the size of int is 4 bytes, this statement will allocate 400 bytes of memory. And, the pointer ptr holds the address of the first byte in the allocated memory. Description The C library function void *malloc(size_t size) allocates the requested memory and returns a pointer to it. Declaration Following is the declaration for malloc() function. void * malloc ( size_t size ) Return Value This function returns a pointer to the allocated memory, or NULL if the request fails If space is insufficient, allocation fails and r...