Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

c programming 1

Status
Not open for further replies.

varalamahesh

Programmer
Jul 7, 2012
20
0
0
exit*(;
should have a prototype?
please provide this...
 
exit();
should have a prototype>
provide this...?
i am kid in c programming
 
There is the exit(int) function in C library (prototype in <stdlib.h> header);
void exit(int);
Using:
Code:
#include <stdlib.h>
...
exit(0); /* normal exit */
/* or */
exit(status_code); /* int status_code != 0 - abnormal termination. */
 
Thanks ArkM.....!
/* Excution a loop for unknow number of times */
#include<stdio.h>
void main()
{
char another;
int num;
do
{
printf("\n Enter a number:");
scanf("%d",&num);
printf("\nSquaaare of %D is %d",num,num*num);
printf("\n wanr to enter another Number y/n");
scanf("%c",&another);
}while(another=='y');
}
what is fault of this programme...?
 
also provide below progrmmes....!
thank you.....1
/* Odd loop using a while loop */
#include<stdio.h>
void main()
{
char another='y';
int num;
while(another=='y')
{
printf("\n Enter a Numer ");
scanf("%d",&num);
printf("\n Square of %d is %d.",num,num*num);
printf("\n Want to enter anothe Numbery/n");
scanf("%d",&another);
}
}
 
/* odd loop using a for loo[ */
#include<stdio.h>
void main()
{
char another='y';
int num;
for(;another=='y';)
{
printf("\n Enter a NUmber ");
scanf("%d",&num);
printf("\nSquare of %d is %d",num,num*num);
printf("\nwant to enter another number u/n");
scanf("$d",&another);
}
}
what is fault of this programme?
 
What a conincidence. :) I'm new to C myself and my training program used to produce a strange error the whole time when I finished it although it seamed to run properly. An hour ago I realized it was, because I named one of my global variables "exit". Should have checked here earlier. ;)





About the programs:
/* Excution a loop for unknow number of times */
...
printf("\n wanr to enter another Number y/n");
scanf("%c",&another);
}while(another=='y');
}​

This should definitly be:
...
printf("\n want to enter another Number y/n");
scanf("%c",&another);
}
while(another=='y')
{
main()
}​

also, when scanning for a character you should use
...
printf("\n Want to enter anothe Number? y/n ");
scanf("%s", &another);
...​
Notice the %s you need to use, not %c or %d, which indicate (*checking this*) vhar and int Data types.
 
I am learning C Language My self....>

Look below proogramme....!


#include<stdio.h>
void main()
{
int goals;
printf("\n Enter no of goals scored against india:");
scanf("%d",&goals);
if(goals<=5)
goto sos;
else
{ printf("Abount time soccer plyerslearnt C.\n");
printf("\n amd said goodbe! adieu! to soccer");
exit(); /*terminate programme excution*/
}

sos:
printf("\n to err is human!");
}

A error message occurred when compiling the programme.....!
exit :should have a prototype...!
 
Well, did you ever read my post above?..
 
