วิชาการเขียนโปรแกรมด้วยภาษาซี (c)

ตัวดำเนินการทางคณิตศาสตร์ ภาษาซี c programming

ตัวดำเนินการทางคณิตศาสตร์ ภาษาซี (c)

ตัวดำเนินการทางคณิตศาสตร์ทางคณิตศาสตร์นับได้ว่าเป็นการใช้งานบ่อยครั้งมาก ๆ ก่อนที่จะเริ่มเขียนคำสั่ง เราจำเป็นต้องรู้สัญลักษณ์ของตัวดำเนินการทางคณิตศาสตร์ของภาษาซี มีดังนี้

  • เครื่องหมาย + 
  • เครื่องหมาย –
  • เครื่องหมายคูณ *
  • เครื่องหมายหาร /
  • เครื่องหมายหารเอาเฉพาะเศษ % 

ตัวอย่างตัวดำเนินการทางคณิตศาสตร์ ภาษาซี (c)

#include <stdio.h> 
main()
{
  int a = 2; 
  int b = 5;
  int c = 10;
  printf ("%d\n",a+b); 
  printf ("%d\n",a-b);
  printf ("%d\n",a*c);
  printf ("%d\n",c/b);
  printf ("%d\n",c%b);
}

ผลลัพธ์ที่ได้คือ

7
-3
20
2
0


ตัวอย่างตัวดำเนินการทางคณิตศาสตร์ ภาษาซี (c)

กรณีที่ตัวแปรเป็นชนิดข้อมูลประเภทเดียวกันเราสามารถ เขียนตัวแปรต่อกันในบรรทัดเดียวกันได้เลยเช่น

#include <stdio.h> 
main()
{
  int a = 2,b = 5,c = 10; 
  printf ("%d\n",a+b); 
  printf ("%d\n",a-b);
  printf ("%d\n",a*c);
  printf ("%d\n",c/b);
  printf ("%d\n",c%b);
}

ผลลัพธ์ที่ได้คือ

7
-3
20
2
0


ตัวอย่างตัวดำเนินการทางคณิตศาสตร์ ภาษาซี (c)

#include <stdio.h> 
main()
{
  int a = 2; 
  int b = 5;
  int c = 10;
  int d = a+b;
  int e = a-b;
  int f = a*b;
  int g = a/b;
  int h = a%b;
  printf ("%d\n",d); 
  printf ("%d\n",e);
  printf ("%d\n",f);
  printf ("%d\n",g);
  printf ("%d\n",h);
}

ผลลัพธ์ที่ได้

7
-3
10
0
2

slot gacor hari ini slot qris slot gacor maxwin slot online slot gacor gampang menang slot gacor hari ini slot online slot slot slot guci777 guci777 fafatogel fafatogel fafatogel fafatogel agen138 slot agen138 slot data hk bola slot bolaslot99 bola slot agen138 link alternatif koin138 link alternatif agen138 slot ag138 slot slot gacor slot thailand