Skip to main content

Posts

C99 and C11 standards

as part of my job  sometimes I get chance to interview C programmers, till today, number of  C programmers I have interviewed none knows about new(C11 & C99) features of C programming language, some even not heard of these standards that made me to add  this page, I strongly believe if we are working on product which is born out of C programming, one needs to know about C99 and C11 features as well as Safe-C libraries which improves software quality and security C99 standard has become absolute by now, considering C11 standard which is part of our products which are written in C programming language.  if we name few missing elements in C89 programming features 1) No array bounds checking 2) No variable length arrays 3) No inline functions 4) No support for Boolean data type 5) No complex number type support 6) No single line comment (//) 7) No variable number of arguments in MACRO 8) No support for declaring variables in blocks . and more .. all above elements are
Recent posts

Static Link or Dynamic linked?

how can you tell whether a program is statically linked? And if it is dynamically linked, how do you know what libraries it needs? The ldd command can answer both questions shiv@ubuntu:~$ ldd /sbin/ldconfig         not a dynamic executable  (ldconfig is not dynmic loadable) shiv@ubuntu:~$ ldd /bin/ln   (ln is dynamic loadable but it needs below listed things to run)         linux-gate.so.1 =>  (0x00918000)         libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0x00110000)         /lib/ld-linux.so.2 (0x0033f000)  ldconfig ------------ you use the ldconfig command without parameters to rebuild ld.so.cache ldconfig  -p | less to display ld.so.cache how does the dynamic loader know where to look for executables? >>As with many things on Linux, there is a configuration file in /etc. shiv@ubuntu:~$ cat /etc/ld.so.conf include /etc/ld.so.conf.d/*.conf shiv@ubuntu:~$ ls /etc/ld.so.conf.d/*.conf | more /etc/ld.so.conf.d/GL.conf /etc/ld.so.conf.d

few program needs to link required library in gcc

lets consider simple program below this program will compile but linker fails to find definition of cos and sin functions #include<stdio.h> #include<math.h> #include <stdlib.h> #define PI 3.142 int main() {  double sinval, cosval;  int x=2;  sinval = sin(PI/x);  cosval = cos(PI/x);  printf("sin and cos of PI/x  is %lf %lf\n", sinval, cosval);  return 0; } followings link errors we will hit, since linker can't able to resolve the definition of cos() and sin() shiv@ubuntu:~$ gcc mat1.c /tmp/ccHOvWsZ.o: In function `main': mat1.c:(.text+0x2a): undefined reference to `sin' mat1.c:(.text+0x4b): undefined reference to `cos' collect2: ld returned 1 exit status so to fix the above issue we need to link the required library, here cos() and sin() are present in the math library so we need to link math library with the program as follows gcc  mat1.c -lm -lm will link the math library with above program so program build properly, so as li

C Program to Simulate Linux Cat command

/*  * C program to Simulate Implementation of  Linux *cat* command  * This program will not handle redirection  * you can try to improve this program to handle redirection of file contents  */ #include<stdio.h> #include<string.h> #define MAX_FILE_NAME_CHARS 255 int main(int argc, char *argv[]) {  FILE *fp;  char file_name[MAX_FILE_NAME_CHARS], ch;  int i;  /*   * after creating a.out, rename it as mycat for our own cat command   * and it usage is same as standard cat command   */  if(argc<1){     printf("Usage mycat <filename> \n");     return 0;  }  /*   * cat handles more than one file   * so we need this loop to handle all the files provided   * on command line   */  for(i=1; i<=argc;i++){     /* no need of copy but for understanding purpose, i have created      * string for each file name      */     strncpy(file_name, argv[i], MAX_FILE_NAME_CHARS);     fp=fopen(file_name, "r");     if(fp == NULL) {   

Simple program to illustrate variadic macros In C

/*  * C99 has  support of variable arguments in macros statement  * (called variadic macro) just like variable argument function, Macros also   * can be of variable number of augments  * This program illustrate variadic macros  */ #include <stdio.h>   /*   * Simple Macro definition   * which allows to print whatever the way you want   *  in Macro definition "..." indicates variadic macro, OP() definition is    * mixture of named args and variable number of aguments, it is possible   * to have macro of complete variable number of args   * like DISPLAY in below example   */ #define OP(format, exp, ...)  printf(format, exp, ##__VA_ARGS__) #define DISPLAY(...)    printf(__VA_ARGS__) int main() {  int a=3, b=4, c=5;  OP("%s %d\n", "Result after add", a+b);  OP("%s %d %s %d\n", "Result after add", a+b+c, "Result after sub", a-b);  OP("%s\n", "This is test program");  OP("%s %f\n", "

C Program to find Armstrong numbers between range m and n

/*  * Program to print Armstrong number between range m and n  * Armstrong  number is nothing but sum of cubes of digits of number is  * equal to the number  * ex: 153 is Armstrong number since 1^3+5^3+3^3=153  * there are very few numbers hold above property; execute   * below program to find all numbers  * b/w m and n  */ #include <stdio.h> void is_armstrong(int num) {   int sum=0, n, d;   n=num;   while(n){   d=n%10;  /* extract digits of number*/   sum=sum+(d*d*d);  /*take sum of them */   n=n/10;  }  if(sum == num)  /*sum is same as number then it is Armstrong number*/     printf("%d\n", sum); } int main() { int i,n,m; printf("Enter range \n"); scanf("%d%d", &m, &n);  for(i=m;i<=n;i++){    is_armstrong(i);  } return 0; }