C PROGRAMMING TIPS AND TRICKS

C-program-tricks-techews.com
If you Don’t have a C Language Compiler , Download it here:
DOWNLOAD TURBO C FOR WINDOWS 7,VISTA,XP
1) SWAP 2 VARIABLES EASILY WITHOUT CREATING A TEMP VARIABLE
void swap(int& a, int& b)
{
a ^= b;
b ^= a;
a ^= b;
}
Note: This will not work if a and b point to the same variable.
In that case both will be reset to 0 after executing swap(a,b).
2) SCANF TRICKS
• To read till a comma:
scanf(“%[^,]”, a); // This doesn’t trash the comma
scanf(“%[^,],”,a); // This one trashes the comma
• To read a string:
scanf(“%[^\n]\n”, a); // It will read until you meet ‘\n’, then trashes the ‘\n’
• To skip input
If you want to skip some input, use * sign after %. For example you want to read last name from “Eby Jimmy” :
scanf(“%*s %s”, last_name); // last_name is a variable
3) SOLVE COMPILER ERROR EASILY
It might sometimes be difficult to find errors in a huge program even if the lines are mentioned where the errors are present. So here is a simple way to solve that (if you have an internet connection onside).
When you get any compiler error, in the Error List Panel, you will see ERROR CODE along corresponding error. Write that ERROR Code with keyword “MSDN” in GOOGLE, and you will get a link on MSDN. In that link, you will find every detail on that error and information on how to fix it.
4) PROGRAM TIMER
Some of you might have seen timer in VB, like if you set an interval and timer is enabled, it waits until the time is complete. Here is a basic code for timer in C Program.
#include <stdio.h>
#include <unistd.h>
int main() {
printf(“wait\n”);
sleep(3);
printf(“time elapsed\n”);
return 0;
}
5) THE GOES TO OPERATOR (–>)
As the name suggests, this code will list the entire numbers between a given range
For example:
#include <stdio.h>
int main()
{
int x = 10;
while( x –> 0 )
{
printf(“%d “, x);
}
}
The above code compiles and runs, listing the numbers from 9 to 0.

0 comments :

Post a Comment