16. ตัวอย่าง
#include <stdio.h>
int Feet2Inch(int);
int feet,inches;
void main()
{
feet = 6;
inches = Feet2Inch(feet);
printf("Height in inches is %d",inches);
}
int Feet2Inch(int f)
{
return f*12;
} Height in inches is 72Height in inches is 72
ผลการทำางาน
28. สร้างไฟล์ 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”
35. 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
36. #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);
...
}
37. a + b
x = y
c = a + b
x == y
++i
นิพจน์อาจประกอบด้วย
ตัวแปร
ค่าคงที่
การเรียกใช้ฟังก์ชัน
หรือมีตัวดำาเนินการร่วมอยู่ก็ได้
43. #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
45. ตัวอย่ำงโปรแกรม
/* 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