Sunday, March 14, 2010
Selection statement in C
C support 2 selection statement
If and Switch
General form of IF is
if ( expression ) statement;
else statement.
Nested IF
if (i)
{
if (j) does something( );
if(k) doessomething( );
else doessomething( );
}
else doessomething( );
? this operator can replace the if-else statement into the general form
? is called ternary operator
General form is
Exp1 ? Exp2 : Exp3
Exp1 is evaluvated if it is true then print Exp2 else print Exp3
Example
X=10;
Y=X>9 ? 100 : 200;
Back slash character constan
c includes some back slash character constant.
so you may easily enter these special character as constant.
They are also reffered as escape sequence.
#include
int main(void)
{
printf("\n\t This is a test.");
return 0;
}
code meaning
\b Back space
\f form used
\n new line
\r carriage return
\t horizantal tab
\" Double quote
\' Single quote
\\ Back slash
\v vertical tab
\a Alert
\? Question mark
\N octal constant
\xN Hexadecimal constant
Friday, March 12, 2010
what is PHP?
PHP
What is PHP ?
It is an acronym for Hypertext Preprocessor.
It is widely used open source general scripting language.
It is especially suited for web development and can be embedded into HTML.
The origins of PHP:
“ Wonderful things come from singular inspiration”
PHP began life as simple way to track visitors to Rasmus Ledorf’s resume.
It also could embed SQL queries in web pages.
What can PHP do ?
Anything. PHP is mainly focused on
server side scripting, so you can do anything any
other CGI program can do, such as collect from data,
generate dynamic page content or send and receive
cookies. But PHP cannot do much more.
What is PHP ?
It is an acronym for Hypertext Preprocessor.
It is widely used open source general scripting language.
It is especially suited for web development and can be embedded into HTML.
The origins of PHP:
“ Wonderful things come from singular inspiration”
PHP began life as simple way to track visitors to Rasmus Ledorf’s resume.
It also could embed SQL queries in web pages.
What can PHP do ?
Anything. PHP is mainly focused on
server side scripting, so you can do anything any
other CGI program can do, such as collect from data,
generate dynamic page content or send and receive
cookies. But PHP cannot do much more.
Thursday, March 4, 2010
we are always in the right situation.
“Some flowers grow best in the Sun while others do well in Shade.
Remember, we are put where we grow the best and accordingly we get people and situations to grow with”
A lot of times we get this question :”Why I am this situation when others are in a better one”.The quote above explains a lot. We all have different lessons to learn to grow. A dishonest man need to learn what is honesty while a greedy man needs to learn how to be generous.
Sometimes I think the whole nature is so perfect. There is a perfect rhythm and balance in all the things in nature. Only a prefect one can design such a perfect system and how can that perfect one put us in a wrong situation. At times things seem bad but when you look back you know there was a good reason why that happened. I feel when we accept what we have and where we are, the intense peace and power that we get enable us to overcome the toughest situation in life.
May we all realize that life is fair and we are always in the right situation
Wednesday, February 24, 2010
EOF plays a major role sometimes..!
So, just how much data is in that file? The exact contents of a file may not be precisely known. Usually the general format style of the file and the type of data contained within the file are known. The amount of data stored in the file, however, is often unknown. So, do we spend our time counting data in a text file by hand, or do we let the computer deal with the amount of data? Of course, we let the computer do the counting. |
C++ provides a special function, eof( ), that returns nonzero (meaning TRUE) when there are no more data to be read from an input file stream, and zero (meaning FALSE) otherwise.
Rules for using end-of-file (eof( )): 1. Always test for the end-of-file condition before processing data read from an input file stream. a. use a priming input statement before starting the loop b. repeat the input statement at the bottom of the loop body 2. Use a while loop for getting data from an input file stream. A for loop is desirable only when you know the exact number of data items in the file, which we do not know. |
Dont miss calloc function..!
calloc
calloc is similar to malloc, but the main difference is that the values stored in the allocated memory space is zero by default. With malloc, the allocated memory could have any value.calloc requires two arguments. The first is the number of variables you'd like to allocate memory for. The second is the size of each variable.
Like malloc, calloc will return a void pointer if the memory allocation was successful, else it'll return a NULL pointer.
This example shows you how to call calloc and also how to reference the allocated memory using an array index. The initial value of the allocated memory is printed out in the for loop.
#include
#include
/* required for the malloc, calloc and free functions */
int main() {
float *calloc1, *calloc2, *malloc1, *malloc2;
int i;
calloc1 = calloc(3, sizeof(float)); /* might need to cast */
calloc2 = calloc(3, sizeof(float));
malloc1 = malloc(3 * sizeof(float));
malloc2 = malloc(3 * sizeof(float));
if(calloc1!=NULL && calloc2!=NULL && malloc1!=NULL && malloc2!=NULL) {
for(i=0 ; i<3 ; i++) {
printf("calloc1[%d] holds %05.5f, ", i, calloc1[i]);
printf("malloc1[%d] holds %05.5f\n", i, malloc1[i]);
printf("calloc2[%d] holds %05.5f, ", i, *(calloc2+i));
printf("malloc2[%d] holds %05.5f\n", i, *(malloc2+i));
}
free(calloc1);
free(calloc2);
free(malloc1);
free(malloc2);
return 0;
}
else {
printf("Not enough memory\n");
return 1;
}
}
Output: calloc1[0] holds 0.00000, malloc1[0] holds -431602080.00000 |
Try changing the data type from float to double - the numbers displayed were too long for me to fit onto this web page :)
C - malloc Function definition
malloc
malloc requires one argument - the number of bytes you want to allocate dynamically.If the memory allocation was successful, malloc will return a void pointer - you can assign this to a pointer variable, which will store the address of the allocated memory.
If memory allocation failed (for example, if you're out of memory), malloc will return a NULL pointer.
Passing the pointer into free will release the allocated memory - it is good practice to free memory when you've finished with it.
This example will ask you how many integers you'd like to store in an array. It'll then allocate the memory dynamically using malloc and store a certain number of integers, print them out, then releases the used memory using free.
#include
#include /* required for the malloc and free functions */
int main() {
int number;
int *ptr;
int i;
printf("How many ints would you like store? ");
scanf("%d", &number);
ptr = malloc(number*sizeof(int)); /* allocate memory */
if(ptr!=NULL) {
for(i=0 ; i
*(ptr+i) = i;
}
for(i=number ; i>0 ; i--) {
printf("%d\n", *(ptr+(i-1))); /* print out in reverse order */
}
free(ptr); /* free allocated memory */
return 0;
}
else {
printf("\nMemory allocation failed - not enough memory.\n");
return 1;
}
}
Output if I entered 3: How many ints would you like store? 3 |
ptr = (int *)malloc(number*sizeof(int));
The above example was tested in MSVC++ but try casting the pointer if your compiler displays an error.
Subscribe to:
Posts (Atom)