How to create a program that you can enter inputs.
1.การรับ Input หลังจากรันโปรแกรม
1.1 ไปที่แถบเมนูของโปรแกรม Virtual studio code ที่ file เลือก New File สร้างไฟล์ชื่อว่า myname.c ขึ้นมา
1.2 ทำการ copy code ด้านล่างลงไปแล้วทำการ save (ctrl + s) โปรแกรมที่เขียนไว้
#include <stdio.h>
#include <string.h>
int main(void) {
char name[50] = "";
char lastname[50] = "";
printf("Please enter your name\n");
scanf("%s",name );
printf("Please enter your lastname\n");
scanf("%s",lastname );
printf("your name is %s %s\n", name, lastname);
return 0;
}
1.3 ไปยัง terminal ใช้คำสั่ง gcc เพื่อ complie โปรแกรมที่เขียนออกมา
gcc myname.c -o myname
1.4 ใช้คำสั่ง ls เพื่อตรวจสอบว่าไฟล์ที่คอมไพล์แล้วสามารถออกมาได้หรือไม่

1.5 ลอง run โปรแกรมที่เขียนโดยใช้คำสั่ง ./ชื่อโปรแกรม
./myname
ทำการบันทึกผลการรันโปรแกรมทีละขั้นตอน โดยใส่ Input ทั้งหมดเข้าไป
2.การรับ Input ขณะเรียก program
สร้าง workspace ใน Linux command และเปิดด้วยคำสั่ง
code ./[workspace created]
2. สร้างไฟล์ใหม่และ Copy โค้ดด้านล่างใส่ใน Visual Studio Code
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("%d\n",argc);
printf("%s\n",argv[0]);
printf("%s\n",argv[1]);
printf("%s\n",argv[2]);
printf("address first argument is 0x%lx\n",(long)&argv[1]);
printf("address second argument is 0x%lx\n",(long)&argv[2]);
return 0;
}
3. ทำการ save ชื่อว่า argument.c ไฟล์และ Compile โค้ดดังกล่าวดัวยคำสั่ง
gcc argument.c -o arg
4. ทำการรันทดสอบโปรแกรม
./arg 11
./arg 54 43
./arg 24 69 420
ทำการบันทึกผลการรันโปรแกรมทีละขั้นตอน อธิบายที่มาของ output
Last updated
Was this helpful?