C LANGUAGE A Short Course

Similar documents
C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

Lecture 02 Summary. C/Java Syntax 1/14/2009. Keywords Variable Declarations Data Types Operators Statements. Functions

Fundamental of Programming (C)

Princeton University COS 333: Advanced Programming Techniques A Subset of C90

Programming Language A

SOFTWARE Ph.D. Qualifying Exam Spring Consider the following C program which consists of two function definitions including the main function.

Dynamic memory allocation

Array Initialization

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Memory. What is memory? How is memory organized? Storage for variables, data, code etc. Text (Code) Data (Constants) BSS (Global and static variables)

The detail of sea.c [1] #include <stdio.h> The preprocessor inserts the header file stdio.h at the point.

Tutorial 1: Introduction to C Computer Architecture and Systems Programming ( )

Question 1. Part (a) [2 marks] error: assignment of read-only variable x ( x = 20 tries to modify a constant) Part (b) [1 mark]

A Short Course for REU Students Summer Instructor: Ben Ransford

A Short Course for REU Students Summer Instructor: Ben Ransford

Language comparison. C has pointers. Java has references. C++ has pointers and references

Arrays and Strings. Antonio Carzaniga. February 23, Faculty of Informatics Università della Svizzera italiana Antonio Carzaniga

COMP 2355 Introduction to Systems Programming

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

Pointers. Part VI. 1) Introduction. 2) Declaring Pointer Variables. 3) Using Pointers. 4) Pointer Arithmetic. 5) Pointers and Arrays

EM108 Software Development for Engineers

Final Intro to C Review

Programming Language B

EL2310 Scientific Programming

Fundamental of Programming (C)

SOFTWARE Ph.D. Qualifying Exam Spring Consider the following C program, which includes three function definitions, including the main function.

Arrays and Pointers (part 2) Be extra careful with pointers!

Arrays and Pointers (part 2) Be extra careful with pointers!

Signals, Instruments, and Systems W2. C Programming (continued)

C programming for beginners

The output: The address of i is 0xbf85416c. The address of main is 0x80483e4. arrays.c. 1 #include <stdio.h> 3 int main(int argc, char **argv) 4 {

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

Flow Control. CSC215 Lecture

Midterm Exam Nov 8th, COMS W3157 Advanced Programming Columbia University Fall Instructor: Jae Woo Lee.

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

Online Judge and C. Roy Chan. January 12, Outline Information Online Judge Introduction to C. CSC2100B Data Structures Tutorial 1

Procedural programming with C

ESC101N: Fundamentals of Computing End-sem st semester

CSC 270 Survey of Programming Languages. Input and Output

Chapter 11 Introduction to Programming in C

Kurt Schmidt. October 30, 2018

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

Arrays. An array is a collection of several elements of the same type. An array variable is declared as array name[size]

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

Chapter 11 Introduction to Programming in C

16.216: ECE Application Programming Fall 2011

CMPT 115. C tutorial for students who took 111 in Java. University of Saskatchewan. Mark G. Eramian, Ian McQuillan CMPT 115 1/32

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

Lecture 5: Multidimensional Arrays. Wednesday, 11 February 2009

Chapter 11 Introduction to Programming in C

High Performance Programming Programming in C part 1

COP Programming Concepts Spring 1999 CLOSED BOOK Exam #1 100 Points NAME

Informatica e Sistemi in Tempo Reale

BİL200 TUTORIAL-EXERCISES Objective:

Fundamentals of Programming

CS 261 Data Structures. Introduction to C Programming

C programming basics T3-1 -

EECE.2160: ECE Application Programming Spring 2016 Exam 1 Solution

CSC 270 Survey of Programming Languages. What is a Pointer?

Chapter 11 Introduction to Programming in C

Syntax Analysis Part VIII

Programming in C. What is C?... What is C?

Programming in C UVic SEng 265

CS360 Midterm 1 - February 21, James S. Plank. Put all answers on the answer sheet. In all of these questions, please assume the following:

Control Structures. Chapter 13 Control Structures. Example If Statements. ! Conditional. if (condition) action;

Lab 1: Introduction to C Programming

Understanding Pointers

Programming in C. What is C?... What is C?

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

211: Computer Architecture Summer 2016

Flow of Control. Selection. if statement. True and False in C False is represented by any zero value. switch

are all acceptable. With the right compiler flags, Java/C++ style comments are also acceptable.

Computer Programming Unit 3

!"#$% &'($) *+!$ 0!'" 0+'&"$.&0-2$ 10.+3&2),&/3+, %&&/3+, C,-"!.&/+"*0.&('1 :2 %*10% *%7)/ 30'&. 0% /4%./

Structured programming

EL2310 Scientific Programming

CSE 333 Lecture 7 - final C details

