ALT_IMG

You are the cause of my pain

You are the cause of my pain, yet the love I feel for you is my only consolation, my only cure..

ALT_IMG

Saw the girl, whom once I thought as my best friend

Remembering my classmates, after few years, My eyes were filled with tears, Everyone is busy a lot, No one escaped destiny's plot, Saw the girl, whom once I thought as my best friend, Today she is some body else's girl friend, After months remembered about her for a little while, Heard she is happy, that made me smile.

Alt img

It’s been raining since you left me

It’s been raining since you left me, now I’m drowning in the flood. You see, I’ve always been a fighter, but without you I give up.

ALT_IMG

Sweet Heart

Sweetheart.....I miss ......THe whisper of your voice...The warmth of your touch and all the wonderful times that we shared together.......

ALT_IMG

When I see you

When I see you smile and know that it's not for me, that's when I miss you the most.

Showing posts with label C Tutorial. Show all posts
Showing posts with label C Tutorial. Show all posts
Tuesday, July 12, 2011

POINTERS

0 comments

A pointer in C is the address of something. It is a rare case indeed when we care what the specific address itself is, but pointers are a quite common way to get at the contents of something. The unary operator ‘&’ is used to produce the address of an object, if it has one. Thus

int a, b;
b = &a;

puts the address of a into b We can’t do much with it except print it or pass it to some other routine, because we haven’t given b the right kind of declaration. But if we declare that b is indeed a pointer to an integer, we’re in good shape:

int a, *b, c;
b = &a;
c = *b;

b contains the address of a and ‘c = *b’ means to use the value in b as an address, i.e., as a pointer. The effect is that we get back the contents of a, albeit rather indirectly. (It’s always the case that ‘*&x’ is thesame as x if x has an address.)

The most frequent use of pointers in C is for walking efficiently along arrays. In fact, in the implementation of an array, the array name represents the address of the zeroth element of the array, so you can’t use it on the left side of an expression. (You can’t change the address of something by assigning to it.) If we say

char *y;
char x[100];

y is of type pointer to character (although it doesn’t yet point anywhere). We can make y point to an element of x by either of

y = &x[0];
y = x;

Since x is the address of x[0] this is legal and consistent.
Now ‘*y’ gives x[0] More importantly,

*(y+1) gives x[1]
*(y+i) gives x[i]

and the sequence

y = &x[0];
y++;

leaves y pointing at x[1]
Let’s use pointers in a function length that computes how long a character array is. Remember that by convention all character arrays are terminated with a ‘\0’. (And if they aren’t, this program will blow up inevitably.) The old way:

length(s)
char s[ ]; 
{
int n;
for( n=0; s[n] != ′\0′; )
n++;
return(n);
}

Rewriting with pointers gives

length(s)
char *s; {
int n;
for( n=0; *s != ′\0′; s++ )
n++;
return(n);
}

You can now see why we have to say what kind of thing s points to _ if we’re to increment it with s++ we have to increment it by the right amount.The pointer version is more efficient (this is almost always true) but even more compact is

for( n=0; *s++ != ′\0′; n++ );

The ‘*s’ returns a character; the ‘++’ increments the pointer so we’ll get the next character next time around.As you can see, as we make things more efficient, we also make them less clear. But ‘*s++’ is an idiom so common that you have to know it.Going a step further, here’s our function strcopy that copies a character array s to another t.

strcopy(s,t)
char *s, *t; {
while(*t++ = *s++);
}

We have omitted the test against ‘\0’, because ‘\0’ is identically zero; you will often see the code this way.(You must have a space after the ‘=’)
For arguments to a function, and there only, the declarations

char s[ ];
char *s;

are equivalent _ a pointer to a type, or an array of unspecified size of that type, are the same thing.If this all seems mysterious, copy these forms until they become second nature. You don’t often need anything more complicated.

<<Prev Home 
Continue reading →
Friday, July 1, 2011

Switch Case Statement in C

0 comments
                    The switch case statement allows you to select from multiple choice based on a set of fixed value for a give expression.The value of the variable given into switch is compared to the value following each of the cases.And when one value matches the value of the variable,the computer continues executing the program from that point.


Syntax of the 'switch case' statement is:
switch(expression)
{
case value1:  /*execute code1*/
break;
case value2: /*execute code2*/
break;
.....
default: /*execute default action*/
break;
}

