×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

c programming

c programming

c programming

(OP)
exit*(;
should have a prototype?
please provide this...

RE: c programming

(OP)
exit();
should have a prototype>
provide this...?
i am kid in c programming

RE: 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. */ 

RE: c programming

(OP)
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...?

RE: c programming

(OP)
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);
}
}

RE: c programming

(OP)
/* 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?

RE: c programming

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.

RE: c programming

(OP)
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...!

RE: c programming

Well, did you ever read my post above?..

RE: c programming

(OP)
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!");
}

RE: c programming

(OP)
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!");
}


RE: c programming

Please don't use a "goto". It should be avoided as it only promotes bad programming. I've NEVER had to use a goto 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.


RE: c programming

NEVER call main() 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 for or while loop.

RE: c programming

(OP)
Thanks Sam.......!

RE: c programming

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...

RE: c programming

Sorry, int main(void) in the snippet above - to err is human...

RE: c programming

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");
}
}

RE: c programming

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.

RE: c programming

Listen to ArkM, he knows what he's talking about! bigsmile


RE: c programming

(OP)
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...?

RE: c programming

(OP)
i am doing some mistake's in the programme


"%C" "$c"
here i have a little doubt.....
Data Type Format
Signed char %c
unsigned char %c
is right?
i miss some printing in The book...;!

RE: c programming

(OP)
i am doing some mistake's in the programme


"%C" "$c"
here i have a little doubt.....
Data Type Format
Signed char - %c
unsigned char -%c
is right?
i miss some printing in The book...!

RE: c programming

(OP)
#include<stdio.h>
#include<conio.h>
#define HLINE for(i=0;i<78;i++)\
printf("%c",196);

#define VLINE {\
gotoxy(X,Y);\
printf("%c",179);\
}
void main()
{
int i,y,X,Y;
clrscr();
gotoxy(1,12); /*position cursor in row x and column y */
HLINE
for(y=1;y<25;y++)
VLINE(39,y);
}
this programme is Right...?
i feel something Wrong///
Why we defined "X" , "Y"?
look below progrrame ..
#include<stdio.h>
#define AREA(x) (3.14*x*x)
void main()
{
float r1=12.5,r2=4.6,a;
a=AREA(r1);
printf("\nArea of circle is %f",a);
a=AREA(r2);
printf("\nA\rea of cidrcle two is %f",a);
we can't deffined "x".
but we get results...

RE: c programming

(OP)
?

RE: c programming

1. Reread your C text-book (C preprocessor, macros).
2. Look at VLINE macros body: there are X and Y variables in generated codes.
3. x is AREA macros parameter name, not a variable name (see #1;).

Please, next time use code tag for your snippets.

RE: c programming

(OP)
thanks ArkM...!

RE: c programming

(OP)
Function "show" should have a prototype?
give me an example..?

RE: c programming

>give me an example...
Nobody (except you) knows what's "function show" in unknown code.
(YOU give us an example ;)
It's impossible to write a prototype of unknown function.

RE: c programming

(OP)
#include<stdio.h>
void disp(int *);
void main()
{
int i;
int marks[]={556,65,75,56,78,78,90};
for(i=0;i<=6;i++)
disp(&marks[i]);
}
void disp(int *m)
{
show(&m);
}

RE: c programming

>You are require to write the function show(); on your own. try your hand at it

Are you waiting for anybody who can write the function show with void show(int*) signature?

It's a wrong way to study the C programming language.

See (click) help icon on the reply panel below. Use TGML code tag for your snippets!

RE: c programming

(OP)
tell me some best c and c++ books....

RE: c programming

B.Kernighan & D.Ritchie The C Programming Language.
Wait a bit, the C++ language is too complex for you now.

Read books, write (and run) programs. Don't waste time on forums asking very basic questions.
Your best friends: good books and good compiler (all except your ancient Turbo C;).

Good luck!

RE: c programming

(OP)
good compiler>>>
i am using borland c++ compiler
and sys config win XP SP 2,512MB RAM
Microsoft Win XP dos version 5.1.2600
Tell me suitable compilers and also downloading links,,
Thank you Very Much..!
Reading Book:
Let US C-8th edition
written by YASHVANTH KANETKAR
I AM Collecting W.Kernighan & D.Ritchie The C Programming Language.

RE: c programming

Forget TC/BC clrscr and gotoxy as soon as possible.

Download and install MS Visual C++ 2010 Express Edition.
Regrettably, it does not implement full C Standard (but it has the best C/C++ debugger in the World;). Search Google for download link.

Later look at Ultimate++ TheIDE if you want to study portable GUI programming.
This excellent package works fine with VC++.

Of course, you may use Windows (only;) Forms in VC++ EE for GUI.

RE: c programming

I've found a good platform for learning C is to take an old PC and download and install Linux. You'll get a full development environment of GNU C, and can move up to C++ when the time is right.

Even if you don't have a spare PC, most Linux distros will now carve a partition out of your Windows installation and set up a multi-boot option.

Ubuntu is a great choice for simplicity. Centos is a good choice if you want to learn RedHat skills (very marketable).

Hope this helps.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close