COP 3223 Introduction to Programming with C - Study Union - Spring 2018

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Arrays, Strings, and Pointers

mith College Computer Science CSC352 Week #7 Spring 2017 Introduction to C Dominique Thiébaut

BSM540 Basics of C Language

Integer Representation. Variables. Real Representation. Integer Overflow/Underflow

United States Naval Academy Electrical and Computer Engineering Department EC310-6 Week Midterm Spring AY2017

Compiling and Running a C Program in Unix

Reminder of midterm 1. Next Thursday, Feb. 14, in class Look at Piazza announcement for rules and preparations

Contents. A Review of C language. Visual C Visual C++ 6.0

Chapter 11 Introduction to Programming in C

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

UNIT III (PART-II) & UNIT IV(PART-I)

Procedures, Parameters, Values and Variables. Steven R. Bagley

CpSc 1010, Fall 2014 Lab 10: Command-Line Parameters (Week of 10/27/2014)

CS16 Midterm Exam 1 E01, 10S, Phill Conrad, UC Santa Barbara Wednesday, 04/21/2010, 1pm-1:50pm

C Tutorial: Part 1. Dr. Charalampos C. Tsimenidis. Newcastle University School of Electrical and Electronic Engineering.

Engineering program development 6. Edited by Péter Vass

{C} Programming. Part 1/2 Basics Variables, Conditions, Loops, Arrays, Pointer basics

int main() { return 0; }

Transcription:

C LANGUAGE A Short Course Alvaro F. M. Azevedo http://www.fe.up.pt/~alvaro January 2002 C Language - Alvaro Azevedo 1

ANSI C Standard (ANSI, ISO) Compiled - efficient Low level / high level Other languages are based on the syntax of the C language Example: C++, Java, C#, etc. January 2002 C Language - Alvaro Azevedo 2

MY FIRST FULL PROGRAM #include <stdio.h> int main() printf("hello world!\n"); return 0; Recommended source file extension:.c Example: my_first_test.c January 2002 C Language - Alvaro Azevedo 3

DECLARATIONS /* This is a comment */ int k; /* k is a signed integer */ float x; /* x is a single precision real number */ double y; /* y is a double precision real number */ k = -3; x = 8.19; y = x / 7 + k; printf("k = %4d, x = %6.2f, y = %12.7lf\n", k, x, y); January 2002 C Language - Alvaro Azevedo 4

if if (k > 3) printf("k is GREATER THAN 3\n"); if (k >= 3) printf("k is GREATER THAN OR EQUAL to 3\n"); if (k == 3) printf("k is EQUAL to 3\n"); if (k!= 3) printf("k is NOT EQUAL to 3\n"); if (i < 0 && j < 0) printf("i AND j are negative\n"); if (i < 0 j < 0) printf("i OR j is negative\n"); if (! (i < 0 && j < 0) ) printf("not(i < 0 and j < 0)\n"); January 2002 C Language - Alvaro Azevedo 5

if int j = 6; int ktest = 4; if (ktest) /* 0 => FALSE; Not equal to 0 => TRUE */ j++; /* j = j + 1 */ printf("j = %d\n", j); /* Output: j = 7 */ else j--; /* j = j - 1 */ printf("j = %d\n", j); /* if */ January 2002 C Language - Alvaro Azevedo 6

if if (ktest > 0) j += 100; /* j = j + 100 */ printf("j = %d\n", j); else if (ktest < 0) j -= 100; /* j = j - 100 */ printf("j = %d\n", j); else printf("j unchanged\n"); /* if */ January 2002 C Language - Alvaro Azevedo 7

OTHER COMPOUND OPERATORS j *= i + 2; /* j = j * (i + 2) */ jnewcoeff /= a - b; /* jnewcoeff = jnewcoeff / (a - b) */ OTHER FORMAT SPECIFIERS j = 37; printf("%5.5d\n", j); /* 00037 */ a = -0.00987; printf("%12.4le\n", a); /* -9.8700e-003 */ Difference must be > 8 January 2002 C Language - Alvaro Azevedo 8

INPUT int myage; double todaystemperature; printf("how old are you? "); scanf("%d", &myage); /* Note the ampersand (&) */ printf("tell me the temperature outside... "); scanf("%lf", &todaystemperature); printf("i am %d years old and ", myage); printf("the temperature\noutside is %5.1lf degrees.\n", todaystemperature); January 2002 C Language - Alvaro Azevedo 9

ARRAYS int v[3]; /* Array of 3 integers */ v[0] = 8000; v[1] = 8001; v[2] = 8002; double a[5][3]; /* 5x3 matrix of doubles */ a[0][0] = 9.00; a[0][1] = 9.01; a[0][2] = 9.02; a[1][0] = 9.10; a[1][1] = 9.11; a[1][2] = 9.12; a[2][0] = 9.20; a[2][1] = 9.21; a[2][2] = -56.7952; a[3][0] = 9.30; a[3][1] = 9.31; a[3][2] = 9.32; a[4][0] = 9.40; a[4][1] = 9.41; a[4][2] = 9.42; January 2002 C Language - Alvaro Azevedo 10

