Introduction to Computer Programming

Introduction to Computer Programming

tment of Computer Engineering y of Engineering, Prince of Songkla Univ 241 - 101 (Introduction to com puter programming) 01: 2 1 tment of Computer Engineering y of Engineering, Prince of Songkla Univ 3

Introduction to C Programming 01: 2 2 1. 2. 3. 4. 01: 2 3 1.

(Computer Program) (Programming) FORTRAN, COBOL, PASCAL, C, C++, Java, Prolog, Lisp 01: 2 4 2.

(Comment) 01: 2 5

(Function)Function) (Execute) 1 main 01: 2 6 main main

int main(void) { .. . return 0; } int main() { .. . return 0; } 01: 2

7 1. Source File (Text File) (Syntax) .c 01: 2 8 2. (Compile) (C Compiler) Source File Object File

- Borland C (DOS, Windows) - GCC (Unix, Linux) - C-Free (Dos, Windows) - MinGW (Windows) 01: 2 9 3. (Link) (Linker) Object File C Library Object File (Execute File) 01: 2

10 01: 2 11 C Compiler LinkerC Library Execute Fi ource File Computer Object File Computer 01: 2 12

(Statement) ; printf("Hello World\n"); (Compound Statement) int main() { printf("Hello World\n"); return 0; } 01: 2 13 (Variable)

int, char, float, double 01: 2 14 (Variable Type) () char 1 1 int 4 float ( 6 4 )

double ( 12 8 ) 01: 2 15 () long - - long int, long double unsigned - - unsigned int, unsigned char, unsigned long int

01: 2 16 _ 01: 2 17

() (Keywords) auto volatile switch short signed sizeof static default break while typedef union unsigned void float

do case char const continue register far double else enum extern 01: 2 struct goto if int long 18

1 number 2value grade1 student_id float _score INT 01: 2 19

: ; int number; char grade; 01: 2 20 2 num_std 1 grade (A,B,C,D,E)

gpa 01: 2 21 , int a; int b; int a,b; 01: 2 22

= int score; score = 80; score 80 01: 2 23 3 int a = b = c =

a,b,c; 2; 3; a + b; c 01: 2 24 4 int a = a = b = a,b; 2; 5; a + 4;

b 01: 2 25 5 int a; a = 2; a = a + 3; a 01: 2 26 (Constant)

2, 100, 0, -5 012 12 8 0x5f 5f16 4 ( int) 1.0, 2.25, 0.00, -10.72 4E-6 4e-6 4 x 10-6 1.75E+8 1.75e+8 1.75 x 108 8 ( double)

01: 2 27 () A 1 'A' (String) "Computer" Computer

String '\0' + 1 Null Character 01: 2 28 (Expression) Operator Operator (Operators)

(Operands) 2 + 3 a - b Operands Operands 01: 2 29

: = ; int num = 2; char ch = 'A'; float a = 1.25, b = 0.5; int std1 = 20, std2; 01: 2 30 (Preprocessor)

# #include #endif #elif #define #if #error #ifndef #else 01: 2 #program #undef #ifdef 31 #include

#include stdio.h (/usr/include)usr/usr/include)include) #include "sample.h" sample.h .h

01: 2 32 #define #define PI 3.14159 3.14159 PI #define START_STR "Welcome to my program."

START_STR Welcome to my program. 01: 2 33 (Comment) 2 /usr/include)*.............*/usr/include) /usr/include)/usr/include).............

comment 01: 2 34 /usr/include)*Name: Pipat Student ID: 5035512049*/usr/include) #include #define PI 3.14159 int main() { float r,contour,area; r = 2.5; /usr/include)/usr/include)calculate contour and area of circle contour = 2*PI*r; area = PI*r*r; : :

return 0; } 01: 2 35 3. (Input and Output) (Output) (Standard Input) :, (Output) (Standard

Output) :- , , 01: 2 36 printf include stdio.h : printf(control,argument list); 01: 2 37 printf

control printf("Computer Engineering"); Computer Engineering 01: 2 38 printf , , argument list ,

, , int a = 2, b = 3; printf("a = %d and b = %d\n", a,b); printf("a + b = %d", a+b); a = 2 and b = 3 a + b = 5 %d (Format Code) 01: 2 39

6 #include int main() { int x = 2,y = 5; y = y + x; printf("x = %d, y = %d\n",x,y); printf("value of x-y = %d\n",x-y); return 0; } 01: 2 40 (Format Code) %c

char 1 %d int %f float 6 %s string (String) 01: 2 41 () %ld long int %u unsigned int %o

int %x int %e float,double %lf double double 01: 2 42 7 #include int main() { int num = 10; printf("num = %d\n",num); printf("In octadecimal, num =

printf("In hexadecimal, num = return 0; num = 10 } In octadecimal, In hexadecimal, 01: 2 %o\n",num); %x\n",num); num = 12 num = a 43 8 #include int main() { int num1,num2;