same p[roblem....!
exit(0);
i am using borland c++..
Windows XP sp2 32 bit
pentiuim4 processor

#include<stdio.h>
void main()
{
int goals;
printf("\n Enter no of goals scored against india:");
scanf("%d",&goals);
if(goals<=5)
goto sos;
else
{ printf("Abount time soccer plyerslearnt C.\n");
printf("\n amd said goodbe! adieu! to soccer");
exit(0); /*terminate programme excution*/
}

sos:
printf("\n to err is human!");
}
 
same problem.....!
my sys config:
Windows XP SP2
32-bit Pentium $ rocessor



#include<stdio.h>
void main()
{
int goals;
printf("\n Enter no of goals scored against india:");
scanf("%d",&goals);
if(goals<=5)
goto sos;
else
{ printf("Abount time soccer plyerslearnt C.\n");
printf("\n amd said goodbe! adieu! to soccer");
exit(0); /*terminate programme excution*/
}

sos:
printf("\n to err is human!");
}


 
Please don't use a "[tt]goto[/tt]". It should be avoided as it only promotes bad programming. I've NEVER had to use a [tt]goto[/tt] in 30 years of C programming.

Also, ArkM told you what to do and you still haven't tried it.

Try this code. It includes the missing header file and has no gotos. And the spelling errors have been corrected.

Code:
#include <stdio.h>
#include <stdlib.h> /* INCLUDE THIS HEADER FILE */

int main()
{
int goals;

printf("\nEnter no of goals scored against india: ");
scanf("%d",&goals);

printf("\nYou entered %d\n\n", goals);

if (goals <= 5)
    {
    printf("to err is human!\n\n");
    exit(1);
    }

printf("About time soccer players learned C.\n");
printf("and said goodbye! adieu! to soccer\n\n");

exit(0); /*terminate programme excution*/
}

Hope this helps.


 
NEVER call [tt]main()[/tt] in your program. This should NEVER be done...

Code:
while(another=='y')
{
main()
}

Yes, it may work for a little test program, but anything bigger that needs to iterate a lot of times, you will eat up your stack and crash.

If you need to loop, use a real [tt]for[/tt] or [tt]while[/tt] loop.

 
Yet another good advice:

The scanf function returns the number of input items successfully assigned or other value on error(s).

For example, if you get not-a-number from the console (to err is human, right;), scanf returns 0 and goals var is in undefined state (so the program prints and tests undefined value later). Always test human input result:
Code:
int nain(void) /* that's (one of) correct main in C */
{
...
    if (scanf("%d",&goals) != 1) {
        printf("Sorry, a number expected...\n");
        return 1;
    }
...
}
Pay attention to the return statement in the snippet above. No need in exit function call inside the main function body! Return value from main has exactly the same effect as exit(value) call...
 
Sorry, int main(void) in the snippet above - to err is human...
 
Sam: Of course, You are right about calling main(), I have no idea what I though. I even used it in a while loop, where an "if" would have been enough ... And a while loop inside main(), around the whole stuff would have been best idea.



Although I disagree with the goto. Well, I never felt like I needed it during my ~4 years of amateur Java programming either [wink] but then again, now that I had it available, I used it last week in a case like:

label:
variables;
input;
switch(condition) {
case 1 : Output1; break;
case 2 : Output2; break;
case 3 : Output3; goto label;
case 4 : Output4; break;
}

Imho this is far more intuitive then ... well, I think the fastest way without goto would be a while loop with another variable loop=true that is set to false by anything but case 3, or maybe a do ... while loop, initialising the loop variable with false and only setting it to true in case 3. However I think neither looks as elegant and intuitive as the simple goto use above.




About the latest programm: What use is exit(int) anyway? Why not simply let the program run out on it's own:

...
printf("\nYou entered %d\n\n", goals);
if (goals <= 5)
printf("to err is human!\n\n");
else {
printf("About time soccer players learned C.\n");
printf("and said goodbye! adieu! to soccer\n\n");
}
}
 
No need in special variables and gotos.
Code:
for(;;) { // We can see the loop started here...
    input;
    switch (...) {
    case 1: Output1; break;
    case 2: Output2; break;
    case 3: Output3; continue; // We can see the next round...
    case 4: Output4; break;
    default: ????... // Never use switch w/o default label...
    }
    break;
}
In case of "label:code" we don't know why this point was marked. Later in "goto label" we must find the label again then think about implicit loop. Is such obscure code looks so elegant?..

About exit(status) or (better) return status. The code w/o these constructs does not send meaningful termination status to OS. However goals < 5 obviously is abnormal termination condition. So it's good practice to return zero code on success and non-zero one on failure.
 
Thanks ArkM ,Sam and Durin.....!
Lokk below code...!


#include<stdio.h>
#include<conio.h>
#define HLINE for(i=0;i<79;i++) printf("\n%c",196);
#define VLINE { gotoxy(X,Y); printf("%C",179); }
void main()
{
int y;
clrscr();
gotoxy(1,12); /*position cursor in row x and Column y*/
HLINE
for(y=1;y<25;y++)
printf("$c",y);
VLINE(39,y);
}
/*This progrmam draws vertical and horizontal line in the
center of the screen */
/* Chapter The C Preprocessor*/
Errors
Undefined Symbol 'i','X','Y',
Have any Idea...?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top