SlideShare a Scribd company logo
Introduction to C
(Reek, Chs. 1-2)
1 CS 3090: Safety Critical Programming in C
C: History
CS 3090: Safety Critical Programming in C
2
 Developed in the 1970s – in conjunction with
development of UNIX operating system
 When writing an OS kernel, efficiency is crucial
This requires low-level access to the underlying
hardware:
 e.g. programmer can leverage knowledge of how data is laid out
in memory, to enable faster data access
 UNIX originally written in low-level assembly language –
but there were problems:
 No structured programming (e.g. encapsulating routines as
“functions”, “methods”, etc.) – code hard to maintain
 Code worked only for particular hardware – not portable
C: Characteristics
CS 3090: Safety Critical Programming in C
3
 C takes a middle path between low-level assembly
language…
 Direct access to memory layout through pointer
manipulation
 Concise syntax, small set of keywords
 … and a high-level programming language like Java:
 Block structure
 Some encapsulation of code, via functions
 Type checking (pretty weak)
C: Dangers
CS 3090: Safety Critical Programming in C
4
 C is not object oriented!
 Can’t “hide” data as “private” or “protected” fields
 You can follow standards to write C code that looks
object-oriented, but you have to be disciplined – will the
other people working on your code also be disciplined?
 C has portability issues
 Low-level “tricks” may make your C code run well on one
platform – but the tricks might not work elsewhere
 The compiler and runtime system will rarely stop
your C program from doing stupid/bad things
 Compile-time type checking is weak
 No run-time checks for array bounds errors, etc. like in
Java
Separate compilation
CS 3090: Safety Critical Programming in C
5
 A C program consists of source code in one or more files
 Each source file is run through the preprocessor and
compiler, resulting in a file containing object code
 Object files are tied together by the linker to form a single
executable program
Source code
file1.c
Preprocessor/
Compiler
Object code
file1.o
Source code
file2.c
Preprocessor/
Compiler
Object code
file2.o
Linker
Libraries
Executable code
a.out
Separate compilation
CS 3090: Safety Critical Programming in C
6
 Advantage: Quicker compilation
 When modifying a program, a programmer typically edits
only a few source code files at a time.
 With separate compilation, only the files that have been
edited since the last compilation need to be recompiled
when re-building the program.
 For very large programs, this can save a lot of time.
How to compile (UNIX)
CS 3090: Safety Critical Programming in C
7
 To compile and link a C program that is contained entirely in one
source file:
cc program.c
 The executable program is called a.out by default.
If you don’t like this name, choose another using the –o option:
cc program.c –o exciting_executable
 To compile and link several C source files:
cc main.c extra.c more.c
 This will produce object (.o) files, that you can use in a later compilation:
cc main.o extra.o more.c
 Here, only more.c will be compiled – the main.o and extra.o files
will be used for linking.
 To produce object files, without linking, use -c:
cc –c main.c extra.c more.c
The preprocessor
CS 3090: Safety Critical Programming in C
8
 The preprocessor takes your source code and – following
certain directives that you give it – tweaks it in various
ways before compilation.
 A directive is given as a line of source code starting with
the # symbol
 The preprocessor works in a very crude, “word-
processor” way, simply cutting and pasting –
it doesn’t really know anything about C!
Your
source
code
Preprocessor
Enhanced and
obfuscated
source code
Compiler
Object
code
A first program: Text rearranger
 Input
 First line: pairs of nonnegative integers, separated by
whitespace, then terminated by a negative integer
x1 y1 x2 y2 … xn yn -1
 Each subsequent line: a string of characters
 Output
 For each string S, output substrings of S:
 First, the substring starting at location x1 and ending at y1;
 Next, the substring starting at location x2 and ending at y2;
 …
 Finally, the substring starting at location xn and ending at xn.
