Archive

Archive for the ‘C_PROG’ Category

POINTERS IN C LANGUAGE

December 7, 2011 Leave a comment

What is pointer in c programming?


Explain pointers in c

Pointer is a user defined data type which creates special types of variables which can hold the address of primitive data type like char, int, float, double or user defined data type like function, pointer etc. or derived data type like array, structure, union, enum.

Examples:

int *ptr;
int (*ptr)();
int (*ptr)[2];

In c programming every variable keeps two type of value.
1. Contain of variable or value of variable.
2. Address of variable where it has stored in the memory.

(1) Meaning of following simple pointer declaration and definition:
int a=5;
int * ptr;
ptr=&a;

Explanation:

About variable a:
1. Name of variable : a
2. Value of variable which it keeps: 5
3. Address where it has stored in memory : 1025 (assume)

About variable ptr:
4. Name of variable : ptr
5. Value of variable which it keeps: 1025
6. Address where it has stored in memory : 5000 (assume)

Pictorial representation:


Note: A variable where it will be stored in memory is decided by operating system. We cannot guess at which location a particular variable will be stored in memory.

(2) Meaning of following pointer declaration and definition:
int a=50;
int *ptr1;
int **ptr2;
ptr1=&a;
ptr2=&pt1;
Explanation:

About variable a:
1. Name of variable : a
2. Value of variable which it keeps: 50
3. Address where it has stored in memory : 5000 (assume)

About variable ptr1:
4. Name of variable : ptr1
5. Value of variable which it keeps: 5000
6. Address where it has stored in memory : 9000 (assume)

About variable ptr2:
7. Name of variable : ptr2
8. Value of variable which it keeps: 9000
9. Address where it has stored in memory : 9555 (assume)

Pictorial representation of above pointer declaration and definition:
Note:
* is known as indirection operator which gives content of any variable.
& is known as reference operator which gives address where variable has stored in memory.
Cancellation rule of above two operators:
* and & operators always cancel to each other i.e.
*&p=p

But it is not right to write:
&*p=p

Simple example:
What will be output of following c program?
#include
int main(){

int x=25;
int *ptr=&x; //statement one
int **temp=&ptr; //statement two
printf(“%d %d %d”.x.*ptr,**temp);
return 0;
}
Output: 25 25 25
Explanation:
As we know value of variable x is 25.
*ptr= *(&x) //from statement one
=*&x
=x //using cancellation rule
=25
**temp= **(&ptr)=*(*&ptr)=*ptr=*(&x)=*&x=x=25
Categories: C_PROG

C PROGRAM TO CLEAR SCREEN WITH OUT USING clrscr() FUCTION

December 3, 2011 2 comments

Author shahabaz:
This program is very simple and easy, you all might be thinking how could I do such thing with out using a clrscr(); function. (NOTE: Am talking for turbo c editor as well if this code does not wotrk on other edit please alter it). Lets have the code.
#include<stdio.h>
int main(){
int i,j;
   for(i=0;i<27;i++){
     printf(“\n”);
     for(j=0;j<80;j++)
       printf(” “);
      }
 gotoxy(1,1);
return 0;
}

Be sure with the code while editing
Categories: C_PROG

C Program To Accept Password [*appearing]

November 28, 2011 Leave a comment

This is a simple login program in C. While accepting password it masks each character using ‘*’ symbol and display the password in the next line after the user hits Enter key. It also accepts backspaces and acts accordingly.

#include<stdio.h>
#include<conio.h>
char pw[25],ch;
int i;
void main()
{
clrscr();
puts("Enter password");
while(1)
{
if(i<0)
i=0;
ch=getch();
if(ch==13)
break; /*13 is ASCII value of ENTER*/
if(ch==8) /*ASCII value of BACKSPACE*/
{
putch('\b');
putch(NULL);
putch('\b');
-i;
continue;
}
pw[i++]=ch;
ch='*';
putch(ch);
}
pw[i]='';
printf("\n\n%s",pw);
getch();
}
Categories: C_PROG

C Program Without a Main Function

November 28, 2011 Leave a comment

How to write a C program without a main function?. Is it possible to do that. Yes there can be a C program without a main function. Here’s the code of the program without a main function

HERE IS THE CODE

__________________________________________
#include<stdio.h>
#define decode(s,t,u,m,p,e,d) m##s##u##t
#define begin decode(a,n,i,m,a,t,e)

int begin()
{
printf(” hello “);
}

__________________________________________


Does the above program run without the main function? Yes, the above program runs perfectly fine even without a main function. But how, whats the logic behind it? How can we have a C program working without main?

Here we are using preprocessor directive #define with arguments to give an impression that the program runs without main. But in reality it runs with a hidden main function.

The ‘##‘ operator is called the token pasting or token merging operator. That is we can merge two or more characters with it.

NOTE: A Preprocessor is program which processess the source code before compilation.

Look at the 2nd line of program –

#define decode(s,t,u,m,p,e,d) m##s##u##t

What is the preprocessor doing here. The macro decode(s,t,u,m,p,e,d) is being expanded as “msut” (The ## operator merges m,s,u & t into msut). The logic is when you pass (s,t,u,m,p,e,d) as argument it merges the 4th,1st,3rd & the 2nd characters(tokens).

Now look at the third line of the program –

#define begin decode(a,n,i,m,a,t,e)

Here the preprocessor replaces the macro “begin” with the expansion decode(a,n,i,m,a,t,e). According to the macro definition in the previous line the argument must be expanded so that the 4th,1st,3rd & the 2nd characters must be merged. In the argument (a,n,i,m,a,t,e) 4th,1st,3rd & the 2nd characters are ‘m’,’a’,’i’ & ‘n’.

So the third line “int begin” is replaced by “int main” by the preprocessor before the program is passed on for the compiler. That’s it…

The bottom line is there can never exist a C program without a main function. Here we are just playing a gimmick that makes us beleive the program runs without main function, but actually there exists a hidden main function in the program. Here we are using the proprocessor directive to intelligently replace the word begin” by “main”. In simple words int begin=int main.

Categories: C_PROG