for LOOP for (i = 1; i <= 6; i++) printf("%4d %4d\n", i, 100 * i); /* for (i) */ for (i = -5, j = 1; j <= 12 && i!= 2 * j; i -= 2, j += 3) printf("%4d %4d %4d\n", i, j, 2 * j); /* for (i,j) */ January 2002 C Language - Alvaro Azevedo 11

for LOOP /* Print a 5x3 matrix */ printf("matrix a:\n"); for (i = 0; i < 5; i++) for (j = 0; j < 3; j++) printf(" %10.4lf", a[i][j]); /* for (j) */ printf("\n"); /* for (i) */ January 2002 C Language - Alvaro Azevedo 12

CHARACTERS char gender; /* Male/Female => 'M'/'F' */ printf("male (M) or Female (F)? "); gender = getchar(); if (gender!= 'M' && gender!= 'F') printf("error!\n"); exit(1); /* #include <stdlib.h> is required */ /* if */ printf("the gender is: %c\n", gender); January 2002 C Language - Alvaro Azevedo 13

STRINGS /* A string is an array of char */ char t[4]; strcpy(t, "ABC"); /* #include <string.h> is required */ printf("t[0] = %3d\n", t[0]); /* 65 => ASCII code of A */ printf("t[1] = %3d\n", t[1]); /* 66 => ASCII code of B */ printf("t[2] = %3d\n", t[2]); /* 67 => ASCII code of C */ printf("t[3] = %3d\n", t[3]); /* 0 => All strings are terminated by a NULL character. */ /* printf("t[4] = %3d\n", t[4]); => ERROR */ printf("the string is: %s\n", t); /* The string is: ABC */ January 2002 C Language - Alvaro Azevedo 14

POINTERS double z; /* z is a double */ double* p; /* p is a pointer to a double */ z = 5.87; /* z is initialized with the value 5.87 */ p = &z; /* p is initialized with the address of z */ printf("z = %6.2lf\n", z); /* z = 5.87 */ printf("p = %d\n", p); /* p = 1245048 */ printf(">>> *p = %6.2lf\n", *p); /* >>> *p = 5.87 */ Notes: & is the address-of operator * is the indirection or dereferencing operator January 2002 C Language - Alvaro Azevedo 15

FILES (input) FILE* f; /* #include <stdio.h> is required */ f = fopen("my_file.txt", "r"); /* "r" means "read" */ if (f == NULL) fprintf(stderr, "File not found\n"); exit(1); /* Program terminates */ /* if */ fscanf(f, "%lf", &z); /* z must be declared as a double */ printf("z = %lg\n", z); fclose(f); /* The file is closed */ January 2002 C Language - Alvaro Azevedo 16

FILES (output) FILE* f; /* #include <stdio.h> is required */ f = fopen("my_file.txt", "w"); /* "w" means "write" */ if (f == NULL) fprintf(stderr, "Unable to open the output file\n"); exit(1); /* Program terminates */ /* if */ fprintf(f, "z = %6.2lf\n", z); /* z is a double */ fclose(f); /* The file is closed */ January 2002 C Language - Alvaro Azevedo 17

break AND continue int i, j; for (i = 20; i <= 40; i++) j = i / 6; /* Integer division */ if (j == 5) break; /* for loop is terminated */ printf("%2d %2d\n", i, j); /* for (i) */ for (i = 20; i <= 40; i++) j = i % 6; /* Remainder of the integer division */ if (j == 0) continue; /* "goto" the beginning */ printf("%3d %3d\n", i, j); /* for (i) */ January 2002 C Language - Alvaro Azevedo 18

break WITH NESTED FOR LOOPS for (i = 1; i <= 8; i++) for (j = 1; j <= 6; j++) k = i + 2 * j; if (k == 9) break; /* for (j) is terminated */ printf("%3d %3d... %3d\n", i, j, k); /* for (j) */ printf("================\n"); /* for (i) */ January 2002 C Language - Alvaro Azevedo 19

OTHER STATEMENTS for (;;) /* Infinite loop (forever) */... if (condition) break;... /* for (;;) */ while (condition)... /* while */ do... while (condition); January 2002 C Language - Alvaro Azevedo 20

switch switch (nchoice) case 1:... /* Executed when nchoice == 1 */ break; case 5: case 8:... /* Executed when nchoice == 5 nchoice == 8 */ break; default: fprintf(stderr, "Valid choices are: 1, 5 or 8\n"); exit(1); /* switch (nchoice) */ January 2002 C Language - Alvaro Azevedo 21