9 CS 3090: Safety Critical Programming in C
Sample input/output
CS 3090: Safety Critical Programming in C
10
 Initial input: 0 2 5 7 10 12 -1
 Next input line: deep C diving
 Output: deeC ding
 Next input line: excitement!
 Output: exceme!
 … continue ad nauseum…
 Terminate with ctrl-D (signals end of keyboard input)
Use of comments
CS 3090: Safety Critical Programming in C
11
/*
** This program reads input lines from the standard input and prints
** each input line, followed by just some portions of the lines, to
** the standard output.
**
** The first input is a list of column numbers, which ends with a
** negative number. The column numbers are paired and specify
** ranges of columns from the input line that are to be printed.
** For example, 0 3 10 12 -1 indicates that only columns 0 through 3
** and columns 10 through 12 will be printed.
*/
 Only /* … */ for comments – no // like Java or C++
Comments on comments
CS 3090: Safety Critical Programming in C
12
 Can’t nest comments within comments
 /* is matched with the very next */ that comes along
 Don’t use /* … */ to comment out code – it won’t
work if the commented-out code contains comments
/* Comment out the following code
int f(int x) {
return x+42; /* return the result */
}
*/
 Anyway, commenting out code is confusing, and
dangerous (easy to forget about) – avoid it
Only this will be
commented out
This will not!
Preprocessor directives
CS 3090: Safety Critical Programming in C
13
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 The #include directives “paste” the contents of the
files stdio.h, stdlib.h and string.h into your source
code, at the very place where the directives appear.
 These files contain information about some library
functions used in the program:
 stdio stands for “standard I/O”, stdlib stands for
“standard library”, and string.h includes useful string
manipulation functions.
 Want to see the files? Look in /usr/include
Preprocessor directives
CS 3090: Safety Critical Programming in C
14
#define MAX_COLS 20
#define MAX_INPUT 1000
 The #define directives perform
“global replacements”:
 every instance of MAX_COLS is replaced with 20, and every
instance of MAX_INPUT is replaced with 1000.
Function prototypes
CS 3090: Safety Critical Programming in C
15
int read_column_numbers( int columns[], int max );
void rearrange( char *output, char const *input,
int n_columns, int const columns[] );
 These look like function definitions – they have the
name and all the type information – but each ends
abruptly with a semicolon. Where’s the body of the
function – what does it actually do?
 (Note that each function does have a real definition,
later in the program.)
Function prototypes
CS 3090: Safety Critical Programming in C
16
 Q: Why are these needed, if the functions are defined
later in the program anyway?
 A: C programs are typically arranged in “top-down” order,
so functions are used (called) before they’re defined.
 (Note that the function main() includes a call to
read_column_numbers().)
 When the compiler sees a call to read_column_numbers() ,
it must check whether the call is valid (the right number and
types of parameters, and the right return type).
 But it hasn’t seen the definition of read_column_numbers()
yet!
 The prototype gives the compiler advance information
about the function that’s being called.
 Of course, the prototype and the later function definition must
match in terms of type information.
The main() function
CS 3090: Safety Critical Programming in C
17
 main() is always the first function called in a program
