Introduction to C - Cornell University · Introduction to C Introduction and Hello World...

25
Introduction to C Introduction and Hello World Instructor: Yin Lou 01/24/2011 Introduction to C CS 2022, Spring 2011, Lecture 1

Transcript of Introduction to C - Cornell University · Introduction to C Introduction and Hello World...

Page 1: Introduction to C - Cornell University · Introduction to C Introduction and Hello World Instructor: Yin Lou 01/24/2011 Introduction to C CS 2022, Spring 2011, Lecture 1

Introduction to CIntroduction and Hello World

Instructor: Yin Lou

01/24/2011

Introduction to C CS 2022, Spring 2011, Lecture 1

Page 2: Introduction to C - Cornell University · Introduction to C Introduction and Hello World Instructor: Yin Lou 01/24/2011 Introduction to C CS 2022, Spring 2011, Lecture 1

Administrivia

I Instructor: Yin Lou, 4144 Upson

I Email: [email protected]

I Lectures: MWF 12:20-1:10pm, HLS 306

I Office Hours: TBD

I http://www.cs.cornell.edu/courses/cs2022/2011sp/

I CMS: https://cms.csuglab.cornell.edu

I Email me your netid if you are not in CMS!

Introduction to C CS 2022, Spring 2011, Lecture 1

Page 3: Introduction to C - Cornell University · Introduction to C Introduction and Hello World Instructor: Yin Lou 01/24/2011 Introduction to C CS 2022, Spring 2011, Lecture 1

Administrivia

I Instructor: Yin Lou, 4144 Upson

I Email: [email protected]

I Lectures: MWF 12:20-1:10pm, HLS 306

I Office Hours: TBD

I http://www.cs.cornell.edu/courses/cs2022/2011sp/

I CMS: https://cms.csuglab.cornell.eduI Email me your netid if you are not in CMS!

Introduction to C CS 2022, Spring 2011, Lecture 1

Page 4: Introduction to C - Cornell University · Introduction to C Introduction and Hello World Instructor: Yin Lou 01/24/2011 Introduction to C CS 2022, Spring 2011, Lecture 1

Goals

I C syntax

I Standard libraries

I Programming for robustness and speed

I Understanding compiler

Introduction to C CS 2022, Spring 2011, Lecture 1

Page 5: Introduction to C - Cornell University · Introduction to C Introduction and Hello World Instructor: Yin Lou 01/24/2011 Introduction to C CS 2022, Spring 2011, Lecture 1

Topics

Lect01: Introduction and Hello World

Lect02: Control Flow

Lect03: Functions and Make

Lect04: Pointers and Arrays

Lect05: Complex Types

Lect06: Memory Model

Lect07: Performance

Lect08: Preprocessor

Lect09: Standard Input and Output

Lect10: File and Variable-length Argument Lists

Lect11: Threads and Synchronization

Lect12: To Be a Master Programmer

Introduction to C CS 2022, Spring 2011, Lecture 1

Page 6: Introduction to C - Cornell University · Introduction to C Introduction and Hello World Instructor: Yin Lou 01/24/2011 Introduction to C CS 2022, Spring 2011, Lecture 1

Environment

I OS: GNU/Linux

I Editor: Vim

I Compiler: gcc

I Debugger: gdb

Introduction to C CS 2022, Spring 2011, Lecture 1

Page 7: Introduction to C - Cornell University · Introduction to C Introduction and Hello World Instructor: Yin Lou 01/24/2011 Introduction to C CS 2022, Spring 2011, Lecture 1

Introduction to Vim

I $ vim hello.c

I i - Enter editing mode

I <esc> - Enter normal mode

More Normal Mode Commands

I :w - Write/Save

I :q - Quit

I :q! - Quit without saving

I :wq - Write and Quite

Introduction to C CS 2022, Spring 2011, Lecture 1

Page 8: Introduction to C - Cornell University · Introduction to C Introduction and Hello World Instructor: Yin Lou 01/24/2011 Introduction to C CS 2022, Spring 2011, Lecture 1

Introduction to Vim

More Commands

I SearchI / + <pattern>I n - next search matchI N - previous search match

I GotoI : + <line no>I gg - Goto first lineI G - Goto last line

I Delete LineI dd

Google “vim cheat sheet” for more!

Introduction to C CS 2022, Spring 2011, Lecture 1

Page 9: Introduction to C - Cornell University · Introduction to C Introduction and Hello World Instructor: Yin Lou 01/24/2011 Introduction to C CS 2022, Spring 2011, Lecture 1

Structure of a C Program

Overall Program

<some pre-processor directives>

<global declarations><global variables>

<functions>

Functions

<function header><local declarations>

<statements>

Introduction to C CS 2022, Spring 2011, Lecture 1

Page 10: Introduction to C - Cornell University · Introduction to C Introduction and Hello World Instructor: Yin Lou 01/24/2011 Introduction to C CS 2022, Spring 2011, Lecture 1

Structure of a C Program

Overall Program

<some pre-processor directives>

<global declarations><global variables>

<functions>

Functions

<function header><local declarations>

<statements>

Introduction to C CS 2022, Spring 2011, Lecture 1

Page 11: Introduction to C - Cornell University · Introduction to C Introduction and Hello World Instructor: Yin Lou 01/24/2011 Introduction to C CS 2022, Spring 2011, Lecture 1

hello.c: Hello World

#include <stdio.h>

int main()

{

printf("Hello World\n");

return 0;

}

Introduction to C CS 2022, Spring 2011, Lecture 1

Page 12: Introduction to C - Cornell University · Introduction to C Introduction and Hello World Instructor: Yin Lou 01/24/2011 Introduction to C CS 2022, Spring 2011, Lecture 1

Compiling and Running

I $ gcc hello.c -o hello

I $ ./helloHello World

Introduction to C CS 2022, Spring 2011, Lecture 1

Page 13: Introduction to C - Cornell University · Introduction to C Introduction and Hello World Instructor: Yin Lou 01/24/2011 Introduction to C CS 2022, Spring 2011, Lecture 1

What Happens?

I $ gcc hello.c -o helloI Compile “hello.c” to machine code named “hello”I “-o” specifies the output file name. (Notice it’s case-sensitive.)

I $ ./helloI Execute program “hello”I “./” is necessay!

hello.c

#include <stdio.h> // "printf" is declared in this header file.

int main() // Main point of execution.

{

printf("Hello World\n"); // Output "Hello World" to console.

return 0; // Tell OS the program terminates normally.

}

Introduction to C CS 2022, Spring 2011, Lecture 1

Page 14: Introduction to C - Cornell University · Introduction to C Introduction and Hello World Instructor: Yin Lou 01/24/2011 Introduction to C CS 2022, Spring 2011, Lecture 1

What Happens?

I $ gcc hello.c -o helloI Compile “hello.c” to machine code named “hello”I “-o” specifies the output file name. (Notice it’s case-sensitive.)

I $ ./helloI Execute program “hello”I “./” is necessay!

hello.c

#include <stdio.h> // "printf" is declared in this header file.

int main() // Main point of execution.

{

printf("Hello World\n"); // Output "Hello World" to console.

return 0; // Tell OS the program terminates normally.

}

Introduction to C CS 2022, Spring 2011, Lecture 1

Page 15: Introduction to C - Cornell University · Introduction to C Introduction and Hello World Instructor: Yin Lou 01/24/2011 Introduction to C CS 2022, Spring 2011, Lecture 1

vars.c: Variables

#include <stdio.h>

int main()

{

int a, b, c;

a = 10;

b = 20;

c = a * b;

printf("a = %d b = %d c = %d\n", a, b, c);

return 0;

}

a = 10 b = 20 c = 200

Introduction to C CS 2022, Spring 2011, Lecture 1

Page 16: Introduction to C - Cornell University · Introduction to C Introduction and Hello World Instructor: Yin Lou 01/24/2011 Introduction to C CS 2022, Spring 2011, Lecture 1

vars.c: Variables

#include <stdio.h>

int main()

{

int a, b, c;

a = 10;

b = 20;

c = a * b;

printf("a = %d b = %d c = %d\n", a, b, c);

return 0;

}

a = 10 b = 20 c = 200

Introduction to C CS 2022, Spring 2011, Lecture 1

Page 17: Introduction to C - Cornell University · Introduction to C Introduction and Hello World Instructor: Yin Lou 01/24/2011 Introduction to C CS 2022, Spring 2011, Lecture 1

cmdarg.c: Command Line Args

#include <stdio.h>

#include <stdlib.h>

int main(int argc, char **argv)

{

int n, m;

n = atoi(argv[1]);

m = atoi(argv[2]);

printf("Argument 1: %d\nArgument 2: %d\n", n, m);

return 0;

}

$ ./cmdarg 10 20Argument 1: 10Argument 2: 20

Introduction to C CS 2022, Spring 2011, Lecture 1

Page 18: Introduction to C - Cornell University · Introduction to C Introduction and Hello World Instructor: Yin Lou 01/24/2011 Introduction to C CS 2022, Spring 2011, Lecture 1

cmdarg.c: Command Line Args

#include <stdio.h>

#include <stdlib.h>

