# [Help] C Programming



## prasadzmultiplex (Feb 21, 2005)

I need to write some programs in C Language that will Print the
Following Outputs:

I]
@@@@
*@@@
**@@
***@ 

II]
@@@@@@@@@@
@@@@***@@@@
@@@******@@@
@@*********@@
@************@


III]
******@
*****@@
****@@@
***@@@@

Note that only the @ symbols need to be printed.Only loops like For,While
& If...Else are Permitted.Any Help will be highly apppreciated.......

No matter How hard i've tried,I could'nt figure out how to do it.
I heard that the Digit Family is a very Helpful one when it comes to 
newbies.I Sincerely Hope that this message of mine shall not be
Deleted/Banned......Hoping for an early Reply.

Adios...


----------



## NikhilVerma (Feb 21, 2005)

I am rather bothered by your username...
Are you a member of the KuKluxKlan?
Or you really appretiate them?

Coz they are virtual terrorists....


----------



## technovice (Feb 21, 2005)

yeah why KuKluxKlan...i mean even Sherlock Holmes fears them   

neways heres my solutions
not tried them out though
hope they work !!

1]
for(i=0;i<4;i++)
{
	for(j=0;j<i;j++)
		printf(" ");
	for(j=0;j<4-i,i++)
		printf("@");
                printf("\n");
}

2]
for(i=0; i<5;i++)
{
	for(j=0;j<5-i;j++)
		printf("@");
	for(j=0;j<i;j++)
		printf(" ");


	for(j=0;j<i;j++)
		printf(" ");
	for(j=0;j<5-i;j++)
		printf("@");

                printf("\n");
}



3]
for(i=0;i<4;i++)
{
	for(j=0;j<4-i;j++)
		printf(" ");
	for(j=0;j<i+1;j++)
		print("@");
                printf("\n");
}

I'm assuming the '*' are blank spaces


----------



## cool_dude_prav (Feb 21, 2005)

@technovice...

Thot I wud give the answers but see tat ya has given the right answers...

Anyways, my word of appreciation for helping the fella...


----------



## prasadzmultiplex (Feb 22, 2005)

Thank you technovice for helping me out with the programs......

I gotta check them out.But I have a problem.I formatted my PC 
Recently and do not have the Turbo C Editor.Is there anywhere I can
Download it off the net?

I am a Std X student & Am trying to learn C on my own from some 
Books by Yeshwant kanetkar. Until now i had done only simple programs
on my own.But now the going seems tough.

Thank You Once Again For Your Help.


----------



## Kl@w-24 (Feb 22, 2005)

Go here for download and instructions: 

*www.pitt.edu/~stephenp/misc/downloadTC.html


----------



## prasadzmultiplex (Feb 22, 2005)

thank You Kl@w for the link.....I shall try compiling the progs now....


----------



## technovice (Feb 22, 2005)

i have the same problem as you KKK 
so im sorry if there are any syntax errors


----------



## whoopy_whale (Feb 23, 2005)

Kl@w-24 said:
			
		

> Go here for download and instructions:
> 
> *www.pitt.edu/~stephenp/misc/downloadTC.html


From where can I download the Turbo C++ compiler,plz?


----------



## Kl@w-24 (Feb 23, 2005)

Free download here : 

*www.borland.com/products/downloads/download_cbuilder.html

Also see this : 

*community.borland.com/article/0,1410,20841,00.html

*community.borland.com/article/0,1410,21751,00.html


----------



## suj_engico (Feb 24, 2005)

BETTER THAN ASKING 
I WOULD WANT U TO GO FOR A C PGMMING TUTORIAL
BUT WHY DO U REQUIRE THESE??


----------



## prasadzmultiplex (Feb 28, 2005)

Thanx a lot Guys....

That made my programming easier...
though i still am in the dark regarding Functions n NEsted Functions...

any tutorials?


----------



## tuxfan (Mar 1, 2005)

What do you want to know about functions? Let us know. May be we can help 

What do you mean by nested functions? Or is it nested loops?

But I think you are already referring to a book by Kanetkar. Does it not explain enough?

BTW, you have a deadly username!!  KuKluxKlan!! What a name!!


----------



## lamrinnirmal (Mar 1, 2005)

thats a crazy nick! hope you aint like em(the klan!)

dude go to *www.programmersheaven.com
you ll get all the tutorials and tools you need.


----------



## sagar_mutha (Mar 2, 2005)

great link lammy

thanx alot


----------



## tuxfan (Mar 2, 2005)

sagar_mutha said:
			
		

> great link lammy
> 
> thanx alot



I second that.  Great link.


----------



## prasadzmultiplex (Mar 2, 2005)

lamnirmal said:
			
		

> thats a crazy nick! hope you aint like em(the klan!)


 Not at all  
Jus' that I tried 2-3 Variations of My name(which is a Quite Common One)
and was disappointed-There were already used.....So Thot of this Unique one



			
				lamnirmal said:
			
		

> dude go to *www.programmersheaven.com
> you ll get all the tutorials and tools you need



Thanx for that Link......I read some and were helpful....
But can I download them as eBooks? or any other Way???



			
				tuxfan said:
			
		

> What do you want to know about functions? Let us know. May be we can help


Functions are Somewhat Easy.....Just the Nested ones have me in a Tizzy



			
				tuxfan said:
			
		

> What do you mean by nested functions? Or is it nested loops?


Nested Functions=Functions Within Functions.....They're a bit confusing...
Particularly the ones with many nestings.....



			
				tuxfan said:
			
		

> But I think you are already referring to a book by Kanetkar. Does it not explain enough?


 Yeah..It helps...but There's
No One to Xplain to me....i'm all on my own.....



			
				tuxfan said:
			
		

> BTW, you have a deadly username!!  KuKluxKlan!! What a name!!


Thanxxx for the Appreciation......


----------



## aadipa (Mar 2, 2005)

KuKluxKlan said:
			
		

> tuxfan said:
> 
> 
> 
> ...



What are those NESTED FUNCTIONs ?


----------



## Tech&ME (Mar 2, 2005)

