logo

Stdin وStdout في C

البرمجة تتطلب مدخل و انتاج الأنشطة، ولغة C com.stdin و com.stdout تدير التدفقات هذه العمليات بشكل فعال. سيشرح هذا المرجع الشامل بدقة الغرض، وبناء الجملة، واستخدام stdin وstdout. تيارات قياسية في ج مُسَمًّى com.stdin و com.stdout تسهيل عمليات الإدخال والإخراج. إنها تجعل التواصل بين البرنامج ومستخدمه أكثر بساطة كأحد مكونات مكتبة C القياسية (stdio.h) . دعونا نفحص هذه التدفقات بمزيد من التفصيل:

ما هو ستدين؟

عادي هو لتقف على الإدخال القياسي . ويمثلها تيار ستدين ، والتي تكون متصلة عادةً بلوحة المفاتيح. فهو يمكّن البرامج من قراءة البيانات التي يدخلها المستخدم أثناء تشغيلها. التخزين المؤقت للخط هو الإعداد الافتراضي ل com.stdin ، الذي يجمع المدخلات حتى يقوم المستخدم بدفع الملف مفتاح الادخال .

ما هو ستدوت؟

ستدوت هو الوقوف ل الإخراج القياسي . ويمثلها تيار stdout ، والذي يتم توصيله بشكل متكرر بوحدة التحكم أو الجهاز الطرفي. فهو يجعل من الممكن للبرامج إظهار معلومات المستخدم أو النتائج. يتم أيضًا تخزين Stdout بشكل افتراضي.

فهم بناء الجملة مطلوب باستخدام com.stdin و com.stdout الكفاءة أمر ضروري:

قراءة المدخلات من Stdin:

ال وظيفة سكانف يستخدم ل قراءة المدخلات من المستخدم عبر com.stdin . وفيما يلي بناء الجملة:

سلسلة إلى جافا المنطقية
 scanf('format_specifier', &variable); 

في هذه الحالة، تتم الإشارة إلى نوع البيانات المقصود بواسطة format_specifier ، ويتم الإشارة إلى عنوان الذاكرة حيث سيتم تخزين بيانات الإدخال بواسطة &متغير.

كتابة الإخراج إلى Stdout:

ال printf يتم استخدام الدالة ل عرض الإخراج للمستخدم من خلال com.stdout . وفيما يلي بناء الجملة:

 printf('format_specifier', variable); 

يتم تعيين تنسيق الإخراج بواسطة format_specifier ، ويتم تخزين القيمة المراد عرضها في المتغير.

لمزيد من الفهم com.stdin و com.stdout ، دعونا نلقي نظرة على بعض الأمثلة الواقعية:

الفرز في القائمة في جافا

مثال 1:

قراءة المدخلات من Stdin: لنفترض أننا نطلب من المستخدم إدخال اسمه وعمره ورقمه المفضل. بعد ذلك، سيرى المستخدم هذه المعلومات مرة أخرى بسبب com.stdout .

 #include int main() { char name[50]; int age; int favoriteNumber; printf('Enter your name: '); scanf('%s', name); printf('Enter your age: '); scanf('%d', &age); printf('Enter your favorite number: '); scanf('%d', &favoriteNumber); printf('Name: %s
', name); printf('Age: %d
', age); printf('Favorite Number: %d
', favoriteNumber); return 0; } 

انتاج:

منع اعلانات اليوتيوب للاندرويد
 Enter your name: John Doe Enter your age: 25 Enter your favorite number: 42 Name: John Doe Age: 25 Favorite Number: 42 

مثال 2:

كتابة الإخراج إلى Stdout: دعونا نحسب مجموع القيمتين المقدمتين من قبل المستخدم ونعرض النتيجة على الشاشة باستخدام com.stdout .

 #include int main() { int num1, num2, sum; printf('Enter the first number: '); scanf('%d', &num1); printf('Enter the second number: '); scanf('%d', &num2); sum = num1 + num2; printf('The sum is: %d
', sum); return 0; } 

انتاج:

 Enter the first number: 10 Enter the second number: 5 The sum is: 15 

مثال 3:

