Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Linux by (18.4k points)

I just want to use the crypt() to generate the encrypted password,and I write the demo which invoke a crypt() method. Here is my code

#include <unistd.h>

#include <stdio.h>

#include <stdlib.h>

int main()

{

    printf("%s\n",crypt("abc","ab"));

    exit(0);

}

I compile it using the "gcc tem.c -lcrypt' and when I run it, everything seems right, but I receive the "segment error" that shows up. Kindly tell me what is wrong with the simple program?

1 Answer

0 votes
by (36.8k points)

If you compile with a flag -Wall you will see why.

Read manual page you will see that it uses #define _XOPEN_SOURCE before including <unistd.h>. It has to be defined before including any header.

If you don't define the _XOPEN_SOURCE then this crypt function will not be prototyped. Then this compiler doesn't know what this actual return type is, or types and number of arguments. So it will assume that a function returns an int and your printf expects a string, so there will be a type mismatch that causes the crash.

Want to be a Linux expert? Come and join this Linux course

Related questions

0 votes
1 answer
asked Dec 17, 2020 in Linux by blackindya (18.4k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...