dude the book by Yeshwant kanetkar is pretty good one there , even we had used it during our early college days. Why don't u try book meant for new programmers, like book by DUMMIES --- C++ Programming For Dummies 3rd Edition by Stephen R. Davis, it a good book for starters.


----------



## lamrinnirmal (Mar 3, 2005)

dude KuKlux pm me your email .... i ll host a few ebooks on C on a hosting server where you can download em off........or just search the forum....there are quite a lot of links to ebook sites(all legal!)

about nested functions............hope the below code clears your confusion.

imagine of a traveller moving to the different countries from main and when hes in australia() he doesnt go to anyother place(cause australia() doesnt call any function) and thus to find his way back to main he climbs up the way he came down.........


```
void india();
void england();
void usa();
void australia();

void main()
{
printf(" i am in main");
india();
printf("jus got back from india!.......finally here...what a ride");
}

void india()
{
printf("im in india! ");
england();
printf("jus got back from england.....!");
return;
}

void england()
{
printf("im in england.......the queen aint hot!");
usa();
printf("jus got back from usa");
return;
}

void usa()
{
printf("im in usa......so whats so great bout this place?pathetic!");
australia();
printf("got back from australia");
return;
}

void australia()
{printf("lost in the outbacks!.....gotta find myself way back");
return;
}
```


your output would be.....