فيما يلي توضيح شامل لكيفية الاستخدام com.stdin و com.stdout في برنامج يحسب متوسط ​​سلسلة من الأرقام المقدمة من المستخدم:

 #include #define MAX_NUMBERS 10 int main() { int numbers[MAX_NUMBERS]; int count, i; float sum = 0, average; printf('Enter the count of numbers (up to %d): ', MAX_NUMBERS); scanf('%d', &count); if (count MAX_NUMBERS) { printf('Invalid count of numbers. Exiting...
'); return 0; } printf('Enter %d numbers:
&apos;, count); for (i = 0; i <count; i++) { printf('number %d: ', i + 1); scanf('%d', &numbers[i]); sum } average="sum" count; printf('
entered numbers: '); for (i="0;" < printf('%d numbers[i]); printf('
sum: %.2f
', sum); printf('average: average); return 0; pre> <p> <strong>Output:</strong> </p> <pre> Enter the count of numbers (up to 10): 5 Enter 5 numbers: Number 1: 10 Number 2: 15 Number 3: 20 Number 4: 25 Number 5: 30 Entered numbers: 10 15 20 25 30 Sum: 100.00 Average: 20.00 </pre> <p> <strong>Explanation:</strong> </p> <p>The following code demonstrates a program that determines the average of a set of numbers that the user provides. The user is first asked to specify the number of numbers they intend to input. After that, the program prompts the user to enter the required number of numbers if the count is accurate. The entered numbers are concurrently added together and stored in an array. After that, the average is determined by dividing the sum by the count in the program. Finally, the user is shown the entered numbers, sum, and average.</p> <h2>Conclusion:</h2> <p>In conclusion, any programmer intending to create effective and interactive apps must know the use of <strong> <em>stdin</em> </strong> and <strong> <em>stdout</em> </strong> in C. Throughout this article, we have learned a lot about these standard streams and how they function in input and output operations.</p> <p>We can quickly collect user input during runtime by using <strong> <em>stdin</em> </strong> . The <strong> <em>scanf function</em> </strong> allows us to use <strong> <em>format specifiers</em> </strong> to specify the expected data type and save the input in variables. Due to the ability to ask users for different inputs and process them appropriately, makes it possible for our programs to be interactive.</p> <p>It&apos;s crucial to remember that while working with <strong> <em>user input, input validation</em> </strong> and <strong> <em>error handling</em> </strong> must be carefully considered. Users may submit unexpected data, such as a character in place of a number or data that is longer than expected. We can include error-checking features and validate user input before moving on to other procedures to make sure our programs are resilient.</p> <p>On the other hand, we can show the <strong> <em>user information, outcomes</em> </strong> , and <strong> <em>messages</em> </strong> using <strong> <em>stdout</em> </strong> . A flexible method for formatting and presenting the result in a style that is easy to understand is provided by the <strong> <em>printf function</em> </strong> . Using <strong> <em>format specifiers</em> </strong> , we can regulate the presentation of different data kinds, including <strong> <em>texts, integers,</em> </strong> and <strong> <em>floating-point numbers</em> </strong> . It enables us to tailor the output and give the user useful information.</p> <p>In some circumstances, we could need <strong> <em>input</em> </strong> or <strong> <em>output</em> </strong> immediately without waiting for the newline character. The <strong> <em>getchar</em> </strong> and <strong> <em>putchar</em> </strong> functions can be used to read and write individual characters in these circumstances. We can process characters one at a time with these functions because they give us more precise control over input and output.</p> <p>Using <strong> <em>stdin</em> </strong> and <strong> <em>stdout</em> </strong> goes beyond interactive command-line interfaces, even though console-based applications are frequently associated with them. The conventional input and output can be redirected to read from or write to files, allowing for batch processing and task automation. We can efficiently handle enormous volumes of data and operate on external files by using file <strong> <em>I/O routines</em> </strong> like <strong> <em>fopen, fread, fwrite, and fclose</em> </strong> .</p> <p>Additionally, to produce even more potent outcomes, <strong> <em>stdin</em> </strong> and <strong> <em>stdout</em> </strong> can be used with other C programming features and tools. For instance, we may use the <strong> <em>string.h library&apos;s</em> </strong> string manipulation functions in conjunction with stdin and stdout to process and modify text input. They can also be used in conjunction with <strong> <em>control structures, loops,</em> </strong> and <strong> <em>functions</em> </strong> to build sophisticated algorithms and user-input-based decision-making systems.</p> <hr></count;>

توضيح:

