Using Decision Making Statements, if and if-else in C program

When you are writing C program, sometimes you need to make certain checks on variables and based on which you need to take some decisions.. this is called as Decision making statements in C programming Language. In this post, we will show you how you can use simple conditional statements “if” and “if-else” in your … Read more

Associativity of Operators in C Programming

When some expression in C contains two operators with same/equal priority, the expression gets executed as per the “Associativity of Operators”, Check below program to understand, why it prints the results as “5” and not “1” $ vim associtivity_in_c.c [c] #include <stdio.h> int main(){ int a = 3/2*5; printf("a=%d\n", a); /*Above Statement will print result … Read more

Understanding gcc compilation steps : Linux compilation steps

Copy paste the below shell script and execute on your terminal. $ vim Copy below highlighted code and run the script as, $ bash ————- code start ——————— #!/bin/bash echo “ For any given input file, the file name suffix determines what kind of compilation is done: file.c C source code that must … Read more

Detect memory leaks using Valgrind in Linux

Valgrind is an instrumentation framework for building dynamic analysis tools. There are Valgrind tools that can automatically detect many memory management and threading bugs, and profile your programs in detail. You can also use Valgrind to build new tools. We will need to install valgrind for ubuntu, $ sudo apt-get install valgrind Reading package lists… … Read more

Using electric fence for debugging memory leaks in Linux

Create a test program which has some memory leaks for understanding how to debug memory leaks with electric fence, $ vim efence.c [c] #include <stdio.h> #include <stdlib.h> int main() { char *ptr = (char *) malloc(1024); ptr[0] = 0; /* Now write beyond the block */ ptr[1024] = 0; exit(0); } [/c] Now, we will … Read more

c program for creating a text file from string in buffer

Create a str_to_file.c file as contents below, OR use from github [c] #include <stdio.h> #include <stdlib.h> #include <string.h> int main (int argc, char **argv) { char *str = (char *) malloc(4096); FILE *fp; int ret; size_t size; strcpy(str, "{\"myname\":"); strcat(str, "\""); strcat(str, "devbee"); strcat(str, "\"}"); fp = fopen( "myfile.txt" , "w" ); size = fwrite(str … Read more

How to implement stack in C programming ?

In continuation of demonstrating simple implementation of data structures, as we shown in our last post “How to implement linked list in C programming ?”, in this post we will show how you can implement simple stack in C programming language. Stack is nothing but a one this kept on another, where we define how … Read more

How to implement linked list in C programming ?

This programs shows a simple way to implement singly link list ( Singly link list is the list which can be traversed only in one direction i.e. forward till end ) $ vim simple_linked_list.c [java] #include <stdio.h> #include <stdlib.h> struct node { int value; struct node *next; }; void init_list(struct node *n, int value) { … Read more