#define AND const #define PI 3.14159265359 /* Simple text replacement */ const double PI = 3.14159265359; /* PI is a const variable */ sscanf AND sprintf char t[20]; double x = -2.87; double y; sprintf(t, "%6.2lf", x); /* Output goes to the string t */ strcat(t, "654"); /* String concatenation */ sscanf(t, "%lf", &y); /* Input from the string t */ printf("y = %12.8lf\n", y); /* y = -2.87654000 */ January 2002 C Language - Alvaro Azevedo 22

malloc AND free #include <stdlib.h> /* malloc, free */... int i, n; double* vect; printf("n? "); scanf("%d", &n); /* Allocate memory: */ vect = (double*)malloc(n * sizeof(double)); for (i = 0; i < n; i++) vect[i] = (double)i / 3; /* i is converted into a double */... free(vect); /* Free memory */ January 2002 C Language - Alvaro Azevedo 23

FUNCTIONS void decorate(char* t) printf("\n"); printf("/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\\n\n"); printf(">>> %s\n\n", t); printf("\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\n\n"); int main() char msg[] = "Very nice!"; decorate(msg); /* The function decorate is called */ return 0; January 2002 C Language - Alvaro Azevedo 24

FUNCTIONS Memory address char calc(int k, double x, double* presult ) k -= 3; /* Modifying a copy of the original k */ if (k == 0) return 1; *presult = x / k; printf("function calc: k = %d\n", k); return 0; int main() int k = 8; double x = 5.55; double result; char c; c = calc(k, x, &result ); /* c == 0 indicates success */ if (c == 0) printf("result = %lg\n", result); printf("main: k = %d\n", k); /* k = 8 (unchanged) */ return 0; January 2002 C Language - Alvaro Azevedo 25

argc AND argv int main(int argc, char* argv[]) /* argc is the n. of command line arguments */ if (argc!= 3) /* The program name is also counted */ fprintf(stderr, "When this program is executed,\n"); fprintf(stderr, "two arguments must follow the\n"); fprintf(stderr, "program name.\n\n"); fprintf(stderr, "Usage: show_args arg1 arg2\n"); exit(1); /* if */ printf("argv[0] = %s\n", argv[0]); printf("argv[1] = %s\n", argv[1]); printf("argv[2] = %s\n", argv[2]); Note: save this file as "show_args.c" return 0; January 2002 C Language - Alvaro Azevedo 26

struct struct Point double x; double y; int size; char color; ; /* Semicolon (;) required */ int main() struct Point A, B; A.x = 70.1; A.y = 70.2; A.size = 5; A.color = 'R'; B = A; printf("b = ( %lg, %lg ) -> %d/%c\n", B.x, B.y, B.size, B.color); return 0; January 2002 C Language - Alvaro Azevedo 27

struct AS A FUNCTION ARGUMENT void PrintPoint(char* description, struct Point K) /* Copies of the data members of */ /* the struct are printed. */ printf("\n%s: ( %lg, %lg ) -> %d/%c\n", description, K.x, K.y, K.size, K.color); int main() struct Point A, B; A.x = 70.1; A.y = 70.2; A.size = 5; A.color = 'R'; B = A; PrintPoint("Point A", A); PrintPoint("Point B", B); return 0; January 2002 C Language - Alvaro Azevedo 28

struct MODIFIED BY A FUNCTION void ReadPoint(char* description, struct Point* pk) printf("\n%s (x,y,size,color)\n", description); printf("x? "); scanf("%lf", &pk->x); printf("y? "); scanf("%lf", &pk->y); printf("size? "); scanf("%d", &pk->size); printf("color [1-5]? "); scanf("%d", &pk->color); pk->color += 64; /* [1-5] -> [A-E] */ int main() struct Point A, B; ReadPoint("Point A?", &A); ReadPoint("Point B?", &B); PrintPoint(">>> Point A", A); PrintPoint(">>> Point B", B); return 0; January 2002 C Language - Alvaro Azevedo 29

struct CONTAINING AN ARRAY struct Vector int size; double* vect; ; int main() struct Vector A, B; A.size = 3; A.vect = (double*)malloc(3 * sizeof(double)); A.vect[0] = 7.00; A.vect[1] = 7.01; A.vect[2] = 7.02; B = A; printf("a => %d, %8.2lf\n", A.size, A.vect[1]); printf("b => %d, %8.2lf\n", B.size, B.vect[1]); A.vect[1] = 5555.55; /* Why was this ignored? */ B.vect[1] = 8888.88; printf("a => %d, %8.2lf\n", A.size, A.vect[1]); printf("b => %d, %8.2lf\n", B.size, B.vect[1]);... January 2002 C Language - Alvaro Azevedo 30

NEXT CHAPTER: C++... January 2002 C Language - Alvaro Azevedo 31