SlideShare a Scribd company logo
1 of 23
Download to read offline
C PROGRAMMING
DR ANTON GERDELAN
<GERDELA@SCSS.TCD.IE>
LEAP IN AND TRY THINGS. IF YOU SUCCEED,
YOU CAN HAVE ENORMOUS INFLUENCE. IF
YOU FAIL, YOU HAVE STILL LEARNED
SOMETHING, AND YOUR NEXT ATTEMPT IS
SURE TO BE BETTER FOR IT.
Brian Kernighan
C PROGRAMMING REFRESHER
C PROGRAMMING REFRESHER
C - THE LANGUAGE OF UNIX
▸ Fundamental
▸ Easy to learn, simple, powerful
▸ DIY memory management
▸ Tools
▸ Features
▸ Plain old data types
▸ Pointers and addresses
Unix creators Ken Thompson (left, designed
B - 1969, Go - 2007) and Dennis Ritchie
(right, designed C - 1972) of Bell Labs. src: ?
C PROGRAMMING REFRESHER
APPLICATIONS OF C
▸ Performance
▸ Operating systems, drivers
▸ Games
▸ 3d graphics, imaging
▸ Desktop software
▸ File i/o, tools
▸ Most things
Prof. Brian Kernighan (Princeton U.) - co-author
of "K&R C". We will come across his algorithms
work later. src: Wikipedia.
C PROGRAMMING REFRESHER
PORTABILITY
▸ Almost all C also compiles as
▸ C++, objective C
▸ Very similar to
▸ C#, Java, D, PHP, JavaScript...
▸ Somewhat poss. to compile C++ into C
C PROGRAMMING REFRESHER
PRACTISE!
▸ How to get better
▸ read functions' instructions in man pages
▸ don't rely on auto-completion and Q&A websites
▸ watch more exp. people coding
▸ collaborate/share/code reviews/help - don’t be shy
C PROGRAMMING REFRESHER
PRACTISE!
▸ Importance of playing around
▸ What don't you know?
▸ make a list
▸ Ask good questions
C PROGRAMMING REFRESHER
POD TYPES
▸ "plain old data" types
▸ C89 had
▸ unsigned and signed versions
▸ long and short versions
▸ people defined their own
boolean type
▸ C99 and C++ have bool
int
char
float
double
size_t
pointers
typedef int custom_name;
C PROGRAMMING REFRESHER
A STRUCTURED DATA TYPE - ARRAYS
▸ pros
▸ multiple storage
▸ simple
▸ fast - looping over adjacent memory
▸ random access
▸ cons
▸ fixed size at compile time - waste space
▸ elements must be same type
▸ insertion requires shuffling
▸ often come with a count variable to say how much is
used
int my_array[2048];
int sum = 0;
for (int i = 0; i < 2048; i++){
sum += my_array[i];
}
printf("%in", sum);
my_array[238] = 10;
C PROGRAMMING REFRESHER
STRUCT
▸ combine variables into single
custom variable
▸ useful for passing and returning
several variables from function
▸ C++ has classes which are just
structs with some extra properties
▸ typedef usually used to
shorthand your struct as a data
type in C (not needed in C++)
typedef struct My_Combo_Type{
int some_variable;
char some_string[256];
} My_Combo_Type;
My_Combo_Type my_combo;
my_combo.some_variable = 10;
strcpy(my_combo.some_string,
"hello struct");
* there are various ways to initialise a struct
C PROGRAMMING REFRESHER
ADDRESSES
▸ Unique location of each variable
▸ Pass by reference
▸ Ampersand
▸ Dynamic blocks of memory don't
have an associated variable
▸ Refer to by their address
▸ A pointer can store an address
void some_func( int* a );
int my_variable = 0;
some_func( &my_variable );
C PROGRAMMING REFRESHER
POINTERS, DEREFERENCING
▸ Stores memory address (just a
number)
▸ Looks confusing in C
▸ Can point to a pointers address
▸ Dereference to get value stored at
that address
▸ Pointers have a type, for
convenience
▸ Dereferencing a pointer to a struct
remind me to do a diagram
on the whiteboard here
and here
and here
C PROGRAMMING REFRESHER
DYNAMIC MEMORY ALLOCATION
▸ We will use C malloc rather than
C++ new keyword
▸ Know size of data in bytes
▸ sizeof() function
▸ Number of elements
▸ Heap
remind me to do some whiteboard
here
C PROGRAMMING REFRESHER
SIMPLIFY STRUCTURE
▸ Don't get carried away - only solve the problem at hand
▸ Time/cost/benefit
▸ Clarity, KISS
▸ Long functions are fine
▸ Blocks are good section separators { }
▸ Global variables are fine sparingly
▸ Less code, fewer files
▸ Quick and dirty is a great start - you can do a better one next time
C PROGRAMMING REFRESHER
COMPILER SEQUENCE
▸ C is a human language
▸ Pre-processor
▸ Compiler
▸ Assembler
▸ Linker
▸ You can stop compiler at each
stage and inspect the output
▸ Type of issues at each stage src: https://www.cs.cf.ac.uk/Dave/C/node3.html
C PROGRAMMING REFRESHER
ASM INSPECTION
▸ Matt Godbolt's
gcc explorer
https://gcc.godbolt.org/
▸ Insight into what your
code compiles into
▸ How smart is your compiler?
▸ What does optimisation do?
▸ How does inline work?
▸ How efficient is C++ STL vs. our own data structures? Why?
C PROGRAMMING REFRESHER
A PROGRAM'S MEMORY MODEL
▸ reserved system stuff
▸ Stack (frame per function)
▸ Heap (malloc)
▸ BSS (uninitialised statics)
▸ .data (initialised statics)
▸ Text (code)
▸ +dyn libraries loaded in-
between stack and heap
src: http://www.firmcodes.com/memory-layout-c-
program-2/
C PROGRAMMING REFRESHER
THE [FUNCTION CALL] STACK
▸ each function launched is awarded a frame of memory for its
local variables
▸ if that function calls another function inside it - push a new
frame on the stack
▸ when a function ends - pop its frame from stack
▸ most debuggers show you the call stack
▸ on crash can get a "backtrace" or "stack trace"
▸ are huge call stacks of tiny functions bad? - recursive vs loop?
▸ https://www.cs.umd.edu/class/sum2003/cmsc311/Notes/
Mips/stack.html
remind me to draw again
C PROGRAMMING REFRESHER
HEADERS AND >1 FILE
▸ Weakness of C - no packages
▸ #include (cut-pastes in)
▸ to share declarations between files
(otherwise type them at top of every file)
▸ Header guards to avoid "symbol already
defined” circular includes
▸ Using extern and static to share or hide
between files
▸ Usually don't put code instructions in
headers
▸ exceptions
// anton.h - header for anton.c
// written in C99 - Anton Gerdelan - date
#pragma once
#include <stdbool.h>
// concise explanation
int my_other_function(int* addr_of_thing);
extern int g_global_counter_thing;
#include <assert.h>
int g_global_counter_thing;
int my_other_function(int* addr_of_thing) {
assert(addr_of_thing);
*addr_of_thing = *addr_of_thing + 1;
g_global_counter_thing++;
return *addr_of_thing;
}
C PROGRAMMING REFRESHER
MAKEFILES AND BUILD SYSTEMS
▸ Gross
▸ Worth learning
Makefile
▸ IDEs have custom
project files
▸ Meta-build systems
exist
▸ Linking libraries is
painful
C PROGRAMMING REFRESHER
LINKING
▸ Dynamic vs. static libraries
▸ Operating systems all have different formats
▸ dynamic: .so .dll .dylib
▸ static or stubs: .a .lib
▸ System libraries vs. local libraries
▸ We will try to avoid this topic
▸ Linux/Apple may need to link math library explicitly
▸ only if you use functions from math.h
▸ clang -o my_prog main.c -lm
C PROGRAMMING REFRESHER
WHAT TO DO THIS WEEK
▸ Make sure that you can log in to lab computers
▸ Find an easy/working build env.
▸ Visual Studio or another IDE?
▸ Do you know how to step through code with a debugger?
▸ GCC or Clang?
▸ Make sure that you can compile a few simple C examples
▸ Do the warm-up assignment
▸ Let me know if it's too easy/too hard
C PROGRAMMING REFRESHER
TUTORIALS
▸ Tutorial
▸ analysing some code, discussion, solving problems
▸ bring pen+paper (or laptop if you want)
▸ We can modify tutorials to suit needs by request
▸ e.g. what are your concept / tools knowledge gaps?
▸ Assignments
▸ 2-3 weeks each (2x 3hr lab sessions for help/grading)
▸ Know how to do everything - work individually, but not in isolation
▸ Starter code or example in a lecture or tutorial

