Showing posts with label C Programming. Show all posts
Showing posts with label C Programming. Show all posts

What is Malloc() function in C Programming Language?



What is Malloc() function in C Programming Language?

A malloc() function is used for  dynamic memory allocation. It is defined in stdlib.h or malloc.h depending on the operating system. Memory allocation by malloc() is persistent.  
  • void  *malloc(size_t size);


Example:

  • #include<stdio.h>
  • #include<stdlib.h>
  • void main()
  • {
  •           int array[10];
  •           int *ptr = (int *) malloc(10 * sizeof (int));
  • if (ptr == NULL)
  • {
  • }
  •           else
  •          {
  •                   free(ptr);
  •                   ptr=NULL;
  •           }
  • }

What is Calloc() function in C Programming Language?


What is Calloc() function in C Programming Language?


A calloc() function allocates space for an array of elements of a certain size and initializes the memory to zero. 

  • void  *calloc(size_t num, size_t size);
Here num is number of objects to allocate and size is the size of each objects. If the allocation is successful then function returns a Pointer to the first byte and if allocation fails then the function returns NULL. 

Examples:

  • #include<stdio.h>
  • #include<stdlib.h>
  • void main()
  • {
  •           unsigned a;
  •           int *ptr;
  •           printf(“Enter the number to allocate:-”);
  •           scanf("%d", &a);
  •           ptr= (int*) calloc(a, sizeof(int));
  •           if(ptr!=NULL)
  •           {
  •                      puts("Memory allocation successful");
  •            }
  •           else
  •           {
  •                     puts("Memory allocation failed");
  •           }
  •           return(0);
  • }


Output:-
  • Enter the number to allocate:- 200
  • Memory allocation successful
  • Enter the number to allocate:- 111111111111
  • Memory allocation failed

Can we declare static variable Global in C Programming Language?



Can we declare static variable as Global in C Programming Language?

We can declare a static variable as Global and Local. There is some difference between declaring a variable as Global and Local. If we declare a variable as local then it can accessed only with in the function where it is declared in a program.
While if we declare a variable as Global then it can be accessed by all functions with in the program. 

Can static variables be declared in Header file in C Programming Language?


Can static variables be declared in Header file in C Programming Language?


We can not declare a static variable without defining it. We can declare static variables in header file when their definition should also be provided in the same header file. It is generally an idea to use static variables in the header file but it creates some problem that it makes the static variables as a private copy of header file i.e it can not be used elsewhere. 



What are the Storage Classes in C Programming Language?



What are the Storage Classes in C Programming Language?

A storage class tells us about the scope and lifetime of a variable or function with in a program. There are following Storage Classes in C:-

  • Automatic Storage Class
  • Static Storage Class
  • Register Storage Class
  • External Storage Class


Automatic Storage Class:-


The keyword for Automatic Storage Class is "auto". It is the default Storage Class for all local variables. If we declared a variable as auto then it is stored in the memory. The default value of this variable is garbage. The scope of this variable is local to block in which it is defined and it's lifetime will be remained till it's control will be with in the block. 


Example:-



  • #include<stdio.h>
  • void main()
  • {
  •           auto int var;
  •           printf(“%d”,var)
  • }



Output:-
  • 1045(Garbage Value)



Static Storage Class:-

The keyword for Static Storage Class is "static". It is the default Storage Class for global variables. If we declared a variable as static then it is stored in the memory. The default value of this variable is zero. The scope of this variable is local to block in which it is defined and it's lifetime will be remained till it's control will be with in the block. 

Example:-

  • #include<stdio.h>
  • static int var1;
  • int var2;
  • {
  •           printf(“%d”,var2)
  • }


Register Storage Class:-

The keyword for Register Storage Class is "register". It is used to define local variables and these local variables should be stored in register instead of RAM . If we declared a variable as register then it is stored in the CPU register. The default value of this variable is garbage. The scope of this variable is local to block in which it is defined and it's lifetime will be remained till it's control will be with in the block. 

Example:-

  • #include<stdio.h>
  • void main()
  • {
  •           register int var;
  •           printf(“%d”,var)
  • }

Output:-
  • 1045(Garbage Value)

External Storage Class:-

The keyword for External Storage Class is "extern". It is used to give reference to global variables that is visible to whole program files. If we declared a variable as extern then it is stored in the memory. The default value of this variable is zero. The scope of this variable is global(or local when it is defined with in block) and it's lifetime will be remained till as long as the program's execution does not finish.

Example:-

  • #include<stdio.h>
  • int var1;
  • void main()
  • {
  •           extern int var2;
  •           printf(“%d %d”,var1,var2)
  • }
  • int var2=20;

Output:-
  • 0,20


What is the meaning of Static variable in C Programming Language?


What is the meaning of Static variable in C Programming Language?

A static variable is a variable that has been allocated statically. A local static variable exists for the period of program but it is only visible in the function body. If the function is declared as static the it is invisible to outer files. If we use static global variable in the file then it is limited to with in the file. 

A program in C:-

  • #include <stdio.h>
  • int a = 10;  
  • main()
  • {
  •     int i =0
  •     void fun();
  •     fun();            
  •     printf("After First Call");
  •     fun();            
  •     printf("After Second Call");
  •     fun();            
  •     printf("After Third Call");
  • }
  • void fun()
  • {
  •     static int p=0;  
  •     int q = 10;             
  •     printf("value of p %d q %d",p,q);
  •     p=p+10;
  • }

Out Put of Above Program:-

After First Call:-      p= 0, q=10
After Second Call:- p=10, q=10
After Third Call:-     p=20, q=10


What are the disadvantages of C Language?


What are the disadvantages of C Language?


  • C has not constructor and destructor.
  • C has not the concept of Namespace.
  • C has not strict type checking.
  • C has not runtime checking.
  • C has not concept of Object Oriented Programming. These OOPs features are very important for regarding programming concept.
  • If the programs has large length then it is difficult to fix the bugs.
  • It is procedure oriented programming language, it is major problem.  

What are the advantages of C Language?



What are the advantages of C Language?

  • C is the basic language. It is the basis for development of other programming languages like C++, Java etc.
  • C programs are very fast and efficient then it is the advantage to save programmer's time.
  • C is simple and compact language.
  • C is flexible language so it provides easiness to programmer.
  • C is a portable language. It provides a high portability to take our software to one Operating System to other.
  • C has a lot libraries and these libraries provide easiness to programmers.
  • C is building block for other programming language.