---
description: This realloc() function tutorial cover topics like definition, syntax, How to use realloc with program examples
title: realloc() Function in C Library: How to use? Syntax &#038; Example
---

 

[Skip to content](#main) 

## What is realloc()?

**realloc()** is a function of C library for adding more memory size to already allocated memory blocks. The purpose of realloc in C is to expand current memory blocks while leaving the original content as it is. realloc() function helps to reduce the size of previously allocated memory by malloc or calloc functions. realloc stands for reallocation of memory.

### Syntax for realloc in C

ptr = realloc (ptr,newsize);

The above statement allocates a new memory space with a specified size in the variable newsize. After executing the [function](https://www.guru99.com/c-functions.html), the pointer will be returned to the first byte of the memory block. The new size can be larger or smaller than the previous memory. We cannot be sure that if the newly allocated block will point to the same location as that of the previous memory block. The realloc function in C will copy all the previous data in the new region. It makes sure that data will remain safe.

**For example:**

#include <stdio.h>
int main () {
   char *ptr;
   ptr = (char *) malloc(10);
   strcpy(ptr, "Programming");
   printf(" %s,  Address = %u\n", ptr, ptr);

   ptr = (char *) realloc(ptr, 20); //ptr is reallocated with new size
   strcat(ptr, " In 'C'");
   printf(" %s,  Address = %u\n", ptr, ptr);
   free(ptr);
   return 0;
}

## How to use realloc()

The below program in C demonstrates how to use realloc in C to reallocate the memory.

#include <stdio.h>
#include <stdlib.h>
    int main() {
        int i, * ptr, sum = 0;
        ptr = malloc(100);
        if (ptr == NULL) {
            printf("Error! memory not allocated.");
            exit(0);
        }
        
        ptr = realloc(ptr,500);
    if(ptr != NULL)
           printf("Memory created successfully\n");
           
    return 0;

    }

### Result of the realloc in C example:

Memory created successfully

Whenever the realloc results in an unsuccessful operation, it returns a [null pointer](https://www.guru99.com/c-pointers.html), and the previous data is also freed.

#### Summarize this post with:

ChatGPT Perplexity Grok Google AI 

**Stay Updated on AI** **Get Weekly AI Skills, Trends, Actionable Advice.** 

##### Sign up for the newsletter

Subscribe for Free 

You have successfully subscribed.  
Please check your inbox. 

![AI-Newsletter]() Chosen by over **350,000+** professionals 

[Scroll to top ](#wrapper)Scroll to top 

× 

Toggle Menu Close 

Search for: 

Search

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://www.guru99.com/#organization","name":"Guru99","sameAs":["https://www.facebook.com/Guru99Official","https://twitter.com/guru99com"],"logo":{"@type":"ImageObject","@id":"https://www.guru99.com/#logo","url":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","contentUrl":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","caption":"Guru99","inLanguage":"en-US"}},{"@type":"WebSite","@id":"https://www.guru99.com/#website","url":"https://www.guru99.com","name":"Guru99","publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US"},{"@type":"BreadcrumbList","@id":"https://www.guru99.com/realloc-in-c-example.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":"1","item":{"@id":"https://www.guru99.com","name":"Home"}},{"@type":"ListItem","position":"2","item":{"@id":"https://www.guru99.com/c-programming","name":"C Programming"}},{"@type":"ListItem","position":"3","item":{"@id":"https://www.guru99.com/realloc-in-c-example.html","name":"realloc() Function in C Library: How to use? Syntax &#038; Example"}}]},{"@type":"WebPage","@id":"https://www.guru99.com/realloc-in-c-example.html#webpage","url":"https://www.guru99.com/realloc-in-c-example.html","name":"realloc() Function in C Library: How to use? Syntax &#038; Example","dateModified":"2024-08-08T19:29:06+05:30","isPartOf":{"@id":"https://www.guru99.com/#website"},"inLanguage":"en-US","breadcrumb":{"@id":"https://www.guru99.com/realloc-in-c-example.html#breadcrumb"}},{"@type":"Person","@id":"https://www.guru99.com/author/benjamin","name":"Benjamin Walker","description":"I'm Benjamin Walker, an expert in C, C++, and C# programming, providing resources to enhance your coding proficiency and project outcomes.","url":"https://www.guru99.com/author/benjamin","image":{"@type":"ImageObject","@id":"https://www.guru99.com/images/benjamin-walker-author.png","url":"https://www.guru99.com/images/benjamin-walker-author.png","caption":"Benjamin Walker","inLanguage":"en-US"},"worksFor":{"@id":"https://www.guru99.com/#organization"}},{"@type":"NewsArticle","headline":"realloc() Function in C Library: How to use? Syntax &#038; Example","keywords":"bigdata, programming, database, server","dateModified":"2024-08-08T19:29:06+05:30","articleSection":"C Programming","author":{"@id":"https://www.guru99.com/author/benjamin","name":"Benjamin Walker"},"publisher":{"@id":"https://www.guru99.com/#organization"},"description":"This realloc() function tutorial cover topics like definition, syntax, How to use realloc with program examples","copyrightYear":"2024","copyrightHolder":{"@id":"https://www.guru99.com/#organization"},"name":"realloc() Function in C Library: How to use? Syntax &#038; Example","@id":"https://www.guru99.com/realloc-in-c-example.html#richSnippet","isPartOf":{"@id":"https://www.guru99.com/realloc-in-c-example.html#webpage"},"inLanguage":"en-US","mainEntityOfPage":{"@id":"https://www.guru99.com/realloc-in-c-example.html#webpage"}}]}
```
