Friday, May 27, 2011

sprintf usuage in perl and C

Sprintf:
Perl :
SYNTAX:sprintf(FORMAT,LIST);

sprintf is to produce the formatted string based on the input string.
It is based on sprintf C library routine .

sprintf is useless if  you give the no of digits to be padded less than the no of digits in your variable.As you may see from the below example:

dd="456456456";
$rr=sprintf("%04d",$dd);
print("$rr"."\n");


Output will be:456456456

But if you give the no of digits less than the padding digits.In addition to the digits available the remaining will be padded.
$dd="2";
$rr=sprintf( "%08d",$dd);
printf("$rr"."\n");

Output:00000002
Here 7 zeros are padded with the available digit 2.

The format has many options.In this case we round the digits to 3 decimal point.
$ff="333.45454545";
$rr=sprintf("%.3f",$ff);
printf("$rr"."\n");

Output:333.455

C language:
sprintf has a provision to save the formatted string in the syntax itself.

SYNTAX:sprintf(saved string ,format,string to be formatted);

Here the change is the formatted string is saved in the syntax itself,instead of saving through equality condition.

#include <stdio.h>int main ()
{
  char buffer [50];
  int n, a=5, b=3;
  n=sprintf (buffer, "%d plus %d is %d", a, b, a+b);
  printf ("[%s] is a %d char long string\n",buffer,n);
  return 0;
}

Output:
[5 plus 3 is 8] is a 13 char long string






Monday, May 23, 2011

finding the memory size in Linux

To my knowledge there are three command to find the memory in Linux:

1)free command:

It is used to print the memory size in bytes by default

>> free
total used free shared buffers cached
Mem: 16438664 16294220 144444 0 235596 13380932
-/+ buffers/cache: 2677692 13760972

options:

-b - to print the bytes

-m - to print the information in MegaBytes

-k - to print the information in kilobytes.

>>free -m
total used free shared buffers cached
Mem: 16053 15953 100 0 214 13119
-/+ buffers/cache: 2619 13433
Swap: 9855 1 9854


2)Top command:

It is used to display the ongoing process in the Cpu and the memory used by the same.

>>Top
PID USERNAME THR PRI NICE SIZE RES SHR STATE TIME CPU COMMAND
4536 root 1 0 -20 37M 8696K 2204K sleep 189:47 1.60% mount
23308 karthik 1 15 0 18M 5948K 4392K sleep 0:00 1.60% vim
3754 root 1 16 0 10M 680K 584K sleep 3:03 0.20% vi


3)dmesg | grep ^Memory
This also gives the information about the available memory.

> dmesg | grep ^Memory
Memory for crash kernel (0x0 to 0x0) notwithin permissible range
Memory: 16435064k/17563644k available (2577k kernel code, 339988k reserved, 1305k data, 212k init

awk unix command

awk is a unix command

Awk is used to get a particular pattern in a ouput

you can use this to print a particular pattern

To print the third column in the output using space as the delimiter
awk -F " " '{print $3}'

If you want to print the particular row and a particular column you can use like this

awk -F " " 'NA==3{print $2}'

-F is used as a the Field separator

awk print

Awk is used to get a particular pattern in a ouput

you can use this to print a particular pattern

To print the third column in the output using space as the delimiter
awk -F " " '{print $3}'

If you want to print the particular row and a particular column you can use like this

awk -F " " 'NA==3{print $2}'

Wednesday, May 18, 2011

Comparing NULL in C example


You can use the below code for comparing String with NULL in C

char * ff;
if (  ff != NULL ) );
{
printf("value isnot null");
}
else
{
printf ("value is null");
}

Comments in C and C++


In C

valid comments are when text are in between /*  .......*/
It can also be used across multiple lines.But it will not support nested comments.

ex:
/*  comments  */
or

/*   First line
Second line  */

It can also be used like:
/*  comments
*   comments
*  comments
*/

 new C compilers also supports inline comments    // .  but old compilers may give syntax error.
ex:
int c;  // for intializing

 C++  supports both type of comments inline comments // and /*  ...*/




strcmp and strncmp in C


strcmp function in c

int strcmp(char * str1,char * str2)
strcmp function  returns a number eitheir 0, positive integer or negative integer

Return vaues:

positive integer       - is returned if string str1 is greater than string str2
0                           - if both the strings are equal
negative integer      -  if string str1 is less than the string str2


In this case comparisons happen using unsigned integers.

for example:
char * i = "hello";
char * j="hello1"; 
printf("%d",strcmp(i,j));
output will be a negative integer

 strncmp in C:

The syntax all are same .but in this case there is an extra argument for setting how many characters you want to compare.

int strncmp(char *str1,char * str2, int );

Functionality all are same.But here int parameter is to set no of characters to compare in two strings.


for example:
char * i = "hello";
char * j="hello1"; 
printf("%d",strcmp(i,j,4));
output will be 0 as we compare only 4 characters and it is same in both the strings.