Instructor: Chien-Ho Ko Introduction to C Instructor: Chien-Ho Ko
Outlines Introduction to Computers Programming Language C Programming Environment How to Run C Programs First Program
Introduction to Computers Computer VS Computer Systems Computer Hardware Categories of Software
Computer VS Computer Systems
Computer VS Computer Systems Hardware (the computer) Data (information) Software (program) People (User)
Computer Hardware
Computer Hardware Memory Storage Processor Output Input Output
Categories of Software System software: tells computers how to use its own components: Windows, Unix, Linux, Macintosh… (interpreter between Hardware & application) Application software: tells computers how to accomplish specific tasks
Programming Language What is Programming Language Machine Languages Assembly Languages High Level Languages
What is Programming Language A set of instructions that perform a specific task What is Programming Language An artificial & formal language making up instructions and a set of precise grammar rules
Machine Languages The natural language of a computer Drawbacks Instructions are extremely difficult Simple problems require large number instructions Machine-dependent Un-portable 0101 1000 0001 0101 1011 0001 0101 0000 0001
Assembly Languages Instructions consists of English-like abbreviations to make computers easier to program. Drawbacks Simple problems require large number instructions Machine-dependent Un-portable L 1, GROSSPAY S 1, TAX ST 1, NETPAY
High Level Languages Instructions are quite English-like Single instruction: operations Drawback Must be converted to machine languages High level Assembly Machine NETPAY = GROSSPAY - TAX;
C Programming Environment Why C How Does C Work How Does NXC Work IDE Language & Libraries
Why C-Never Out of Date 1970’s Dennis Ritchie, Bell Lab. High level (convenient) + assembly (powerful) Approved by American National Standard Institute Portable C++=C + OO + New Feature
Why C-Never Out of Date NXC adopts C grammar The most popular language for NXT
How Does C Work After loader transfers load module to main memory, CPU executes Process Text (source program) Preprocessor (special instructions) Translator (compiler) Object Program (machine language) Linker (library) Load module (Executable machine language)
How Does NXC Work After firmware loads bytecode to memory, CPU executes Process Text (NXT code, source program) Preprocessor (special instructions) Translator (compiler) Linker (library) Object Program (Next bytecode in PC) Object Program (Next bytecode in NXT)
Integrated Develop Environment Text editor+ Preprocessor +C Compiler+ Library + Debug Microsoft VC++ Borland Turbo C++ Text editor+ Preprocessor +NXC Compiler+ Library + Debug BricxCC
Language & Libraries Language Libraries Provides basic operations Extend C Standard libraries: # include <stdio.h> Programmer-defined libraries: # include <chko.h>
How to Run C Programs Edit: Create text, 03_c01.c Preprocess: Special instructions Compile: machine language Link: libraries Run: executable machine language
First Program 1 /************************************************************* 2 Program Filename: 03_c01.c 3 Author: Chien-Ho Ko 4 Purpose: Draw triangle and trapezoid. 5 Output to: Screen 6 *************************************************************/ 7 #include <stdio.h> 8 9 int main(void) { 10 11 /* Function body: */ 12 13 printf("+----------------+\n"); 14 printf("| |\n"); 15 printf("| |\n"); 16 printf("| |\n"); 17 printf("+----------------+\n"); 18 19 printf(" /\\\n"); 20 printf(" / \\\n"); 21 printf(" / \\\n"); 22 printf(" / \\\n"); 23 printf("+------------+\n"); 24 25 return 0; 26 } /* end function main */
First Program NXC 1 /************************************************************* 2 Program Filename: 03_NXC01.nxc 3 Author: Chien-Ho Ko 4 Purpose: Basic robot control. 5 Output to: Motors 6 *************************************************************/ 7 8 task main() 9 { 10 OnFwd(OUT_A, 75); 11 OnFwd(OUT_C, 75); 12 Wait(100); 13 Off(OUT_AC); 14 } /* end function main */