C Programming

Hiiiiii Friends,

I created this blog to share my knowledge. Whenever i find something Interesting, i post it so that everyone may know about this.
If you like my posts then tell your friends and spread the knowledge.

Wednesday, July 18, 2012

Shared Objects

Static & Dynamic Linking

Static Libraries:  libtest.a

Name can be anything but should start with ‘lib’
Steps:
- Create source files
- Compile with –c option
  - gcc –Wall –c test1.c test2.c
- Create library
  - ar –cvq libtest.a test1.o test2.o
- List files in library
  - ar –t libtest.a
- Linking the library
  - gcc –o main main.c libtest.a

Dynamically linked “Shared Object” libraries: (.so)

This is a two step process
- Create object code
- Create library
- Optional: create default version using a symbolic link
- Commands:
- Compile all files
   - gcc –Wall –fPIC –c *.c
- create a .so file with .o files
   - gcc –shared –o libtest.so test1.o test2.o
Using standard .so path: /usr/lib/ is a place where .so files are kept and it is predefined to the OS, so it is searched by default.
- Create a soft link in /usr/lib
  - ln –s /path/to/.so file /usr/lib/link name
- Compile the main file with the soft link
  - gcc –o main main.c –lxxx

To list the dependency

- ldd binary_name

Remark: so name is started with lib and end with .so like libxxx.so
Link is created with the same name in other location and is start with –l ex: -lxxx for above .so file.
Prefix and suffix of xxx are removed by system and while link xxx is used.
PIC stands for “Position Independent Code”
Using user defined path: -L option
use –L option to tell the path where .so is exists
gcc –L /path/to/.so file –o main main.c –lxxx

Note: at compile time gcc search for the included so file, if it exists at predefined path, it automatically links, but if programmer save it at some other place, its path must be specified with –L option at compile time.
For running the binary: there are following ways
either .so is kept at predefined path
or create a soft link of desired .so at predefined path
or set environmental variable LD_LIBRARY_PATH
or define path in ld.so.conf

Tuesday, July 17, 2012

Attachment using sendmail in Linux

sendmail is one of the famous tool to send emails in Linux environment, but it does not have no direct option to send attachments.
To send attachment in email you can use MIME, its header allows us to send attachments. So first you need to generate a text file that contains email information as defined in MIME standard and later use this file as an input to sendmail, you will get your problem solved.

Saturday, May 28, 2011

Which is the largest Data Type in C? And what is its Size?

long double is the largest data type in C.
Size of long double is:
10 byte on 16 bit compiler (Turbo C++ IDE)
12 byte on 32 bit compiler (GCC)
16 byte on 64 bit compiler (Not tested yet)

Thursday, March 31, 2011

Program to convert Currancy to Indian Style ?

#include<stdio.h>
#include<conio.h>
char one[20][10]=  {"","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Ninteen"};
char tens[10][10]={"","","Twenty","Thirty","Fourty","Fifty","Sixty","Seventy","Eighty","Ninty"};
void display(int n)
{
int r;
if(n<20)
printf(" %s ",one[n]);
else
{
   r=n%10;
   n=n/10;
   printf(" %s %s ",tens[n],one[r]);
}
}
void main()
{
long n,m;
int a[5];
int c=0;
clrscr();
printf("Enter number : ");
scanf("%ld",&n);
m=n;
while (n>0)
{
 if (c==1)
 {
  a[c]=n%10;
  n=n/10;
 }
 else
 {
  a[c]=n%100;
  n/=100;
 }
 c++;
}
c--;
if (m>=10000000L)
{
 display (a[c]);printf("Crore");c--;
 if (a[c]>0)
 {
  display (a[c]);printf("Lack");
 }c--;
 if (a[c]>0)
 {
  display(a[c]);printf("Thousand");
 }c--;
 if (a[c]>0)
 {
  display(a[c]);printf("Hundred");
 }c--;
 if (a[c]>0)
  display(a[c]);
}
else if(m>=100000L)
{
 display (a[c]);printf("Lack");c--;
 if (a[c]>0)
 {
  display(a[c]);printf("Thousand");
 }c--;
 if (a[c]>0)
 {
  display(a[c]);printf("Hundred");
 }c--;
 if (a[c]>0)
  display(a[c]);
}
else if(m>=1000)
{
 display(a[c]);printf("Thousand");c--;
 if (a[c]>0)
 {
  display(a[c]);printf("Hundred");
 }c--;
 if (a[c]>0)
  display(a[c]);
}
else if(m>=100)
{
 display (a[c]);printf("Hundred");c--;
 if (a[c]>0)
  display(a[c]);
}
else
 display(a[c]);
getch();
}