C Snippets, Examples, Code

C Snippets [29]

1. How to get the length of an array in C?

2. Difference between const int*, const int * const, and int const * in C?

3. Pre Increment Operators.

4. Boolean values in C language.

5. What is size_t in C?.

6. What is a static variable in C?

7. How do you print a boolean in C?

8. Generate random integers in C.

9. How to list all files, sub-directories in a directory using C?

10. What is the efficient way to define the main() function in C?

11. What is the present C language standard?

12. Find the Size of int, float, double, char, pointers and structure?

13. What is the difference between float and double in C?

14. Printing leading 0's in C.

15. Implementing a custom printf() function in C.

16. Example of C custom strcmp() function.

17. Difference between char * const and const char *?

18. What is the difference between char a[] and char *a?

19. How to convert a character to an integer, float in C ?

20. Implementing a custom memcpy() function.

21. Implementing a custom atoi() function.

22. C program to define Macro to find maximum/minimum of two numbers.

23. C program to find the size of a structure and why does the structure's area differ from each member's?

24. What is the difference between char array and char pointer in C language with example?

25. How do function pointers work in C?

26. How to pass a function as a parameter in C?

27. Difference between typedef struct and struct definitions with example?

28. Implementing a custom strlen() function.

29. Implementing a custom abs() function.

More to Come !

Follow us on Facebook and Twitter for latest update.

C Programming: Tips of the Day

Returning an array using C

You can't return arrays from functions in C. You also can't (shouldn't) do this:

char *returnArray(char array []){
 char returned [10];
 //methods to pull values from array, interpret them, and then create new array
 return &(returned[0]); //is this correct?

returned is created with automatic storage duration and references to it will become invalid once it leaves its declaring scope, i.e., when the function returns.

You will need to dynamically allocate the memory inside of the function or fill a preallocated buffer provided by the caller.

Dynamically allocate the memory inside of the function (caller responsible for deallocating ret)

char *foo(int count) {
    char *ret = malloc(count);
        return NULL;

    for(int i = 0; i < count; ++i) 
        ret[i] = i;

    return ret;

Call it like so:

int main() {
    char *p = foo(10);
    if(p) {
        // do stuff with p

    return 0;

Ref : https://bit.ly/3yFIeao

We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook