วันจันทร์ที่ 20 สิงหาคม พ.ศ. 2555

Code ตัวอย่างโปรแกรม และหน้าตาโปรแกรมภาษาซี

Code ตัวอย่างโปรแกรม

และหน้าตาโปรแกรมภาษาซี

ตัวอย่างที่ 1 แสดงตัวอย่างโปรแกรมภาษาซีเบื้องต้น

#include <stdio.h>

void main( ) {

/* Display message to standard output */

printf(“My first program.”);



ตัวอย่างที่ 2 แสดงตัวอย่างโปรแกรมภาษาซีเบื้องต้น

#include <stdio.h> void main( ) { /* Display message to standard output */ printf

(“My first program.”); }



ตัวอย่างที่ 3 แสดงตัวอย่างโปรแกรมภาษาซีเบื้องต้น

#include

<stdio.h>

void

main

()

{

/* Display message

to standard

output */

printf

(

My first program.”

)

;

}



ตัวอย่างที่ 4 แสดงตัวอย่างการใช้ค่าของตัวแปรชนิด char

#include <stdio.h>

void main( ) {

int no;

char ch;

ch = ‘J’;

printf(“char : %c, dec : %d, oct : %o, hex : %x”, ch, ch, ch, ch);

no = ch;

printf(“\nno : %d, ch : %c”, no, ch);

no = 68;

ch = no;

printf(“\nno : %d, ch : %c”, no, ch);

}

ผลการทำงานของโปรแกรม

char : J, dec : 74, oct : 112, hex : 4a

no : 74, ch : J

no : 68, ch : D



ตัวอย่างที่ 5 แสดงตัวอย่างการรับข้อมูลเพื่อนำมาแสดงผล

#include <stdio.h>

void main( ) {

char name[100];

printf("What is your name ?\n");

scanf("%s", name);

printf("Very glad to know you, ");

printf("%s.",name);

}

ผลลัพธ์ของการทำงาน

What is your name ?

Willy

Very glad to know you, Willy.



ตัวอย่างที่ 6 แสดงการกำหนดค่าจำนวนจริงให้กับตัวแปรจำนวนเต็ม

#include <stdio.h>

void main( ) {

int x;

x = 14.8328;

printf(“x value is %d”, x);

}

ผลการทำงานของโปรแกรม

x value is 14



ตัวอย่างที่ 7 โปรแกรมหาผลรวมของเลขจำนวนเต็ม 2 จำนวนที่รับข้อมูลจากผู้ใช้

#include <stdio.h>

void main( ) {

int x, y, z;

printf(“Enter X value : “);

scanf(“%d”, &x);

printf(“Enter Y value : “);

scanf(“%d”, &y);

z = x + y;

printf(“Summary of X and Y is %d”, z);

}

ผลการทำงานของโปรแกรม

Enter X value : 15

Enter Y value : 20

Summary of X and Y is 35



ตัวอย่างที่ 8 แสดงการใช้ตัวดำเนินการเพิ่มค่า

#include <stdio.h>

void main( ) {

int y, count;

count = 1;

y = count++;

printf(“y = %d, count = %d”, y, count);

count = 1;

y = ++count;

printf(“\ny = %d, count = %d”, y, count);

}

ผลการทำงานของโปรแกรม

y = 1, count = 2

y = 2, count = 2



ตัวอย่างที่ 9 แสดงการใช้ตัวดำเนินการเปลี่ยนชนิดข้อมูล

#include <stdio.h>

void main( ) {

int x;

x = 2.5 * 2;

printf(“x value is %d”, x);

x = (int)2.5 * 2;

printf(“\nx value is %d”, x);

x = (int)(2.5 * 2);

printf(“\nx value is %d”, x);

}

ผลการทำงานของโปรแกรม

x value is 5

x value is 4

x value is 5



ตัวอย่างที่ 10 แสดงของการเปรียบเทียบด้วยตัวกาํเนินการความสัมพันธ์

#include <stdio.h>

void main( ) {

int x, y

printf(“Enter X : “);

scanf(“%d”, &x);

printf(“Enter Y : “);

scanf(“%d”, &y);

printf(“X > Y is %d”, x>y);

}



ผลการทำงานของโปรแกรม

Enter X : 32

Enter Y : 24

X > Y is 1

ไม่มีความคิดเห็น:

แสดงความคิดเห็น