More Related Content

Similar to Lecture 2 - C Programming.pdf

GNU Compiler Collection - August 2005
GNU Compiler Collection - August 2005GNU Compiler Collection - August 2005
GNU Compiler Collection - August 2005Saleem Ansari
 
OOPS using C++
OOPS using C++OOPS using C++
OOPS using C++cpjcollege
 
Introduction-to-C-Part-1 (1).doc
Introduction-to-C-Part-1 (1).docIntroduction-to-C-Part-1 (1).doc
Introduction-to-C-Part-1 (1).docMayurWagh46
 
Introduction-to-C-Part-1.pdf
Introduction-to-C-Part-1.pdfIntroduction-to-C-Part-1.pdf
Introduction-to-C-Part-1.pdfAnassElHousni
 
C++ helps you to format the I/O operations like determining the number of dig...
C++ helps you to format the I/O operations like determining the number of dig...C++ helps you to format the I/O operations like determining the number of dig...
C++ helps you to format the I/O operations like determining the number of dig...bhargavi804095
 
Introduction-to-C-Part-1.pptx
Introduction-to-C-Part-1.pptxIntroduction-to-C-Part-1.pptx
Introduction-to-C-Part-1.pptxNEHARAJPUT239591
 
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJIntroduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJmeharikiros2
 
