Calling a function in C
Here is my code
#include<stdio.h>
orders_char(int c1, int c2, int c3);
void orders_char(int c1, int c2, int c3)
{
if(c1 < c2)
if(c2 < c3)
printf("Ordered characters are: %c %c %c", c1, c2, c3);
else if(c3 < c2 &开发者_Go百科& c1 < c3)
printf("Ordered characters are: %c %c %c", c1, c3, c2);
else if(c2 < c1
if(c1 < c3)
printf("Ordered characters are: %c %c %c", c2, c1, c3);
else if(c3 < c1 && c3 < c2)
printf("Ordered characters are: %c %c %c", c3, c2, c1);
else if(c1 > c3)
if (c3 < c2 && c2 > c1)
printf("Ordered characters are: %c %c %c", c3, c1, c2);
else if(c3 > c2 && c2 < c1)
printf("Ordered characters are: %c %c %c", c2, c3, c1);
return;
}
int main(void)
{
char c1, c2 ,c3;
int i = 65;
printf("Please enter 3 capital letters with no spaces: \n");
scanf("%c%c%c", &c1, &c2, &c3);
orders_char(c1, c2, c3);
return 0;
}
But I get the errors:
8.7.c:3: warning: data definition has no type or storage class
8.7.c:6: error: conflicting types for 'orders_char'
8.7.c:3: error: previous declaration of 'orders_char' was here
8.7.c:6: error: conflicting types for 'orders_char'
8.7.c:3: error: previous declaration of 'orders_char' was here
8.7.c: In function `orders_char':
8.7.c:14: error: syntax error before "if"
You really should use { }
for your if
expressions. I run your code through formatter and it clearly shows that the if
and else if
don't work the way you want them to work.
EDIT:
- you have to much cases - there are only six possible sort-orders for the three variables
- you use int for char, while that works it's not nice - better use
char
for chars.
void orders_char(int c1, int c2, int c3);
Because without anything, it defaults to int.
See you, Beco
Edited:
BTW, you need a brace in line
else if(c2 < c1
Also, you are not matching if's-else's. The else will always match with the near if. So:
if(a>b)
if(b>c)
printf("a>b>c");
else
printf("a<=b"); // <<== wrong! This else matches with if(b>c)
You need to put some brackets:
if(a>b)
{
if(b>c)
printf("a>b>c");
}
else
printf("a<=b"); // <<== right! This else matches with if(a>b)
Take care.
Two errors I can see:
One:
orders_char(int c1, int c2, int c3);
should be
void orders_char(int c1, int c2, int c3);
If you don't specify a function's return type, the compiler assumes int
. Therefore it complains later when it sees the function returning void
that you can't make up your mind.
Two:
else if(c2 < c1
missing closing paren.
An else always binds to the closest preceding unmatched if. Indenting doesn't show what is actually happening here.
if (condition)
if (condition2)
Something;
else if (condition3)
SomethingElse;
else if (condition4)
DoMore;
is what actually happens in the code. To work as you seem to want, you will have to write it like this:
if (condition)
{
if (condition2)
Something;
else if (condition3)
SomethingElse;
}
else if (condition4)
DoMore;
Right at the top:
orders_char(int c1, int c2, int c3);
void orders_char(int c1, int c2, int c3)
{ //...
you first declare and then define a function.
But your declaration lacks a return type (the first warning) and the compiler will default to returning int
, and as such is different from the definition (the first error).
Generally, c compilers will then spew out a cascade of error messages, so you should look only at the first one (or perhaps two) when reading the error messages.
Ofcourse, it won't return anything.
You aren't fulfilling the first if condition:
if (c1 < c2) {
//all your code is here
}
C uses ASCII values when comparing characters. A decent ASCII chart is here:
http://www.elec-intro.com/logic-ascii
The values for S A R are 83, 65, 82. So your first if is basically:
if (83 < 65)
Which is obviously false. So - none of your printfs get hit and nothing is output. The fastest way to sort the numbers would be to do a series of swaps:
char temp = c1;
if(c1 > c2)
{
temp = c1;
c1 = c2;
c2 = temp;
}
if(c2 > c3)
{
temp = c2;
c2 = c3;
c3 = temp;
}
if(c1 > c3)
{
temp = c1;
c1 = c3;
c3 = temp;
}
Edit: I should say that this is probably the most straightforward way that uses no built-ins or 3rd party tools. There are obviously faster sorting algorithms than this.
You don't need the orders_char on the third line. It's not an appropriate prototype (as the return type is inferred as int) and you have the full definition of the function before main() anyway.
No offense, but it's basic debugging skills. First check that the characters are coming in as you expect them to, perhaps with a printf() before the call to orders_char(). Second, you should use std::sort() for what you're trying to do.
std::vector vC; vC.push_back(c1); vC.push_back(c2); vC.push_back(c3); std::sort(vC.begin(),vc.end());
Or something similar. You can search for std::sort() in your favorite browser.
Good luck.
Much simpler implementation:
#include <stdio.h>
#include <stdlib.h>
void orders_char (int c1, int c2, int c3);
int compare (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}
void orders_char (int c1, int c2, int c3)
{
int a[] = {c1, c2, c3};
qsort(a, 3, sizeof(int), compare);
printf("Ordered characters are: %c %c %c\n", a[0], a[1], a[2]);
}
int main (void)
{
char c1, c2, c3;
int i = 65;
printf("Please enter 3 capital letters with no spaces: \n");
scanf("%c%c%c", &c1, &c2, &c3);
orders_char(c1, c2, c3);
return 0;
}
Generally, it makes no sense to try and rebuild existing algorithms. Better to learn which ones to use right off the bat.
Of course, there are better ways to implement the above, but this is super simple and quick.
That seems to be rather complicated code for finding the sequence when you can simply unroll a bubble sort (and, before anyone attacks the use of the bubble sort, you should be aware that it's perfectly acceptable in certain cases, such as where the data is already mostly sorted, or where the data set is small - in those cases, it can very often outperform other sorts):
#include <stdio.h>
void orders_char(int c1, int c2, int c3) {
int tmp;
// Unrolled bubble sort for three elements.
if (c1 > c2) { tmp = c1; c1 = c2; c2 = tmp; }
if (c2 > c3) { tmp = c2; c2 = c3; c3 = tmp; }
if (c1 > c2) { tmp = c1; c1 = c2; c2 = tmp; }
printf("Ordered characters are: %c %c %c\n", c1, c2, c3);
}
int main (void) {
char c1, c2 ,c3;
printf("Please enter 3 capital letters with no spaces: \n");
scanf("%c%c%c", &c1, &c2, &c3);
orders_char(c1, c2, c3);
return 0;
}
The function above simply moves the elements around to the correct order with the judicious use of three compares and zero to three swaps, then prints them out. You can swap them around within the function since they're only local variables in there and won't affect what was passed in from the caller.
If you comment out the prompt line (the printf
in the main()
function), and execute the following script, you can see that it works for all inputs:
pax> for i in abc acb bac bca cab cba ; do
...> echo $i | ./sort3
...> done
Ordered characters are: a b c
Ordered characters are: a b c
Ordered characters are: a b c
Ordered characters are: a b c
Ordered characters are: a b c
Ordered characters are: a b c
精彩评论