struct person
        char name[10];
        int rno;
typedef struct person NAME;
NAME  stud[10], temp[10];

void main()
     int no,i;

     void sort(int N);  

     printf("Enter the number of students in the list\n");

     for(i = 0; i < no; i++)
         printf("\nEnter the name of  person %d : ", i+1);

         printf("Enter the roll number of %d : ", i+1);
         temp[i] = stud[i];

     printf ("     Names before sorting     \n");
     /* Print the list of names before sorting */


     printf ("     Names after sorting     \n");
     /* Display the sorted names */


void sort(int N)
         int i,j;
         NAME temp;

         for(i = 0; i < N-1;i++)
                for(j = i+1; j < N; j++)
                    if(strcmp(stud[i].name,stud[j].name) > 0 )
                        temp    = stud[i];
                        stud[i] = stud[j];
                        stud[j] = temp;

Edited by deceptikon: Moved to C forum

5 Years
Discussion Span
Last Post by abrarsyed

You didn't ask a question, so I'm assuming you want feedback.

  • void main() is a non-standard extension to the C language. Prefer int main(void) (or int main(int argc, char *argv[]) when you want to use command line arguments).
  • <conio.h> is also non-standard and also very very old. Don't use it or any of the functions it provides. (Clearing the screen when you have not been explicitly asked to do so is annoying to the user.)
  • fflush(stdin) invokes undefined behavior because fflush() should only be used on output streams. If you want to discard input (which is usually a bad idea anyway) then you must write your own code to do so.

  • <conio.h> is also non-standard and also very very old. Don't use it or any of the functions it provides. (Clearing the screen when you have not been explicitly asked to do so is annoying to the user.)

  • Your program will have undefined behavior and may fail when the user inputs N > 10. You need to ensure that all inputs are within the ranges your program can handle.

  • Never ever use gets(). It can be made to overflow the array it's given very easily, which causes undefined behavior. Use fgets() instead and pass in the size of the array.

You should avoid relying on non-standard features because they don't work for everyone. I didn't try to compile and run your program because I knew as soon as I saw #include <conio.h> that it wouldn't work for me as it does for you. If you want help on forums like this one, do your best to write code that compiles for everyone.

Undefined behavior (UB) is even worse because a compiler (or your program) has free rein to do whatever it wants when it encounters UB. Hypothetically this could mean anything from wiping your hard drive to causing demons to fly out of your nose. In practice, code that relies on undefined behavior tends to exhibit more subtle bugs. Since some of these bugs can be exploited by malware, and bugs caused by UB can be maddeningly difficult to track down, you should avoid relying on it even when it seems to work at first glance.

If you're learning C from a book, get rid of it -- stuff like this is a sure sign of incompetence. Cprogramming.com has an... acceptable tutorial if you don't want to spend money on a reliable book; if you have money to spend, I always recommend The C Programming Language.

And a few more stylistic points:

  • typedef struct person NAME; is unhelpful because referring to a struct by a typedef when you intend to access its members can be confusing. In fact, I generally advise (beginners) against using typedef because it is more often used to hide information than to add it. Just use struct person and call a spade a spade.
  • Your array stud would be better if it were defined locally within main(). Then if you change the sort() function to take the array as an additional argument, it makes sort() more useful and more general.
  • While you're at it, the array temp doesn't seem to have a purpose since you only use its values when it contains the same thing as stud. Maybe get rid of it altogether?
  • Personally, I'd rename stud to students and no to size. Descriptive variable names are important to making your code as readable as possible.

Edited by Trentacle: typo


There are many ways to do this. What about the idea to use linked list and insert new names in correct place every time it is entered? Or even better would be binary tree. Or use some array of pointers to swap pointer values instead of struct values you can do it in this program too no?

Edited by Sokurenko: more


Here's a quick example of the sorting part, done with gnome sort (stupid sort).

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 5

void GnomeSort(char* p[SIZE]){
    int i=0;
    char* j;
       if (i==0||strcmp(p[i-1],p[i])==-1||strcmp(p[i-1], p[i])==0)
    for (i=0;i<SIZE;i++)
        printf("%s\n", p[i]);

int main(){
    char* p[SIZE]={"Bob", "Joe", "Andy", "Joseph", "Josephine"};
    return (0);


Not good if you want to apply it to a large amount of data. It's complexity in WC and AC is O(n^2) and in the best case is O(n), so it's really slow, but it's an example of how you should think your sorting program.

Edited by Lucaci Andrew

Votes + Comments
nice gnome

he uses bubble sort oh no so does it work ? what will happen if he try to sort sorted ? worst case scenario yes ? need some flag atleast if nothing sorted in second cycle then no need to continue

Edited by Sokurenko: more


Firdous you should look for some better sorting algorithms or atleast make this sorting technique more efficient.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.