Basics of C Lecture 2[16097].pptx
Basics of C Lecture 2[16097].pptxBasics of C Lecture 2[16097].pptx
Basics of C Lecture 2[16097].pptxCoolGamer16
 
object oriented programming language fundamentals
object oriented programming language fundamentalsobject oriented programming language fundamentals
object oriented programming language fundamentalsChaithraCSHirematt
 
Object oriented programming c++
Object oriented programming c++Object oriented programming c++
Object oriented programming c++Ankur Pandey
 
Not Your Fathers C - C Application Development In 2016
Not Your Fathers C - C Application Development In 2016Not Your Fathers C - C Application Development In 2016
Not Your Fathers C - C Application Development In 2016maiktoepfer
 

Similar to Lecture 2 - C Programming.pdf (20)

02 c++g3 d (1)
02 c++g3 d (1)02 c++g3 d (1)
02 c++g3 d (1)
 
1 c introduction
1 c introduction1 c introduction
1 c introduction
 
C++Basics2022.pptx
C++Basics2022.pptxC++Basics2022.pptx
C++Basics2022.pptx
 
GNU Compiler Collection - August 2005
GNU Compiler Collection - August 2005GNU Compiler Collection - August 2005
GNU Compiler Collection - August 2005
 
C++ How to program
C++ How to programC++ How to program
C++ How to program
 
OOPS using C++
OOPS using C++OOPS using C++
OOPS using C++
 
Introduction-to-C-Part-1 (1).doc
Introduction-to-C-Part-1 (1).docIntroduction-to-C-Part-1 (1).doc
Introduction-to-C-Part-1 (1).doc
 
Introduction-to-C-Part-1.pdf
Introduction-to-C-Part-1.pdfIntroduction-to-C-Part-1.pdf
Introduction-to-C-Part-1.pdf
 
c++ referesher 1.pdf
c++ referesher 1.pdfc++ referesher 1.pdf
c++ referesher 1.pdf
 
C tutorial
C tutorialC tutorial
C tutorial
 
Introduction to CMake
Introduction to CMakeIntroduction to CMake
Introduction to CMake
 
C tutorial
C tutorialC tutorial
C tutorial
 
11 cpp
11 cpp11 cpp
11 cpp
 
C++ helps you to format the I/O operations like determining the number of dig...
C++ helps you to format the I/O operations like determining the number of dig...C++ helps you to format the I/O operations like determining the number of dig...
C++ helps you to format the I/O operations like determining the number of dig...
 
Introduction-to-C-Part-1.pptx
Introduction-to-C-Part-1.pptxIntroduction-to-C-Part-1.pptx
Introduction-to-C-Part-1.pptx
 
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJIntroduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
 
