Found 1401 Articles for C

Write your own memcpy() in C

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

3K+ Views

Here we will see how to implement memcpy() function in C. The memcpy() function is used to copy a block of data from one location to another. The syntax of the memcpy() is like below −void * memcpy(void * dest, const void * srd, size_t num);To make our own memcpy, we have to typecast the given address to char*, then copy data from source to destination byte by byte. Just go through the following code to get better idea.Example#include #include void custom_memcpy(void *dest, void *src, size_t n) {    int i;    //cast src and dest to char*    char ... Read More

How to write a running C code without main()?

Samual Sam
Updated on 30-Jul-2019 22:30:26

7K+ Views

Here we will see, one program can be written without main or not? The answer is yes. We can write program, that has no main() function.In many places, we have seen that the main() is the entry point of a program execution. Just from the programmers perspective this is true. From the system’s perspective it is not true. So the system at first calls the _start(), this sets up the environment, then main is called.To execute this program we have to use this option ‘-nostartfiles’.Example#include extern void _exit(register int); int _start() {    printf("Program without main");       ... Read More

Print a long int in C using putchar() only

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

2K+ Views

Here we will see how to print long int value using the putchar() function in C. We can easily print the value of some variables using printf() in C, but here the restriction is, we cannot use any other function except putchar().As we know that the putchar() is used to print only characters. We can use this function to print each digit of the number. When one numeric value is passed, we have to add character ‘0’ with it to get the ASCII form. Let us see the code to get the better idea.Example#include void print_long(long value) {   ... Read More

How to count set bits in a floating point number in C?

Samual Sam
Updated on 30-Jul-2019 22:30:26

408 Views

In this problem, one floating point value is given. We have to find number of set bits in the binary representation of it.For example, if a floating point number is 0.15625, so there are six set bits. A typical C compiler used single precision floating point representation. So it will be look like this.To convert into its bit values, we have to take the number into one pointer variable, then typecast the pointer to char* type data. Then process each byte one by one. Then we can count set bits of each char.Example#include int char_set_bit_count(char number) {    unsigned ... Read More

How will implement Your Own sizeof in C

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

2K+ Views

To use the sizeof(), we can take the value using a variable x, using &x, it will print the address of it. Now if we increase the value of &x then it may increase in different way. If only one byte is increased, that means it is character, if the increased value is 4, then it is int or float and so on. So by taking the difference between &x + 1 and &x, we can get the size of x.Here we will use macro as the datatype is not defined in the function. And one more thing, we are ... Read More

How will you show memory representation of C variables?

Samual Sam
Updated on 30-Jul-2019 22:30:26

454 Views

Here we will see how to print the memory representation of C variables. Here we will show integers, floats, and pointers.To solve this problem, we have to follow these steps −Get the address and the size of the variableTypecast the address to the character pointer to get byte addressNow loop for the size of the variable and print the value of typecasted pointer.Example#include typedef unsigned char *byte_pointer; //create byte pointer using char* void disp_bytes(byte_pointer ptr, int len) {     //this will take byte pointer, and print memory content    int i;    for (i = 0; i < ... Read More

What is Memory Leak in C/C++?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

3K+ Views

The memory leak occurs, when a piece of memory which was previously allocated by the programmer. Then it is not deallocated properly by programmer. That memory is no longer in use by the program. So that place is reserved for no reason. That’s why this is called the memory leak.For the memory leak, some block of memory may have wasted. If the system has enough memory, in that case also this may slow down the performance.Examplevoid my_func() {    int *data = new int;    *data = 50; }Here the problem is *data pointer is never deleted, so memory is ... Read More

Parameter Passing Techniques in C/C++

Samual Sam
Updated on 30-Jul-2019 22:30:26

6K+ Views

In C we can pass parameters in two different ways. These are call by value, and call by address, In C++, we can get another technique. This is called Call by reference. Let us see the effect of these, and how they work.First we will see call by value. In this technique, the parameters are copied to the function arguments. So if some modifications are done, that will update the copied value, not the actual value.Example#include using namespace std; void my_swap(int x, int y) {    int temp;    temp = x;    x = y;    y = ... Read More

Nested functions in C

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

4K+ Views

In some applications, we have seen that some functions are declared inside another function. This is sometimes known as nested function, but actually this is not the nested function. This is called the lexical scoping. Lexical scoping is not valid in C because the compiler is unable to reach correct memory location of inner function.Nested function definitions cannot access local variables of surrounding blocks. They can access only global variables. In C there are two nested scopes the local and the global. So nested function has some limited use. If we want to create nested function like below, it will ... Read More

Implicit return type int in C

Samual Sam
Updated on 30-Jul-2019 22:30:26

378 Views

If some function has no return type, then the return type will be int implicitly. If return type is not present, then it will not generate any error. However, C99 version does not allow return type to be omitted even if it is int.Example#include my_function(int x) {    return x * 2; } main(void) {    printf("Value is: %d", my_function(10)); }OutputValue is: 20

Advertisements