Skip to content

Commit 06381f1

Browse files
authored
Add files via upload
1 parent b0865f0 commit 06381f1

26 files changed

Lines changed: 717 additions & 0 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* stdin.c
3+
*
4+
* stdin is the standard input stream.
5+
* It normally reads from the keyboard.
6+
*/
7+
8+
#include <stdio.h>
9+
10+
main()
11+
{
12+
int c;
13+
14+
printf("Type a character: ");
15+
c = getchar(); /* قراءة من stdin */
16+
17+
printf("You typed: ");
18+
putchar(c); /* طباعة إلى stdout */
19+
printf("\n");
20+
21+
/* stdin يمكن إعادة توجيهه من ملف */
22+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* stdout.c
3+
*
4+
* stdout is the standard output stream.
5+
* It normally writes to the screen.
6+
*/
7+
8+
#include <stdio.h>
9+
10+
main()
11+
{
12+
fprintf(stdout, "Hello, world\n"); /* كتابة مباشرة إلى stdout */
13+
14+
printf("Same as stdout\n"); /* printf تكتب إلى stdout */
15+
16+
/* stdout يمكن إعادة توجيهه إلى ملف */
17+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* printf_format.c
3+
*
4+
* printf formats output according to a format string.
5+
* Each % specification converts one argument.
6+
*/
7+
8+
#include <stdio.h>
9+
10+
main()
11+
{
12+
int i = 42;
13+
float f = 3.14;
14+
char c = 'A';
15+
char *s = "hello";
16+
17+
printf("%d\n", i); /* عدد صحيح */
18+
printf("%f\n", f); /* عدد عشري */
19+
printf("%.2f\n", f); /* رقمان بعد الفاصلة */
20+
printf("%c\n", c); /* حرف */
21+
printf("%s\n", s); /* نص */
22+
printf("%5d\n", i); /* عرض 5 خانات */
23+
printf("%-5d\n", i); /* محاذاة لليسار */
24+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* variable_args.c
3+
*
4+
* Functions can accept a variable number of arguments.
5+
* This is how printf handles any number of values.
6+
*/
7+
8+
#include <stdio.h>
9+
#include <stdarg.h>
10+
11+
void mini_printf(char *fmt, ...);
12+
13+
main()
14+
{
15+
mini_printf("int: %d, float: %f\n", 42, 3.14);
16+
}
17+
18+
void mini_printf(char *fmt, ...)
19+
{
20+
va_list ap; /* قائمة الوسائط المتغيرة */
21+
22+
va_start(ap, fmt); /* تهيئة ap */
23+
24+
/* في printf الحقيقي، fmt يُفحص لمعالجة % */
25+
vprintf(fmt, ap); /* طباعة باستخدام قائمة الوسائط */
26+
27+
va_end(ap); /* تنظيف */
28+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* scanf.c
3+
*
4+
* scanf reads formatted input.
5+
* It returns the number of items successfully read.
6+
*/
7+
8+
#include <stdio.h>
9+
10+
main()
11+
{
12+
int i;
13+
float f;
14+
char s[100];
15+
16+
printf("Enter integer, float, string: ");
17+
scanf("%d %f %s", &i, &f, s); /* تمرير العناوين */
18+
19+
printf("int: %d\n", i);
20+
printf("float: %f\n", f);
21+
printf("string: %s\n", s);
22+
23+
/* scanf يتخطى المسافات البيضاء تلقائياً */
24+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* fopen.c
3+
*
4+
* fopen opens a file and returns a file pointer.
5+
* NULL is returned if the file cannot be opened.
6+
*/
7+
8+
#include <stdio.h>
9+
10+
main()
11+
{
12+
FILE *fp;
13+
14+
fp = fopen("test.txt", "r"); /* فتح للقراءة */
15+
if (fp == NULL) {
16+
printf("Cannot open file\n");
17+
return 1;
18+
}
19+
20+
printf("File opened successfully\n");
21+
22+
fclose(fp);
23+
24+
/* الأوضاع: "r" قراءة، "w" كتابة، "a" إضافة */
25+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* fclose.c
3+
*
4+
* fclose closes a file and flushes buffers.
5+
* It should be called when done with a file.
6+
*/
7+
8+
#include <stdio.h>
9+
10+
main()
11+
{
12+
FILE *fp;
13+
14+
fp = fopen("output.txt", "w");
15+
if (fp == NULL)
16+
return 1;
17+
18+
fprintf(fp, "Hello, file\n"); /* كتابة للملف */
19+
20+
fclose(fp); /* إغلاق الملف */
21+
22+
/* عدم إغلاق الملف قد يفقد البيانات */
23+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* error_handling.c
3+
*
4+
* File operations can fail.
5+
* Always check return values for errors.
6+
*/
7+
8+
#include <stdio.h>
9+
#include <stdlib.h>
10+
11+
main()
12+
{
13+
FILE *fp;
14+
int c;
15+
16+
fp = fopen("data.txt", "r");
17+
if (fp == NULL) { /* فحص الخطأ */
18+
fprintf(stderr, "Error: cannot open file\n");
19+
exit(1);
20+
}
21+
22+
while ((c = fgetc(fp)) != EOF) {
23+
if (ferror(fp)) { /* فحص خطأ أثناء القراءة */
24+
fprintf(stderr, "Read error\n");
25+
break;
26+
}
27+
putchar(c);
28+
}
29+
30+
fclose(fp);
31+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* fgets.c
3+
*
4+
* fgets reads a line from a file.
5+
* It stops at newline or EOF.
6+
*/
7+
8+
#include <stdio.h>
9+
10+
main()
11+
{
12+
FILE *fp;
13+
char line[100];
14+
15+
fp = fopen("test.txt", "r");
16+
if (fp == NULL)
17+
return 1;
18+
19+
while (fgets(line, sizeof(line), fp) != NULL) {
20+
printf("%s", line); /* line يحتوي على \n */
21+
}
22+
23+
fclose(fp);
24+
25+
/* fgets أكثر أماناً من gets */
26+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* fputs.c
3+
*
4+
* fputs writes a string to a file.
5+
* It does not add a newline automatically.
6+
*/
7+
8+
#include <stdio.h>
9+
10+
main()
11+
{
12+
FILE *fp;
13+
14+
fp = fopen("output.txt", "w");
15+
if (fp == NULL)
16+
return 1;
17+
18+
fputs("First line\n", fp); /* يجب إضافة \n يدوياً */
19+
fputs("Second line\n", fp);
20+
21+
fclose(fp);
22+
}

0 commit comments

Comments
 (0)