Basics of C Lecture 2[16097].pptx
Basics of C Lecture 2[16097].pptxBasics of C Lecture 2[16097].pptx
Basics of C Lecture 2[16097].pptx
 
object oriented programming language fundamentals
object oriented programming language fundamentalsobject oriented programming language fundamentals
object oriented programming language fundamentals
 
Object oriented programming c++
Object oriented programming c++Object oriented programming c++
Object oriented programming c++
 
Not Your Fathers C - C Application Development In 2016
Not Your Fathers C - C Application Development In 2016Not Your Fathers C - C Application Development In 2016
Not Your Fathers C - C Application Development In 2016
 

Recently uploaded

如何办理(Adelaide毕业证)阿德莱德大学毕业证成绩单Adelaide学历认证真实可查
如何办理(Adelaide毕业证)阿德莱德大学毕业证成绩单Adelaide学历认证真实可查如何办理(Adelaide毕业证)阿德莱德大学毕业证成绩单Adelaide学历认证真实可查
如何办理(Adelaide毕业证)阿德莱德大学毕业证成绩单Adelaide学历认证真实可查awo24iot
 
定制(UI学位证)爱达荷大学毕业证成绩单原版一比一
定制(UI学位证)爱达荷大学毕业证成绩单原版一比一定制(UI学位证)爱达荷大学毕业证成绩单原版一比一
定制(UI学位证)爱达荷大学毕业证成绩单原版一比一ss ss
 
如何办理伦敦大学伯贝克学院毕业证(BBK毕业证) 成绩单留信学历认证原版一比一
如何办理伦敦大学伯贝克学院毕业证(BBK毕业证) 成绩单留信学历认证原版一比一如何办理伦敦大学伯贝克学院毕业证(BBK毕业证) 成绩单留信学历认证原版一比一
如何办理伦敦大学伯贝克学院毕业证(BBK毕业证) 成绩单留信学历认证原版一比一ga6c6bdl
 
(办理学位证)加州州立大学北岭分校毕业证成绩单原版一比一
(办理学位证)加州州立大学北岭分校毕业证成绩单原版一比一(办理学位证)加州州立大学北岭分校毕业证成绩单原版一比一
(办理学位证)加州州立大学北岭分校毕业证成绩单原版一比一Fi sss
 
(SANA) Call Girls Landewadi ( 7001035870 ) HI-Fi Pune Escorts Service
(SANA) Call Girls Landewadi ( 7001035870 ) HI-Fi Pune Escorts Service(SANA) Call Girls Landewadi ( 7001035870 ) HI-Fi Pune Escorts Service
(SANA) Call Girls Landewadi ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
萨斯喀彻温大学毕业证学位证成绩单-购买流程
萨斯喀彻温大学毕业证学位证成绩单-购买流程萨斯喀彻温大学毕业证学位证成绩单-购买流程
萨斯喀彻温大学毕业证学位证成绩单-购买流程1k98h0e1
 
Call Girls in Nagpur Bhavna Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Bhavna Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Bhavna Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Bhavna Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
vip Model Basti Call Girls 9999965857 Call or WhatsApp Now Book
vip Model Basti Call Girls 9999965857 Call or WhatsApp Now Bookvip Model Basti Call Girls 9999965857 Call or WhatsApp Now Book
vip Model Basti Call Girls 9999965857 Call or WhatsApp Now Bookmanojkuma9823
 
(MEGHA) Hinjewadi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune E...
(MEGHA) Hinjewadi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune E...(MEGHA) Hinjewadi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune E...
(MEGHA) Hinjewadi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune E...ranjana rawat
 
Call Girls Service Kolkata Aishwarya 🤌 8250192130 🚀 Vip Call Girls Kolkata
Call Girls Service Kolkata Aishwarya 🤌  8250192130 🚀 Vip Call Girls KolkataCall Girls Service Kolkata Aishwarya 🤌  8250192130 🚀 Vip Call Girls Kolkata
Call Girls Service Kolkata Aishwarya 🤌 8250192130 🚀 Vip Call Girls Kolkataanamikaraghav4
 
Vip Noida Escorts 9873940964 Greater Noida Escorts Service
Vip Noida Escorts 9873940964 Greater Noida Escorts ServiceVip Noida Escorts 9873940964 Greater Noida Escorts Service
Vip Noida Escorts 9873940964 Greater Noida Escorts Serviceankitnayak356677
 
