G52CPP C++ Programming Lecture 8. Dr Jason Atkin
|
|
- Emory Ambrose Hart
- 6 years ago
- Views:
Transcription
1 G52CPP C++ Programming Lecture 8 Dr Jason Atkin 1
2 Last lecture Dynamic memory allocation Memory re-allocation to grow arrays Linked lists Use -> rather than. pcurrent = pcurrent -> pnext; 2
3 Aside: do not use variable sized arrays Variable length arrays are NOT valid in C++ Sadly, gcc on avon, bann etc will allow them in C++ E.g.: int myfunc( int isize ) { } char array[isize]; Size of array is not a constant, it depends upon the value of variable You must use a numeric literal or a constant for a size You can use a #define to set it to a literal If you need variable size arrays, use malloc() or new Use: g++ -pedantic myfile.cpp to get a warning 3
4 This lecture More pre-processor And macros #ifdef Volatile and register keywords Unions and bitwise structs Sizes of structs and unions 4
5 #define and macros 5
6 #define A semi-intelligent find and replace facility Often considered bad in C++ code (useful in C) const is used more often, especially for members Template functions are better than macros Example: define a constant : #define MAX_ENTRIES 100 Replace occurrences of MAX_ENTRIES by the text 100 (without quotes), e.g. in: if ( entry_num < MAX_ENTRIES ) { } Remember: Done by the pre-processor! E.g. NOT actually a definition of a constant Constant #defines usually written in CAPITALS 6
7 #define and macro definitions You can use #define to define a macro: #define max(a,b) (((a)>(b))? (a) : (b)) int v1 = max( 40, 234 ); int v1 = (((40)>(234))? (40) : (234)) int v2 = max( v1, 99 ); int v2 = (((v1)>(99))? (v1) : (99)) int v3 = max ( v1, v2 ); int v3 = (((v1)>(v2))? (v1) : (v2)) Remember: done by the pre-processor! NOT a function call 7
8 MyHeader.h What is the output here? #ifndef MY_HEADER_H #define MY_HEADER_H #define max(a,b) (((a)>(b))? (a) : (b)) #endif MyTest.cpp #include <cstdio> #include MyHeader.h int main( int argc, char* argv[] ) { int a = 1, b = 1; while ( a < 10 ) { printf( a = %d, b = %d, a, b ); printf( max = %d\n, max(a++,b++) ); } } 8
9 The (surprise?) output printf( a = %d, b = %d, a, b ); printf( max = %d\n, max(a++,b++) ); The output is: a = 1, b = 1 max = 2 a = 2, b = 3 max = 4 a = 3, b = 5 max = 6 a = 4, b = 7 max = 8 a = 5, b = 9 max = 10 a = 6, b = 11 max = 12 a = 7, b = 13 max = 14 a = 8, b = 15 max = 16 a = 9, b = 17 max = 18 Why? 9
10 The (surprise?) output printf( a = %d, b = %d, a, b ); printf( max = %d\n, max(a++,b++) ); The output is: a = 1, b = 1 max = 2 a = 2, b = 3 max = 4 a = 3, b = 5 max = 6 a = 4, b = 7 max = 8 a = 5, b = 9 max = 10 a = 6, b = 11 max = 12 a = 7, b = 13 max = 14 a = 8, b = 15 max = 16 a = 9, b = 17 max = 18 Why? max( a++, b++ ) expands to: ((a++)>(b++))? (a++) : (b++) So, whichever number is greater will get incremented twice, and the lesser number only once 10
11 Warning about macros Do not use a macro where the evaluation of the parameters may have a side-effect E.g. max(a++,b++) Evaluating these parameters alters a value A side-effect inline template functions are better 11
12 #define and #ifdef 12
13 Conditional compilation You can remove parts of the source code if desired Done by the pre-processor (not even compiled) E.g. Only include code if some name has been defined earlier (in the code or included header file) #ifdef <NAME_OF_DEFINE> <Include this code if there was a matching #define> #else <Include this code if there was NOT a matching #define> #endif To include only if not defined use #ifndef There is also a #if <condition> 13
14 Conditional compilation Platform-dependent code can be included e.g. Include only if on a specific machine: #ifdef WINDOWS windows code here #elif SYS5UNIX System 5 code here #endif Often used for cross-platform code The correct #define has to be made somewhere to specify the current platform Know that this can be done, recognise it 14
15 Avoiding multiple inclusion Code to include the contents of a file only once: #ifndef UNIQUE_DEFINE_NAME_FOR_FILE #define UNIQUE_DEFINE_NAME_FOR_FILE include the rest of the file here #endif To work, the name in the #define has to be unique throughout the program E.g. you probably should include the path of the header file, not just the filename Example: mycode/game/graphics/screen.h could be called MYCODE_GAME_GRAPHICS_SCREEN_H By convention, #defines are in upper case 15
16 Example coursework file #ifndef DISPLAYABLEOBJECT_H #define DISPLAYABLEOBJECT_H #include "BaseEngine.h" If not already marked as included Mark it as included now, by setting the #define Includes the header files it needs class DisplayableObject { public: // Constructor DisplayableObject(BaseEngine* pengine); // Destructor virtual ~DisplayableObject(void); private: // True if item is visible bool m_bvisible; }; #endif End of the #ifdef around the contents 16
17 Three rules for header files 1. Ensure that the header file #includes everything that it needs itself i.e. #include any headers it depends upon 2. Ensure that it doesn t matter if the header file is included multiple times See previous slides 3. Ensure that header files can be included in any order A consequence of the first two rules 17
18 Volatile and register (so that you know that they exist) 18
19 volatile and register keywords The volatile keyword is important if other threads or processes may access the data (e.g. shared memory) Know that it exists and when you should use it Tells the compiler that data may change outside this thread or program (similar meaning in Java) Will turn off some potential optimisations/caching Value must be checked every time it is needed Compiler cannot assume it is unchanged Example: volatile int v = 4; volatile float f = 16.7f; Another one to know: the register keyword Request to store value in a register not a variable Again, know that it exists and what it does Not usually needed with modern optimising compilers 19
20 unions Treating something as one thing OR another Very rarely used compared with structs usually for low-level (e.g. o/s) code 20
21 Unions unions are very similar to structs except that the data members are in the same place In structs data members are one after another in memory (possibly with gaps) In unions data members all have the same address i.e. data is of one type OR another, not both 21
22 Unions Elements of unions are in the SAME place Elements of unions may be different sizes A union is as big as the biggest thing in it (plus any packing) Unions are a way of providing different ways of looking at the same memory union charorlong { }; unsigned long ul; char ar[8]; Size 4? Size 8 Addr: ul ar 1000 [0] 1001 ul [1] 1002 [2] 1003 [3] 1004 [4] 1005 [5] 1006 [6] [7]
23 Bitfields 23
24 Bit fields Within structs you can specify fields with size less than a byte struct position { unsigned char x : 3; /* 3 bits */ unsigned char y : 3; /* 3 bits */ unsigned char z : 2; /* 2 bits */ }; Which order the bits appear in the bytes is undefined (i.e. it could be high bits first, but could be low bits first, so bit order is implementation dependent) For clarity only: no faster at runtime that using a char/int and the bitwise operators (&,, etc ) 24
25 typedef 25
26 typedef Declare a new type name using typedef Usage: typedef <old_type> <new_name> E.g. typedef struct DATE { int d, m, y; } Date; Even C code can then use type Date instead of struct DATE In C++ (not C) you can omit the keywords struct, enum, union anyway Similar to an automatic typedef 26
27 Sizes and packing 27
28 structs struct DateTime { int time; char day; char month; short year; }; int main( int argc, char* argv[] ) { DateTime dt = { 80000, 01, 04, 1990 }; printf( "DOB: %5d %02d/%02d/%04d\n", dt.time, dt.day, dt.month, dt.year ); } return 0; 28
29 struct content positions struct DateTime { int time; char day; char month; short year; }; printf( "Address of dt = %p, size %d\n", &dt, sizeof(dt) ); printf( "Address of dt.time = %p, size %d\n", &(dt.time), sizeof(dt.time) ); printf( "Address of dt.day = %p, size %d\n", &(dt.day), sizeof(dt.day) ); printf( "Address of dt.month = %p, size %d\n", &(dt.month), sizeof(dt.month) ); printf( "Address of dt.year = %p, size %d\n", &(dt.year), sizeof(dt.year) ); 29
30 Positions in memory Time first time day month year Address Size dt 0x7fffaab dt.time 0x7fffaab dt.day 0x7fffaab dt.month 0x7fffaab dt.year 0x7fffaab
31 Gaps when day is first Day first day 0x7fff69becaf0 Size of structure: 12 time 0x7fff69becaf4 month 0x7fff69becaf8 year 0x7fff69becafa 31
32 May have gaps at the end Month last day 0x7fff69becaf0 Size of structure: 12 time 0x7fff69becaf4 year 0x7fff69becaf8 month 0x7fff69becafa 32
33 Tell it to pack on 1 byte boundaries #pragma pack(1) day time Address Size dt 0x7fff7e dt.day 0x7fff7e dt.time 0x7fff7e dt.month 0x7fff7e dt.year 0x7fff7e month year 33
34 Positions in memory Time first Day first #pragma pack(1) time day day time day month year time month year month year 34
35 #pragma structs may get empty space in them To align members for maximum speed You can usually tell compiler to pack structs e.g. with gcc can use the command: #pragma pack(1) #pragma means a compiler/operating system specific pre-processor directive 35
36 #pragma pack(1) #include <cstdio> struct A { int i; char c; }; union B { int i; char c; }; #pragma pack(1) struct C { int i; char c; }; union D { int i; char c; }; Example: char : 1 int : 4 struct A :? union B :? struct C :? union D :? int main( int argc, char** argv ) { printf( "sizeof(char): %d\n", sizeof(char) ); printf( "sizeof(int): %d\n", sizeof(int) ); printf( "sizeof(struct A): %d\n", sizeof(struct A) ); printf( "sizeof(union B): %d\n", sizeof(union B) ); printf( "sizeof(struct C): %d\n", sizeof(struct C) ); printf( "sizeof(union D): %d\n", sizeof(union D) ); return 0; } 36
37 #pragma pack(1) #include <cstdio> struct A { int i; char c; }; union B { int i; char c; }; #pragma pack(1) struct C { int i; char c; }; union D { int i; char c; }; int main( int argc, char** argv ) { printf( "sizeof(char): %d\n", sizeof(char) ); printf( "sizeof(int): %d\n", sizeof(int) ); printf( "sizeof(struct A): %d\n", sizeof(a) ); printf( "sizeof(union B): %d\n", sizeof(b) ); printf( "sizeof(struct C): %d\n", sizeof(c) ); printf( "sizeof(union D): %d\n", sizeof(d) ); return 0; } Example: char : 1 int : 4 struct A : 8 union B : 4 struct C : 5 union D : 4 37
38 Sizes of unions and structs If there is no excess space for packing: sizeof(struct) is total of the size of the members (i.e. sum of member sizes) Members are one after another in memory Bitfield structs use minimum number of bytes necessary sizeof(union) is size of the largest member (i.e. maximum of member sizes) All members are in the same place Largest member determines size 38
39 Next lecture classes (and C++ structs) Member functions inline functions 39
G52CPP C++ Programming Lecture 9
G52CPP C++ Programming Lecture 9 Dr Jason Atkin http://www.cs.nott.ac.uk/~jaa/cpp/ g52cpp.html 1 Last lecture const Constants, including pointers The C pre-processor And macros Compiling and linking And
More informationG52CPP C++ Programming Lecture 7. Dr Jason Atkin
G52CPP C++ Programming Lecture 7 Dr Jason Atkin 1 This lecture classes (and C++ structs) Member functions inline functions 2 Last lecture: predict the sizes 3 #pragma pack(1) #include struct A
More informationG52CPP C++ Programming Lecture 6. Dr Jason Atkin
G52CPP C++ Programming Lecture 6 Dr Jason Atkin 1 Last lecture The Stack Lifetime of local variables Global variables Static local variables const (briefly) 2 Visibility is different from lifetime Just
More informationCOMP322 - Introduction to C++ Lecture 02 - Basics of C++
COMP322 - Introduction to C++ Lecture 02 - Basics of C++ School of Computer Science 16 January 2012 C++ basics - Arithmetic operators Where possible, C++ will automatically convert among the basic types.
More informationSISTEMI EMBEDDED. The C Pre-processor Fixed-size integer types Bit Manipulation. Federico Baronti Last version:
SISTEMI EMBEDDED The C Pre-processor Fixed-size integer types Bit Manipulation Federico Baronti Last version: 20160302 The C PreProcessor CPP (1) CPP is a program called by the compiler that processes
More informationCS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco
CS 326 Operating Systems C Programming Greg Benson Department of Computer Science University of San Francisco Why C? Fast (good optimizing compilers) Not too high-level (Java, Python, Lisp) Not too low-level
More informationSISTEMI EMBEDDED. The C Pre-processor Fixed-size integer types Bit Manipulation. Federico Baronti Last version:
SISTEMI EMBEDDED The C Pre-processor Fixed-size integer types Bit Manipulation Federico Baronti Last version: 20170307 The C PreProcessor CPP (1) CPP is a program called by the compiler that processes
More informationCOMP322 - Introduction to C++
COMP322 - Introduction to C++ Winter 2011 Lecture 2 - Language Basics Milena Scaccia School of Computer Science McGill University January 11, 2011 Course Web Tools Announcements, Lecture Notes, Assignments
More information1d: tests knowing about bitwise fields and union/struct differences.
Question 1 1a: char ptr[] = Hello World ; char a = ptr[1], b = *(ptr+6); Creates an array of 12 elements, 11 visible letters and a character value 0 at the end. i true ii true iii false iv false v true
More informationThe University of Nottingham
The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 2 MODULE, AUTUMN SEMESTER 2009-2010 C/C++ for Java Programmers Time allowed TWO hours Candidates may complete the front cover of their answer
More informationSISTEMI EMBEDDED. The C Pre-processor Fixed-size integer types Bit Manipulation. Federico Baronti Last version:
SISTEMI EMBEDDED The C Pre-processor Fixed-size integer types Bit Manipulation Federico Baronti Last version: 20180312 The C PreProcessor CPP (1) CPP is a program called by the compiler that processes
More informationBasic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island
Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island Data Types Basic Types Enumerated types The type void Derived types
More informationCSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community
CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community http://csc.cs.rit.edu History and Evolution of Programming Languages 1. Explain the relationship between machine
More informationProcedures, Parameters, Values and Variables. Steven R. Bagley
Procedures, Parameters, Values and Variables Steven R. Bagley Recap A Program is a sequence of statements (instructions) Statements executed one-by-one in order Unless it is changed by the programmer e.g.
More informationCOMP 2355 Introduction to Systems Programming
COMP 2355 Introduction to Systems Programming Christian Grothoff christian@grothoff.org http://grothoff.org/christian/ 1 Functions Similar to (static) methods in Java without the class: int f(int a, int
More informationContents Lecture 3. C Preprocessor, Chapter 11 Declarations, Chapter 8. Jonas Skeppstedt Lecture / 44
Contents Lecture 3 C Preprocessor, Chapter 11 Declarations, Chapter 8 Jonas Skeppstedt (js@cs.lth.se) Lecture 3 2014 1 / 44 C Preprocessor Predefined macros Macro replacement Conditional inclusion Source
More informationEL6483: Brief Overview of C Programming Language
EL6483: Brief Overview of C Programming Language EL6483 Spring 2016 EL6483 EL6483: Brief Overview of C Programming Language Spring 2016 1 / 30 Preprocessor macros, Syntax for comments Macro definitions
More informationFundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University
Fundamental Data Types CSE 130: Introduction to Programming in C Stony Brook University Program Organization in C The C System C consists of several parts: The C language The preprocessor The compiler
More informationCommon Misunderstandings from Exam 1 Material
Common Misunderstandings from Exam 1 Material Kyle Dewey Stack and Heap Allocation with Pointers char c = c ; char* p1 = malloc(sizeof(char)); char** p2 = &p1; Where is c allocated? Where is p1 itself
More informationSection Notes - Week 1 (9/17)
Section Notes - Week 1 (9/17) Why do we need to learn bits and bitwise arithmetic? Since this class is about learning about how computers work. For most of the rest of the semester, you do not have to
More informationIMPORTANT QUESTIONS IN C FOR THE INTERVIEW
IMPORTANT QUESTIONS IN C FOR THE INTERVIEW 1. What is a header file? Header file is a simple text file which contains prototypes of all in-built functions, predefined variables and symbolic constants.
More informationCPSC 427: Object-Oriented Programming
CPSC 427: Object-Oriented Programming Michael J. Fischer Lecture 3 September 7, 2016 CPSC 427, Lecture 3 1/27 Insertion Sort Example Program specification Monolithic solution Modular solution in C Modular
More informationHave examined process Creating program Have developed program Written in C Source code
Preprocessing, Compiling, Assembling, and Linking Introduction In this lesson will examine Architecture of C program Introduce C preprocessor and preprocessor directives How to use preprocessor s directives
More informationVariables Data types Variable I/O. C introduction. Variables. Variables 1 / 14
C introduction Variables Variables 1 / 14 Contents Variables Data types Variable I/O Variables 2 / 14 Usage Declaration: t y p e i d e n t i f i e r ; Assignment: i d e n t i f i e r = v a l u e ; Definition
More informationAppendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.
A Appendix Grammar There is no worse danger for a teacher than to teach words instead of things. Marc Block Introduction keywords lexical conventions programs expressions statements declarations declarators
More informationCS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Bernhard Boser & Randy Katz
CS 61C: Great Ideas in Computer Architecture Lecture 3: Pointers Bernhard Boser & Randy Katz http://inst.eecs.berkeley.edu/~cs61c Agenda Pointers in C Arrays in C This is not on the test Pointer arithmetic
More informationShort Notes of CS201
#includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system
More informationAgenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture
CS 61C: Great Ideas in Computer Architecture Krste Asanović & Randy Katz http://inst.eecs.berkeley.edu/~cs61c And in Conclusion, 2 Processor Control Datapath Components of a Computer PC Registers Arithmetic
More informationCS201 - Introduction to Programming Glossary By
CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with
More informationC Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:
C Programming Code: MBD101 Duration: 10 Hours Prerequisites: You are a computer science Professional/ graduate student You can execute Linux/UNIX commands You know how to use a text-editing tool You should
More informationCS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz
CS 61C: Great Ideas in Computer Architecture Lecture 3: Pointers Krste Asanović & Randy Katz http://inst.eecs.berkeley.edu/~cs61c Agenda Pointers in C Arrays in C This is not on the test Pointer arithmetic
More informationCS Programming In C
CS 24000 - Programming In C Week Two: Basic C Program Organization and Data Types Zhiyuan Li Department of Computer Science Purdue University, USA 2 int main() { } return 0; The Simplest C Program C programs
More informationCE221 Programming in C++ Part 1 Introduction
CE221 Programming in C++ Part 1 Introduction 06/10/2017 CE221 Part 1 1 Module Schedule There are two lectures (Monday 13.00-13.50 and Tuesday 11.00-11.50) each week in the autumn term, and a 2-hour lab
More informationELEC 377 C Programming Tutorial. ELEC Operating Systems
ELE 377 Programming Tutorial Outline! Short Introduction! History & Memory Model of! ommon Errors I have seen over the years! Work through a linked list example on the board! - uses everything I talk about
More information1c) (iv) and (v) OR (v) only (hindsight showed that the question was more ambiguous than intended)
Answers to 2008/2009 G52CFJ exam Below are the answers to last year s exam, so that you can check how you did. In many cases I have given an example answer and an idea of what elements we would look for.
More information6.096 Introduction to C++ January (IAP) 2009
MIT OpenCourseWare http://ocw.mit.edu 6.096 Introduction to C++ January (IAP) 2009 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms. Welcome to 6.096 Lecture
More informationG52CPP C++ Programming Lecture 12
G52CPP C++ Programming Lecture 12 Dr Jason Atkin http://www.cs.nott.ac.uk/~jaa/cpp/ g52cpp.html 1 Last lecture this and static members References Act like pointers Look like values More const And mutable
More informationRicardo Rocha. Department of Computer Science Faculty of Sciences University of Porto
Ricardo Rocha Department of Computer Science Faculty of Sciences University of Porto Adapted from the slides Revisões sobre Programação em C, Sérgio Crisóstomo Compilation #include int main()
More informationStructures, Unions Alignment, Padding, Bit Fields Access, Initialization Compound Literals Opaque Structures Summary. Structures
Structures Proseminar C Grundlagen und Konzepte Michael Kuhn Research Group Scientific Computing Department of Informatics Faculty of Mathematics, Informatics und Natural Sciences University of Hamburg
More information#include. Practical C Issues: #define. #define Macros. Example. #if
#include Practical C Issues: Preprocessor Directives, Typedefs, Multi-file Development, and Makefiles Jonathan Misurda jmisurda@cs.pitt.edu Copies the contents of the specified file into the current file
More informationC Review. MaxMSP Developers Workshop Summer 2009 CNMAT
C Review MaxMSP Developers Workshop Summer 2009 CNMAT C Syntax Program control (loops, branches): Function calls Math: +, -, *, /, ++, -- Variables, types, structures, assignment Pointers and memory (***
More informationIntroduction to Programming in C Department of Computer Science and Engineering. Lecture No. #54. Organizing Code in multiple files
Introduction to Programming in C Department of Computer Science and Engineering Lecture No. #54 Organizing Code in multiple files (Refer Slide Time: 00:09) In this lecture, let us look at one particular
More informationModifiers. int foo(int x) { static int y=0; /* value of y is saved */ y = x + y + 7; /* across invocations of foo */ return y; }
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
More informationQUIZ. What are 3 differences between C and C++ const variables?
QUIZ What are 3 differences between C and C++ const variables? Solution QUIZ Source: http://stackoverflow.com/questions/17349387/scope-of-macros-in-c Solution The C/C++ preprocessor substitutes mechanically,
More informationBasic Types, Variables, Literals, Constants
Basic Types, Variables, Literals, Constants What is in a Word? A byte is the basic addressable unit of memory in RAM Typically it is 8 bits (octet) But some machines had 7, or 9, or... A word is the basic
More informationG52CPP C++ Programming Lecture 10. Dr Jason Atkin
G52CPP C++ Programming Lecture 10 Dr Jason Atkin 1 Last lecture Constructors Default constructor needs no parameters Default parameters Inline functions Like safe macros in some ways Function definitions
More informationG52CPP C++ Programming Lecture 3. Dr Jason Atkin
G52CPP C++ Programming Lecture 3 Dr Jason Atkin E-Mail: jaa@cs.nott.ac.uk 1 Revision so far C/C++ designed for speed, Java for catching errors Java hides a lot of the details (so can C++) Much of C, C++
More informationIntermediate Programming, Spring 2017*
600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general
More informationFrom Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides
From Java to C Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides 1 Outline Overview comparison of C and Java Good evening Preprocessor
More informationRule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead.
Chapter 9: Rules Chapter 1:Style and Program Organization Rule 1-1: Organize programs for readability, just as you would expect an author to organize a book. Rule 1-2: Divide each module up into a public
More informationMotivation was to facilitate development of systems software, especially OS development.
A History Lesson C Basics 1 Development of language by Dennis Ritchie at Bell Labs culminated in the C language in 1972. Motivation was to facilitate development of systems software, especially OS development.
More informationSo far, system calls have had easy syntax. Integer, character string, and structure arguments.
Pointers Page 1 So far, system calls have had easy syntax Wednesday, September 30, 2015 10:45 AM Integer, character string, and structure arguments. But this is not always true. Today, we begin to explore
More informationFundamental of Programming (C)
Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 3 Constants, Variables, Data Types, And Operations Department of Computer Engineering
More informationFinal CSE 131B Spring 2004
Login name Signature Name Student ID Final CSE 131B Spring 2004 Page 1 Page 2 Page 3 Page 4 Page 5 Page 6 Page 7 Page 8 (25 points) (24 points) (32 points) (24 points) (28 points) (26 points) (22 points)
More informationCSE 374 Programming Concepts & Tools
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2017 Lecture 8 C: Miscellanea Control, Declarations, Preprocessor, printf/scanf 1 The story so far The low-level execution model of a process (one
More informationUEE1302 (1102) F10: Introduction to Computers and Programming
Computational Intelligence on Automation Lab @ NCTU Learning Objectives UEE1302 (1102) F10: Introduction to Computers and Programming Programming Lecture 00 Programming by Example Introduction to C++ Origins,
More informationMemory. What is memory? How is memory organized? Storage for variables, data, code etc. Text (Code) Data (Constants) BSS (Global and static variables)
Memory Allocation Memory What is memory? Storage for variables, data, code etc. How is memory organized? Text (Code) Data (Constants) BSS (Global and static variables) Text Data BSS Heap Stack (Local variables)
More informationOutline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C
Lecture 1 C primer What we will cover A crash course in the basics of C You should read the K&R C book for lots more details Various details will be exemplified later in the course Outline Overview comparison
More information4) In C, if you pass an array as an argument to a function, what actually gets passed?
Paytm Programming Sample paper: 1) What will be the output of the program? #include int main() } int y=128; const int x=y; printf("%d\n", x); return 0; a. 128 b. Garbage value c. Error d. 0 2)
More informationME 461 C review Session Fall 2009 S. Keres
ME 461 C review Session Fall 2009 S. Keres DISCLAIMER: These notes are in no way intended to be a complete reference for the C programming material you will need for the class. They are intended to help
More informationcs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).
cs3157: another C lecture (mon-21-feb-2005) C pre-processor (1). today: C pre-processor command-line arguments more on data types and operators: booleans in C logical and bitwise operators type conversion
More informationLab 1: First Steps in C++ - Eclipse
Lab 1: First Steps in C++ - Eclipse Step Zero: Select workspace 1. Upon launching eclipse, we are ask to chose a workspace: 2. We select a new workspace directory (e.g., C:\Courses ): 3. We accept the
More informationC: Program Structure. Department of Computer Science College of Engineering Boise State University. September 11, /13
Department of Computer Science College of Engineering Boise State University September 11, 2017 1/13 Scope Variables and functions are visible from the point they are defined until the end of the source
More informationPRINCIPLES OF OPERATING SYSTEMS
PRINCIPLES OF OPERATING SYSTEMS Tutorial-1&2: C Review CPSC 457, Spring 2015 May 20-21, 2015 Department of Computer Science, University of Calgary Connecting to your VM Open a terminal (in your linux machine)
More informationArrays and Memory Management
Arrays and Memory Management 1 Pointing to Different Size Objects Modern machines are byte-addressable Hardware s memory composed of 8-bit storage cells, each has a unique address A C pointer is just abstracted
More informationC Programming Review CSC 4320/6320
C Programming Review CSC 4320/6320 Overview Introduction C program Structure Keywords & C Types Input & Output Arrays Functions Pointers Structures LinkedList Dynamic Memory Allocation Macro Compile &
More informationKurt Schmidt. October 30, 2018
to Structs Dept. of Computer Science, Drexel University October 30, 2018 Array Objectives to Structs Intended audience: Student who has working knowledge of Python To gain some experience with a statically-typed
More informationA Fast Review of C Essentials Part I
A Fast Review of C Essentials Part I Structural Programming by Z. Cihan TAYSI Outline Program development C Essentials Functions Variables & constants Names Formatting Comments Preprocessor Data types
More informationMacros and Preprocessor. CGS 3460, Lecture 39 Apr 17, 2006 Hen-I Yang
Macros and Preprocessor CGS 3460, Lecture 39 Apr 17, 2006 Hen-I Yang Previously Operations on Linked list (Create and Insert) Agenda Linked List (More insert, lookup and delete) Preprocessor Linked List
More informationCMSC445 Compiler design Blaheta. Project 2: Lexer. Due: 15 February 2012
CMSC445 Compiler design Blaheta Project 2: Lexer Due: 15 February 2012 In this project we ll actually get started on our C compiler; we ll use flex to process a C program into a stream of tokens (not just
More informationComputer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary
GATE- 2016-17 Postal Correspondence 1 C-Programming Computer Science & Information Technology (CS) 20 Rank under AIR 100 Postal Correspondence Examination Oriented Theory, Practice Set Key concepts, Analysis
More informationMid-term Exam. Fall Semester 2017 KAIST EE209 Programming Structures for Electrical Engineering. Name: Student ID:
Fall Semester 2017 KAIST EE209 Programming Structures for Electrical Engineering Mid-term Exam Name: This exam is closed book and notes. Read the questions carefully and focus your answers on what has
More informationCOSC 2P91. Bringing it all together... Week 4b. Brock University. Brock University (Week 4b) Bringing it all together... 1 / 22
COSC 2P91 Bringing it all together... Week 4b Brock University Brock University (Week 4b) Bringing it all together... 1 / 22 A note on practicality and program design... Writing a single, monolithic source
More informationAgenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011
CS 61C: Great Ideas in Computer Architecture (Machine Structures) Introduction to C (Part II) Instructors: Randy H. Katz David A. Patterson http://inst.eecs.berkeley.edu/~cs61c/sp11 Spring 2011 -- Lecture
More informationCprE 288 Introduction to Embedded Systems Exam 1 Review. 1
CprE 288 Introduction to Embedded Systems Exam 1 Review http://class.ece.iastate.edu/cpre288 1 Overview of Today s Lecture Announcements Exam 1 Review http://class.ece.iastate.edu/cpre288 2 Announcements
More informationHello, World! in C. Johann Myrkraverk Oskarsson October 23, The Quintessential Example Program 1. I Printing Text 2. II The Main Function 3
Hello, World! in C Johann Myrkraverk Oskarsson October 23, 2018 Contents 1 The Quintessential Example Program 1 I Printing Text 2 II The Main Function 3 III The Header Files 4 IV Compiling and Running
More informationVariables and literals
Demo lecture slides Although I will not usually give slides for demo lectures, the first two demo lectures involve practice with things which you should really know from G51PRG Since I covered much of
More informationQUIZ. What is wrong with this code that uses default arguments?
QUIZ What is wrong with this code that uses default arguments? Solution The value of the default argument should be placed in either declaration or definition, not both! QUIZ What is wrong with this code
More informationLectures 5-6: Introduction to C
Lectures 5-6: Introduction to C Motivation: C is both a high and a low-level language Very useful for systems programming Faster than Java This intro assumes knowledge of Java Focus is on differences Most
More informationFirst of all, it is a variable, just like other variables you studied
Pointers: Basics What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the address (rather than the value)
More informationCSC 1600 Memory Layout for Unix Processes"
CSC 16 Memory Layout for Unix Processes" 1 Lecture Goals" Behind the scenes of running a program" Code, executable, and process" Memory layout for UNIX processes, and relationship to C" : code and constant
More informationG52CPP C++ Programming Lecture 18. Dr Jason Atkin
G52CPP C++ Programming Lecture 18 Dr Jason Atkin 1 Last lecture Operator Overloading Strings and streams 2 Operator overloading - what to know Know that you can change the meaning of operators Know that
More informationunsigned char memory[] STACK ¼ 0x xC of address space globals function KERNEL code local variables
Graded assignment 0 will be handed out in section Assignment 1 Not that bad Check your work (run it through the compiler) Factorial Program Prints out ENTERING, LEAVING, and other pointers unsigned char
More informationMotivation was to facilitate development of systems software, especially OS development.
A History Lesson C Basics 1 Development of language by Dennis Ritchie at Bell Labs culminated in the C language in 1972. Motivation was to facilitate development of systems software, especially OS development.
More informationG52CPP C++ Programming Lecture 14. Dr Jason Atkin
G52CPP C++ Programming Lecture 14 Dr Jason Atkin 1 Last Lecture Automatically created methods: A default constructor so that objects can be created without defining a constructor A copy constructor used
More informationStructures and Unions in C. Alan L. Cox
Structures and Unions in C Alan L. Cox alc@rice.edu Administrivia Assignment 1 is due tonight Textbook Lectures begin covering material that is also covered by the textbook on 1/30 Assignment 3 (assigned
More informationData types: Struct, Union, Enum, Bit Fields. Preprocessor and Building Programs
Data types: Struct, Union, Enum, Bit Fields. Preprocessor and Building Programs Jan Faigl Department of Computer Science Faculty of Electrical Engineering Czech Technical University in Prague Lecture 05
More informationPart I Data types Struct, Union, Enum and Bit Fields
Data types: Struct, Union, Enum, Bit Fields. Preprocessor and Building Programs Jan Faigl Department of Computer Science Faculty of Electrical Engineering Czech Technical University in Prague Lecture 05
More informationThe New C Standard (Excerpted material)
The New C Standard (Excerpted material) An Economic and Cultural Derek M. Jones derek@knosof.co.uk Copyright 2002-2008 Derek M. Jones. All rights reserved. 1378 type specifier type-specifier: void char
More informationVariables and Constants
HOUR 3 Variables and Constants Programs need a way to store the data they use. Variables and constants offer various ways to work with numbers and other values. In this hour you learn: How to declare and
More informationCSE 333 Midterm Exam 5/9/14 Sample Solution
Question 1. (20 points) C programming. Implement the C library function strncpy. The specification of srncpy is as follows: Copy characters (bytes) from src to dst until either a '\0' character is found
More informationDesign and development of embedded systems for the Internet of Things (IoT) Fabio Angeletti Fabrizio Gattuso
Design and development of embedded systems for the Internet of Things (IoT) Fabio Angeletti Fabrizio Gattuso Why C? Test on 21 Android Devices with 32-bits and 64-bits processors and different versions
More information18-642: Code Style for Compilers
18-642: Code Style for Compilers 9/25/2017 1 Anti-Patterns: Coding Style: Language Use Code compiles with warnings Warnings are turned off or over-ridden Insufficient warning level set Language safety
More informationG52CPP C++ Programming Lecture 13
G52CPP C++ Programming Lecture 13 Dr Jason Atkin http://www.cs.nott.ac.uk/~jaa/cpp/ g52cpp.html 1 Last lecture Function pointers Arrays of function pointers Virtual and non-virtual functions vtable and
More informationCS61, Fall 2012 Section 2 Notes
CS61, Fall 2012 Section 2 Notes (Week of 9/24-9/28) 0. Get source code for section [optional] 1: Variable Duration 2: Memory Errors Common Errors with memory and pointers Valgrind + GDB Common Memory Errors
More informationP.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above
P.G.TRB - COMPUTER SCIENCE Total Marks : 50 Time : 30 Minutes 1. C was primarily developed as a a)systems programming language b) general purpose language c) data processing language d) none of the above
More informationSyntax and Variables
Syntax and Variables What the Compiler needs to understand your program, and managing data 1 Pre-Processing Any line that starts with # is a pre-processor directive Pre-processor consumes that entire line
More informationC PROGRAMMING LANGUAGE. POINTERS, ARRAYS, OPERATORS AND LOOP. CAAM 519, CHAPTER5
C PROGRAMMING LANGUAGE. POINTERS, ARRAYS, OPERATORS AND LOOP. CAAM 519, CHAPTER5 1. Pointers As Kernighan and Ritchie state, a pointer is a variable that contains the address of a variable. They have been
More informationME240 Computation for Mechanical Engineering. Lecture 4. C++ Data Types
ME240 Computation for Mechanical Engineering Lecture 4 C++ Data Types Introduction In this lecture we will learn some fundamental elements of C++: Introduction Data Types Identifiers Variables Constants
More informationCSE 374 Programming Concepts & Tools. Brandon Myers Winter 2015 C: Linked list, casts, the rest of the preprocessor (Thanks to Hal Perkins)
CSE 374 Programming Concepts & Tools Brandon Myers Winter 2015 C: Linked list, casts, the rest of the preprocessor (Thanks to Hal Perkins) Linked lists, trees, and friends Very, very common data structures
More information