*i am in main
im in india!
im in england.......the queen aint hot!
im in usa......so whats so great bout this place?pathetic!
lost in the outbacks!.....gotta find myself way back
got back from australia( now you are in usa)
jus got back from usa(now in england)
jus got back from england.....!(now in india)
jus got back from india!.......finally here...what a ride(home sweet main.....program ends*


----------



## hafees (Mar 3, 2005)

Hi Klux 

which book u r using?? Let us C? 

There is no nested functions in C as u 've mentioned.

u may be mentioning something like this.

main()
{
     a();
}


a()
{
   b();
}

b
{
   c();
}

c()
{

}

are u mentioning this type of function call?????


----------



## hafees (Mar 3, 2005)

if u ve that program - the one with nested functions - pls post it here.

Let Us C is a good book (u may skip some advanced topics which is discussed on that book)

then Pointers in C
then Projects in C
then TSR programming thru C


----------



## aadipa (Mar 3, 2005)

lamrinnirmal said:
			
		

> dude KuKlux pm me your email .... i ll host a few ebooks on C on a hosting server where you can download em off........or just search the forum....there are quite a lot of links to ebook sites(all legal!)
> 
> about nested functions............hope the below code clears your confusion.
> 
> ...



This is not a nested function. Just a simple function calling other function.


----------



## prasadzmultiplex (Mar 3, 2005)

aadipa said:
			
		

> lamrinnirmal said:
> 
> 
> 
> ...



Thanx Nirmal for the prog..I'm sure gonna try it out.
And aadipa a function calling another function is called Nesting of functions.
Atleast I think it is...Please correct me If I'm wrong.

As in case of Loop within a loop leads to various iterations.a function
within another function leads to nesting of functions.I have jus stepped
onto Recursions and they make my head go ga-ga  

And Hafees.....the Book I'm presently referring to is Programming in C
By Venugopal & Prasad a/w the Kanetkar & Balagurusamy ones.....


----------



## lamrinnirmal (Mar 3, 2005)

@aadipa- the poster has defined nested functions in his post as: 





> Nested Functions=Functions Within Functions.....They're a bit confusing...
> Particularly the ones with many nestings.....



since every function called from any other function returns back to the point where it was called a nest is formed is'nt it?.........
anyways i made up the program reading his post as he has defined...... if im wrong let me know pal....


----------



## tuxfan (Mar 4, 2005)

Ok. So the confusion is cleared about what is *nested function*. Actually, there is nothing called nested function. There may be nested loop. For example


```
while (TRUE)
{
   some code.... ;
  for (x=1; x<=20; x++)
  {
     some code .... ;
  }
}
```

These functions calling another function is pretty simple. Tell us what you don't understand about them.


----------



## prasadzmultiplex (Mar 8, 2005)

tuxfan said:
			
		

> Ok. So the confusion is cleared about what is *nested function*. Actually, there is nothing called nested function. There may be nested loop. For example
> 
> 
> ```
> ...



Yeah I pretty much understand what the nesting of loops is all about...
But u ce ethe thing with functions is that they return to where it was called 
from after performing the task.That is where I get into a mess....

For eg..I call a function at line 6 of my prog....It goes to its definition
which i have provided at the end,carrieds out the task & returns back 
to line 7 and then the prog continues....

WIth nesting of functions,it becomes complicated.....If suppose there is 
a function within the definition of the earlier function,where does it go to
after it gets executed? Back to the Definition part or the Earlier function
called part??
Also what do u mean when u say " A Function returns with a value"?


----------



## hafees (Mar 9, 2005)

Actually y do u use the term nested function like nested if or nested loops. Actually u CAN'T define a function inside another function body. 

A function returns to from where it is called. If A calls B and B calls C and C calls D, then D is returned to C and C is returned to B and so on. In no way D can return to A(unless D is called from A).

Return with a value means a function is not void.
By default every function is supposed to return an int

that is 

myfunc() is same as int myfunc()
.if u want to make it return nothing then make it void
eg: void my_func(int x) { ... }
A function can return only one value.


----------



## tuxfan (Mar 9, 2005)

prasadzmultiplex said:
			
		

> Yeah I pretty much understand what the nesting of loops is all about...
> But u ce ethe thing with functions is that they return to where it was called
> from after performing the task.That is where I get into a mess....



If this doesn't happen, you will be in a bigger mess  Where do you think should it go after the function is finished? Just think, what will make it convenient?



			
				prasadzmultiplex said:
			
		

> For eg..I call a function at line 6 of my prog....It goes to its definition which i have provided at the end,carrieds out the task & returns back to line 7 and then the prog ontinues....



This is called sequential programming as compared to event based programming. Usually (not a rule), under GUI, event based programming is used and under text based environments sequential programming is used.



			
				prasadzmultiplex said:
			
		

> WIth nesting of functions,it becomes complicated.....If suppose there is  a function within the definition of the earlier function,where does it go to after it gets executed? Back to the Definition part or the Earlier function called part??


Huh?? What are you trying to ask? Control is never returned back the statement that defines a function. It is always retuned to the statement that calls it.



			
				prasadzmultiplex said:
			
		

> Also what do u mean when u say " A Function returns with a value"?


Take a very simple example. You have a function called Add3Numbers(). This is how you call it. Here the function returns a value.


```
Sum1 = Add3Numbers(23,12,34);
Sum2 = Add3Numbers(num1,num2,num3);
```

This function will be coded like this.

```
int Add3Numbers (int a, int b, int c)
{
  int total;
  total = a + b + c;
  return (total);
}
```

Hope you have understood.


----------



## prasadzmultiplex (Mar 11, 2005)

Thanxx a lot Tuxfan & hafees.....I seem to have got Functions and their nesting
all taped out.As tuxfan explained,the 'Returning' part was a bit foggy 4 me and
thanx to u & some books/tutorials,i seem to have grasped them.

Now I hate bothering u guys again but I have hit a wall again.This time
its the arrays.I have seen some tutorials as supplied by nirmal and they're good.
But there are no programs on the net are there? Googling is os little use.
I have managed most of the programs given in the exercise but two of them
are eating my head away.

1)A Prog to accept data in2 an array so that Even nos. go into Even
Indexes & odd nos. go into Odd Indexes.

2)A Prog to enter data into a 3X3 matrix so that 1st data goes into 9th
cell,2nd into 8th & so on.....


----------



## tuxfan (Mar 12, 2005)

Mr. Racist aka Mr. Facist, what are you trying to ask? I didn't understand the first question  If its only about odds and evens, why not use a mod operator?

As for second, it can be achieved by 2 for() loops. Instead of incrementing counters, you will have to decrement them.

I hope these hints will help you do it


----------



## Santosh Halemani (Mar 12, 2005)

hey guys what about using unix for programming. is anybody using. then has nayone tried graphics in c or c++. And i think i shold suggest Harbeld Schild for c++ using unix. The complete refrence. Not sure about the name though. But he has given a very good examples on every concept. The price might be Rs.395.


----------



## prasadzmultiplex (Mar 12, 2005)

tuxfan said:
			
		

> Mr. Racist aka Mr. Facist, what are you trying to ask? I didn't understand the first question  If its only about odds and evens, why not use a mod operator?



see, an array a[5] has a[0],a[1],a[2],a[3],& a[4] indexes......
u hafta enter data into the array such that Odd nos go into the odd indexes
and even nos go into the even indexes.
also this should be dome that as one enters,the data,it gets into the respective
index....No storing in a temp array (Say b[5] ) and then re-arrange the inedexes...

Hope i'v emade it clear  

And the second one i got the idea...thanx a ton


----------



## ujjwal (Mar 12, 2005)

For the first program, you just keep two seperate variables, say i for even numbers (initialised at 0), and k for odd numbers (initialised at 1). Now, if the number is even (check it using the remainder operator), put the number into the i'th element of the array, and then increment i by 2. Similiarly, you can place odd numbers into odd positions


----------



## lamrinnirmal (Mar 12, 2005)

gave it a lot of thought and i now conclude that you can do it using functions ...... your array then must be declared global and your loop counters must be static.... give it a shot.... if someone posts a better solution here hats off to him!


----------



## lamrinnirmal (Mar 12, 2005)

@ujwal - i tried your technique..... there is a flaw.... 


```
for(i=1,j=0;i<6;i=i+2,j=j+2)
{
scanf("%d",&data);
if(data%2!=0)
a[i]=data;
else
a[j]=data;
}
```

say i enter an even no... then sure a[0] contains the even no but my j is now 3 not 1!!.... and hence a[1] will never get filled!


----------



## ujjwal (Mar 12, 2005)

@lamrin - the idea was not to put i=i+2 and j=j+2 in the loop update expression, but

if(data%2!=0) 
  { a_=data;
   i=i+2;
  }

I think this should work without trouble._


----------



## lamrinnirmal (Mar 12, 2005)

hmm......that sounds plausibe ujwal....though i ve not tried it does your program sounds like the below code?:


```
i=0;
j=1;
while(i<=20&&j<=19)
{
scanf("%d",&data);
if(data%2!=0)
{
a[j]=data;
j=j+2;
}
else
{a[i]=data;
i=i+2;
}
}
```

yeah.....looks good.... my logic (previous post) was too roundabout.....

hats off to you man!  *d *
hey get an avatar with that guy removing his hat "off"...it ll look neat!



			
				prasadzmultiplex said:
			
		

> Now I hate bothering u guys again but I have hit a wall again



dude....we come here to get bothered and to listen to people who run into walls .....and sometimes when we run into a wall we bother the other guys..... this is a community to remove all walls.period.


----------



## prasadzmultiplex (Mar 14, 2005)

Thanx a Ton  Ujjwal & nirmal,u guys must be really hardcore Programmers!!!

I shall try thm out & let u know the results....

Can i save the Prog i typed as a *txt file or should I have to type it again in Word?


----------



## tuxfan (Mar 14, 2005)

How can you guys directly give code to a learner? Then how will he learn? Don't spoon feed him. Give him hints, not the code. It will be more satisfying even for him if he solves his problems on his own 

@ prasadzmultiplex, can you see the %2 in their codes? That % is a mod operator. That is what I told you to use


----------



## lamrinnirmal (Mar 14, 2005)

tuxfan said:
			
		

> How can you guys directly give code to a learner? Then how will he learn? Don't spoon feed him. Give him hints, not the code. It will be more satisfying even for him if he solves his problems on his own



bullzeye tuxfan!..... couldnt disagree with you on that!... 

@prasadmultiplex- hope you understood the mod operator as tuxfan explained it.... it is really important to get the mod concept right as it simplifies hell lot of problems in C...
in a nutshell: a%b (read % as mod) will return the remainder when a is divided by b. hence if a%b is 0 then b completely divides a...
hope you got the concept wrt to finding even nos.


----------



## hafees (Mar 15, 2005)

Hi 
i 've added C puzzles topic . pls try it out.  and provide ur own c puzzles. 

*www.thinkdigit.com/forum/viewtopic.php?t=16416&start=0&postdays=0&postorder=asc&highlight=


----------



## prasadzmultiplex (Mar 15, 2005)

tuxfan said:
			
		

> How can you guys directly give code to a learner? Then how will he learn? Don't spoon feed him. Give him hints, not the code. It will be more satisfying even for him if he solves his problems on his own
> 
> @ prasadzmultiplex, can you see the %2 in their codes? That % is a mod operator. That is what I told you to use



I Agree Tuxfan. But lemme assure you that I'm not Copying the code,you helpful guys
are giving,but am grasping the underlying idea and am writing on my own.
Rest assured that I am not blatantly using the code u guys provide.

And yes,I know abt the Modulus operator.Checking for Even/Odd nos. is always
done using the % operator. In case of the array program,I am able to enter
data first into a temporary array (As Ujjwal said abt "Data") and then check for 
it being Even/Odd.But I want a prog which Checks a number whether it is even or odd
at the time of entry itself and then Insert it into the respective array.

I thot of using the getch(); operator but what i am abl eto accomplish is that, when I enter an odd number,it gets checked & then scanned into a[1],
next,if i enter  another odd number,it gets overwritten onto a[1] but does'nt get into a[3].Same is the case with Even nos.So at the end I have only
a[0] & a[1] in my array.

The Loop within which I'm using the getch(); operator is not able to make the control move onto
the next variables......


----------



## lamrinnirmal (Mar 15, 2005)

heres a very rough algorithm you could use for the way you want to enter the variables

 keep a counter variable to count the no of values entered in the array. also keep two more variables to monitor the odd indices and even indices. and INITIALIZE em!!

 start the loop ,monitoring the counter variable

 take the variables , more preferrably using getche so that it "echoes" on your screen

 check for its oddness/evenness and enter it into the array *while also monitoring the odd/even index so that it doesnt exceed the array limit*(lack of bounds checking is a pain in the A$$). increment the odd/even variable to keep it ready for the next input.

@hafees- good thread started dude.....thanks!


----------



## #/bin/sh (Mar 15, 2005)

No class work


----------



## prasadzmultiplex (Mar 15, 2005)

lamrinnirmal said:
			
		

> heres a very rough algorithm you could use for the way you want to enter the variables
> 
> keep a counter variable to count the no of values entered in the array. also keep two more variables to monitor the odd indices and even indices. and INITIALIZE em!!
> 
> ...



I tried that before....Not working....Here's the code:


```
main()
{
int a[20],i,j,temp;
printf("Enter a Number Please\n");
for(i=0,j=1;i<20,j<20;i=i+2,j=j+2)
{
temp=getch();
if(temp%2=='0')
{
a[i]=temp;
}
else
{
a[j]=temp;
}
```

This way I'm able to fill only a[0] & a[1].Itz not moving ahead....


----------



## prasadzmultiplex (Mar 25, 2005)

Thanks to all who have Helped me in this Endeavour.I am glad to say that
I have almost Finished Learning C.(If Files are the Last part,then I am Finished  )But I've heard that Data Structures & Databases ar an Essential
part of C.Which Books are the best for them? 
Also I have studied Structures in C.Are they Diff. frm Data Structures?


Now that I have completed C,am thinking of moving towards C++.
Regarding the current market Scenario,Which Course would be best
suitable:.Net or Java? All the Experienced Guys Plz Advise.I have a keen
interest in web Designing,so which one would be more suitable?


----------



## hafees (Mar 25, 2005)

Congrats !

Now try these puzzles also. 
*www.thinkdigit.com/forum/viewtopic.php?t=16416&start=0&postdays=0&postorder=asc&highlight=

Structures are the essential building blocks of data structure.
eg:
  struct Stack{
     ....
    ..
  }
  struct LinkedList{
 ...
} etc.
 i recommend these books

i) Data Structures using C & C++ by Tenenbaum and others (published by PHI )
ii) book by Yashwant Kanitkar (similar name)
and this book comes with a nice CD with flash animated illustration.


----------



## tuxfan (Mar 26, 2005)

prasadzmultiplex said:
			
		

> I am glad to say that
> I have almost Finished Learning C.



Are you sure?  C & C++ are too vast to be finished like that. Don't sit on your laurels. There are lots of things that you still wouldn't know. Be hungry for more. Learn this from Sachin Tendulkar. The man has scored tone and tons of runs but is still hungry for more and that is the reason for his success 

Have a look at the link given by hafees. Solve the puzzles and increase your knowledge.


----------



## prasadzmultiplex (Mar 28, 2005)

tuxfan said:
			
		

> prasadzmultiplex said:
> 
> 
> 
> ...



Yeah...I did'n mean that I have Become a Thorough C professional but for a Beginner,
I have learned most of the concepts.i.e. Upto Files & Structures.Other Topics
I shall study as & when Needed.I am still in my intermediate,and would like
to know which topics should be covered further to become a Good Coder.

If Sunny Gavaskar (Hafees) & Kapil Dev (You) will provide me with some 
needful tips,I too shall try 2 bcome Sachin


----------



## theKonqueror (Mar 28, 2005)

For C & CPP, Let us C and Let Us C++ r gr8 books... I scored 98% in BTE by using them...


----------



## hafees (Mar 29, 2005)

> If Sunny Gavaskar (Hafees) & Kapil Dev (You) will provide me with some
> needful tips,I too shall try 2 bcome Sachin


----------



## tuxfan (Mar 29, 2005)

prasadzmultiplex said:
			
		

> If Sunny Gavaskar (Hafees) & Kapil Dev (You) will provide me with some needful tips, I too shall try 2 bcome Sachin



 But we are not yet retired!!  I feel, you must master the pointers and classes if you want to get good with C/C++. Leave pointers aside if Java is what you want to learn finally.


----------



## aadipa (Mar 29, 2005)

pointers pointers and more pointers 

function pointers too...


----------



## vasanth_12345 (Mar 29, 2005)

*Help*

Actually i am a little interested in c programming(know a little).One of my friends did a project by which numslock key would be turned on.now i inferred that he had used pointers to change the valu from either 0 to 1 or  1 to 0(binary).Now where will this value be located in the ram or the keyboards buffer.Further can we access the buffer of a device using c


----------



## prasadzmultiplex (Mar 29, 2005)

tuxfan said:
			
		

> prasadzmultiplex said:
> 
> 
> 
> ...



Sure sure....

On the Puzzles Tread ther was talk abt VDU ,Classes n all Hi-fi Jargon.....
How come I did'nt come across them in C?


----------



## hafees (Mar 29, 2005)

> One of my friends did a project by which numslock key would be turned on.


yes! he should 've used pointers or the library function pokeb();
the memory location 0x417(segment:0000h offset:0417h) stores the value of num lock, caps lock, scroll lock, etc. by changing the appropriate value u can alter the present status of num lock, caps lock etc.
Now i think it can be added in the puzzles thread. some one try it.


----------



## tuxfan (Mar 30, 2005)

*Re: Help*



			
				vasanth_12345 said:
			
		

> Actually i am a little interested in c programming(know a little).One of my friends did a project by which numslock key would be turned on.now i inferred that he had used pointers to change the valu from either 0 to 1 or  1 to 0(binary).Now where will this value be located in the ram or the keyboards buffer.Further can we access the buffer of a device using c



Here's you go  use these functions to manipulate caps lock, num lock, scroll lock & insert state. These functions that I post the code here are part of my function library 

There are some more functions to find out if Alt, Ctrl, Shift, Caps, etc. were pressed. But that code, after a short break 

*CAPS LOCK*

```
int caps(cmd)
/*cmd = 0 turns it off
  cmd = 1 turns it on
  cmd = 2 returns current status*/
int cmd;
{
     int far *capstat = 0x417;
     int state;
     state = *capstat & 64;
     if ( cmd == 2 )   /* just return the current status */
     {
          if ( state == 64 )
               return(1);
          else
               return(0);
     }
     if ( cmd == 0 )  /* if on, turn it off */
     {
          if ( state == 64 )
               *capstat = *capstat - 64;
          return(0);
     }
     if ( cmd == 1 ) /* if off, turn it on */
     {
          if ( state != 64 )
               *capstat = *capstat + 64;
          return(1);
     }
}
```

*NUM LOCK*

```
int num(cmd)
/*cmd = 0 turns it off
  cmd = 1 turns it on
  cmd = 2 returns current status*/
int cmd;
{
     int far *numstat = 0x417;
     int state;
     state = *numstat & 32;
     if ( cmd == 2 )   /* just return the current status */
     {
          if ( state == 32 )
               return(1);
          else
               return(0);
     }
     if ( cmd == 0 )  /* if on, turn it off */
     {
          if ( state == 32 )
               *numstat = *numstat - 32;
          return(0);
     }
     if ( cmd == 1 ) /* if off, turn it on */
     {
          if ( state != 32 )
               *numstat = *numstat + 32;
          return(1);
     }
}
```

*SCROLL LOCK*

```
int scrolock(cmd)
/*cmd = 0 turns it off
  cmd = 1 turns it on
  cmd = 2 returns current status*/
int cmd;
{
     int far *scrolstat = 0x417;
     int state;
     state = *scrolstat & 16;
     if ( cmd == 2 )   /* just return the current status */
     {
          if ( state == 16 )
               return(1);
          else
               return(0);
     }
     if ( cmd == 0 )  /* if on, turn it off */
     {
          if ( state == 16 )
               *scrolstat = *scrolstat - 16;
          return(0);
     }

     if ( cmd == 1 ) /* if off, turn it on */
     {
          if ( state != 16 )
               *scrolstat = *scrolstat + 16;
          return(1);
     }
}
```

*INSERT*

```
int insert(cmd)
/*cmd = 0 turns it off
  cmd = 1 turns it on
  cmd = 2 returns current status*/
int cmd;
{
     int far *insertstat = 0x417;
     int state;
     state = *insertstat & 128;
     if ( cmd == 2 )   /* just return the current status */
     {
          if ( state == 128 )
               return(1);
          else
               return(0);
     }
     if ( cmd == 0 )  /* if on, turn it off */
     {
          if ( state == 128 )
               *insertstat = *insertstat - 128;
          return(0);
     }
     if ( cmd == 1 ) /* if off, turn it on */
     {
          if ( state != 128 )
               *insertstat = *insertstat + 128;
          return(1);
     }
}
```


----------



## tuxfan (Mar 30, 2005)

prasadzmultiplex said:
			
		

> On the Puzzles Tread ther was talk abt VDU, Classes n all Hi-fi Jargon.....
> How come I did'nt come across them in C?



That is why I said, you are still not through with C 

VDU stands for Visual Display Unit. IF you find out which part of the memory stores what is displayed on screen, you can easily manipulate what gets displayed and have a greater control. But you must be comfortable with pointers to do that. So master the pointers 

As for classes, they don't exist in C. At max structures come near it. For example see how you have a user defined variable type. There you will have to use structure and a . operator to access member variables.

You will get loads of classes in C++ and Java and many other programming languages including VB, C#, etc.


----------



## vasanth_12345 (Mar 30, 2005)

coldn't undestand it tuxfan "int far *capstat" far is a variable right.Couldnt uderstand his properly "state = *capstat & 64" what does the state function do.Further "0x417" denotes the ram value right then the keyboards buffer rests in the ram and not in the keyboard right.pls correct me if i am wrong further all the keys can be accessed like this or what.Pls help


----------



## tuxfan (Mar 30, 2005)

vasanth_12345 said:
			
		

> coldn't undestand it tuxfan "int far *capstat" far is a variable right.Couldnt uderstand his properly "state = *capstat & 64" what does the state function do.Further "0x417" denotes the ram value right then the keyboards buffer rests in the ram and not in the keyboard right.



*int far *capstat* creats a far integer pointer variable

*state* is also an integer variable only and not any specific function.

0x417 is the address in the memory that stores the current status of caps, num, insert, etc. Its just one (or two) byte. Compare the functions that I have posted. They are almost same except the *state = *capstat & 64* statement. One has 64, other has 32, another one has 16, etc. So one byte, different bits store these values. 

Keyboard buffer is different. Keyboard buffer stores the keypresses till the OS can handle them. At times what happens is that an application is busy in process A and an impatient user keeps pressing some keys or the other.

That could create unexpected processing to start with the application after that long process A is over. So I also have a function called clearkeybuff()  8)  So I can call this function immediately after process A is over. Nothing great in code. Its pretty common sense with 4-5 lines. Here it is


```
void clrkeybuff()
{
     int istherech = 1, dummy;
     while ( istherech != 0 )
     {
          istherech = bioskey(1);
          if ( istherech != 0 )
               dummy = bioskey(0);
     }
}
```



			
				vasanth_12345 said:
			
		

> pls correct me if i am wrong further all the keys can be accessed like this or what.Pls help


Which other key do you want to access like this? Is it necessary to access any one of them like this? Just think  Caps, Num, Scroll, Shift, Ctrl, Alt, etc. are basically dormant key and doesn't send any report to the application once they are pressed. So to capture their state or to find if they are pressed, you need to write code.


----------



## technovice (Mar 30, 2005)

tuxfan said:
			
		

> Which other key do you want to access like this? Is it necessary to access any one of them like this? Just think  Caps, Num, Scroll, Shift, Ctrl, Alt, etc. are basically dormant key and doesn't send any report to the application once they are pressed. So to capture their state or to find if they are pressed, you need to write code.



Just wanted to know if the escape key can also be detected in a similar manner?

Also how is it possible to have i/p through a mouse in a c program?


----------



## hafees (Mar 31, 2005)

> Just wanted to know if the escape key can also be detected in a similar manner?


ESC key is not a toggle key. that is once u pressed ESC key  it will generate a scan code. think about Caps LOck, it is a toggle key. once u pressed it it becomes on, if u press again it becomes off. But how will u detect the present status of the Caps Lock. that is y their states is stored in RAM. 

to detect whether ESC key is pressed, u only need to check the ascii code.
eg:
main()
{
       char ch;
       ch=getch();
       if(ch==27) printf("Esc Key is pressed");
}


----------



## technovice (Mar 31, 2005)

thanks hafees!
btw do you have the ascii codes for all other keys
say left, right, down, up etc


----------



## tuxfan (Mar 31, 2005)

technovice said:
			
		

> thanks hafees!
> btw do you have the ascii codes for all other keys
> say left, right, down, up etc



Here you go   Save this file in notepad and name it as key_code.h In your code #include the file and you can start using it. Enjoy   As for mouse with C programs, I have another set of .h files and another set of functions.  More on that later.


```
/***********************************************************
*
*   key_code.h
*
*   Header file containing Scan Codes
*
*   (c) Copyright all C programmers, 1994.
*   All Rights Reserved.
*
*   This file released under GNU/GPL on 31st Mar '05
*   File released by tuxfan @ Digit Forums
*
************************************************************/

#define ESC        0x1b
#define ENTER      0xd
#define TAB        0x09
#define SHIFT_TAB  0x0f
#define PGUP       0x49
#define PGDN       0x51
#define UPARROW    0x48
#define DNARROW    0x50
#define LEFTARROW  0x4b
#define RIGHTARROW 0x4d
#define HOME       0x47
#define END        0x4f
#define BACKSPACE  0x08
#define INSERT     0x52
#define DELETE     0x53
#define NUM_PAD_5  0x4c   /*  with Num Lock off  */

#define CTRL_ENTER 0xa
#define CTRL_TAB   0x94
#define CTRL_PGUP  0x84
#define CTRL_PGDN  0x76
#define CTRL_UP    0x8d
#define CTRL_DN    0x91
#define CTRL_RIGHT 0x74
#define CTRL_LEFT  0x73
#define CTRL_HOME  0x77
#define CTRL_END   0x75
#define CTRL_BKSP  0x7f
#define CTRL_INS   0x92
#define CTRL_DEL   0x93

#define ALT_ENTER  0x1c
#define ALT_TAB    0xa5
#define ALT_PGUP   0x99
#define ALT_PGDN   0xa1
#define ALT_UP     0x98
#define ALT_DN     0xa0
#define ALT_LEFT   0x9b
#define ALT_RIGHT  0x9d
#define ALT_HOME   0x97
#define ALT_END    0x9f
#define ALT_BKSP   0xe
#define ALT_INS    0xa2
#define ALT_DEL    0xa3

#define F1         0x3b
#define F2         0x3c
#define F3         0x3d
#define F4         0x3e
#define F5         0x3f
#define F6         0x40
#define F7         0x41
#define F8         0x42
#define F9         0x43
#define F10        0x44
#define F11        0x85
#define F12        0x86

#define SHIFT_F1   0x54
#define SHIFT_F2   0x55
#define SHIFT_F3   0x56
#define SHIFT_F4   0x57
#define SHIFT_F5   0x58
#define SHIFT_F6   0x59
#define SHIFT_F7   0x5a
#define SHIFT_F8   0x5b
#define SHIFT_F9   0x5c
#define SHIFT_F10  0x5d
#define SHIFT_F11  0x87
#define SHIFT_F12  0x88

#define CTRL_F1    0x5e
#define CTRL_F2    0x5f
#define CTRL_F3    0x60
#define CTRL_F4    0x61
#define CTRL_F5    0x62
#define CTRL_F6    0x63
#define CTRL_F7    0x64
#define CTRL_F8    0x65
#define CTRL_F9    0x66
#define CTRL_F10   0x67
#define CTRL_F11   0x89
#define CTRL_F12   0x8a

#define ALT_F1     0x68
#define ALT_F2     0x69
#define ALT_F3     0x6a
#define ALT_F4     0x6b
#define ALT_F5     0x6c
#define ALT_F6     0x6d
#define ALT_F7     0x6e
#define ALT_F8     0x6f
#define ALT_F9     0x70
#define ALT_F10    0x71
#define ALT_F11    0x8b
#define ALT_F12    0x8c

#define ALT_A      0x1e
#define ALT_B      0x30
#define ALT_C      0x2e
#define ALT_D      0x20
#define ALT_E      0x12
#define ALT_F      0x21
#define ALT_G      0x22
#define ALT_H      0x23
#define ALT_I      0x17
#define ALT_J      0x24
#define ALT_K      0x25
#define ALT_L      0x26
#define ALT_M      0x32
#define ALT_N      0x31
#define ALT_O      0x18
#define ALT_P      0x19
#define ALT_Q      0x10
#define ALT_R      0x13
#define ALT_S      0x1f
#define ALT_T      0x14
#define ALT_U      0x16
#define ALT_V      0x2f
#define ALT_W      0x11
#define ALT_X      0x2d
#define ALT_Y      0x15
#define ALT_Z      0x2c

#define CTRL_A     0x1
#define CTRL_B     0x2
#define CTRL_C     0x3
#define CTRL_D     0x4
#define CTRL_E     0x5
#define CTRL_F     0x6
#define CTRL_G     0x7
#define CTRL_H     0x8
#define CTRL_I     0x9
#define CTRL_J     0xa
#define CTRL_K     0xb
#define CTRL_L     0xc
#define CTRL_M     0xd
#define CTRL_N     0xe
#define CTRL_O     0xf
#define CTRL_P     0x10
#define CTRL_Q     0x11
#define CTRL_R     0x12
#define CTRL_S     0x13
#define CTRL_T     0x14
#define CTRL_U     0x15
#define CTRL_V     0x16
#define CTRL_W     0x17
#define CTRL_X     0x18
#define CTRL_Y     0x19
#define CTRL_Z     0x1a

#define PLUS       0x2b
#define MINUS      0x2d

#define TRUE       1
#define FALSE      0
#define AND        &&
#define OR         ||
```


----------



## aadipa (Mar 31, 2005)

For checking key strocks, getch() may not work for all keys. 

getch() returns ASCII code of char you pressed but some keys will only work correctly with key code.

So it is better to handle key codes than ascii codes.


Every key on key board is assigned to different key code.

Also use a separate function to get the status of special keys like LEFT/RIGHT SHIFT/CTRL/ALT keys.

You can get all required intrupts to read scan code and special key status in good old "Let Us C"


----------



## technovice (Mar 31, 2005)

HI tuxfan!!
fantafabulous man!! 
thanks for the header files

only one more thing...
can u please explain how to use it in our functions (an eg would be great!)


----------



## hafees (Mar 31, 2005)

> For checking key strocks, getch() may not work for all keys.
> 
> getch() returns ASCII code of char you pressed but some keys will only work correctly with key code.



in such cases getch() can be used twice to get the scan code. For eg: Arrow keys don't have an ascii code. in that case u can either use bioskey() or getch() twice. eg:

```
//this program prints the corresponding Ascii value or scan code of the key 
//that is being pressed. Press ESC to quit.

#define ESC 27
int main()
{
	char ch;
	do
	{
		ch=getch();
		if(ch==0) //that is no ascii code
		{
			ch=getch();
			printf("\nScan Code is %d",ch);
		}
		else
			printf("\nAscii Code is %d",ch);

	}
	while(ch!=ESC); //loop until ESC key is pressed
}
```


----------



## hafees (Mar 31, 2005)

to use mouse in ur programs u should use interrupts. mouse uses interrupt 0x33.
u can use the function int86() (software interrupt) to invoke an interrupt. This is some what advanced topics in C. 
For supporting mouse in ur programs copy the following code into a file (say, mouse.c) and include it in ur programs. a demonstration of functions is included. **REMEMBER : before including it in ur programs, u need to comment the main() function.


```
//Mouse Functions

#include<dos.h>

#define MOUSE_LEFT   1
#define MOUSE_RIGHT  2
#define MOUSE_BOTH   3

//prototypes

/*int initmouse();
void show_mouseptr();
void mouse_window(int x1,int y1,int x2,int y2);
void hide_mouse();
void mouse_status(int *x_ptr,int * y_ptr,int * button);
void set_mousecursor_to(int x,int y);
void button_press(int button,int *status,int *presses,int *x,int *y);
void button_release(int button,int *status,int *presses,int *x,int *y);
*/

int initmouse() //use this function to initialize mouse
{
	union REGS in,out;
	in.x.ax=0;
	int86(0x33,&in,&out);
	return out.x.ax;
}

void show_mouseptr() //this will show the mouse pointer
{
	union REGS in,out;
	in.x.ax=1;
	int86(0x33,&in,&out);
}

//this will define the window (don't need to use unless u need to restrict 
//the mouse pointer to some area

void mouse_window(int x1,int y1,int x2,int y2)
{
	union REGS in,out;
	in.x.ax=0x07;
	in.x.cx=x1;
	in.x.dx=x2;
	int86(0x33,&in,&out);
	in.x.ax=0x08;
	in.x.cx=y1;
	in.x.dx=y2;
	int86(0x33,&in,&out);
}

//use this to hide mouse
//before drawing or changing screen contents use this function to hide
//mouse pointer, and then show it using show_mouseptr()

void hide_mouse()
{
	union REGS in,out;
	in.x.ax=0x2;
	int86(0x33,&in,&out);
}


//this will show the mouse cursor in (x,y) position
void set_mousecursor_to(int x,int y)
{
	union REGS in,out;
	in.x.ax=0x4;
	in.x.cx=y;
	in.x.dx=x;
	int86(0x33,&in,&out);
}

//get the current mouse status
//including button information and screen location

void mouse_status(int *x_ptr,int *y_ptr,int *button)
{
	union REGS in,out;
	in.x.ax=0x3;
	int86(0x33,&in,&out);
	*x_ptr=out.x.cx/8;
	*y_ptr=out.x.dx/8;
	*button=out.x.bx;

}



//This is a demo. To include mouse functions in ur programs comment the main()
//function, and include this file eg: #include "mouse.c"

int main()
{
	int button,presses,status,x,y,flag=0;
	initmouse();
	clrscr();
	//show mouse pointer;
         show_mouseptr();
	while(1) 
	{
		
		if(bioskey(1)) //If any key is pressed
		{
			char ch=getch();
			if(ch==27) break;  //if itis the esc key then quit
		}

		mouse_status(&x,&y,&button);
		if(button & MOUSE_LEFT) //if Left button is down
		{
			if(!flag) printf("\n Mouse down at (%d,%d)",x,y);
			flag=1;

		}
		else
		{
			if(flag)  //or if (flag==1)
			{
				flag=0;
				printf("\n Mouse Up at (%d,%d)",x,y);
			}
		}
	}
}
```


----------



## aadipa (Apr 1, 2005)

You can also change  the mouse pointer in C graphics.


```
union  REGS i,o;
struct SREGS s;

// Some mouse pointer shapes

int cursor[][32]={
  /*cursor1*/
  0x0010,0x0010,0x0010,0x0010,
  0x0010,0x0010,0x0010,0x0010,
  0x0010,0x0010,0x0010,0x0010,
  0x0010,0x0010,0x0010,0x0010,
  0x0010,0x0010,0x0010,0x0010,
  0x0010,0x0010,0x0010,0x0010,
  0x0010,0x0010,0x0010,0x0010,
  0x0010,0x0010,0x0010,0x0010,
  /*cursor2*/
  0xff00,0xff00,0xff00,0xff00,
  0x1234,0x1234,0x1234,0x1234,
  0x0000,0x0010,0x0010,0x0000,
  0x0000,0x0010,0x0010,0x0000,
  0x0010,0x0000,0x0000,0x0000,
  0x1234,0x0034,0x0034,0x1234,
  0x5678,0x5789,0x5789,0x5678,
  0x99ff,0x9a0f,0x9a0f,0x99ff,
  /*cursor3*/
  0x0000,0x0000,0x0000,0x0000,
  0x0000,0x0000,0x0000,0x0000,
  0x0000,0x0000,0x0000,0x0000,
  0x0000,0x0000,0x0000,0x0000,
  0xffff,0xffff,0xffff,0x0007,
  0x0007,0xeee7,0x0007,0x0007,
  0xeee7,0x0007,0x0007,0xeee7,
  0x0007,0x0007,0xeee7,0xeee7,
  /*cursor4*/
  0x3fff,0x3fff,0xdfff,0xdaff,
  0xe5ff,0xfdff,0xf07f,0xeabf,
  0xe5ff,0xedbf,0xf231,0x1aa0,
  0xf064,0xc000,0xffe0,0xffe4,
  0xc000,0xc000,0x2000,0x2400,
  0x1a00,0x0200,0x0f80,0x1540,
  0x1dc0,0x1240,0x1dce,0x155f,
  0x0f9b,0x001f,0x001f,0x001b

  };

void changecursor(int *shape)
{ i.x.ax = 9;
  i.x.bx = 0;
  i.x.cx = 0;
  i.x.dx = (unsigned)shape;
  segread(&s);
  s.es = s.ds;
  int86x(0x33,&i,&i,&s);
  return;
  }
```

You can call it as 

```
changecursor(cursor[i]);   // cursor[i]
```


Also the function to read scan codes


```
/*returns scan code of the key that has been hit*/
//Up : 72 Down : 80 Left : 75 Right : 77

int getkey()
{ while(!kbhit()) delay(25);
  i.h.ah=0x00;
  int86(0x16,&i,&o);
  return(o.h.ah);
  // ah : scan code
  // al : ascii code
  }

// This will return the scan code without waiting
int xgetkey()
{ while(!kbhit()) delay(25);
  i.h.ah=0x00;
  int86(0x16,&i,&o);
  return(o.h.al);
  // ah : scan code
  // al : ascii code
  }

// Get special key status

unsigned char getShiftStatus() {
   i.h.ah=0x02;
   int86(0x16,&i,&o);
   return(o.h.al);
   // Bit No : Status
   // 0	     : Right shift depressed
   // 1      : Left shift depressed
   // 2      : Ctrl depressed
   // 3      : Alt depressed
   // 4      : Scroll lock on
   // 5      : Num lock on
   // 6      : Caps lock on
   // 7      : Insert on
   }
```


----------



## technovice (Apr 1, 2005)

boy oh boy!!!
these are some really bhaari codes 
thanks for these tuxfan, aadipa, and hafees
you guys must be hardcore programmers?
great job


----------



## tuxfan (Apr 1, 2005)

technovice: Here's your chance to boost your c functions collection. Mind you, all these functions seem to be general purpose ones so can be used and reused and resued and ....  At lease ones that I post are general purpose and I have used them many times over in different programs 



			
				technovice said:
			
		

> can u please explain how to use it in our functions (an eg would be great!)


I think before I could come here, examples have already flown in  Check out hafees's post.


----------



## Sourabh (Apr 1, 2005)

lol i even found the C in my first sem Engg difficult for starters

now its C++, i find it a lil easier dunno y


----------



## tuxfan (Apr 2, 2005)

BTW, I must mention that a CASE statement will be more useful instead of repetitive if(). Check out the syntax of CASE.



			
				Sourabh said:
			
		

> lol i even found the C in my first sem Engg difficult for starters
> 
> now its C++, i find it a lil easier dunno y



Thats great!! As for OOPS, it is more of a thinking pattern than anything else. If you can think in objects, you can take to OOPS like fish to water. You seem to be doing that


----------



## hafees (Apr 3, 2005)

yes tuxfan! 
 thinking in terms of objects is more natural, once u got the concept.


----------



## prasadzmultiplex (Apr 7, 2005)

I Got a Doubt....LEarning C++ really Essential to learn JAVA?

Any Nice Tutorials/ebooks for C++??


----------



## aadipa (Apr 8, 2005)

No way..... 

Infact u will need to unlearn some basics of C++ to learn Java. (Like, Java don't support multiple inheritance directly which is used in almost every big program in C++)

I learned C, mastered it, then moved on to learn Java, and finally C++.

Now I am working in Java. So I know all these langueges.

Make sure about C as it is the basic in terms of logic development and syntax for Java and C++.

About books on C++ and Java
*mindview.net/Books

Even some recent Digit had these e-books.


----------