Call Girls In Munirka>༒9599632723 Incall_OutCall Available
Call Girls In Munirka>༒9599632723 Incall_OutCall AvailableCall Girls In Munirka>༒9599632723 Incall_OutCall Available
Call Girls In Munirka>༒9599632723 Incall_OutCall AvailableCall Girls in Delhi
 
(ANIKA) Wanwadi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(ANIKA) Wanwadi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(ANIKA) Wanwadi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(ANIKA) Wanwadi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
定制(USF学位证)旧金山大学毕业证成绩单原版一比一
定制(USF学位证)旧金山大学毕业证成绩单原版一比一定制(USF学位证)旧金山大学毕业证成绩单原版一比一
定制(USF学位证)旧金山大学毕业证成绩单原版一比一ss ss
 
Russian Call Girls Kolkata Chhaya 🤌 8250192130 🚀 Vip Call Girls Kolkata
Russian Call Girls Kolkata Chhaya 🤌  8250192130 🚀 Vip Call Girls KolkataRussian Call Girls Kolkata Chhaya 🤌  8250192130 🚀 Vip Call Girls Kolkata
Russian Call Girls Kolkata Chhaya 🤌 8250192130 🚀 Vip Call Girls Kolkataanamikaraghav4
 
WhatsApp 9892124323 ✓Call Girls In Khar ( Mumbai ) secure service - Bandra F...
WhatsApp 9892124323 ✓Call Girls In Khar ( Mumbai ) secure service -  Bandra F...WhatsApp 9892124323 ✓Call Girls In Khar ( Mumbai ) secure service -  Bandra F...
WhatsApp 9892124323 ✓Call Girls In Khar ( Mumbai ) secure service - Bandra F...Pooja Nehwal
 

Recently uploaded (20)

CIVIL ENGINEERING
CIVIL ENGINEERINGCIVIL ENGINEERING
CIVIL ENGINEERING
 
如何办理(Adelaide毕业证)阿德莱德大学毕业证成绩单Adelaide学历认证真实可查
如何办理(Adelaide毕业证)阿德莱德大学毕业证成绩单Adelaide学历认证真实可查如何办理(Adelaide毕业证)阿德莱德大学毕业证成绩单Adelaide学历认证真实可查
如何办理(Adelaide毕业证)阿德莱德大学毕业证成绩单Adelaide学历认证真实可查
 
定制(UI学位证)爱达荷大学毕业证成绩单原版一比一
定制(UI学位证)爱达荷大学毕业证成绩单原版一比一定制(UI学位证)爱达荷大学毕业证成绩单原版一比一
定制(UI学位证)爱达荷大学毕业证成绩单原版一比一
 
如何办理伦敦大学伯贝克学院毕业证(BBK毕业证) 成绩单留信学历认证原版一比一
如何办理伦敦大学伯贝克学院毕业证(BBK毕业证) 成绩单留信学历认证原版一比一如何办理伦敦大学伯贝克学院毕业证(BBK毕业证) 成绩单留信学历认证原版一比一
如何办理伦敦大学伯贝克学院毕业证(BBK毕业证) 成绩单留信学历认证原版一比一
 
(办理学位证)加州州立大学北岭分校毕业证成绩单原版一比一
(办理学位证)加州州立大学北岭分校毕业证成绩单原版一比一(办理学位证)加州州立大学北岭分校毕业证成绩单原版一比一
(办理学位证)加州州立大学北岭分校毕业证成绩单原版一比一
 
(SANA) Call Girls Landewadi ( 7001035870 ) HI-Fi Pune Escorts Service
(SANA) Call Girls Landewadi ( 7001035870 ) HI-Fi Pune Escorts Service(SANA) Call Girls Landewadi ( 7001035870 ) HI-Fi Pune Escorts Service
(SANA) Call Girls Landewadi ( 7001035870 ) HI-Fi Pune Escorts Service
 
萨斯喀彻温大学毕业证学位证成绩单-购买流程
萨斯喀彻温大学毕业证学位证成绩单-购买流程萨斯喀彻温大学毕业证学位证成绩单-购买流程
萨斯喀彻温大学毕业证学位证成绩单-购买流程
 
