Sunday, February 15, 2009

Type Qualifiers

0 comments
Type Qualifiers  
  • C defines type qualifiers that control how variables may be accessed or modified. 
  • C89 defines two of these qualifiers: const and volatile. (C99 adds a third, called restrict) 
  • The type qualifiers must precede the type names that they qualify.
  •  const Variables of type const may not be changed by your program. (A const variable can be given an initial value, however.) 
  • The compiler is free to place variables of this type into read-only memory(ROM).
      For example,
      const int a=10;
  • creates an integer variable called a with an initial value of 10 that your program may not modify.However, you can use the variable a in other types of expressions. A const variable will receive itsvalue either from an explicit initialization or by some hardware-dependent means.
  • The const qualifier can be used to prevent the object pointed to by an argument to a function from being modified by that function. That is, when a pointer is passed to a function, that function can modify the actual object pointed to by the pointer.

Wednesday, January 7, 2009

Advantages of using Absolute paths

0 comments
Better way to use a command,whether to give absolute path or just command.
For example: Is it better to use /usr/bin/ls or ls

1)Using absolute path is advisable for "root" userid because that will provide:
security :using a full path adds security, then your system is insecure to begin with. Using a full path will do little if anything to improve it.

2)if somebody creates dummy "ls" command in your home directory and you add
your home > in PATH, then when you enter "ls" , "dummy ls" will be executed

anyways in some cases it is necessary to use absolute paths. For example, cron table entries require full paths to the commands because the cron daemon doesn't maintain a PATH variable.

Thursday, January 1, 2009

Time Structure (struct tm) with an example

0 comments
Time structure

struct tm
{
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
}

Description of each element is :
tm_sec seconds after the minute 0-61
tm_min minutes after the hour 0-59
tm_hour hours since midnight 0-23
tm_mday day of the month 0-31
tm_mon month since january 0-11
tm_year years since 1900
tm_wday weekday(days since sunday) 0-6
tm_yday yearday(days since Jan10 0-365
tm_isdst daylight saving time flag
The Daylight Saving Time flag (tm_isdst) is greater than zero if Daylight Saving Time is in effect, zero if Daylight Saving Time is not in effect, and less than zero if the information is not available.
* tm_sec is generally 0-59. Extra range to accommodate for leap seconds in certain systems

Example:
/* time example */
#include <stdio.h>
#include <time.h>
#include <conio.h>

int main ()
{
time_t rawtime;

rawtime = time (NULL);
printf ("current time is :%s", ctime(&rawtime));

struct tm *mytime;
mytime = localtime(&rawtime);
printf ("current time is :%s",asctime(mytime));
printf("%d %d %d %d %d %d %d %d %d",mytime->tm_year,mytime->tm_mon ,mytime->tm_yday,mytime->tm_mday, \
mytime->tm_mday ,mytime->tm_hour , \
mytime->tm_min , \
mytime->tm_sec , \
mytime->tm_isdst);


getch();
return 0;
}

Hit Counter


Statistics

Followers

 

Programmer's Guide. Copyright 2008 All Rights Reserved