Modifiers. int foo(int x) { static int y=0; /* value of y is saved */ y = x + y + 7; /* across invocations of foo */ return y; }

Similar documents
C: Program Structure. Department of Computer Science College of Engineering Boise State University. September 11, /13

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

Topic 6: A Quick Intro To C

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

CSE 333 Lecture 7 - final C details

Computational Methods of Scientific Programming Fall 2007

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

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

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Input/Output: Advanced Concepts

Chapter 11 Introduction to Programming in C

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Introduction to the C Programming Language

CSE 374 Programming Concepts & Tools

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

Page 1. Agenda. Programming Languages. C Compilation Process

G52CPP C++ Programming Lecture 6. Dr Jason Atkin

Practical C Issues:! Preprocessor Directives, Multi-file Development, Makefiles. CS449 Fall 2017

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

#include. Practical C Issues: #define. #define Macros. Example. #if

Standard File Pointers

Slide Set 5. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Stream Model of I/O. Basic I/O in C

F28HS2 Hardware-Software Interface. Lecture 1: Programming in C 1

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

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

CSE 333 Lecture 2 Memory

Program Translation. text. text. binary. binary. C program (p1.c) Compiler (gcc -S) Asm code (p1.s) Assembler (gcc or as) Object code (p1.

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Programs. Function main. C Refresher. CSCI 4061 Introduction to Operating Systems

Chapter 11 Introduction to Programming in C

CS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files

Lecture 03 Bits, Bytes and Data Types

C Programming Review CSC 4320/6320


CS240: Programming in C

Compiler Theory. (GCC the GNU Compiler Collection) Sandro Spina 2009

PRINCIPLES OF OPERATING SYSTEMS

Final CSE 131B Spring 2004

1 You seek performance 3

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

Midterm Exam 2 Solutions C Programming Dr. Beeson, Spring 2009

Pointers cause EVERYBODY problems at some time or another. char x[10] or char y[8][10] or char z[9][9][9] etc.

Structured Programming. Functions and Structured Programming. Functions. Variables

Chapter 11 Introduction to Programming in C

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

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

A Fast Review of C Essentials Part II

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

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead.

A Fast Review of C Essentials Part I

File Access. FILE * fopen(const char *name, const char * mode);

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

G52CPP C++ Programming Lecture 9

gcc hello.c a.out Hello, world gcc -o hello hello.c hello Hello, world

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Programming and Data Structure

Lectures 5-6: Introduction to C

G52CPP C++ Programming Lecture 8. Dr Jason Atkin

Chapter 14 - Advanced C Topics

Memory Allocation in C

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code

File I/O. Preprocessor Macros

C Introduction. Comparison w/ Java, Memory Model, and Pointers

CS Programming In C

Friday, September 16, Lab Notes. Command line arguments More pre-processor options Programs: Finish Program 1, begin Program 2 due next week

Errors During Compilation and Execution Background Information

COSC 2P91. Introduction Part Deux. Week 1b. Brock University. Brock University (Week 1b) Introduction Part Deux 1 / 14

Introduction to C An overview of the programming language C, syntax, data types and input/output

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0)

Input / Output Functions

Unit 4 Preprocessor Directives

BLM2031 Structured Programming. Zeyneb KURT

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

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

ANSI C Programming Simple Programs

File IO and command line input CSE 2451

Model Viva Questions for Programming in C lab

EL6483: Brief Overview of C Programming Language

Unit 4. Input/Output Functions

PROGRAMMAZIONE I A.A. 2017/2018

Advanced C Programming Topics

The C Pre Processor ECE2893. Lecture 18. ECE2893 The C Pre Processor Spring / 10

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

CSCI-243 Exam 2 Review February 22, 2015 Presented by the RIT Computer Science Community

BİL200 TUTORIAL-EXERCISES Objective:

Engineering program development 7. Edited by Péter Vass

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

SU2017. LAB 1 (May 4/9) Introduction to C, Function Declaration vs. Definition, Basic I/O (scanf/printf, getchar/putchar)

6.s096. Introduction to C and C++

Have examined process Creating program Have developed program Written in C Source code

Unit 6 Files. putchar(ch); ch = getc (fp); //Reads single character from file and advances position to next character

ELEC 377 C Programming Tutorial. ELEC Operating Systems

CSE 303 Lecture 8. Intro to C programming

C - Basics, Bitwise Operator. Zhaoguo Wang

Transcription:

Modifiers unsigned. For example unsigned int would have a range of [0..2 32 1] on a 32-bit int machine. const Constant or read-only. Same as final in Java. static Similar to static in Java but not the same. Here is an example for its use in a C function. int foo(int x) static int y=0; /* value of y is saved */ y = x + y + 7; /* across invocations of foo */ return y; extern. The variable is declared external to the source file. volatile. Ask the compiler to not apply optimization to the variable. register. Variables declared with qualifier register are (if possible) stored in fast registers of the machine. It can only be used for variables declared inside a function and the address operator & cannot be applied to such variables.

Control-flow statements and Expressions Control-Flow statements. The basic statements if/else, while, do-while, for, switch are the same as in Java. In addition, the break/continue statement exit from the innermost enclosing loop like in Java but cannot use a label to break to as in Java. C also has a goto statement that Java does not have. Operators and expressions. These are the same as in Java with some minor differences.

