27. โปรแกรมที่ 2
สร้างไฟล์ print.c โดย
กำาหนด #define ดังต่อไปนี้
จำานวนเต็ม X มีค่า 65
จำานวนจริง Y มีค่า 1.23456
ตัวอักษร CH มีค่า ‘C’
ชุดตัวอักษร SU “มีค่า Silpakorn
university”
พิมพ์ค่าต่างๆ ที่กำาหนด ให้แสดงผลดังรูป
X in decimal = 65
X in octadecimal = 101
X in Hexadecimal = 41
Y = 1.234
Y = 1.23e+00
CH = %C
SU = “Sipakorn
university”
34. คำาสงวนคำาสงวน Reserve WordsReserve Words
ของภาษาของภาษา CC
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static While
asm _cs _ds _es
_ss cdecl far huge
interrupt near pascal _export
35. วีธีการสร้างตัวแปรและกำาหนดค่า
#include <stdio.h>
void main ()
{
int age;
char sex;
float grade;
age = 20;
sex = ‘ f ’;
grade = 3.14;
}
#include <stdio.h>
void main ()
{
int age = 20;
char sex = ‘ f ’;
float grade = 3.14;
char name[10] = “malee”
printf(“you are
%sn”,name);
...
}
36. a + b
x = y
c = a + b
x == y
++i
นิพจน์
นิพจน์อาจประกอบด้วย
ตัวแปร
ค่าคงที่
การเรียกใช้ฟังก์ชัน
หรือมีตัวดำาเนินการร่วมอยู่ก็ได้
42. ตัวอย่างโปรแกรม
#include <stdio.h>
void main() {
float b,h,area ;
printf("Input Base = ");
scanf("%%f",&&b);
printf("Input Height = ");
scanf("%%f",&&h);
area = 0.5*b*h ;
printf("Area of triangle is %%5.2f",area);
return ;
}
Input Base = 12.0
Input Height = 6.0
Area of triangle is 36.00
Input Base = 3.2
Input Height = 1.2
Area of triangle is 1.92
44. ตัวอย่างโปรแกรม
/* program to calculate area of a circle */
#include <stdio.h>
#define PI 3.14159
main()
{
float radius, area;
printf(“Input Radius = ?");
scanf("%f", &radius);
area = PI * radius * radius;
printf("Area of circle is %7.2f ", area);
}
Input Radias = 12.0
Area of circle is 452.39