int main(int argc, char **argv)

{

int n, m;

n = atoi(argv[1]);

m = atoi(argv[2]);

printf("Argument 1: %d\nArgument 2: %d\n", n, m);

return 0;

}

$ ./cmdarg 10 20Argument 1: 10Argument 2: 20

Introduction to C CS 2022, Spring 2011, Lecture 1

Page 19: Introduction to C - Cornell University · Introduction to C Introduction and Hello World Instructor: Yin Lou 01/24/2011 Introduction to C CS 2022, Spring 2011, Lecture 1

More on printf

I printf(format string, val1, val2);

I format string can include placeholders that specify how thearguments val1, val2, etc. should be formatted

I %c : format as a characterI %d : format as an integerI %f : format as a floating-point numberI %% : print a % character

Examples

float f = 0.95;

printf("f = %f%%\n", f * 100);

f = 95.000000%

Introduction to C CS 2022, Spring 2011, Lecture 1

Page 20: Introduction to C - Cornell University · Introduction to C Introduction and Hello World Instructor: Yin Lou 01/24/2011 Introduction to C CS 2022, Spring 2011, Lecture 1

More on printf

I printf(format string, val1, val2);I format string can include placeholders that specify how the

arguments val1, val2, etc. should be formattedI %c : format as a characterI %d : format as an integerI %f : format as a floating-point numberI %% : print a % character

Examples

float f = 0.95;

printf("f = %f%%\n", f * 100);

f = 95.000000%

Introduction to C CS 2022, Spring 2011, Lecture 1

Page 21: Introduction to C - Cornell University · Introduction to C Introduction and Hello World Instructor: Yin Lou 01/24/2011 Introduction to C CS 2022, Spring 2011, Lecture 1

More on printf

I printf(format string, val1, val2);I format string can include placeholders that specify how the

arguments val1, val2, etc. should be formattedI %c : format as a characterI %d : format as an integerI %f : format as a floating-point numberI %% : print a % character

Examples

float f = 0.95;

printf("f = %f%%\n", f * 100);

f = 95.000000%

Introduction to C CS 2022, Spring 2011, Lecture 1

Page 22: Introduction to C - Cornell University · Introduction to C Introduction and Hello World Instructor: Yin Lou 01/24/2011 Introduction to C CS 2022, Spring 2011, Lecture 1

More on printf

I printf(format string, val1, val2);I format string can include placeholders that specify how the

arguments val1, val2, etc. should be formattedI %c : format as a characterI %d : format as an integerI %f : format as a floating-point numberI %% : print a % character

Examples

float f = 0.95;

printf("f = %f%%\n", f * 100);

f = 95.000000%

Introduction to C CS 2022, Spring 2011, Lecture 1

Page 23: Introduction to C - Cornell University · Introduction to C Introduction and Hello World Instructor: Yin Lou 01/24/2011 Introduction to C CS 2022, Spring 2011, Lecture 1

More on printf

I Placeholders can also specify widths and precisionsI %10d : add spaces to take up at least 10 charactersI %010d : add zeros to take up at least 10 charactersI %.2f : print only 2 digits after decimal pointI %5.2f : print 1 decimal digit, add spaces to take up 5 chars

Examples

float f = 0.95;

printf("f = %.2f%%\n", f * 100);

// f = 95.00%

printf("f = %10.2f%%\n", f * 100);

// f = 95.00%

Introduction to C CS 2022, Spring 2011, Lecture 1

Page 24: Introduction to C - Cornell University · Introduction to C Introduction and Hello World Instructor: Yin Lou 01/24/2011 Introduction to C CS 2022, Spring 2011, Lecture 1

More on printf

I Placeholders can also specify widths and precisionsI %10d : add spaces to take up at least 10 charactersI %010d : add zeros to take up at least 10 charactersI %.2f : print only 2 digits after decimal pointI %5.2f : print 1 decimal digit, add spaces to take up 5 chars

Examples

float f = 0.95;

printf("f = %.2f%%\n", f * 100);

// f = 95.00%

printf("f = %10.2f%%\n", f * 100);

// f = 95.00%

Introduction to C CS 2022, Spring 2011, Lecture 1

Page 25: Introduction to C - Cornell University · Introduction to C Introduction and Hello World Instructor: Yin Lou 01/24/2011 Introduction to C CS 2022, Spring 2011, Lecture 1

Warning about printf

I printf is powerful, but potentially dangerous

What does this code output?

int i = 90;

float f = 3;

printf("f = %f i = %d\n", f);

printf("f = %f\n", f, i);

printf("i = %d f = %f\n", f, i);

Introduction to C CS 2022, Spring 2011, Lecture 1