Example:
/*Program to convert number into word*/
#include<stdio.h>
void main()
{
int num;
printf("Enter a number:\n");
scanf("%d",&num);
switch(num)
{
case 1:
printf("ONE!\n");
break;
case 2:
printf("TWO!\n");
break;
case 3:
printf("THREE!\n");
break;
case 4:
printf("FOUR!\n");
break;
case 5:
printf("FIVE!\n");
break;
case 6:
printf("SIX!\n");
break;
case 7:
printf("SEVEN!\n");
break;
case 8:
printf("EIGHT!\n");
break;
case 9:
printf("NINE!\n");
break;
case 0:
printf("ZERO!\n");
break;
default:
printf("Invalid Number!\n");
break;
}
getch();
}
In the switch case statement,the selection is determined by the value of an expression that you specify,which is enclosed between the parentheses after the keyword switch.The data type of value which is returned by expression must be an integer value otherwise the statement will not compile.when a break statement is executed,it causes execution to continue with statement following the closing brace for switch.The default statement is the default choice of the switch statement if all cases statement are not satisfy with expression.
Continue reading →
Tuesday, June 28, 2011

IF statement in C program

0 comments

                    The if statement controls conditional branching.The body of an if statement is executed if the value of the expression is nonzero.The if statement allows you to control if a program enters a section of code or nor based on whether a given condition is true or false.One of the important function of the if statement is that it allows the program to select an action based upon the user's input.For example we can check user entered password by using if statement,and decide whether a user is allowed access to the program. 

Syntax is:

if(expression)
Execute this line of code


Example:


#include<stdio.h>
void main()
{
int number;
printf("Enter the number");
scanf("%d",&number);
if(number<5) /*if number less than five*/
{
printf("The number is less than five");
}
getch():
}


I recommended always putting braces following if statements.If you do this,you never have to remember to put them in when you want more than one statement to executed,and you make the body of the if statement more visually clear.

Else

 When the condition in an if statement evaluates to false,it would be nice to some code instead of the code executed when the statement evaluates to true. The else statement effectively says that whatever code after it is executed if the if statement is false.

Syntax is:

if(expression is TRUE)
 { 
 Execute this code 
 }
else 

Execute this code
 }

Example:

#include<stdio.h>
void main()
{
int num;
printf("Enter the number");
scanf("%d",&num);
if(num==5);
{
printf("The entered number is 5");
}
else
{
printf("The entered number is not 5");
}
getch():
}

Else if

When there are multiple conditional statements that may all evaluates to true,yet you want only if statements body to execute.You can use an else if statement following an if statement and it's body.If the first statement is true,the else if will be ignored,but if the if statement is false,it will then check condition for the else if statement.we can use numerous else if statements to ensure that only one block of code is executed.

Example:

#include<stdio.h>
void main()
{
int num;
printf("Enter the number 1 or 2 or 3");
scanf("%d",&num);
if(num==1)
{
printf("The entered number is one");
}
else if(num==2)
{
printf("The entered number is two");
}
else if(num==3)
{
printf("The entered number is three");
}
else
{
printf("number is invalid");
}
getch();
}


<<Prev Home Next>>

Continue reading →
Monday, June 27, 2011

How Use Loops in C-'DO WHILE' Loops

0 comments



'DO WHILE' loops are useful for things that want to loop at least once.The syntax is:
do
{
Code to execute
}while(condition);

Example:
#include<stdio.h>
int main()
{
int x;
x=0;
do
{/*"Hello World!" is printed at least one time even though the condition is false*/
printf("Hello World!");
}
while(x!=0);
getchar();
}
Notice that the condition is tested at the end of the block instead of beginning ,so the block will be executed at least once.If the condition is true,we jump back to the beginning of the block and execute it again.A DO WHILE loop is almost  the same as WHILE loop except that the loop body is guaranteed to execute at least once.A WHILE loop says"Loop while the condition is true,and execute this block of code".A DO WHILE loop says"execute this block of code,and then continue to loop while the condition is true". Notice that this loop will execute once,because it automatically execute before checking the condition.


<<Prev Home Next>>
Continue reading →
Sunday, June 26, 2011

How Use Loops in C-'While' Loops

0 comments


WHILE loops are very simple and the syntax is:
while(condition)
{
Code to execute while the condition is true
}
 The true represent a boolean expression which could be while(x==1) or while(x!=7),x does not equal to 7.It can be any combination of boolean statements that are legal.Even, while(x==5||y==7) which says execute the code while x equal to 5 or y equal to 7.