Input/Output The header file to include is <stdio.h>. Console I/O. The commonly used functions are scanf(), printf(), getc(), putc(), fgets(), fputs(). int n; double x; char *s1 = "ha ha"; x = 123456.6666333; /* read in value of n */ scanf("%d",&n); /* must provide the address of n */ printf("%d monkeys, %lf bananas %s \n",100,x,s1); File I/O int n; FILE *infile, *outfile; infile = fopen("datafile","r"); outfile = fopen("outputfile","w"); fscanf(infile, "%d",&n); fprintf(outfile, "%d monkeys",n); fclose(infile); fclose(outfile); String I/O. Use sprintf(), snprintf() and sscanf() using a C-style string variable. char cmd[maxsize]; char *filename = "prog.c"; snprintf(cmd, MAXSIZE, "gcc %s",filename); system(cmd); /* run the command */

Formats for printing and scanning The printf() and scanf function and their variants all use a format string. See the man page for scanf for all the options in the format statement. Here we list some common options that are used within the format string. type specifier description %d int variable %ld long int variable %f float or double variable %s string (char *) variable %c char variable %p pointer variable We can also control the appearance using the width modifiers, which are added before the type specifier and after the % character. Here are some examples: type specifier description %6d at least 6 spaces wide field for printing %6.2f 2 places after decimal point, total 6 spaces %-s left justified (default: right justified) %05d at least 5 spaces with zero padding %p pointer variable

C Preprocessor File inclusion. The command is #include. Files in the standard library can be specified using angle brackets. Files specified in double quotes are relative to the current directory. For example: #include <stdio.h> #include "myheader.h" Macro substitution. The command is #define. Macros expand into in-line code. There is no compile-time check for type correctness. For example: #define max(a,b) ((a)>(b)? (a) : (b)) Defining constants #define MAX 323239283 Conditional inclusion #ifdef LINUX printf("i am the Happy Penguin!\n"); #elsif WINDOWS98 printf("welcome to MS Windows.\n"); #else printf("uh! who am i?\n"); #endif /* To avoid including a header file more than once */ #ifndef OURHDR_H #define OURHDR_H /*... the declarations... */ #endif /* OURHDR_H */

/* C-examples/c-preprocessor/ex1.c */ #include <stdio.h> #include <stdlib.h> #define DEBUG 1 const int IN=1; /* inside a word */ const int OUT=0; /* outside a word */ /* count number of characters, words and lines in the standard input */ int main(int argc, char *argv[]) char c; long nc, nw, nl; int state; state = OUT; nl = nw = nc = 0; while ((c = getchar())!= EOF ) nc++; #ifdef DEBUG printf("nc=%d\n",nc); #endif if (c == \n ) nl++; #ifdef DEBUG printf("nl=%d\n",nl); #endif if (c == c == \n c == \t ) state = OUT; else if (state == OUT) state = IN; nw++; #ifdef DEBUG printf("nw=%d\n",nw); #endif printf("%ld %ld %ld\n", nl, nw, nc); exit(0);

Scope Variables and functions are visible from the point they are defined until the end of the source file. Creating function prototypes at the start of the source file (or in an included header file) makes them visible throughout the source file. Add modifier static in front of a function declaration makes the scope of the function to be limited to the containing source file (similar to private modifier in Java). Global variables are not visible to code in another source file. To make a global variable be visible to multiple source files, declare it fully in only one source file and then declare it as extern in all the other source files. /* file1.c */ int moomoo = 10; /* file2.c */ extern int moomoo; /* file3.c */ extern int moomoo; Discuss examples scope1.c and scope2.c.

ourhdr.h /* C-examples/scope/ourhdr.h */ #ifndef OURHDR_H #define OURHDR_H /* function prototyes */ int f1(int, double); void f2(int); void f3(void); extern int total; #endif /* OURHDR_H */

scope1.c /* C-examples/scope/scope1.c */ #include <stdio.h> #include <stdlib.h> #include "ourhdr.h" int total = 1; static int f4(int); /* private function */ int f1(int x, double z) static int counter=0; counter++; printf("counter in f1() = %d\n", counter); x += f4((int) z); return (x*z); static int f4(int x) return 2*x; int subtotal = 0; void f3() total += 10; subtotal = total; if (total < 100) total = f1(total, 1.5); int main (int argc, char **argv) int m; double z; int x; if (argc!= 3) fprintf(stderr,"usage: %s <int> <double> \n",argv[0]); exit(1); /* return unsuccessful status to the shell */ m = atoi(argv[1]); z = atof(argv[2]); x = f1(m, z); f2(m); f3(); x = x + f1(m,z); printf("total = %d\n",total); printf("subtotal = %d\n",subtotal); exit(0); /* return successful status back to the shell */

scope2.c /* C-examples/scope/scope2.c */ #include <stdio.h> #include <stdlib.h> #include "ourhdr.h" void f2(int x) total += x;

Compiling, Linking and Running Programs Source code prog.c preprocessor cpp Source code compiler gcc/g++ Object Code prog.o Shared Libraries (Dynamically Loaded Libraries) linker ld/ld.so Static Libraries Running Program (process) loader ld/ld.so Executable a.out (default name) Note: Normally the GNU C compiler gcc invokes the preprocessor and the linker for the user automatically.

The GNU C/C++ compiler An integrated C/C++ open-source compiler. Available on a wide variety of platforms. The following shows some commonly used command line arguments to the compiler. See the man page for hundreds of other options. -c Compile but do not link. -Wall Print all warnings. Always use this option...some of the warnings can save you hours of debugging! -O Optimize option. Usually speeds up the execution time significantly. -g -gstabs+ Enable debugging. Need this option if you want to use a debugger. Also gcc allows you to use the optimize option (-O) at the same time as the debug option. -o <filename> Name the output executable file to the given filename. Otherwise the default file name is a.out. -D<macro>=[<name>]. Define a macro. The C file is processed with the macro defined. For example gcc -Wall -DDEBUG ex1.c would define the DEBUG macro to be true for the file being compiled.