Call Girls in Nagpur Bhavna Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Bhavna Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Bhavna Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Bhavna Call 7001035870 Meet With Nagpur Escorts
 
vip Model Basti Call Girls 9999965857 Call or WhatsApp Now Book
vip Model Basti Call Girls 9999965857 Call or WhatsApp Now Bookvip Model Basti Call Girls 9999965857 Call or WhatsApp Now Book
vip Model Basti Call Girls 9999965857 Call or WhatsApp Now Book
 
(MEGHA) Hinjewadi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune E...
(MEGHA) Hinjewadi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune E...(MEGHA) Hinjewadi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune E...
(MEGHA) Hinjewadi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune E...
 
young call girls in Gtb Nagar,🔝 9953056974 🔝 escort Service
young call girls in Gtb Nagar,🔝 9953056974 🔝 escort Serviceyoung call girls in Gtb Nagar,🔝 9953056974 🔝 escort Service
young call girls in Gtb Nagar,🔝 9953056974 🔝 escort Service
 
Low rate Call girls in Delhi Justdial | 9953330565
Low rate Call girls in Delhi Justdial | 9953330565Low rate Call girls in Delhi Justdial | 9953330565
Low rate Call girls in Delhi Justdial | 9953330565
 
9953330565 Low Rate Call Girls In Jahangirpuri Delhi NCR
9953330565 Low Rate Call Girls In Jahangirpuri  Delhi NCR9953330565 Low Rate Call Girls In Jahangirpuri  Delhi NCR
9953330565 Low Rate Call Girls In Jahangirpuri Delhi NCR
 
Call Girls Service Kolkata Aishwarya 🤌 8250192130 🚀 Vip Call Girls Kolkata
Call Girls Service Kolkata Aishwarya 🤌  8250192130 🚀 Vip Call Girls KolkataCall Girls Service Kolkata Aishwarya 🤌  8250192130 🚀 Vip Call Girls Kolkata
Call Girls Service Kolkata Aishwarya 🤌 8250192130 🚀 Vip Call Girls Kolkata
 
Vip Noida Escorts 9873940964 Greater Noida Escorts Service
Vip Noida Escorts 9873940964 Greater Noida Escorts ServiceVip Noida Escorts 9873940964 Greater Noida Escorts Service
Vip Noida Escorts 9873940964 Greater Noida Escorts Service
 
Call Girls In Munirka>༒9599632723 Incall_OutCall Available
Call Girls In Munirka>༒9599632723 Incall_OutCall AvailableCall Girls In Munirka>༒9599632723 Incall_OutCall Available
Call Girls In Munirka>༒9599632723 Incall_OutCall Available
 
(ANIKA) Wanwadi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(ANIKA) Wanwadi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(ANIKA) Wanwadi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(ANIKA) Wanwadi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
定制(USF学位证)旧金山大学毕业证成绩单原版一比一
定制(USF学位证)旧金山大学毕业证成绩单原版一比一定制(USF学位证)旧金山大学毕业证成绩单原版一比一
定制(USF学位证)旧金山大学毕业证成绩单原版一比一
 
Russian Call Girls Kolkata Chhaya 🤌 8250192130 🚀 Vip Call Girls Kolkata
Russian Call Girls Kolkata Chhaya 🤌  8250192130 🚀 Vip Call Girls KolkataRussian Call Girls Kolkata Chhaya 🤌  8250192130 🚀 Vip Call Girls Kolkata
Russian Call Girls Kolkata Chhaya 🤌 8250192130 🚀 Vip Call Girls Kolkata
 
WhatsApp 9892124323 ✓Call Girls In Khar ( Mumbai ) secure service - Bandra F...
WhatsApp 9892124323 ✓Call Girls In Khar ( Mumbai ) secure service -  Bandra F...WhatsApp 9892124323 ✓Call Girls In Khar ( Mumbai ) secure service -  Bandra F...
WhatsApp 9892124323 ✓Call Girls In Khar ( Mumbai ) secure service - Bandra F...
 

