/*----------------------------------------------------Program: ScanfDemoObjectives: Using scanf() for read input from keyboardCreated by Panupong Sornkhomemail: panupongs@nu.ac.th-----------------------------------------------------*/#include <stdio.h>intmain()
{
int iMyVar1,iMyVar2,iAnswer;
printf("Enter data 1 (integer):");
/* Read input from keyboard and store in iMyVar */
scanf("%d",&iMyVar1);
printf("Enter data 2 (integer):");
scanf("%d",&iMyVar2);
iAnswer =3*iMyVar1 +5*iMyVar2 -10;
printf("You enter %d and %d\n",iMyVar1,iMyVar2);
printf("Answer = %d\n",iAnswer);
return0;
}
/*----------------------------------------------------Program: ScanfDemo2Objectives: Using scanf() for read input from keyboardCreated by Panupong Sornkhomemail: panupongs@nu.ac.th-----------------------------------------------------*/#include <stdio.h>intmain()
{
char c;
int i;
float f;
printf("Enter a single character: ");
scanf("%c",&c); // read a character
printf("Enter an integer number: ");
scanf("%d",&i); // read an integer
printf("Enter a real number: ");
scanf("%f",&f); // read a real number/* Show data read from keyboard */
printf("Here are your entered data\n");
printf("%c %d %f\n",c,i,f);
return0;
}
/*----------------------------------------------------Program: ScanfDemo3Objectives: Using scanf() for read multiple datainput from keyboardCreated by Panupong Sornkhomemail: panupongs@nu.ac.th-----------------------------------------------------*/#include <stdio.h>intmain()
{
char c;
int i;
float f;
printf("Enter data character integer");
printf(" and real number respectively: ");
scanf("%c%d%f",&c,&i,&f); // read a set of data/* Show data read from keyboard */
printf("Here are your entered data\n");
printf("%c %d %f\n",c,i,f);
return0;
}
/*----------------------------------------Program: Datatypes in C programObjectives: (1) Show various datatypes and size of memory required (2) Show format identifiers related to datatypes (3) Show the limitation value of each datatypeCreated by Panupong Sornkhomemail: panupongs@nu.ac.th----------------------------------------*/#include <stdio.h>#include <limits.h> // ใช้กับค่าคงที่ เช่น INT_MAX #include <float.h> // ใช้กับค่าคงที่ที่เกี่ยวกับกับ floating-point numberintmain()
{
// declare character variable cchar c ='x';
// declare short variable sshort s =12345;
// declare integer variable iint i =1234567890;
// declare long variable llong l =1234567890123;
// declare floating-point vairable ffloat f =12.9876543210;
// declare double precision variable ddouble d =12.9876543210;
// declare long double variable ldlongdouble ld =12.9876543210L;
/* * ใช้ sizeof เพื่อดูขนาดของหน่วยความจำ * เป็นจำนวน byte ที่ใช้ในแต่ละตัวแปร */
printf("Size of char = %lu byte\n",sizeof c);
printf("Size of short = %lu bytes\n",sizeof s);
printf("Size of int = %lu bytes\n",sizeof i);
printf("Size of long = %lu bytes\n",sizeof l);
printf("Size of float = %lu bytes\n",sizeof f);
printf("Size of double = %lu bytes\n",sizeof d);
printf("Size of long double = %lu bytes\n",sizeof ld);
/* * แสดงความแตกต่างระหว่าง signed กับ unsigned * โดยแสดงค่าคงที่ที่ประกาศไว้ในไฟล์ limits.h */
printf("Minimum value of type (signed) int = %d\n",INT_MIN);
printf("Maximum value of type (signed) int = %d\n",INT_MAX);
printf("Minimum value of type unsigned int = %d\n",0);
printf("Maximum value of type unsigned int = %u\n",UINT_MAX);
/* * แสดงค่าของตัวแปรชนิดต่าง ๆ ด้วย printf */
printf("c = %c\n",c);
printf("s = %d\n",s);
printf("i = %d\n",i);
printf("l = %ld\n",l);
printf("f = %e\n",f);
printf("f = %f\n",f);
printf("f = %.6f\n",f);
printf("d = %e\n",d);
printf("d = %f\n",d);
printf("d = %.15f\n",f);
printf("ld = %Le\n",ld);
printf("ld = %Lf\n",ld);
printf("ld = %.18Lf\n",ld);
printf("Number of digits in float = %d\n",FLT_DIG);
printf("Number of digits in double = %d\n",DBL_DIG);
printf("Number of digits in long double = %d\n",LDBL_DIG);
return0;
}
/*-------------------------------------------------Program: simple C programObjective: to demonstrate a simple C programCreated by Panupong Sornkhomemail: panupongs@nu.ac.th---------------------------------------------------*/#include <stdio.h>intmain()
{
printf("Hello World\n");
printf("Happy New Year\n");
printf("Computer Programming\n");
return0;
}
อธิบายโปรแกรม
โปรแกรมนี้ทำการแสดงข้อความบนจอภาพ (Console)
เป็นข้อความดังนี้
Hello World
Happy New Year
Computer Programming