Download presentation
Presentation is loading. Please wait.
Published byAntonia Annice Morton Modified over 9 years ago
1
C Programming Laboratory I
2
Introduction to C Language /* the first program for user */ #include int a=0; int main(void) { printf(“Hello World\n”); return a; } Comment Header file Decaration Program Body
3
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
4
Preprocessing Directives (#) Use for inform compiler to do something before compiling #define#endif#error #ifndef#elif#if #include#else#ifdef #pragma#undef
5
#define Directive Define word expression or command #define Example #define TEN 10 #define PI 3.141592654 #define R 2*PI #define MUCH 3*6+5 \ -(7*3)
6
#include Directive Read the according files to co-compile #include #include “filename” Example #include #include “sample.c”
7
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; }
8
Identifiers Definition of all of name such as variable 1. Keywords 2. User-defined word Auto default floatregister structvolatile Break do forreturn switchwhile Case doublegotoshort typedef Char else ifsigned union Const enum intsizeof unsigned Continue externlongstatic void
9
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
10
Type of Identifiers Global Identifiers : define external a set of command Local Identifiers : define internal a set of command A B
11
DATA TYPE Size char-128 to 1271 Byte unsigned char 0 to 2551 Byte int-32768 to 327672 Byte unsigned int 0 to 655352 Byte long-2147483648 to 21474836474 Byte unsigned long0 to4 Byte float-3.4E+38 to 3.4E+384 Byte double1.7E+3088 Byte voidNo-specified / No-Value return -
12
constant Integer constant Floating constant Character constant Interger : 5 10 -12 75 Long int : 100000L Hex Form : 0x7a 0xfe 0x100 0.0e0 : 3.2e5 0.0E0 : 3.4E-8 ‘A’ ‘b’ ‘#’
13
Variables Name for calling the value in memory Format for define variables [, ]; Example char a,b,c; unsigned int d;
14
Expressions Expressions in form operand1 operator operand2 OperatorMeaningExpressionResult +Add7 + 310 -Substract7 – 34 *Multiple7 * 321 /Divide7 / 32.333 %Modulate7 % 31
15
Statements The complete command to instruct computer to do something Enclose with ; (semi-colon) Blank space is not important
16
Assignment Statements following Form It mean calculate expression in right side and assign in variable in left side = Expression;
17
Input & Output Character Code String Output Statement Input Statement
18
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’
19
STRING A sequence of characters Represent enclose with “ and ” (double qoute) “Hello world!”
20
Output Statement command present string on monitor printf(charformat[,argument]); Example printf(“Hello World!”); a = 12; printf(“Hello! Number%d”,a);
21
Output Statement Type this program #include /* Call module stdio.h */ void main(void) /* Main function*/ { char *msg = “ ชื่อของนักศึกษา \n”; intid = รหัสนักศึกษา 4 ตัวท้าย ; printf(”%s student-code = %d \n”,msg,id); }
22
Input Statement Command get value from keyboard scanf(char-format[,address]); #include 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); }
23
งานปฏิบัติการ 5 จงเขียนโปรแกรมรับค่าความยาวฐานของ สามเหลี่ยมและความสูงของสามเหลี่ยมแล้วนำมา ประมวลผลหาค่าพื้นที่ของสามเหลี่ยม Input Base Length : 10 Input Height : 5 Output Area of Triangle = 25
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.