C Programming Laboratory I. Introduction to C Language /* the first program for user */ #include int...

23
C Programming Laboratory I

Transcript of C Programming Laboratory I. Introduction to C Language /* the first program for user */ #include int...

Page 1: C Programming Laboratory I. Introduction to C Language /* the first program for user */ #include int a=0; int main(void) { printf(“Hello World\n”); return.

C Programming

Laboratory I

Page 2: C Programming Laboratory I. Introduction to C Language /* the first program for user */ #include int a=0; int main(void) { printf(“Hello World\n”); return.

Introduction to C Language

/* the first program for user */#include <stdio.h>int a=0;int main(void){

printf(“Hello World\n”); return a;

}

CommentHeader fileDecaration

Program Body

Page 3: C Programming Laboratory I. Introduction to C Language /* the first program for user */ #include int a=0; int main(void) { printf(“Hello World\n”); return.

Comment

Do not want to compile this section There are 2 forms

/* comment part */

// comment part used only borland C compiler

/* This is comment *//* For produce the good program, should Write comment every 10-15 line of program *///Or use line comment for Borland C

Page 4: C Programming Laboratory I. Introduction to C Language /* the first program for user */ #include int a=0; int main(void) { printf(“Hello World\n”); return.

Preprocessing Directives (#)

Use for inform compiler to do something before compiling

#define #endif #error#ifndef #elif #if#include #else#ifdef#pragma #undef

Page 5: C Programming Laboratory I. Introduction to C Language /* the first program for user */ #include int a=0; int main(void) { printf(“Hello World\n”); return.

#define Directive

Define word expression or command

#define <name> <value|expression>

Example #define TEN 10#define PI 3.141592654#define R 2*PI#define MUCH 3*6+5 \

-(7*3)

Page 6: C Programming Laboratory I. Introduction to C Language /* the first program for user */ #include int a=0; int main(void) { printf(“Hello World\n”); return.

#include Directive

Read the according files to co-compile

#include <filename>#include “filename”

Example #include <dos.h>#include “sample.c”

Page 7: C Programming Laboratory I. Introduction to C Language /* the first program for user */ #include int a=0; int main(void) { printf(“Hello World\n”); return.

Structure of module

Program, written by C consisted of modules

Main() is the principle module One program have to have one main()

Module-type module-name(argument){

a sequence of commands;}

Page 8: C Programming Laboratory I. Introduction to C Language /* the first program for user */ #include int a=0; int main(void) { printf(“Hello World\n”); return.

Identifiers

Definition of all of name such as variable

1. Keywords

2. User-defined word

Auto default float register struct volatileBreak do for return switch whileCase double goto short typedefChar else if signed unionConst enum int sizeof unsignedContinue extern long static void

Page 9: C Programming Laboratory I. Introduction to C Language /* the first program for user */ #include int a=0; int main(void) { printf(“Hello World\n”); return.

Rule of User-defined word

Case sensitive Character “_” Number First with Character or “_” Have to define before use Define in any command or expression Don’t same as KeyWord

Page 10: C Programming Laboratory I. Introduction to C Language /* the first program for user */ #include int a=0; int main(void) { printf(“Hello World\n”); return.

Type of Identifiers

Global Identifiers : define external a set of command

Local Identifiers : define internal a set of command

A

B

Page 11: C Programming Laboratory I. Introduction to C Language /* the first program for user */ #include int a=0; int main(void) { printf(“Hello World\n”); return.

DATA TYPE

DATA TYPE Size

char -128 to 127 1 Byte

unsigned char 0 to 255 1 Byte

int -32768 to 32767 2 Byte

unsigned int 0 to 65535 2 Byte

long -2147483648 to 2147483647 4 Byte

unsigned long 0 to 4 Byte

float -3.4E+38 to 3.4E+38 4 Byte

double 1.7E+308 8 Byte

void No-specified / No-Value return -

Page 12: C Programming Laboratory I. Introduction to C Language /* the first program for user */ #include int a=0; int main(void) { printf(“Hello World\n”); return.

constant

Integer constant

Floating constant

Character constant

Interger : 5 10 -12 75Long int : 100000LHex Form : 0x7a 0xfe 0x100

0.0e0 : 3.2e50.0E0 : 3.4E-8

‘A’ ‘b’ ‘#’

Page 13: C Programming Laboratory I. Introduction to C Language /* the first program for user */ #include int a=0; int main(void) { printf(“Hello World\n”); return.

Variables

Name for calling the value in memory Format for define variables

<DATA-TYPE> <VAR-NAME>[,<VAR-NAME>];

Examplechar a,b,c;unsigned int d;

Page 14: C Programming Laboratory I. Introduction to C Language /* the first program for user */ #include int a=0; int main(void) { printf(“Hello World\n”); return.

Expressions

Expressions in form

operand1 operator operand2

Operator Meaning Expression Result

+ Add 7 + 3 10

- Substract 7 – 3 4

* Multiple 7 * 3 21

/ Divide 7 / 3 2.333

% Modulate 7 % 3 1

Page 15: C Programming Laboratory I. Introduction to C Language /* the first program for user */ #include int a=0; int main(void) { printf(“Hello World\n”); return.

Statements

The complete command to instruct computer to do something

Enclose with ; (semi-colon) Blank space is not important

Page 16: C Programming Laboratory I. Introduction to C Language /* the first program for user */ #include int a=0; int main(void) { printf(“Hello World\n”); return.

Assignment Statements

following Form

It mean calculate expression in right side and assign in variable in left side

<Variable> = Expression;

Page 17: C Programming Laboratory I. Introduction to C Language /* the first program for user */ #include int a=0; int main(void) { printf(“Hello World\n”); return.

Input & Output

Character Code String Output Statement Input Statement

Page 18: C Programming Laboratory I. Introduction to C Language /* the first program for user */ #include int a=0; int main(void) { printf(“Hello World\n”); return.

Character Code

Character represent to computer in number,called ASCII Code ( American Standard Code for Information Interchange) 128 characters,7 Bits

Extended ASCII Code 128 characters for special character such as Greek-Latin character, including Thai font

Big program occurred when using more than 2 language or the language require more character such Chinese Language.

Represent enclose with ‘ and ’ (qoute symbol)

‘a’ ‘4’ ‘\0’ ‘\n’

Page 19: C Programming Laboratory I. Introduction to C Language /* the first program for user */ #include int a=0; int main(void) { printf(“Hello World\n”); return.

STRING

A sequence of characters Represent enclose with “ and ” (double qoute)

“Hello world!”

Page 20: C Programming Laboratory I. Introduction to C Language /* the first program for user */ #include int a=0; int main(void) { printf(“Hello World\n”); return.

Output Statement

command present string on monitor

printf(charformat[,argument]);

Exampleprintf(“Hello World!”);a = 12;

printf(“Hello! Number%d”,a);

Page 21: C Programming Laboratory I. Introduction to C Language /* the first program for user */ #include int a=0; int main(void) { printf(“Hello World\n”); return.

Output Statement

Type this program

#include<stdio.h> /* Call module stdio.h */void main(void) /* Main function*/{

char *msg = “ชื่��อของนั�กศึ�กษา \n”;int id = รหั�สนั�กศึ�กษา 4 ตั�วท้�าย ;printf(”%s student-code = %d \n”,msg,id);

}

Page 22: C Programming Laboratory I. Introduction to C Language /* the first program for user */ #include int a=0; int main(void) { printf(“Hello World\n”); return.

Input Statement

Command get value from keyboard

scanf(char-format[,address]);

#include<stdio.h>void main(void){ int date,month,year; printf(“Please enter date in dd/mm/yy :”); printf(“date :”); scanf(“%d”,&date); printf(“month:”); scanf(“%d”,&month); printf(“year :”); scanf(“%d”,&year); printf(“Date: %.2d/%.2d/%.2d\n”,date,month,year);}

Page 23: C Programming Laboratory I. Introduction to C Language /* the first program for user */ #include int a=0; int main(void) { printf(“Hello World\n”); return.

งานัปฏิ�บั�ตั�การ 5

จงเขี�ยนโปรแกรมร�บค่�าค่วามยาวฐานขีองสามเหลี่��ยมแลี่ะค่วามส�งขีองสามเหลี่��ยมแลี่�วน�ามาประมวลี่ผลี่หาค่�าพื้ !นที่��ขีองสามเหลี่��ยมInput Base Length : 10Input Height : 5Output Area of Triangle = 25