Lecture 2 - C Programming.pdf

  • 1. C PROGRAMMING DR ANTON GERDELAN <GERDELA@SCSS.TCD.IE>
  • 2. LEAP IN AND TRY THINGS. IF YOU SUCCEED, YOU CAN HAVE ENORMOUS INFLUENCE. IF YOU FAIL, YOU HAVE STILL LEARNED SOMETHING, AND YOUR NEXT ATTEMPT IS SURE TO BE BETTER FOR IT. Brian Kernighan C PROGRAMMING REFRESHER
  • 3. C PROGRAMMING REFRESHER C - THE LANGUAGE OF UNIX ▸ Fundamental ▸ Easy to learn, simple, powerful ▸ DIY memory management ▸ Tools ▸ Features ▸ Plain old data types ▸ Pointers and addresses Unix creators Ken Thompson (left, designed B - 1969, Go - 2007) and Dennis Ritchie (right, designed C - 1972) of Bell Labs. src: ?
  • 4. C PROGRAMMING REFRESHER APPLICATIONS OF C ▸ Performance ▸ Operating systems, drivers ▸ Games ▸ 3d graphics, imaging ▸ Desktop software ▸ File i/o, tools ▸ Most things Prof. Brian Kernighan (Princeton U.) - co-author of "K&R C". We will come across his algorithms work later. src: Wikipedia.
  • 5. C PROGRAMMING REFRESHER PORTABILITY ▸ Almost all C also compiles as ▸ C++, objective C ▸ Very similar to ▸ C#, Java, D, PHP, JavaScript... ▸ Somewhat poss. to compile C++ into C
  • 6. C PROGRAMMING REFRESHER PRACTISE! ▸ How to get better ▸ read functions' instructions in man pages ▸ don't rely on auto-completion and Q&A websites ▸ watch more exp. people coding ▸ collaborate/share/code reviews/help - don’t be shy
  • 7. C PROGRAMMING REFRESHER PRACTISE! ▸ Importance of playing around ▸ What don't you know? ▸ make a list ▸ Ask good questions
  • 8. C PROGRAMMING REFRESHER POD TYPES ▸ "plain old data" types ▸ C89 had ▸ unsigned and signed versions ▸ long and short versions ▸ people defined their own boolean type ▸ C99 and C++ have bool int char float double size_t pointers typedef int custom_name;
  • 9. C PROGRAMMING REFRESHER A STRUCTURED DATA TYPE - ARRAYS ▸ pros ▸ multiple storage ▸ simple ▸ fast - looping over adjacent memory ▸ random access ▸ cons ▸ fixed size at compile time - waste space ▸ elements must be same type ▸ insertion requires shuffling ▸ often come with a count variable to say how much is used int my_array[2048]; int sum = 0; for (int i = 0; i < 2048; i++){ sum += my_array[i]; } printf("%in", sum); my_array[238] = 10;
  • 10. C PROGRAMMING REFRESHER STRUCT ▸ combine variables into single custom variable ▸ useful for passing and returning several variables from function ▸ C++ has classes which are just structs with some extra properties ▸ typedef usually used to shorthand your struct as a data type in C (not needed in C++) typedef struct My_Combo_Type{ int some_variable; char some_string[256]; } My_Combo_Type; My_Combo_Type my_combo; my_combo.some_variable = 10; strcpy(my_combo.some_string, "hello struct"); * there are various ways to initialise a struct
  • 11. C PROGRAMMING REFRESHER ADDRESSES ▸ Unique location of each variable ▸ Pass by reference ▸ Ampersand ▸ Dynamic blocks of memory don't have an associated variable ▸ Refer to by their address ▸ A pointer can store an address void some_func( int* a ); int my_variable = 0; some_func( &my_variable );
  • 12. C PROGRAMMING REFRESHER POINTERS, DEREFERENCING ▸ Stores memory address (just a number) ▸ Looks confusing in C ▸ Can point to a pointers address ▸ Dereference to get value stored at that address ▸ Pointers have a type, for convenience ▸ Dereferencing a pointer to a struct remind me to do a diagram on the whiteboard here and here and here
  • 13. C PROGRAMMING REFRESHER DYNAMIC MEMORY ALLOCATION ▸ We will use C malloc rather than C++ new keyword ▸ Know size of data in bytes ▸ sizeof() function ▸ Number of elements ▸ Heap remind me to do some whiteboard here
  • 14. C PROGRAMMING REFRESHER SIMPLIFY STRUCTURE ▸ Don't get carried away - only solve the problem at hand ▸ Time/cost/benefit ▸ Clarity, KISS ▸ Long functions are fine ▸ Blocks are good section separators { } ▸ Global variables are fine sparingly ▸ Less code, fewer files ▸ Quick and dirty is a great start - you can do a better one next time
  • 15. C PROGRAMMING REFRESHER COMPILER SEQUENCE ▸ C is a human language ▸ Pre-processor ▸ Compiler ▸ Assembler ▸ Linker ▸ You can stop compiler at each stage and inspect the output ▸ Type of issues at each stage src: https://www.cs.cf.ac.uk/Dave/C/node3.html
  • 16. C PROGRAMMING REFRESHER ASM INSPECTION ▸ Matt Godbolt's gcc explorer https://gcc.godbolt.org/ ▸ Insight into what your code compiles into ▸ How smart is your compiler? ▸ What does optimisation do? ▸ How does inline work? ▸ How efficient is C++ STL vs. our own data structures? Why?
  • 17. C PROGRAMMING REFRESHER A PROGRAM'S MEMORY MODEL ▸ reserved system stuff ▸ Stack (frame per function) ▸ Heap (malloc) ▸ BSS (uninitialised statics) ▸ .data (initialised statics) ▸ Text (code) ▸ +dyn libraries loaded in- between stack and heap src: http://www.firmcodes.com/memory-layout-c- program-2/
  • 18. C PROGRAMMING REFRESHER THE [FUNCTION CALL] STACK ▸ each function launched is awarded a frame of memory for its local variables ▸ if that function calls another function inside it - push a new frame on the stack ▸ when a function ends - pop its frame from stack ▸ most debuggers show you the call stack ▸ on crash can get a "backtrace" or "stack trace" ▸ are huge call stacks of tiny functions bad? - recursive vs loop? ▸ https://www.cs.umd.edu/class/sum2003/cmsc311/Notes/ Mips/stack.html remind me to draw again
  • 19. C PROGRAMMING REFRESHER HEADERS AND >1 FILE ▸ Weakness of C - no packages ▸ #include (cut-pastes in) ▸ to share declarations between files (otherwise type them at top of every file) ▸ Header guards to avoid "symbol already defined” circular includes ▸ Using extern and static to share or hide between files ▸ Usually don't put code instructions in headers ▸ exceptions // anton.h - header for anton.c // written in C99 - Anton Gerdelan - date #pragma once #include <stdbool.h> // concise explanation int my_other_function(int* addr_of_thing); extern int g_global_counter_thing; #include <assert.h> int g_global_counter_thing; int my_other_function(int* addr_of_thing) { assert(addr_of_thing); *addr_of_thing = *addr_of_thing + 1; g_global_counter_thing++; return *addr_of_thing; }
  • 20. C PROGRAMMING REFRESHER MAKEFILES AND BUILD SYSTEMS ▸ Gross ▸ Worth learning Makefile ▸ IDEs have custom project files ▸ Meta-build systems exist ▸ Linking libraries is painful
  • 21. C PROGRAMMING REFRESHER LINKING ▸ Dynamic vs. static libraries ▸ Operating systems all have different formats ▸ dynamic: .so .dll .dylib ▸ static or stubs: .a .lib ▸ System libraries vs. local libraries ▸ We will try to avoid this topic ▸ Linux/Apple may need to link math library explicitly ▸ only if you use functions from math.h ▸ clang -o my_prog main.c -lm
  • 22. C PROGRAMMING REFRESHER WHAT TO DO THIS WEEK ▸ Make sure that you can log in to lab computers ▸ Find an easy/working build env. ▸ Visual Studio or another IDE? ▸ Do you know how to step through code with a debugger? ▸ GCC or Clang? ▸ Make sure that you can compile a few simple C examples ▸ Do the warm-up assignment ▸ Let me know if it's too easy/too hard
  • 23. C PROGRAMMING REFRESHER TUTORIALS ▸ Tutorial ▸ analysing some code, discussion, solving problems ▸ bring pen+paper (or laptop if you want) ▸ We can modify tutorials to suit needs by request ▸ e.g. what are your concept / tools knowledge gaps? ▸ Assignments ▸ 2-3 weeks each (2x 3hr lab sessions for help/grading) ▸ Know how to do everything - work individually, but not in isolation ▸ Starter code or example in a lecture or tutorial