Found 33676 Articles for Programming

Use of realloc() in C

Samual Sam
Updated on 26-Jun-2020 07:58:12

12K+ Views

The function realloc is used to resize the memory block which is allocated by malloc or calloc before.Here is the syntax of realloc in C language, void *realloc(void *pointer, size_t size)Here, pointer − The pointer which is pointing the previously allocated memory block by malloc or calloc.size − The new size of memory block.Here is an example of realloc() in C language, Example Live Demo#include #include int main() {    int n = 4, i, *p, s = 0;    p = (int*) calloc(n, sizeof(int));    if(p == NULL) {       printf("Error! memory not allocated.");     ... Read More

EOF, getc() and feof() in C

karthikeya Boyini
Updated on 14-Sep-2023 20:45:57

27K+ Views

EOFEOF stands for End of File. The function getc() returns EOF, on success..Here is an example of EOF in C language, Let’s say we have "new.txt" file with the following content.This is demo! This is demo!Now, let us see the example.Example#include int main() {    FILE *f = fopen("new.txt", "r");    int c = getc(f);    while (c != EOF) {       putchar(c);       c = getc(f);    }    fclose(f);    getchar();    return 0; }OutputThis is demo! This is demo!In the above program, file is opened by using fopen(). When integer variable c ... Read More

fseek() vs rewind() in C

Samual Sam
Updated on 26-Jun-2020 08:00:08

2K+ Views

fseek()fseek() in C language is used to move file pointer to a specific position. Offset and stream are the destination of pointer, given in the function parameters. If successful, it returns zero, else non-zero value is returned.Here is the syntax of fseek() in C language, int fseek(FILE *stream, long int offset, int whence)Here are the parameters used in fseek(), stream − This is the pointer to identify the stream.offset − This is the number of bytes from the position.whence − This is the position from where offset is added.whence is specified by one of the following constants.SEEK_END − End of ... Read More

calloc() versus malloc() in C

karthikeya Boyini
Updated on 26-Jun-2020 08:01:39

2K+ Views

calloc()The function calloc() stands for contiguous location. It works similar to the malloc() but it allocate the multiple blocks of memory each of same size.Here is the syntax of calloc() in C language, void *calloc(size_t number, size_t size);Here, number − The number of elements of array to be allocated.size − Size of allocated memory in bytes.Here is an example of calloc() in C language, Example Live Demo#include #include int main() {    int n = 4, i, *p, s = 0;    p = (int*) calloc(n, sizeof(int));    if(p == NULL) {       printf("Error! memory not allocated."); ... Read More

fgetc() and fputc() in C

Samual Sam
Updated on 26-Jun-2020 08:02:42

5K+ Views

fgetc()The function fgetc() is used to read the character from the file. It returns the character pointed by file pointer, if successful otherwise, returns EOF.Here is the syntax of fgetc() in C language, int fgetc(FILE *stream)Here is an example of fgetc() in C language, Let’s say we have “new.txt” file with the following content −0, hell!o 1, hello! 2, gfdtrhtrhrt 3, demoNow, let us see the example −Example#include #include void main() {    FILE *f;    char s;    clrscr();    f=fopen("new.txt", "r");    while((s=fgetc(f))!=EOF) {       printf("%c", s);    }    fclose(f);    getch(); }Here is the ... Read More

strcspn() in C

karthikeya Boyini
Updated on 26-Jun-2020 07:52:51

351 Views

The function strcspn() counts the number of characters before first match of characters in both strings. This is declared in “string.h” header file. It returns the number of characters of first string before the occurrence of first matched character.Here is the syntax of strcspn() in C language, size_t strcspn(const char *string1, const char *string2)Here, string1 − The first string which is to be scanned.string2 − The second string which is used to search the matching character in first string.Here is an example of strcspn() in C language, Example Live Demo#include #include int main() {    char str1[] = "Helloworld!";    char ... Read More

delete() operator in C++

karthikeya Boyini
Updated on 26-Jun-2020 07:54:39

509 Views

The delete operator is used to deallocate the memory. User has privilege to deallocate the created pointer variable by this delete operator.Here is the syntax of delete operator in C++ language,delete pointer_variable;Here is the syntax to delete the block of allocated memory,delete[ ] pointer_variable;Here is an example of delete operator in C++ language,Example Live Demo#include using namespace std; int main () {    int *ptr1 = NULL;    ptr1 = new int;    float *ptr2 = new float(299.121);    int *ptr3 = new int[28];    *ptr1 = 28;    cout

Isprint() in C++

Samual Sam
Updated on 26-Jun-2020 07:42:01

92 Views

The function isprint() is predefined function and it checks that the passed characters are printable or not. It returns non-zero value, if successful otherwise, zero. This function is declared in “cctype” header file.Here is the syntax of isprint() in C++ language,int isprint(int character);Here,character − The character is to be checked.Here is an example of isprint() in C++ language,Example Live Demo#include #include using namespace std; int main() {    int val1 = 28;    int val2 = 's';    int val3 = '';    if(isprint(val1))    cout

delete() and free() in C++

Tapas Kumar Ghosh
Updated on 03-Jun-2025 11:19:24

1K+ Views

In C++, both delete and free() are used to deallocate the dynamically created memory. In this article, we will learn about the delete operator and free() function with the help of examples. C++ delete Operator The delete operator is used to deallocate the memory. User has privilege to deallocate the created pointer variable by this delete operator. Syntax Here is the syntax of delete operator in C++ language: delete pointer_variable; Here is the syntax to delete the block of allocated memory: delete[ ] pointer_variable; Example Following is the example of delete operator to see its usage in memory ... Read More

fgets() and gets() in C

Samual Sam
Updated on 26-Jun-2020 07:46:12

2K+ Views

fgets()The function fgets() is used to read the string till the new line character. It checks array bound and it is safe too.Here is the syntax of fgets() in C language, char *fgets(char *string, int value, FILE *stream)Here, string − This is a pointer to the array of char.value − The number of characters to be read.stream − This is a pointer to a file object.Here is an example of fgets() in C language, Example Live Demo#include #define FUNC 8 int main() {    char b[FUNC];    fgets(b, FUNC, stdin);    printf("The string is: %s", b);    return 0; }OutputThe ... Read More

Advertisements