Introduction to C Tom Chao Zhou CSC2100B Data Structures Tutorial 1.

Slides:



Advertisements
Similar presentations
C Language.
Advertisements

I/O means Input and Output. One way: use standard input and standard output. To read in data, use scanf() (or a few other functions) To write out data,
Computer Programming w/ Eng. Applications
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Winter2015 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University C: Advanced Topics.
Introduction to C Programming
C Programming Revision Malcolm Wilson. Variables Types int, char, double, long. NO type for string see later. unsigned above. assignment X=2 ; C=‘v’;
Introduction to Systems Programming - Recitation Omer Kotlicki Two instances: 1.Tuesdays 15:00-16:00; Kitot Wednesdays.
Switch Switch case statements are a substitute for long if statements that compare a variable to several "integral" values ("integral" values are simply.
CSSE221: Software Dev. Honors Day 27 Announcements Announcements Projects turned in? Projects turned in? The 2 required Angel surveys are due by 9 pm tonight.
C. About the Crash Course Cover sufficient C for simple programs: variables and statements control functions arrays and strings pointers Slides and captured.
15213 C Primer 17 September Outline Overview comparison of C and Java Good evening Preprocessor Command line arguments Arrays and structures Pointers.
C Programming Language tutorial Powered by:-
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
Pointers CSE 2451 Rong Shi.
By: Mr. Baha Hanene Chapter 3. Learning Outcomes We will cover the learning outcome 02 in this chapter i.e. Use basic data-types and input / output in.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students C File Input and Output Checking input for errors.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
#include int main(void) { printf("Hello, world!\n"); return 0; } entry point called on program start only one main( ) in any program # for preprocessor.
C Programming Lecture 10-1 : Array & Pointer. Character Array String A sequence of characters The last character should be ‘\0’ that indicates “the end.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
CECS 130 EXAM 1.  int main() { printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, L); printf (" %10d \n", 1977); printf ("%010d \n", 1977);
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
1 CSC2100B Data Structure Tutorial 1 Online Judge and C.
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
DATA STRUCTURE & ALGORITHMS Pointers & Structure.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve.
What we will cover A crash course in the basics of C “Teach yourself C in 21 days”
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
COMP Loop Statements Yi Hong May 21, 2015.
BY ILTAF MEHDI (MCS, MCSE, CCNA)1. INSTRUCTOR: ILTAF MEHDI (MCS, MCSE, CCNA, Web Developer) BY ILTAF MEHDI (MCS, MCSE, CCNA)2 Chapter No: 04 “Loops”
Pointer Lecture 2 Course Name: High Level Programming Language Year : 2010.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Introduction to C Zhengwei Yang CSC2100 Data Structures Tutorial 1.
CISC105 – General Computer Science Class 4 – 06/14/2006.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
Functions, Scope & File IO C++ Lecture 4 Bhaskar Bhattacharya.
Chapter 1.2 Introduction to C++ Programming
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Chapter 1.2 Introduction to C++ Programming
User-Written Functions
MT262A Review.
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
C Primer.
CSE 220 – C Programming Pointers.
A bit of C programming Lecture 3 Uli Raich.
C Programming Tutorial – Part I
What's a Computer? Monitor Disk Main mouse Memory Keyboard Network
File Access (7.5) CSE 2031 Fall July 2018.
Quiz 11/15/16 – C functions, arrays and strings
EDUSITE Introduction to C manjari2707.wordpress.com.
Week 4 – Repetition Structures / Loops
C programming language
CS111 Computer Programming
Administrative things
Programming in C Input / Output.
LINKED LISTS.
Programming in C Input / Output.
Yuanming Yu CSC2100B Data Structures Tutorial 1
CS1100 Computational Engineering
بنام خدا زبان برنامه نویسی C (21814( Lecture 11 Pointers
CSC215 Lecture Control Flow.
C Programming Getting started Variables Basic C operators Conditionals
Programming in C Input / Output.
CSC215 Lecture Control Flow.
Course Outcomes of Programming In C (PIC) (17212, C203):
15213 C Primer 17 September 2002.
Files Chapter 8.
Getting Started With Coding
Presentation transcript:

Introduction to C Tom Chao Zhou CSC2100B Data Structures Tutorial 1

Outline Information Introduction to C  Basics  If Statement  Loops  Functions  Switch case  Pointers  Structures  File I/O

Information Your TA Team:  Jianye Hao jyhao AT cse.cuhk.edu.hk  Tom Chao Zhou czhou AT cse.cuhk.edu.hk  Xin Xin xxin AT cse.cuhk.edu.hk  Haifeng Wan hfwan AT cse.cuhk.edu.hk

Information Course Information: Web Page:  Tutorial Page:  Anti-plagiarism Policy: 

Information Assignment  There will be both written and programming parts in assignments. Written part: submit to the assignment box in 10/F SHB. Programming part: via Online Judge systems. (Will be introduced next week)  You will receive your login Id for CSC2100B online judge via your account. (A few days later). Keep it safe and do not disclose it.

Introduction to C Basics If Statement Loops Functions Switch case Pointers Structures File I/O

Introduction to C: Basics //a simple program that has variables #include int main() { int x; //(32 bits) char y; //(8 bits) float z; //(32 bits) double t; //(64 bits) printf(“hello world…\n”); int test; //wrong, The variable declaration must appear first return 0; }

Introduction to C: Basics //reading input from console #include int main() { int num1; int num2; printf( "Please enter two numbers: " ); scanf( "%d %d", &num1,&num2 ); printf( "You entered %d %d", num1, num2 ); return 0; }

Introduction to C: if statement #include int main() { int age; /* Need a variable... */ printf( "Please enter your age" ); /* Asks for age */ scanf( "%d", &age ); /* The input is put in age */ if ( age < 100 ) { /* If the age is less than 100 */ printf ("You are pretty young!\n" ); /* Just to show you it works... */ } else if ( age == 100 ) { /* I use else just to show an example */ printf( "You are old\n" ); } else { printf( "You are really old\n" ); /* Executed if no other statement is*/ } return 0; }

Introduction to C: Loops(for) #include int main() { int x; /* The loop goes while x < 10, and x increases by one every loop*/ for ( x = 0; x < 10; x++ ) { /* Keep in mind that the loop condition checks the conditional statement before it loops again. consequently, when x equals 10 the loop breaks. x is updated before the condition is checked. */ printf( "%d\n", x ); } return 0; }

Introduction to C: Loops(while) #include int main() { int x = 0; /* Don't forget to declare variables */ while ( x < 10 ) { /* While x is less than 10 */ printf( "%d\n", x ); x++; /* Update x so the condition can be met eventually */ } return 0; }

Introduction to C: Loops(do while) #include int main() { int x; x = 0; do { /* "Hello, world!" is printed at least one time even though the condition is false*/ printf( "%d\n", x ); x++; } while ( x != 10 ); return 0; }

Introduction to C: Loops(break and continue) #include int main() { int x; for(x=0;x<10;x++) { if(x==5) { break; } printf("%d\n",x); } return 0; } #include int main() { int x; for(x=0;x<10;x++) { if(x==5) { continue; } printf("%d\n",x); } return 0; }

Introduction to C: function #include //function declaration int mult ( int x, int y ); int main() { int x; int y; printf( "Please input two numbers to be multiplied: " ); scanf( "%d", &x ); scanf( "%d", &y ); printf( "The product of your two numbers is %d\n", mult( x, y ) ); return 0; } //define the function body //return value: int //utility: return the multiplication of two integer values //parameters: take two int parameters int mult (int x, int y) { return x * y; }

#include //function declaration, need to define the function body in other places void playgame(); void loadgame(); void playmultiplayer(); int main() { int input; printf( "1. Play game\n" ); printf( "2. Load game\n" ); printf( "3. Play multiplayer\n" ); printf( "4. Exit\n" ); printf( "Selection: " ); scanf( "%d", &input ); switch ( input ) { case 1: /* Note the colon, not a semicolon */ playgame(); break; //don't forget the break in each case case 2: loadgame(); break; case 3: playmultiplayer(); break; case 4: printf( "Thanks for playing!\n" ); break; default: printf( "Bad input, quitting!\n" ); break; } return 0; } switch case

Introduction to C: pointer variables Pointer variables are variables that store memory addresses. Pointer Declaration:  int x, y = 5;  int *ptr;  /*ptr is a POINTER to an integer variable*/ Reference operator &:  ptr = &y;  /*assign ptr to the MEMORY ADDRESS of y.*/ Dereference operator *:  x = *ptr;  /*assign x to the int that is pointed to by ptr */

Introduction to C: pointer variables

#include //swap two values void swap(int* iPtrX,int* iPtrY); void fakeswap(int x, int y); int main() { int x = 10; int y = 20; int *p1 = &x; int *p2 = &y; printf("before swap: x=%d y=%d\n",x,y); swap(p1,p2); printf("after swap: x=%d y=%d\n",x,y); printf(" \n"); printf("before fakeswap: x=%d y=%d\n",x,y); fakeswap(x,y); printf("after fakeswap: x=%d y=%d",x,y); return 0; } void swap(int* iPtrX, int* iPtrY) { int temp; temp = *iPtrX; *iPtrX = *iPtrY; *iPtrY = temp; } void fakeswap(int x,int y) { int temp; temp = x; x = y; y = temp; }

Introduction to C: struct #include //group things together struct database { int id_number; int age; float salary; }; int main() { struct database employee; employee.age = 22; employee.id_number = 1; employee.salary = ; }

//content in in.list //foo 70 //bar 98 //biz 100 #include int main() { FILE *ifp, *ofp; char *mode = "r"; char outputFilename[] = "out.list"; char username[9]; int score; ifp = fopen("in.list", mode); if (ifp == NULL) { fprintf(stderr, "Can't open input file in.list!\n"); exit(1); } ofp = fopen(outputFilename, "w"); if (ofp == NULL) { fprintf(stderr, "Can't open output file %s!\n", outputFilename); exit(1); } while (fscanf(ifp, "%s %d", username, &score) == 2) { fprintf(ofp, "%s %d\n", username, score+10); } fclose(ifp); fclose(ofp); return 0; } mode: r - open for reading w - open for writing (file need not exist) a - open for appending (file need not exist) r+ - open for reading and writing, start at beginning w+ - open for reading and writing (overwrite file) a+ - open for reading and writing (append if file exists) File I/O

Q & A