يوضح التعليمة البرمجية التالية برنامجًا يحدد متوسط ​​مجموعة من الأرقام التي يوفرها المستخدم. يُطلب من المستخدم أولاً تحديد عدد الأرقام التي ينوي إدخالها. بعد ذلك يقوم البرنامج بمطالبة المستخدم بإدخال العدد المطلوب من الأرقام إذا كان العد دقيقا. تتم إضافة الأرقام المدخلة معًا بشكل متزامن وتخزينها في مصفوفة. وبعد ذلك يتم تحديد المتوسط ​​بقسمة المجموع على العدد الموجود في البرنامج. وأخيرا، يظهر للمستخدم الأرقام المدخلة، والمجموع، والمتوسط.

ما هو أمر التصدير في لينكس

خاتمة:

في الختام، يجب على أي مبرمج ينوي إنشاء تطبيقات فعالة وتفاعلية أن يعرف كيفية استخدامها com.stdin و com.stdout في لغة C. خلال هذه المقالة، تعلمنا الكثير عن هذه التدفقات القياسية وكيفية عملها في عمليات الإدخال والإخراج.

يمكننا جمع مدخلات المستخدم بسرعة أثناء وقت التشغيل باستخدام com.stdin . ال وظيفة سكانف يسمح لنا باستخدام محددات التنسيق لتحديد نوع البيانات المتوقع وحفظ المدخلات في المتغيرات. نظرًا للقدرة على مطالبة المستخدمين بمدخلات مختلفة ومعالجتها بشكل مناسب، فإن ذلك يجعل من الممكن لبرامجنا أن تكون تفاعلية.

من المهم أن تتذكر ذلك أثناء العمل معه إدخال المستخدم، التحقق من صحة الإدخال و معالجة الأخطاء يجب النظر فيها بعناية. يمكن للمستخدمين إرسال بيانات غير متوقعة، مثل حرف بدلاً من رقم أو بيانات أطول من المتوقع. يمكننا تضمين ميزات التحقق من الأخطاء والتحقق من صحة إدخالات المستخدم قبل الانتقال إلى إجراءات أخرى للتأكد من مرونة برامجنا.

ومن ناحية أخرى، يمكننا أن نظهر معلومات المستخدم، النتائج ، و رسائل استخدام com.stdout . يتم توفير طريقة مرنة لتنسيق النتيجة وتقديمها بأسلوب يسهل فهمه بواسطة وظيفة برينتف . استخدام محددات التنسيق ، يمكننا تنظيم عرض أنواع البيانات المختلفة، بما في ذلك الأعداد الصحيحة, و أرقام النقطة العائمة . إنها تمكننا من تصميم المخرجات وتزويد المستخدم بمعلومات مفيدة.

في بعض الظروف قد نحتاج مدخل أو انتاج على الفور دون انتظار حرف السطر الجديد. ال com.getchar و com.putchar يمكن استخدام الوظائف لقراءة وكتابة الأحرف الفردية في هذه الظروف. يمكننا معالجة الأحرف واحدًا تلو الآخر باستخدام هذه الوظائف لأنها تمنحنا تحكمًا أكثر دقة في الإدخال والإخراج.

استخدام com.stdin و com.stdout يتجاوز واجهات سطر الأوامر التفاعلية، على الرغم من أن التطبيقات المستندة إلى وحدة التحكم ترتبط بها بشكل متكرر. يمكن إعادة توجيه المدخلات والمخرجات التقليدية للقراءة من الملفات أو الكتابة إليها، مما يسمح بمعالجة الدفعات وأتمتة المهام. يمكننا التعامل بكفاءة مع كميات هائلة من البيانات والعمل على الملفات الخارجية باستخدام file إجراءات الإدخال/الإخراج يحب fopen، fread، fwrite، وfclos .

تجاوز طريقة جافا

بالإضافة إلى ذلك، لتحقيق نتائج أكثر فعالية، com.stdin و com.stdout يمكن استخدامها مع ميزات وأدوات برمجة C الأخرى. على سبيل المثال، قد نستخدم مكتبة string.h وظائف معالجة السلسلة جنبًا إلى جنب مع stdin وstdout لمعالجة إدخال النص وتعديله. ويمكن أيضًا استخدامها جنبًا إلى جنب مع هياكل التحكم، الحلقات، و المهام لبناء خوارزميات متطورة وأنظمة صنع القرار القائمة على مدخلات المستخدم.