| Source Code to get Local Time of System in C/ C++ |
|
struct tm
{
int
tm_sec;
/* seconds after the minute - [0,59] */
int
tm_min;
/* minutes after the hour - [0,59] */
int tm_hour; /*
hours since midnight - [0,23] */
int tm_mday; /* day of
the month - [1,31] */
int tm_mon;
/* months since January - [0,11] */
int tm_year; /*
years since 1900 */
int tm_wday; /* days
since Sunday - [0,6] */
int tm_yday; /* days
since January 1 - [0,365] */
int tm_isdst; /*
daylight savings time flag */
};
Above Strcuture is already defined in time.h. You only use code below to get
system time
//use the functions in time.h
#include <Time.h>
int main()
{
time_t now;
struct tm* tm;
now = time(0);
tm = localtime(&now);
printf("%d",tm.tm_min);
// similarly you can print rest of the tm structure.
return 0;
}
|
|
|
|
| How to Open Executable files from C++(Website
from C++ Code) |
|
ShellExecute(NULL, "open", "http://www.itworld.com.pk", NULL, NULL,
SW_SHOWNORMAL);
|
|
Making KQEMU for Window Host
|
For window host. You cannot compile on
windows,because the code has a ELF assembler option You have to cross-compile
it on Fedora Core host. I recommend to use Fedora Core 6. Download Mingw's rpm
packagesDownload and install.
After installing rpms, configure in the source code.
./configure --enable-mingw32
make
|
|
|
| Source Code[program][ to know that Number is Power of 2 |
|
bool isPowerOf2(int
num){
return ((num>0)
&& (num & (num-1))==0);
}
For example if the given number num is 8(00001000) then
num-1 would be 7 (00000111). Now we notice that these two bit patterns dont
have a 1 in the same bit position. Further observation suggests that if we bitwise
and (&) both these numbers we would get zero.
|
| Source Code[Program] to modify the value of Constant variable
in C++/C |
|
First Method
#include <stdio.h>
void change (int*);
int main()
{
const int a=20;
change (&a);
printf ("After Change %d\n",a);
}
void change (int *a)
{
*a= 22;
}
------------------------------------------------
Second Method
int main(){
const int a=10;
printf("a: %d",a);
*(int *)&a=34;
printf("\nModified a: %d",a);
return 0;
}
III Method
#include<stdio.h>
int main()
{
const int q=100;
int *m=(int *)&q;
*m=*m+10;
printf("%u %d\n",m,*m);
printf("%u %d\n",&q,q);
getchar();
}
|
|
Date Conversion (Format)
|
|
i have string as follows.
>char dest[11;
> char buffer[9];
> strcpy(buffer,"20071201");
>
> /* I need to convert it into YYYY-MM-DD format.
Soultion:
#include <stdio.h>
int main(void){
int year, month, day;
char newdate[128];
char olddate[] = "20071201";
if (sscanf(olddate, "%4d%2d%2d", &year, &month, &day) == 3){
sprintf(newdate, "%04d-%02d-%02d\n", year, month, day);
printf("%s\n", newdate);
}else{
fprintf(stderr, "Problem scanning '%s' - wrong format\n", olddate);
}
return 0;
}
|
| How do you write a function that can reverse a linked-list? |
|
void reverselist(void)
{
if(head==0)
return;
if(head->next==0)
return;
if(head->next==tail)
{
head->next = 0;
tail->next = head;
}
else
{
node* pre = head;
node* cur = head->next;
node* curnext = cur->next;
head->next = 0; cur-> next = head;
for(; curnext!=0; )
{
cur->next = pre;
pre = cur;
cur = curnext;
curnext = curnext->next;
}
curnext->next = cur;
}
}
|
| How to get the index of first occurance of
substring in a string using strstr string built in function |
#include <stdio.h>
#include <string.h>
int main()
{
char *url = "Pakistan";
char *search_string = "ki";
char *position;
int index;
position = strstr(url, search_string);
if(position==NULL) {
printf("%s not found in %s\n", search_string, url);
}
else {
printf("Address of url: 0x%p\n", url);
printf("Address of position: 0x%p\n", position);
index = url - position; /* pointer arithmetic! */
if(index<0) {
index = -index; /* take the positive value of index */
}
printf("First occurrence of %s in %s\n", search_string, url);
printf("is at letter %d\n", index);
}
return 0;
}
|
| How to find the greatest number of the two
numbers without using relational operator in C++ |
#include < IOSTREAM.H >
#include < CONIO.H >
#include < MATH.H >
void main()
{
int x=9;
int y=88;
cout<<(x+y+abs(x-y))/2;
getche();
}
|
| How to Reverse the Integer (int) using
recursion in C/C++ |
#include < IOSTREAM.H >
#include < CONIO.H >
int reverse(int num)
{
int temp ;
static int factor = 1;
if(num)
{
temp = reverse(num/10) + (factor * (num %10)) ;
factor *= 10; return temp;
}
else
return 0;
}
void main()
{
cout << reverse(1234);
} |
| How to Swap value of variables without
using logical operator ( in one line) |
|
#include < iostream.h >
#include<conio.h>
void main()
{
int a=9;< BR> intb=8;<BR>a=a-(b=(a=a+b)-b);
cout<<a<<endl;
cout<<b;
}
|
| I want to print some word..for ex : "Lahore". I want to print
this word without entering main function.I mean main function contains only
starting brace and ending brace, nothing inside main.So is it possible to print
it?
|
#include < STDIO.H >
void prnt()
{
printf("Lahore");
}
#pragma startup prnt 64
void main()
{
} |