Scroll to navigation

strcmp(3) Library Functions Manual strcmp(3)

الاسم

strcmp - مقارنة السلاسل

المكتبة

مكتبة سي المعيارية (libc، -lc)

موجز

#include <string.h>
int strcmp(const char *s1, const char *s2);

الوصف

تقارن الدالة strcmp() سلسلتي المحارف s1 و s2، كما لو كانت تستدعي memcmp(3).

يكافئ


memcmp(s1, s2, MIN(strlen(s1),strlen(s2))+1)

قيمة الإرجاع

انظر memcmp(3).

السمات

للاطلاع على شرح للمصطلحات المستخدمة في هذا القسم، انظر attributes(7).

الواجهة السمة القيمة
strcmp() سلامة الخيوط MT-Safe

المعايير

C11, POSIX.1-2008.

التاريخ

POSIX.1-2001، C89، SVr4، 4.3BSD.

تحذيرات

لا يُؤخذ الإعداد المحلي في الاعتبار (لمقارنة تراعي الإعداد المحلي، انظر strcoll(3)).

أمثلة

يمكن استخدام البرنامج أدناه لتوضيح عمل strcmp().


$ ./strcmp ABC ABC;
<str1> and <str2> are equal
$ ./strcmp ABC AB;      # 'C' is ASCII 67; 'C' - '\0' = 67
<str1> is greater than <str2> (67)
$ ./strcmp ABA ABZ;     # 'A' is ASCII 65; 'Z' is ASCII 90
<str1> is less than <str2> (-25)
$ ./strcmp ABJ ABC;
<str1> is greater than <str2> (7)
$ ./strcmp $'\201' A;   # 0201 - 0101 = 0100 (or 64 decimal)
<str1> is greater than <str2> (64)

يستخدم المثال الأخير بناء جملة خاصًا بـ bash(1) لإنتاج سلسلة محارف تحتوي على رمز ASCII 8-بت؛ تُظهر النتيجة أن مقارنة السلاسل تستخدم محارف غير مُوقَّعة.

مصدر البرنامج

// strcmp.c
// Licensed under GNU General Public License v2 or later.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int
main(int argc, char *argv[])
{

int res;
if (argc != 3) {
fprintf(stderr, "Usage: %s <str1> <str2>\n", argv[0]);
exit(EXIT_FAILURE);
}
res = strcmp(argv[1], argv[2]);
if (res == 0) {
printf("<str1> and <str2> are equal");
printf("\n");
} else if (res < 0) {
printf("<str1> is less than <str2> (%d)\n", res);
} else {
printf("<str1> is greater than <str2> (%d)\n", res);
}
exit(EXIT_SUCCESS); }

انظر أيضًا

memcmp(3), strcasecmp(3), strcoll(3), streq(3), string(3), strverscmp(3), wcscmp(3)

ترجمة

تُرجمت هذه الصفحة من الدليل بواسطة زايد السعيدي <zayed.alsaidi@gmail.com>

هذه الترجمة هي وثيقة مجانية؛ راجع رخصة جنو العامة الإصدار 3 أو ما بعده للاطلاع على شروط حقوق النشر. لا توجد أي ضمانات.

إذا وجدت أي أخطاء في ترجمة صفحة الدليل هذه، يرجى إرسال بريد إلكتروني إلى قائمة بريد المترجمين: kde-l10n-ar@kde.org.

10 أغسطس 2026 صفحات دليل لينكس 6.20