execution.
int
main( void )
{ …
 void indicates that the function takes no arguments
 int indicates that the function returns an integer value
 Q: Integer value? Isn’t the program just printing out some stuff
and then exiting? What’s there to return?
 A: Through returning particular values, the program can
indicate whether it terminated “nicely” or badly; the operating
system can react accordingly.
The printf() function
CS 3090: Safety Critical Programming in C
18
printf( "Original input : %sn", input );
 printf() is a library function declared in <stdio.h>
 Syntax: printf( FormatString, Expr, Expr...)
 FormatString: String of text to print
 Exprs: Values to print
 FormatString has placeholders to show where to put the
values (note: #placeholders should match #Exprs)
 Placeholders: %s (print as string), %c (print as char),
%d (print as integer),
%f (print as floating-point)
 n indicates a newline character
Make sure you pick
the right one!
Text line printed only
when n encountered
Don’t forget n when
printing “final results”
return vs. exit
CS 3090: Safety Critical Programming in C
19
 Let’s look at the return statement in main():
return EXIT_SUCCESS;
 EXIT_SUCCESS is a constant defined in stdlib ; returning this
value signifies successful termination.
 Contrast this with the exit statement in the function
read_column_numbers():
puts( “Last column number is not paired.” );
exit( EXIT_FAILURE );
 EXIT_FAILURE is another constant, signifying that something bad
happened requiring termination.
 exit differs from return in that execution terminates immediately –
control is not passed back to the calling function main().
Pointers, arrays, strings
CS 3090: Safety Critical Programming in C
20
 In this program, the notions of string, array, and
pointer seem to be somewhat interchangeable:
 In main(), an array of characters is declared, for
purposes of holding the input string:
char input[MAX_INPUT];
 Yet when it’s passed in as an argument to the
rearrange() function, input has morphed into a pointer
to a character (char *):
void
rearrange( char *output, char const *input,…
Pointers, arrays, strings
CS 3090: Safety Critical Programming in C
21
 In C, the three concepts are indeed closely related:
 A pointer is simply a memory address. The type char *
“pointer to character” signifies that the data at the
pointer’s address is to be interpreted as a character.
 An array is simply a pointer – of a special kind:
 The array pointer is assumed to point to the first of a sequence
of data items stored sequentially in memory.
 How do you get to the other array elements? By incrementing
the pointer value.
 A string is simply an array of characters – unlike Java,
which has a predefined String class.
String layout and access
CS 3090: Safety Critical Programming in C
22
p
(char)
o
(char)
i
(char)
n
(char)
t
(char)
e
(char)
r
(char)
NUL
(char)
(char *)
input
What is input?
It’s a string!
It’s a pointer to char!
It’s an array of char!
How do we get to the “n”?
Follow the input pointer,
then hop 3 to the right
*(input + 3)
- or -
input[3]
NUL is a special value
indicating end-of-string

More Related Content

Similar to C Introduction and bascis of high level programming (20)

PPT
(Lect. 2 & 3) Introduction to C.ppt
atulchaudhary821
 
PPTX
C++ AND CATEGORIES OF SOFTWARE
UNIVERSITY OF ENGINEERING AND TECHNOLOGY TAXILA
 
PPT
Lecture 1
Soran University
 
PPT
Advanced+pointers
Rubal Bansal
 
PDF
C programming day#1
Mohamed Fawzy
 
PPT
Fp201 unit2 1
rohassanie
 
PPTX
Programming Fundamentals lecture 5
REHAN IJAZ
 
PPTX
computer programming omputer programming
Jifarnecho
 
PPT
c.ppt
jazzcashlimit
 
PPTX
Introduction to cpp language and all the required information relating to it
PushkarNiroula1
 
PDF
Chapter 2 - Structure of C++ Program
Deepak Singh
 
PPTX
Lecture 1
marvellous2
 
PDF
Introduction of c language
farishah
 
PPT
intro to programming languge c++ for computer department
MemMem25
 
PPTX
Lab 1.pptx
MohammedAlobaidy16
 
PPT
Introduction to c programming
gajendra singh
 
PPTX
Intro To C++ - Class 3 - Sample Program
Blue Elephant Consulting
 
PPTX
Intro To C++ - Class 03 - An Introduction To C++ Programming, Part II
Blue Elephant Consulting
 
PDF
Embedded concepts
sartaj ahmed
 
PDF
CP Handout#2
trupti1976
 
(Lect. 2 & 3) Introduction to C.ppt
atulchaudhary821
 
C++ AND CATEGORIES OF SOFTWARE
UNIVERSITY OF ENGINEERING AND TECHNOLOGY TAXILA
 
Lecture 1
Soran University
 
Advanced+pointers
Rubal Bansal
 
C programming day#1
Mohamed Fawzy
 
Fp201 unit2 1
rohassanie
 
Programming Fundamentals lecture 5
REHAN IJAZ
 
computer programming omputer programming
Jifarnecho
 
Introduction to cpp language and all the required information relating to it
PushkarNiroula1
 
Chapter 2 - Structure of C++ Program
Deepak Singh
 
Lecture 1
marvellous2
 
Introduction of c language
farishah
 
intro to programming languge c++ for computer department
MemMem25
 
Lab 1.pptx
MohammedAlobaidy16
 
Introduction to c programming
gajendra singh
 
Intro To C++ - Class 3 - Sample Program
Blue Elephant Consulting
 
Intro To C++ - Class 03 - An Introduction To C++ Programming, Part II
Blue Elephant Consulting
 
Embedded concepts
sartaj ahmed
 
CP Handout#2
trupti1976
 

More from vipulkondekar (20)

PPTX
Free-Counselling-and-Admission-Facilitation-at-WIT-Campus.pptx
vipulkondekar
 
PPTX
Machine Learning Presentation for Engineering
vipulkondekar
 
PPTX
Exploring-Scholarship-Opportunities.pptx
vipulkondekar
 
PPTX
Documents-Required-for- Engineering Admissions.pptx
vipulkondekar
 
PPTX
Backpropagation algorithm in Neural Network
vipulkondekar
 
PPTX
Min Max Algorithm in Artificial Intelligence
vipulkondekar
 
PPTX
AO Star Algorithm in Artificial Intellligence
vipulkondekar
 
PPTX
A Star Algorithm in Artificial intelligence
vipulkondekar
 
PPTX
Artificial Intelligence Problem Slaving PPT
vipulkondekar
 
PPT
Deep Learning approach in Machine learning
vipulkondekar
 
PPT
Artificial Neural Network and Machine Learning
vipulkondekar
 
PPT
Microcontroller Timer Counter Modules and applications
vipulkondekar
 
PPT
Microcontroller 8051 timer and counter module
vipulkondekar
 
PPT
Microcontroller 8051 Timer Counter Interrrupt
vipulkondekar
 
PPTX
Microcontroller Introduction and the various features
vipulkondekar
 
PPTX
Avishkar competition presentation template
vipulkondekar
 
PPTX
Introduction to prototyping in developing the products
vipulkondekar
 
PPTX
KNN Algorithm Machine_Learning_KNN_Presentation.pptx
vipulkondekar
 
PPTX
New Education Policy Presentation at VIT
vipulkondekar
 
PPT
Random Forest algorithm in Machine learning
vipulkondekar
 
Free-Counselling-and-Admission-Facilitation-at-WIT-Campus.pptx
vipulkondekar
 
Machine Learning Presentation for Engineering
vipulkondekar
 
Exploring-Scholarship-Opportunities.pptx
vipulkondekar
 
Documents-Required-for- Engineering Admissions.pptx
vipulkondekar
 
Backpropagation algorithm in Neural Network
vipulkondekar
 
Min Max Algorithm in Artificial Intelligence
vipulkondekar
 
AO Star Algorithm in Artificial Intellligence
vipulkondekar
 
A Star Algorithm in Artificial intelligence
vipulkondekar
 
Artificial Intelligence Problem Slaving PPT
vipulkondekar
 
Deep Learning approach in Machine learning
vipulkondekar
 
Artificial Neural Network and Machine Learning
vipulkondekar
 
Microcontroller Timer Counter Modules and applications
vipulkondekar
 
Microcontroller 8051 timer and counter module
vipulkondekar
 
Microcontroller 8051 Timer Counter Interrrupt
vipulkondekar
 
Microcontroller Introduction and the various features
vipulkondekar
 
Avishkar competition presentation template
vipulkondekar
 
Introduction to prototyping in developing the products
vipulkondekar
 
KNN Algorithm Machine_Learning_KNN_Presentation.pptx
vipulkondekar
 
New Education Policy Presentation at VIT
vipulkondekar
 
Random Forest algorithm in Machine learning
vipulkondekar
 
Ad

Recently uploaded (20)

PDF
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
PDF
Submit Your Papers-International Journal on Cybernetics & Informatics ( IJCI)
IJCI JOURNAL
 
PDF
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
PDF
mbse_An_Introduction_to_Arcadia_20150115.pdf
henriqueltorres1
 
PPTX
MODULE 04 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
PPTX
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
PPTX
Water Resources Engineering (CVE 728)--Slide 4.pptx
mohammedado3
 
PPTX
OCS353 DATA SCIENCE FUNDAMENTALS- Unit 1 Introduction to Data Science
A R SIVANESH M.E., (Ph.D)
 
PDF
20ES1152 Programming for Problem Solving Lab Manual VRSEC.pdf
Ashutosh Satapathy
 
PPTX
MODULE 05 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
PPTX
MODULE 03 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
PPT
New_school_Engineering_presentation_011707.ppt
VinayKumar304579
 
PDF
MODULE-5 notes [BCG402-CG&V] PART-B.pdf
Alvas Institute of Engineering and technology, Moodabidri
 
PDF
WD2(I)-RFQ-GW-1415_ Shifting and Filling of Sand in the Pond at the WD5 Area_...
ShahadathHossain23
 
PPTX
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
PDF
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
PDF
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
PDF
Electrical Machines and Their Protection.pdf
Nabajyoti Banik
 
PPTX
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
PDF
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
Submit Your Papers-International Journal on Cybernetics & Informatics ( IJCI)
IJCI JOURNAL
 
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
mbse_An_Introduction_to_Arcadia_20150115.pdf
henriqueltorres1
 
MODULE 04 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
Water Resources Engineering (CVE 728)--Slide 4.pptx
mohammedado3
 
OCS353 DATA SCIENCE FUNDAMENTALS- Unit 1 Introduction to Data Science
A R SIVANESH M.E., (Ph.D)
 
20ES1152 Programming for Problem Solving Lab Manual VRSEC.pdf
Ashutosh Satapathy
 
MODULE 05 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
MODULE 03 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
New_school_Engineering_presentation_011707.ppt
VinayKumar304579
 
MODULE-5 notes [BCG402-CG&V] PART-B.pdf
Alvas Institute of Engineering and technology, Moodabidri
 
WD2(I)-RFQ-GW-1415_ Shifting and Filling of Sand in the Pond at the WD5 Area_...
ShahadathHossain23
 
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
Electrical Machines and Their Protection.pdf
Nabajyoti Banik
 
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
Ad

C Introduction and bascis of high level programming

  • 1. Introduction to C (Reek, Chs. 1-2) 1 CS 3090: Safety Critical Programming in C
  • 2. C: History CS 3090: Safety Critical Programming in C 2  Developed in the 1970s – in conjunction with development of UNIX operating system  When writing an OS kernel, efficiency is crucial This requires low-level access to the underlying hardware:  e.g. programmer can leverage knowledge of how data is laid out in memory, to enable faster data access  UNIX originally written in low-level assembly language – but there were problems:  No structured programming (e.g. encapsulating routines as “functions”, “methods”, etc.) – code hard to maintain  Code worked only for particular hardware – not portable
  • 3. C: Characteristics CS 3090: Safety Critical Programming in C 3  C takes a middle path between low-level assembly language…  Direct access to memory layout through pointer manipulation  Concise syntax, small set of keywords  … and a high-level programming language like Java:  Block structure  Some encapsulation of code, via functions  Type checking (pretty weak)
  • 4. C: Dangers CS 3090: Safety Critical Programming in C 4  C is not object oriented!  Can’t “hide” data as “private” or “protected” fields  You can follow standards to write C code that looks object-oriented, but you have to be disciplined – will the other people working on your code also be disciplined?  C has portability issues  Low-level “tricks” may make your C code run well on one platform – but the tricks might not work elsewhere  The compiler and runtime system will rarely stop your C program from doing stupid/bad things  Compile-time type checking is weak  No run-time checks for array bounds errors, etc. like in Java
  • 5. Separate compilation CS 3090: Safety Critical Programming in C 5  A C program consists of source code in one or more files  Each source file is run through the preprocessor and compiler, resulting in a file containing object code  Object files are tied together by the linker to form a single executable program Source code file1.c Preprocessor/ Compiler Object code file1.o Source code file2.c Preprocessor/ Compiler Object code file2.o Linker Libraries Executable code a.out
  • 6. Separate compilation CS 3090: Safety Critical Programming in C 6  Advantage: Quicker compilation  When modifying a program, a programmer typically edits only a few source code files at a time.  With separate compilation, only the files that have been edited since the last compilation need to be recompiled when re-building the program.  For very large programs, this can save a lot of time.
  • 7. How to compile (UNIX) CS 3090: Safety Critical Programming in C 7  To compile and link a C program that is contained entirely in one source file: cc program.c  The executable program is called a.out by default. If you don’t like this name, choose another using the –o option: cc program.c –o exciting_executable  To compile and link several C source files: cc main.c extra.c more.c  This will produce object (.o) files, that you can use in a later compilation: cc main.o extra.o more.c  Here, only more.c will be compiled – the main.o and extra.o files will be used for linking.  To produce object files, without linking, use -c: cc –c main.c extra.c more.c
  • 8. The preprocessor CS 3090: Safety Critical Programming in C 8  The preprocessor takes your source code and – following certain directives that you give it – tweaks it in various ways before compilation.  A directive is given as a line of source code starting with the # symbol  The preprocessor works in a very crude, “word- processor” way, simply cutting and pasting – it doesn’t really know anything about C! Your source code Preprocessor Enhanced and obfuscated source code Compiler Object code
  • 9. A first program: Text rearranger  Input  First line: pairs of nonnegative integers, separated by whitespace, then terminated by a negative integer x1 y1 x2 y2 … xn yn -1  Each subsequent line: a string of characters  Output  For each string S, output substrings of S:  First, the substring starting at location x1 and ending at y1;  Next, the substring starting at location x2 and ending at y2;  …  Finally, the substring starting at location xn and ending at xn. 9 CS 3090: Safety Critical Programming in C
  • 10. Sample input/output CS 3090: Safety Critical Programming in C 10  Initial input: 0 2 5 7 10 12 -1  Next input line: deep C diving  Output: deeC ding  Next input line: excitement!  Output: exceme!  … continue ad nauseum…  Terminate with ctrl-D (signals end of keyboard input)
  • 11. Use of comments CS 3090: Safety Critical Programming in C 11 /* ** This program reads input lines from the standard input and prints ** each input line, followed by just some portions of the lines, to ** the standard output. ** ** The first input is a list of column numbers, which ends with a ** negative number. The column numbers are paired and specify ** ranges of columns from the input line that are to be printed. ** For example, 0 3 10 12 -1 indicates that only columns 0 through 3 ** and columns 10 through 12 will be printed. */  Only /* … */ for comments – no // like Java or C++
  • 12. Comments on comments CS 3090: Safety Critical Programming in C 12  Can’t nest comments within comments  /* is matched with the very next */ that comes along  Don’t use /* … */ to comment out code – it won’t work if the commented-out code contains comments /* Comment out the following code int f(int x) { return x+42; /* return the result */ } */  Anyway, commenting out code is confusing, and dangerous (easy to forget about) – avoid it Only this will be commented out This will not!
  • 13. Preprocessor directives CS 3090: Safety Critical Programming in C 13 #include <stdio.h> #include <stdlib.h> #include <string.h>  The #include directives “paste” the contents of the files stdio.h, stdlib.h and string.h into your source code, at the very place where the directives appear.  These files contain information about some library functions used in the program:  stdio stands for “standard I/O”, stdlib stands for “standard library”, and string.h includes useful string manipulation functions.  Want to see the files? Look in /usr/include
  • 14. Preprocessor directives CS 3090: Safety Critical Programming in C 14 #define MAX_COLS 20 #define MAX_INPUT 1000  The #define directives perform “global replacements”:  every instance of MAX_COLS is replaced with 20, and every instance of MAX_INPUT is replaced with 1000.
  • 15. Function prototypes CS 3090: Safety Critical Programming in C 15 int read_column_numbers( int columns[], int max ); void rearrange( char *output, char const *input, int n_columns, int const columns[] );  These look like function definitions – they have the name and all the type information – but each ends abruptly with a semicolon. Where’s the body of the function – what does it actually do?  (Note that each function does have a real definition, later in the program.)
  • 16. Function prototypes CS 3090: Safety Critical Programming in C 16  Q: Why are these needed, if the functions are defined later in the program anyway?  A: C programs are typically arranged in “top-down” order, so functions are used (called) before they’re defined.  (Note that the function main() includes a call to read_column_numbers().)  When the compiler sees a call to read_column_numbers() , it must check whether the call is valid (the right number and types of parameters, and the right return type).  But it hasn’t seen the definition of read_column_numbers() yet!  The prototype gives the compiler advance information about the function that’s being called.  Of course, the prototype and the later function definition must match in terms of type information.
  • 17. The main() function CS 3090: Safety Critical Programming in C 17  main() is always the first function called in a program execution. int main( void ) { …  void indicates that the function takes no arguments  int indicates that the function returns an integer value  Q: Integer value? Isn’t the program just printing out some stuff and then exiting? What’s there to return?  A: Through returning particular values, the program can indicate whether it terminated “nicely” or badly; the operating system can react accordingly.
  • 18. The printf() function CS 3090: Safety Critical Programming in C 18 printf( "Original input : %sn", input );  printf() is a library function declared in <stdio.h>  Syntax: printf( FormatString, Expr, Expr...)  FormatString: String of text to print  Exprs: Values to print  FormatString has placeholders to show where to put the values (note: #placeholders should match #Exprs)  Placeholders: %s (print as string), %c (print as char), %d (print as integer), %f (print as floating-point)  n indicates a newline character Make sure you pick the right one! Text line printed only when n encountered Don’t forget n when printing “final results”
  • 19. return vs. exit CS 3090: Safety Critical Programming in C 19  Let’s look at the return statement in main(): return EXIT_SUCCESS;  EXIT_SUCCESS is a constant defined in stdlib ; returning this value signifies successful termination.  Contrast this with the exit statement in the function read_column_numbers(): puts( “Last column number is not paired.” ); exit( EXIT_FAILURE );  EXIT_FAILURE is another constant, signifying that something bad happened requiring termination.  exit differs from return in that execution terminates immediately – control is not passed back to the calling function main().
  • 20. Pointers, arrays, strings CS 3090: Safety Critical Programming in C 20  In this program, the notions of string, array, and pointer seem to be somewhat interchangeable:  In main(), an array of characters is declared, for purposes of holding the input string: char input[MAX_INPUT];  Yet when it’s passed in as an argument to the rearrange() function, input has morphed into a pointer to a character (char *): void rearrange( char *output, char const *input,…
  • 21. Pointers, arrays, strings CS 3090: Safety Critical Programming in C 21  In C, the three concepts are indeed closely related:  A pointer is simply a memory address. The type char * “pointer to character” signifies that the data at the pointer’s address is to be interpreted as a character.  An array is simply a pointer – of a special kind:  The array pointer is assumed to point to the first of a sequence of data items stored sequentially in memory.  How do you get to the other array elements? By incrementing the pointer value.  A string is simply an array of characters – unlike Java, which has a predefined String class.
  • 22. String layout and access CS 3090: Safety Critical Programming in C 22 p (char) o (char) i (char) n (char) t (char) e (char) r (char) NUL (char) (char *) input What is input? It’s a string! It’s a pointer to char! It’s an array of char! How do we get to the “n”? Follow the input pointer, then hop 3 to the right *(input + 3) - or - input[3] NUL is a special value indicating end-of-string