Example:


To print numbers up to 10:
#include<stdio.h>
int main()
{
int x=0;  /*Don't forget to declare variables*/
while(x<10) /*while x less than 10*/
{
printf("%d",x);
x++; /*Update x so the condition can be met eventually */
}
getchar();
}
This was another example ,but it is longer than FOR loop.The easiest way to think of the loop is that when it reach the brace at the end it jumps back up to the beginning of the loop,which check the condition again and decides whether to repeat the block another time,or stop and move to the next statement after the block.


More example using 'while' loop


<<Prev Home Next>>

Continue reading →

How Use Loops in C-'FOR' Loops

0 comments

Loops are used to repeat a block of code.Loops are one of the most basic but useful task in programming.We can simply produce extremely complex out put using loops.A loops let you write a very simple statement to produce a significantly greater result  simply by repetition.


FOR LOOPS:


FOR loops are the most useful one.The syntax of FOR loops:
for(variable initialization; condition; variable update)
{
code to execute while the condition is true
}
Example:


To print numbers upto 10:
#include<stdio.h>

int main()
{
    int x;
    /* The loop goes while x < 10, and x increases by one every loop*/
    for ( x = 0; x < 10; x++ ) {
        /* Keep in mind that the loop condition checks 
           the conditional statement before it loops again.
           consequently, when x equals 10 the loop breaks.
           x is updated before the condition is checked. */  
        printf( "%d\n", x );
    }
    getchar();
}


The variable initialization allows you to either declare a variable and give it a value or give it a value to already existing variable.The condition tells the program that while the conditional expression is true the loop should continue to repeat it self.The variable update section is the easiest way for a FOR loop to handle changing of the variable.It is possible to do things like X++,X=X+10 or even X=random(3).Notice that a semicolon separates each of these sections. Also note that every single one of the sections may be empty,though the semicolon still have to be there


This program is a very simple example of a for loop.x is set to zero,while x is less than 10 it calls printf to display the value of variable x,and it adds 1 to x until the condition is met.Keep in mind also that the variable is  incremented after the code in the loop is run for the first time


<<Prev Home Next>>

Continue reading →
Thursday, June 16, 2011

How to Compile C in Linux

0 comments


How to Compile C program in Linux/Unix ?:

Some people have doubts about how to compile C source code in Linux/Unix.Firstly you need GNU project C and C++ compiler for compiling C program and create executable(EXE) file.Most Linux and Unix user start compiling their C program by the name cc.But you can use gcc command to compile program.


  • First make sure you have gcc installed.
Type the following Command in Terminal to verify that gcc is installed:
which gcc

Output:
/usr/bin/gcc
  •   Find out version of gcc:
gcc --version

Output:
gcc (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE

  • To compile C program you need to use syntax as follows:
gcc program.c -o program-output

Use text editor such as vi or gedit to creat a c program called sample.c
For example open gedit and type your code and save as sample.c


Type the following code:
#include<stdio.h>
int main(void){
printf("Hello! World.This is my sample program in C\n");
return 0;
}

How do compile my C program?:

  • To compile my sample.c program:
gcc sample.c -o sample

  • To execute C program:
./sample




Continue reading →
Sunday, August 8, 2010

BASICS OF C PROGRAMMING

0 comments



                        The C program language is a popular and widely used programming language for creating computer programs.If you are interested in becoming a programmer,i will help you .I will teach you to read and write C program.I  explained the basic features of C program for beginners which should help you to understand how does a C program work.In this article,we will walk through the entire language and show you how to become a C programmer,starting at the beginning.You will be amazed at all of the different things you can create once you know C!.


Execution of C program


 Here we will discuss about the execution of C program  step by step.


step1:The line int main() declares the main function.Every C program must have a function named main somewhere in the code.At run time,program execution start at the first line of the main function.In C,the { and } symbols mark beginning and end of a block of code.The line int a,b,c; created three variables(created three memory locations with a address).It help us to store data







step2:Here,the first prompt displayed to the user by using printf statement.The printf statement in C allows you to send output to standard out(for us,the screen).





step3:If you enter a data.It is stored into the memory location "a"."&a" means write  data into the location"a".






step4:Here,the second prompt displayed to the user.






step5:And the second data stored into the second memory location "b".






step6:Here,we will used a formula c=a+b; (the value of a+b is stored into "c")






step7:The line "5+4=9" is formed and displayed to the user.








Home Next>>

Continue reading →