num1 = 0xb; num2 = 010; printf("num1 = %d\n",num1); printf("num2 = %d\n",num2); return 0; } num1 = 11 num2 = 8 01: 2 44 9 #include int main() { float r1; r1 = 12.5; printf("r1 = %f\n",r1); printf("r1 = %e\n",r1);

return 0; r1 = 12.500000 } r1 = 1.250000e+01 01: 2 45 ASCII ASCII American Standard Code for Information Interchange 0 - 127: , , Enter, Back Space, Space Bar, Tab

128 - 255: Graphic Character 01: 2 46 (Characters) char A A ASCII 65

a a ASCII 97 4 4 ASCII 52 \66 ASCII 66 B 01: 2 47 10 #include int main() { char alpha,ch; alpha = 'A'; printf("letter %c ASCII = %d\n",alpha,alpha); alpha = 66; printf("letter %c ASCII = %d\n",alpha,alpha); ch = alpha + 1; printf("ch = %c\n",ch); letter A ASCII = 65

return 0; letter B ASCII = 66 } ch = C 01: 2 48 (String) "" ( Double Quote) char char name[10]; name string

9 1 '\0' 10 01: 2 49 string #include int main() { char my_name[10] = Gerrard"; printf("My name is %s\n",my_name); printf("%s %s\n","Hello",my_name); return 0; } My name is Gerrard Hello Gerrard 01: 2

50 Escape Sequence Escape Sequence (ASCII) \ (Back \n 0x0a -Slash) \t 0x09

\b 0x08 \" 0x22 tab " 01: 2 51 (Field)

+, -, int a = 20; printf("%5d\n",a); a 5 01: 2 52 #include int main() { int num = 12; printf("|12345|\n"); printf("|%5d|\n",num);

printf("|%-5d|\n",num); printf("|%.5d|\n",num); return 0; } 01: 2 |12345| | 12| |12 | |00012| 53 : %a.bf

a b %7.2f 7 2 01: 2 54 #include int main() { printf("|123456|\n"); printf("|%6.1f|\n",4.23); printf("|%6.2f|\n",1.629); printf("|%-6.2f|\n",1.629);

return 0; } |123456| | 4.2| | 1.63| |1.63 | 01: 2 55 String #include int main() { printf("|12345678|\n"); printf("|%8s|\n","ComEng"); printf("|%8s|\n","Computer Engineering"); printf("|%.8s|\n","Computer Engineering"); return 0; |12345678|

} | ComEng| |Computer Engineering| |Computer| 01: 2 56 scanf include stdio.h : scanf(control,argument list); control : (%c,%d,%f,%s,...) ""

argument list : (Address) & Address a &a 01: 2 57 scanf scanf("%d",&a); - a scanf("%c",&ch); - 1

ch scanf("%f",&num); - num scanf("%s",str); - (String) str 01: 2 58 #include int main() { int age; char name[50];

printf("Enter your name: "); scanf("%s",name); printf("Enter your age: "); scanf("%d",&age); printf("%s is %d years old.\n",name,age); return 0; } Enter your name: Paris Enter your age: 23 Paris is 23 years old. 01: 2 59 #include int main() { int num1,num2,diff; printf("Enter two number: "); scanf("%d%d",&num1,&num2); diff = num1 - num2;

printf("num1 - num2 = %d\n",diff); return 0; } Enter two number: 5 3 num1 - num2 = 2 01: 2 60 getchar putchar gets

puts include stdio.h 01: 2 61 getchar 1 Enter scanf %c putchar

1 printf %c 01: 2 62 #include int main() { char ch; ch = getchar(); printf("Here is the output...\n"); putchar(ch); return 0; } A Here is the output... A

01: 2 63 gets string scanf %s puts string printf %s

01: 2 64 #include int main() { char name[50]; gets(name); puts("Here is the output..."); puts(name); return 0; } Steven Gerrard Here is the output... Steven Gerrard 01: 2

65 (Operators) (Arithmetic Operators) (Relational Operators) (Equality Operators) (Logical Operators) (Compound Operators) (Sizeof Operator) (Conditional

Operator) 01: 2 66 (Arithmetic Operator) + * /usr/include) % ++ -- (Modulo) 1

1 01: 2 67 11. 10/usr/include)5 _____________ 3/usr/include)9 _____________ 7/usr/include)2 _____________ 7.0/usr/include)2 _____________ 21%4 _____________ 10%7 _____________ 10%2 _____________ 3%7

_____________ 01: 2 68 () Ex. -1,-2 * /usr/include) % + 01: 2 69 12.

10 + 2 * 3 * -1 _____________ 2 * 5 % 8 + -7 _____________ (3 + 5/usr/include)2)*(7 - 3 _____________ - 5%2) 01: 2 70 13 ( 5 3 ) #include

int main() { int a,b,c; printf("Enter value of scanf("%d",&a); printf("Enter value of scanf("%d",&b); c = a*(a + a/usr/include)b); printf("c = %d\n",c); return 0; } a: "); b: "); Enter value of a: 5 Enter value of b: 3 ___________________ 01: 2 71 14.

y y = x2 + 2x + 4 x y x y 1. x 2. y 3. y 01: 2 72 #include int main() { int x,y; printf("Enter value of x: "); scanf("%d",&x);

y = x*x + 2*x + 4; printf("y = %d\n",y); return 0; } 01: 2 73 (Increment & Decrement) ++ -- x = ++n; x = n++; n

5 x = 6, n = 6 x = 5, n = 6 01: 2 74 15. #include int main() { int a = 2,b,c; b = ++a; printf("b = %d\n",b); c = b++; printf("b = %d,c = %d\n",b,c); printf("%d\n",c++); ___________________ return 0;

___________________ ___________________ } 01: 2 75 > <= >= ==

!= < 2 (1) (0) 01: 2 76 16. 5 > 6 _____________ 2 <= (3-1)

_____________ (2 == 3) != (4 >= 5) 8 > 5 > 3 _____________ _____________ 01: 2 77 (Logical Operators) !, &&, ||

2 (1) (0) ! (Not) p !p 0 1 1 0 01: 2 78

&& (And) || (Or) p q p && q p q

p || q 0 0 0 0 0 0 0 1 0 0 1

1 1 0 0 1 0 1 1 1 1 1

1 1 01: 2 79 17. !(1 && 0) _____________ (2 || 0) && 3 _____________ (2 < 3) && (5 > 6) _____________ 01: 2

80 () - ! * /usr/include) % + < > <= >= == != && || 01: 2 81

(Compound Operator) x = x + 1; x += 1; a = a - b; a -= b; x = x * 2; x *= 2; y = y /usr/include) 5; x /usr/include)= 5; 01: 2 82

18 int num1 = 3,num2 = 1; num1 *= 4; num2 += num1 - 2; num2 01: 2 83 (Sizeof Operator) : sizeof()

sizeof() int num; printf("size of num = %d\n",sizeof(num)); 01: 2 84 (Conditional Operator) a?b:c a b a c

num = (x < y) ? x : y x = 2 y = 3 2 x = 5 y = 4 4 num num 01: 2 85 (Casting)

: () float div; int a = 1, b = 2; div = (float)a/usr/include)b; 01: 2 86

Recently Viewed Presentations

  • สถาปัตยกรรมคอมพิวเตอร์ (Computer Architecture)

    สถาปัตยกรรมคอมพิวเตอร์ (Computer Architecture)

    Multiprocessor support Re-entrant code inside OS Plug and play Power management FAT-32 file system Optional NTFS file system Win32 API Run all old MS-DOS program Some critical OS data writable by user เดิมโค้ดในการพัฒนาเวอร์ชันต่อไปของ Windows NT 4.0 ใช้ชื่อ ...
  • The Writing Revolution

    The Writing Revolution

    What Makes a Sentence a Sentence?Fragments. A fragment is a group of words that is not grammatically a complete sentence. It is missing a key component of the sentence, usually lacking a subject, verb, or both. Read the following and...
  • Career Exploration Learning Objectives:  Students can give at

    Career Exploration Learning Objectives: Students can give at

    Gather information from job descriptions, Career Central, O*NET, and other websites to learn more about job duties, responsibilities, required education/training, minimum experience, earnings, and advancement opportunities.
  • Transformation of the Academic Library Kurt De Belder ...

    Transformation of the Academic Library Kurt De Belder ...

    Transformation of the Academic LibraryKurt De Belder. OCLC Research Distinguished Seminar, 10 May 2013
  • Improving Group Climate - UMW Blogs

    Improving Group Climate - UMW Blogs

    Improving Group Climate Group Cohesiveness attraction loyalty "Esprit de corps" Group's goal becomes one's own Groupthink! Gibb's Defensive Supportive Climate behaviors Evaluation Support Control Problem-orientation Strategy Spontaneity Neutrality Empathy Superiority Equality Certainty Provisionalism Confirming & Disconfirming Responses Confirming : direct,...
  • Chapter 25 Organic and Biological Chemistry

    Chapter 25 Organic and Biological Chemistry

    Structure of Alkanes. Carbons in alkanes are . sp. 3 hybridized, tetrahedral, and have 109.5° bond angles. In the straight chain form, all carbons connect in a continuous chain. C can make four bonds, so it is possible for a...
  • Economic Evaluation Tools - Michigan State University

    Economic Evaluation Tools - Michigan State University

    Economic Evaluation Tools Benefit-Cost Analysis Cost Effectiveness Analysis Financial Analysis/Feasibility Fiscal Impact Analysis Economic Impact Analysis Social/Environmental Impact Analysis TYPES OF ECONOMIC ANALYSIS Economic impact analysis (EIA) -- What is the contribution of tourism activity to the economy of the...
  • Search Engines and Cloud Computing - PY4E

    Search Engines and Cloud Computing - PY4E

    Helvetica Arial Calibri Lucida Grande Gill Sans MS Pゴシック White 1_White 2_White 3_White 4_White 5_White 6_White 7_White 8_White 9_White 10_White 11_White 12_White 13_White 14_White 15_White 16_White 17_White 18_White 19_White 20_White 21_White 22_White Retrieving and Visualizing Data Multi-Step Data Analysis Many...