Found 1401 Articles for C

malloc() vs new() in C/C++

karthikeya Boyini
Updated on 26-Jun-2020 07:45:24

5K+ Views

malloc()The function malloc() is used to allocate the requested size of bytes and it returns a pointer to the first byte of allocated memory. It returns null pointer, if fails.Here is the syntax of malloc() in C++ language, pointer_name = (cast-type*) malloc(size);Here, pointer_name − Any name given to the pointer.cast-type − The datatype in which you want to cast the allocated memory by malloc().size − Size of allocated memory in bytes.Here is an example of malloc() in C language, Example Live Demo#include #include int main() {    int n = 4, i, *p, s = 0;    p = ... Read More

fgets() and gets() in C

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

1K+ 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

What should main() return in C/C++?

karthikeya Boyini
Updated on 26-Jun-2020 07:46:45

5K+ Views

The return value of main() function shows how the program exited. The normal exit of program is represented by zero return value. If the code has errors, fault etc., it will be terminated by non-zero value.In C++ language, the main() function can be left without return value. By default, it will return zero.Here is the syntax of main() function in C language, int main() {    ….    return 0; }Here is an example of main() function in C language, Example Live Demo#include int main() {    int a = 10;    char b = 'S';    float c = ... Read More

size_t data type in C

Samual Sam
Updated on 26-Jun-2020 07:47:14

8K+ Views

The datatype size_t is unsigned integral type. It represents the size of any object in bytes and returned by sizeof operator. It is used for array indexing and counting. It can never be negative. The return type of strcspn, strlen functions is size_t.Here is the syntax of size_t in C language, const size_t var_name;Here, var_name − This the name of variable.Here is an example of size_t in C language, Example Live Demo#include #include #include int main(void) {    const size_t x = 150;    int a[x];    for (size_t i = 0;i < x; ++i)    a[i] = ... Read More

modf() in C/C++

Samual Sam
Updated on 26-Jun-2020 07:35:27

298 Views

The function modf() is used to split the passed argument in integer and fraction. It is declared in “math.h” header file for the mathematical calculations. It returns the fractional value of passed argument.Here is the syntax of modf() in C language, double modf(double value, double *integral_pointer);Here, value − The value which splits into integer and fraction.integral_pointer − It points the integer part of argument after splitting.Here is an example of modf() in C language, Example Live Demo#include #include int main () {    double val, x, res;    val = 28.856;    res = modf(val, &x);    printf("Integral part of val ... Read More

isgreater() in C/C++

karthikeya Boyini
Updated on 26-Jun-2020 07:35:55

209 Views

The function isgreater() is used to check that the first argument is greater than the second one or not. It is declared in “math.h” header file in C language. It returns true, if successful, otherwise false.Here is the syntax of isgreater().bool isgreater(value1 , value2);Here, value1 − This is the first argument which will be checked with value2.value2 − This is the second argument which is used to check value1 that is greater or not.Here is an example of isgreater().Example Live Demo#include #include using namespace std; int main() {    int val1 = 28;    int val2 = 8;    bool result; ... Read More

rand() and srand() in C

Samual Sam
Updated on 24-Jun-2020 11:26:35

12K+ Views

rand()The function rand() is used to generate the pseudo random number. It returns an integer value and its range is from 0 to rand_max i.e 32767.Here is the syntax of rand() in C language, int rand(void);Here is an example of rand() in C language, Example Live Demo#include #include int main() {    printf("%d", rand());    printf("%d", rand());    return 0; }Output1804289383 846930886srand()The function srand() is used to initialize the generated pseudo random number by rand() function. It does not return anything.Here is the syntax of srand() in C language, void srand(unsigned int number);Here is an example of srand() in C ... Read More

strdup() and strdndup() in C/C++

karthikeya Boyini
Updated on 24-Jun-2020 11:27:03

5K+ Views

strdup()The function strdup() is used to duplicate a string. It returns a pointer to null-terminated byte string.Here is the syntax of strdup() in C language, char *strdup(const char *string);Here is an example of strdup() in C language, Example Live Demo#include #include int main() {    char *str = "Helloworld";    char *result;    result = strdup(str);    printf("The string : %s", result);    return 0; }OutputThe string : Helloworldstrndup()The function strndup works similar to the function strndup(). This function duplicates the string at most size bytes i.e. the given size in the function. It also returns a pointer to null-terminated ... Read More

Use of fflush(stdin) in C

Samual Sam
Updated on 24-Jun-2020 11:27:30

6K+ Views

The function fflush(stdin) is used to flush the output buffer of the stream. It returns zero, if successful otherwise, returns EOF and feof error indicator is set.Here is the syntax of fflush(stdin) in C language,int fflush(FILE *stream);Here is an example of fflush(stdin) in C language,Example Live Demo#include #include int main() {    char s[20] = "Helloworld";    printf("The string : %s", s);    fflush(stdin);    return 0; }OutputThe string : Helloworld

ispunct() in C

karthikeya Boyini
Updated on 24-Jun-2020 11:28:00

86 Views

The function ispunct() is used to check that the passing character is a punctuation or not. It returns zero, if it is not a punctuation, otherwise it returns a non-zero value.Here is the syntax of ispunct() in C language, int ispunct(int character);Here is an example of ispunct() in C language, Example Live Demo#include #include int main() {    int a = '!';    int b = 'a';    if(ispunct(a))    printf("The character is a punctuation.");    else    printf("The character is not a punctuation.");    if(ispunct(b))    printf("The character is a punctuation.");    else    printf("The character is not a ... Read More

Advertisements