Ok. Here is a quick review of your code. Comments embedded.
#include<stdio.h>
Leave a space between #include
and <stdio.h>
.
#include<conio.h>
This is a non-standard Windows-only header that you don't need. Don't include this.
#include<math.h>
#include<string.h>
Again use a space, when including your headers.
void main(void)
While this is legal, it is more common to find the signature int main(int argc, char* argv[])
as the signature for the main function. I would suggest that you use that signature.
{
static int sum;
Why are you making this static? Are you planning to invoke main repeatedly and have the previous result for sum
persist from one invocation of main to another? If not, then don't make it static.
int i,x,y,z;
char string[10];
Consider allocating more space for your string. Ten characters is quite small. Also consider creating a variable to represent the size of your string, rather than using a magic number, since you will likely have to reference the buffer size in multiple places.
printf("Enter a string:\n");
gets(string);
No. Don't do that!!! The function gets is a major security vulnerability!. It makes your program susceptible to buffer overflow attacks. Instead, use fgets, and specify the size of the buffer that you want to fill, so that it doesn't overrun your buffer. You should never, ever use plain gets.
x=strlen(string);
Consider choosing a more descriptive name for x. Perhaps len
. It is perfectly ok (and good) to create variables that have identifiers longer than a single letter.
for(i=0; ;i++)
{
if(string[i]=='\0')
{
break;
}
Consider putting the termination condition in the for-loop; for(i = 0; string[i]!='\0'; i++)
.
y=pow(10,i);
z=string[x-i+1]*y;
Hint: there is a smarter way to do this than using pow.
sum+=z;
}
printf("%d",sum);
Ok. The above is fine, although you might want to use "%d\n".
getch();
You really shouldn't be doing this on all systems. Instead, do:
#ifdef _WIN32
system("pause");
#endif
If possible, though, I would suggest you avoid that weird pausing behavior. Suppose your professor uses an automated script to validate the output of your program. Putting any sort of pause in the program (even on Windows), will break such a script. If you don't want the terminal window to disappear while on Windows, you should invoke your program from the command prompt.
}
If you were to change the signature to something returning int as I suggested, then you would want to add the statement return 0;
before the end of the function.