Chapter 13 Storage classes

Save this PDF as:
 WORD  PNG  TXT  JPG

Size: px
Start display at page:

Download "Chapter 13 Storage classes"

Transcription

1 Chapter 13 Storage classes 1. Storage classes 2. Storage Class auto 3. Storage Class extern 4. Storage Class static 5. Storage Class register 6. Global and Local Variables 7. Nested Blocks with the Same Variable Name 8. Storage Classes for Multiple Source Files 9. Storage Classes for Functions 10. ANSI C Type Qualifiers

2 1. Storage classes The storage class of a variable is a set of properties about the variable. The storage class of a variable determines 3 things: scope: the sections of code that can use the variable. (i.e., variable visibility domain) linkage: how a variable can be used in a multiple source-file program. storage duration: how long the variable persists in memory. The storage class of a variable is determined by where it is defined and by what keyword (e.g. extern, auto, static, register) is used.

3 Scope of Variable A C variable has one of the following 3 scopes: file scope: the variable is visible from the point it is defined to the end of file containing the definition. block scope: the variable is visible from the point it is defined until the end of the block containing the definition. Function prototype scope: The variable is visible till the end of the function prototype declaration (int funct1(int a, double b);)

4 Example int global_var; /* global_var has file scope*/ double myfunction(int a; double b); /*a & b have function prototype scope */ main(void) { }... return 0; double myfunction(int x, double y) { } /* x, y have block scope */ double c=x + 3*y; /* c has block scope */ return c;

5 Linkage of Variables A C variable has one of the following linkages: external linkage: the variable can be used anywhere in a multifile program. internal linkage: the variable can be used anywhere in a single file. no linkage: the variable can be used only in the block in which it is defined. Variables with block scope or function prototype scope have no linkage.

6 Storage Duration of Variables The storage duration of a C variable can be: static storage duration: the variable exists throughout the program execution. automatic storage duration: the variable can exist only while the program is executing the block in which the variable is defined.

7 Example #include <stdio.h> int global_var = 5; /* static storage duration */ void function1(int var); main(void) { } function1(global_var); return 0; void function1(int var1) { /* var1 has auto storage */ } int var2; /* var2 has auto storage duration */ for (var2=0; var2<var1; var2++) puts( Welcome to C programming );

8 2. Storage Class auto The automatic variables have the following descriptions: They are defined as function arguments, e.g. k in fn(), or They are defined in a block. A block is a complex statement which begins with a { and ends with a }. The definition of automatic variables comes right after the {, e.g. i and j (next).

9 Example fn(int k) /* automatic var */ { } int i; /* automatic */ for (i=0; i<10; i++) { } int j; /* automatic */ for (j=0; j<4; j++) if (k%j==0) k++; return(k); /* j no longer available */

10 Properties An automatic variable has automatic storage: the life time of an automatic variable is only within the function or block it is defined in. Consequently, it has no meaning outside the function or block. It is called the local variable of the corresponding function or block. The automatic variables are deleted after execution of the corresponding function or block where they are defined. It is because they are no longer needed and memory occupied by the variables can be reused. Consequently, the value stored in an automatic variable is lost after exiting from a function or block.

11 Continued An automatic variable has block scope. Only the function in which the variable is defined can access that variable by name. An automatic variable has no linkage: The variable cannot be declared twice in the same block. An automatic variable can be defined explicitly using the keyword auto as

12 Example fn(int k) /* k automatic var */ { } auto int i; /* auto variable */ for (i=0; i<10; i++) { auto int j; /* auto variable */ for (j=0; j<4; j++) if (k%j==0) k++; } /* j invisible hereafter */ return(k);

13 3. Storage Class extern A variable defined outside a function is external. An external variable can also be declared inside a function that uses it by using the extern keyword. If the variable is defined in another file, or after the function definition, then declaring the variable with extern is mandatory.

14 Example double Earray[10]; /* externally defined array */ extern char Echar; /* mandatory declaration Echar is defined in another file */ int next(void); main(void) { } extern int Eint; /* mandatory declaration */ extern double Earray[]; /* optional declaration */ Eint += 10;... int Eint; /* externally defined variable */ void next(void) { } Eint++;... /* subsequent functions can use Eint */

15 Properties External variables have external linkage. External variables have static storage duration. For external variables, the compiler may not distinguish between upper- and lowercase, and it may recognize only the 1st 6 characters in a name.

16 4. Storage Class static The duration of a static variable is fixed. Static variables are created at the start of the program and are destroyed only at the end of program execution. Static variables can be defined inside or outside of a function. Here, we focus on the first case. #include <stdio.h> void function(); main(void) { int i; for (i=0; i<3; i++) function(); return 0; }

17 void function() { static int static_var=0; /* static variable */ int auto_var = 0; /* automatic variable */ ++static_var; ++auto_var; printf("the value of the static variable is: %d\n", static_var); printf("the value of the auto variable is: %d\n", auto_var); } $./Program output The value of the static variable is: 1 The value of the auto variable is: 1 The value of the static variable is: 2 The value of the auto variable is: 1 The value of the static variable is: 3 The value of the auto variable is: 1

18 5. Storage Class register An automatic variable can be defined using the keyword register as fn(register int k) { /* automatic var */ register int i; for (i=0; i<10; i++) { register int j; for (j=0; j<4; j++) if (k%j==0) k++; } return(k); }

19 Properties Inform the compiler that variables will be referenced on numerous occasions and, where possible, to use the CPU registers. Instructions using register variables executes faster than instructions that use no register variables. There are limited number of registers available. Applicable to automatic variables and function argument only Restricted to certain data types (is machine dependent), often int, char and pointer Not allowed to take address of register variables, i.e., & operator will not work with register variables.

20 6. Local and Global Variables Local variables: are variables defined inside a function. They have function or block scope Global variables: are variables defined outside the functions. They have file (or program) scope When should global variables be used? Advantages of using global variables: simplest way of communication between functions efficiency Disadvantages of using global variables: less readable program more difficult to debug and modify

21 Storage Classes for Different Variables Variables Keywords Scope Linkage Storage Duration automatic auto block no automatic external extern file/program external static static static file/block internal static register register block no automatic local none/auto/ static/register block global none/extern file no internal/ external automatic/ static static

22 7. Nested Blocks Variables are allowed to have the same name as long as they are defined under different scope or block A block may be nested with other blocks When variables with same name are declared in different nested blocks, the variables with smaller scope will temporarily hide the other variables within that block Local variables declared static still have block scope (although they exist from program start)

23 Example #include <stdio.h> main(void) { /* begin block 1 */ int i = 10; i += 10; printf("variable i in block 1 = %d\n", i); { /* begin block 2 */ int i = 100; i += 100; printf("variable i in block 2 = %d\n", i); { /* begin block 3 */ int i = 300; i += 300; printf("variable i in block 3 = %d\n", i); } /* end block 3 */ } /* end block 2 */ i += 10; printf("variable i in block 1 = %d\n", i); return 0; } /* end block 1 */

24 Program output Variable i in block 1 = 20 Variable i in block 2 = 200 Variable i in block 3 = 600 Variable i in block 1 = 30

25 Example modified #include <stdio.h> main(void) { /* begin block 1 */ int i = 10; i += 10; printf("variable i in block 1 = %d\n", i); { /* begin block 2 */ int i = 100; i += 100; printf("variable i in block 2 = %d\n", i); { /* begin block 3 */ // int i = 300; Commented out i += 300; printf("variable i in block 3 = %d\n", i); } /* end block 3 */ } /* end block 2 */ i += 10; printf("variable i in block 1 = %d\n", i); return 0; } /* end block 1 */

26 Program output Variable i in block 1 = 20 Variable i in block 2 = 200 Variable i in block 3 = 500 Variable i in block 1 = 30

27 8. Storage Classes for Multiple Source Files Multiple files can be used in C to store different pieces of codes of a single program One file may contain main() and some declaration; another may contain functions definitions and third may be a header file with prototypes and data structure declarations Consider the following multiple-sourcefile program:

28 #include <stdio.h> /* file1.c */ int index1; /* global to all files */ static int index2; /* global to file 1 */ main(void) { int i; /* auto */ extern int fn(int, int); /* fn() is in another source file*/ for (i=0; i<2; i++) { int j; /* auto */ for (j=1; j<2; j++) { index2 = fn(i, j); index1 = index2 + 1; printf("in Main index1 = %d, index2 = %d\n\n", index1, index2); } } return(0); }

29 /* file2.c */ #include <stdio.h> extern int index1; /* index1 is in another file */ fn(int i, int j) { int k; static int m=0; /*declare m to be static*/ k = index1; if (m == 0) printf("k is %d \t PRINTED ONLY ONCE \n\n", k); m++; printf("call number %d, m = %d, index1=%d\n\n", m, m, index1); return(m); }

30 A test run k is 0 PRINTED ONLY ONE TIME Call number 1, m = 1, index1 = 0 In Main index1 = 2, index2 = 1 Call number 2, m = 2, index1 = 2 In Main index1 = 3, index2 = 2

31 Properties The variable index1 is a global variable int index1; ( in file1.c ) The variable is visible to all functions and statements even if the functions are defined in different files For example, the statement index1 = index2 + 1; (in file1.c) Both file1.c and file2.c make use of the variable index1 although index1 is defined in file1.c

32 Continued However, global variables such has index1 defined in one file are not visible to other files, unless they are also declared in other files. For example, to define index1 in file1.c to be available to all files, index1 is declared as extern int index1; in file2.c. The keyword extern in file2.c declares that the real definition of index1 is not in file2.c but in other file. When the C compiler compile file2.c, it will treat index1 as if it has been defined in file2.c. We can use index1 in file2.c as k = index1; (in file2.c)

33 Continued Any variable can be given the static specifier as in file1.c. The variable is known as static global variable. static int index2; The keyword static has special meaning here. When a program consists of more than one file, the keyword static declared the following variable name, e.g., index2, invisible to other files. For example, index2 is invisible to file2.c but index1 is visible to file1.c and file2.c.

34 9. Storage Classes for Functions Functions can have storage classes: external (the default), or static e.g., double gamma(); extern double delta(); An external function can be accessed by functions in other files. A static function can be used only within the defining file. Examples: double gamma(); /*local & can be external */ static double beta(); /* local to the current file */ extern double delta(); /* defined in another file */

35 Localizing the Use of Variables and Functions C can localize information and functions in a file by defining functions and global variables as static. For example, int index1; /* program scope */ static int index2; /* file scope */ fn(...) { /* program scope: allow this function to be used in other part of the program */... } static int fn2(...) { /* file scope: do not want this function to be seen in other files */... }

36 10. ANSI C Type Qualifiers ANSI C adds 2 more properties to variables: constant volatile (for compiler optimization - Not discussed here) The const Type Qualifier The const keyword in a declaration establishes a variable whose value cannot be modified by assignment or by incrementing or decrementing. const int nochange; /* nochange is constant */ nochange = 12; /* not allowed */

37 Continued Constant variables can only be initialized at its declaration. const int nochange = 12; /* ok */ const int scores[5] = {70,54,34,23,65}; Pointer to constant Vs. Constant pointer Case 1: const float *pf; /* pf points to a constant float value */ The pointed-to value (i.e. *pf ) cannot be changed but the value of pf can change.

38 Continued Case 2: float * const pt; /* pt is a const pointer */ The value of pt cannot be changed but the pointed-to value (i.e. *pt ) can change. Case 3: const float * const ptr; Both ptr and *ptr cannot be changed.

39 Continued The const keyword is used in formal function parameters. For example, int strlen(const char *str); The declaration says that the string pointed to by str cannot be changed, but str can be changed. In other words, within the strlen(), it is possible to do things like str++. How do you interpret the following? char *strcat(char *, const char *);

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas CS 110B - Rule Storage Classes Page 18-1 Attributes are distinctive features of a variable. Data type, int or double for example, is an attribute. Storage class is another attribute. There are four storage

More information

Comp151. Definitions & Declarations

Comp151. Definitions & Declarations Comp151 Definitions & Declarations Example: Definition /* reverse_printcpp */ #include #include using namespace std; int global_var = 23; // global variable definition void reverse_print(const

More information

1. Constants. 2. Variables. 3. Reserved words or key words. 4. Constants. Character set in C

1. Constants. 2. Variables. 3. Reserved words or key words. 4. Constants. Character set in C Character set in C We should use only the following characters in writing a C program. These characters can be combined to create C words. Alphabet: A, B, C, D.. Z, a, b, c, d..z Numeric digits: 0, 1,

More information

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program. Name: Class: Date: Exam #1 - Prep True/False Indicate whether the statement is true or false. 1. Programming is the process of writing a computer program in a language that the computer can respond to

More information

TN203. Porting a Program to Dynamic C. Introduction

TN203. Porting a Program to Dynamic C. Introduction TN203 Porting a Program to Dynamic C Introduction Dynamic C has a number of improvements and differences compared to many other C compiler systems. This application note gives instructions and suggestions

More information

C++ Keywords. If/else Selection Structure. Looping Control Structures. Switch Statements. Example Program

C++ Keywords. If/else Selection Structure. Looping Control Structures. Switch Statements. Example Program C++ Keywords There are many keywords in C++ that are not used in other languages. bool, const_cast, delete, dynamic_cast, const, enum, extern, register, sizeof, typedef, explicit, friend, inline, mutable,

More information

Keil C51 Cross Compiler

Keil C51 Cross Compiler Keil C51 Cross Compiler ANSI C Compiler Generates fast compact code for the 8051 and it s derivatives Advantages of C over Assembler Do not need to know the microcontroller instruction set Register allocation

More information

Object Oriented Software Design II

Object Oriented Software Design II Object Oriented Software Design II Introduction to C++ Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa February 20, 2012 G. Lipari (Scuola Superiore Sant Anna) C++ Intro February

More information

SYSTEMS PROGRAMMING C++ INTRODUCTION

SYSTEMS PROGRAMMING C++ INTRODUCTION Faculty of Computer Science / Institute of Systems Architecture / Operating Systems SYSTEMS PROGRAMMING C++ INTRODUCTION Alexander Warg WHY C++? C++ is the language that allows to express ideas from the

More information

C Programming Dr. Hasan Demirel

C Programming Dr. Hasan Demirel C How to Program, H. M. Deitel and P. J. Deitel, Prentice Hall, 5 th edition (3 rd edition or above is also OK). Introduction to C Programming Dr. Hasan Demirel Programming Languages There are three types

More information

C++ INTERVIEW QUESTIONS

C++ INTERVIEW QUESTIONS C++ INTERVIEW QUESTIONS http://www.tutorialspoint.com/cplusplus/cpp_interview_questions.htm Copyright tutorialspoint.com Dear readers, these C++ Interview Questions have been designed specially to get

More information

The C Programming Language course syllabus associate level

The C Programming Language course syllabus associate level TECHNOLOGIES The C Programming Language course syllabus associate level Course description The course fully covers the basics of programming in the C programming language and demonstrates fundamental programming

More information

Sources: On the Web: Slides will be available on:

Sources: On the Web: Slides will be available on: C programming Introduction The basics of algorithms Structure of a C code, compilation step Constant, variable type, variable scope Expression and operators: assignment, arithmetic operators, comparison,

More information

Java CPD (I) Frans Coenen Department of Computer Science

Java CPD (I) Frans Coenen Department of Computer Science Java CPD (I) Frans Coenen Department of Computer Science Content Session 1, 12:45-14:30 (First Java Programme, Inheritance, Arithmetic) Session 2, 14:45-16:45 (Input and Programme Constructs) Materials

More information

Chapter 5 Names, Bindings, Type Checking, and Scopes

Chapter 5 Names, Bindings, Type Checking, and Scopes Chapter 5 Names, Bindings, Type Checking, and Scopes Chapter 5 Topics Introduction Names Variables The Concept of Binding Type Checking Strong Typing Scope Scope and Lifetime Referencing Environments Named

More information

The programming language C. sws1 1

The programming language C. sws1 1 The programming language C sws1 1 The programming language C invented by Dennis Ritchie in early 1970s who used it to write the first Hello World program C was used to write UNIX Standardised as K&C (Kernighan

More information

ECE 341 Coding Standard

ECE 341 Coding Standard Page1 ECE 341 Coding Standard Professor Richard Wall University of Idaho Moscow, ID 83843-1023 rwall@uidaho.edu August 27, 2013 1. Motivation for Coding Standards The purpose of implementing a coding standard

More information

Lecture 03 Bits, Bytes and Data Types

Lecture 03 Bits, Bytes and Data Types Lecture 03 Bits, Bytes and Data Types In this lecture Computer Languages Assembly Language The compiler Operating system Data and program instructions Bits, Bytes and Data Types ASCII table Data Types

More information

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C Embedded Systems A Review of ANSI C and Considerations for Embedded C Programming Dr. Jeff Jackson Lecture 2-1 Review of ANSI C Topics Basic features of C C fundamentals Basic data types Expressions Selection

More information

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

KITES TECHNOLOGY COURSE MODULE (C, C++, DS) KITES TECHNOLOGY 360 Degree Solution www.kitestechnology.com/academy.php info@kitestechnology.com technologykites@gmail.com Contact: - 8961334776 9433759247 9830639522.NET JAVA WEB DESIGN PHP SQL, PL/SQL

More information

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6) Semantic Analysis Scoping (Readings 7.1,7.4,7.6) Static Dynamic Parameter passing methods (7.5) Building symbol tables (7.6) How to use them to find multiply-declared and undeclared variables Type checking

More information

Glossary of Object Oriented Terms

Glossary of Object Oriented Terms Appendix E Glossary of Object Oriented Terms abstract class: A class primarily intended to define an instance, but can not be instantiated without additional methods. abstract data type: An abstraction

More information

El Dorado Union High School District Educational Services

El Dorado Union High School District Educational Services El Dorado Union High School District Course of Study Information Page Course Title: ACE Computer Programming II (#495) Rationale: A continuum of courses, including advanced classes in technology is needed.

More information

Strings in C++ and Java. Questions:

Strings in C++ and Java. Questions: Strings in C++ and Java Questions: 1 1. What kind of access control is achieved by the access control modifier protected? 2 2. There is a slight difference between how protected works in C++ and how it

More information

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner. Handout 1 CS603 Object-Oriented Programming Fall 15 Page 1 of 11 Handout 1 Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner. Java

More information

PRI-(BASIC2) Preliminary Reference Information Mod date 3. Jun. 2015

PRI-(BASIC2) Preliminary Reference Information Mod date 3. Jun. 2015 PRI-(BASIC2) Table of content Introduction...2 New Comment...2 Long variable...2 Function definition...3 Function declaration...3 Function return value...3 Keyword return inside functions...4 Function

More information

Introduction to Object-Oriented Programming

Introduction to Object-Oriented Programming Introduction to Object-Oriented Programming Programs and Methods Christopher Simpkins chris.simpkins@gatech.edu CS 1331 (Georgia Tech) Programs and Methods 1 / 8 The Anatomy of a Java Program It is customary

More information

Object Oriented Software Design II

Object Oriented Software Design II Object Oriented Software Design II C++ intro Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa February 26, 2012 G. Lipari (Scuola Superiore Sant Anna) C++ Intro February 26,

More information

Moving from CS 61A Scheme to CS 61B Java

Moving from CS 61A Scheme to CS 61B Java Moving from CS 61A Scheme to CS 61B Java Introduction Java is an object-oriented language. This document describes some of the differences between object-oriented programming in Scheme (which we hope you

More information

First Java Programs. V. Paúl Pauca. CSC 111D Fall, 2015. Department of Computer Science Wake Forest University. Introduction to Computer Science

First Java Programs. V. Paúl Pauca. CSC 111D Fall, 2015. Department of Computer Science Wake Forest University. Introduction to Computer Science First Java Programs V. Paúl Pauca Department of Computer Science Wake Forest University CSC 111D Fall, 2015 Hello World revisited / 8/23/15 The f i r s t o b l i g a t o r y Java program @author Paul Pauca

More information

Your first C program. Which one is best?

Your first C program. Which one is best? Your first C program #include void main(void) { printf( Hello, world!\n ); } #include void main(void) { printf( Hello, ); printf( world! ); printf( \n ); } Which one is best? #include

More information

Course Title: Software Development

Course Title: Software Development Course Title: Software Development Unit: Customer Service Content Standard(s) and Depth of 1. Analyze customer software needs and system requirements to design an information technology-based project plan.

More information

C++ Programming Language

C++ Programming Language C++ Programming Language Lecturer: Yuri Nefedov 7th and 8th semesters Lectures: 34 hours (7th semester); 32 hours (8th semester). Seminars: 34 hours (7th semester); 32 hours (8th semester). Course abstract

More information

Informatica e Sistemi in Tempo Reale

Informatica e Sistemi in Tempo Reale Informatica e Sistemi in Tempo Reale Introduction to C programming Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa October 25, 2010 G. Lipari (Scuola Superiore Sant Anna)

More information

Functions. Recursion

Functions. Recursion CandC++ 2. Functions Preprocessor Stephen Clark University of Cambridge (heavily based on last year s notes (Andrew Moore) with thanks to Alastair R. Beresford and Bjarne Stroustrup) Michaelmas Term 2011

More information

1 Problem Description

1 Problem Description CSCE2014 Programming Foundations II Lab Two (week 9-9 to 9-13) By Wing Ning Li 1 Problem Description We have introduced and studied the concept of pointer variable and learned that the name of an array

More information

Illustration 1: Diagram of program function and data flow

Illustration 1: Diagram of program function and data flow The contract called for creation of a random access database of plumbing shops within the near perimeter of FIU Engineering school. The database features a rating number from 1-10 to offer a guideline

More information

Install Java Development Kit (JDK) 1.8 http://www.oracle.com/technetwork/java/javase/downloads/index.html

Install Java Development Kit (JDK) 1.8 http://www.oracle.com/technetwork/java/javase/downloads/index.html CS 259: Data Structures with Java Hello World with the IntelliJ IDE Instructor: Joel Castellanos e-mail: joel.unm.edu Web: http://cs.unm.edu/~joel/ Office: Farris Engineering Center 319 8/19/2015 Install

More information

Basic Common Unix commands: Change to directory d

Basic Common Unix commands: Change to directory d Basic Common Unix commands: cd d Change to directory d mkdir d rmdir d mv f1 [f2...] d mv d1 d2 ls [d] [f...] ls -1 [f...] vi [f] emacs [f] more f cp f1 f2 mv f1 f2 rm f gcc [-o f1] f2 gnuplot Create new

More information

1 Abstract Data Types Information Hiding

1 Abstract Data Types Information Hiding 1 1 Abstract Data Types Information Hiding 1.1 Data Types Data types are an integral part of every programming language. ANSI-C has int, double and char to name just a few. Programmers are rarely content

More information

Dalhousie University CSCI 2132 Software Development Winter 2015 Lab 7, March 11

Dalhousie University CSCI 2132 Software Development Winter 2015 Lab 7, March 11 Dalhousie University CSCI 2132 Software Development Winter 2015 Lab 7, March 11 In this lab, you will first learn how to use pointers to print memory addresses of variables. After that, you will learn

More information

Pemrograman Dasar. Basic Elements Of Java

Pemrograman Dasar. Basic Elements Of Java Pemrograman Dasar Basic Elements Of Java Compiling and Running a Java Application 2 Portable Java Application 3 Java Platform Platform: hardware or software environment in which a program runs. Oracle

More information

Adjusted/Modified by Nicole Tobias. Chapter 2: Basic Elements of C++

Adjusted/Modified by Nicole Tobias. Chapter 2: Basic Elements of C++ Adjusted/Modified by Nicole Tobias Chapter 2: Basic Elements of C++ Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types

More information

Curriculum Map. Discipline: Computer Science Course: C++

Curriculum Map. Discipline: Computer Science Course: C++ Curriculum Map Discipline: Computer Science Course: C++ August/September: How can computer programs make problem solving easier and more efficient? In what order does a computer execute the lines of code

More information

Chapter 8. Arithmetic in C++

Chapter 8. Arithmetic in C++ Christian Jacob Chapter 8 Arithmetic in C++ 8.1 The C++ Vocabulary 8.2 Variables and Types 8.2.1 Data Objects 8.2.2 Variables 8.2.3 Declaration of Variables 8.3 Elementary C++ Data Types 8.3.1 Integers

More information

/* File: blkcopy.c. size_t n

/* File: blkcopy.c. size_t n 13.1. BLOCK INPUT/OUTPUT 505 /* File: blkcopy.c The program uses block I/O to copy a file. */ #include main() { signed char buf[100] const void *ptr = (void *) buf FILE *input, *output size_t

More information

C Interview Questions

C Interview Questions http://techpreparation.com C Interview Questions And Answers 2008 V i s i t T e c h P r e p a r a t i o n. c o m f o r m o r e i n t e r v i e w q u e s t i o n s a n d a n s w e r s C Interview Questions

More information

Object-Oriented Programming, Iouliia Skliarova

Object-Oriented Programming, Iouliia Skliarova Object-Oriented Programming, Iouliia Skliarova Data types define the way you use the storage (memory) in the programs. By specifying a data type, you tell the sompiler how to create a particular piece

More information

Lecture 9. Semantic Analysis Scoping and Symbol Table

Lecture 9. Semantic Analysis Scoping and Symbol Table Lecture 9. Semantic Analysis Scoping and Symbol Table Wei Le 2015.10 Outline Semantic analysis Scoping The Role of Symbol Table Implementing a Symbol Table Semantic Analysis Parser builds abstract syntax

More information

MISRA-C:2012 Standards Model Summary for C / C++

MISRA-C:2012 Standards Model Summary for C / C++ MISRA-C:2012 Standards Model Summary for C / C++ The LDRA tool suite is developed and certified to BS EN ISO 9001:2000. This information is applicable to version 9.4.2 of the LDRA tool suite. It is correct

More information

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement? 1. Distinguish & and && operators. PART-A Questions 2. How does an enumerated statement differ from a typedef statement? 3. What are the various members of a class? 4. Who can access the protected members

More information

Standard printing function in C is printf Prints everything numbers, strings, etc. May be complex to use. Standard C library is called libc

Standard printing function in C is printf Prints everything numbers, strings, etc. May be complex to use. Standard C library is called libc Arrays and Structs and Pointers, Oh My! Programming in C Input and output Using printf Standard input and output Pointers Arrays Structures Combining these things together Arrays and Structs and Pointers,

More information

Pointers and Strings. Objectives

Pointers and Strings. Objectives 5 Pointers and Strings Objectives To be able to use pointers. To be able to use pointers to pass arguments to functions by reference. To understand the close relationships among pointers, arrays and strings.

More information

Understanding Valgrind memory leak reports

Understanding Valgrind memory leak reports Understanding Valgrind memory leak reports Aleksander Morgado aleksander@es.gnu.org Thanks to the development team of Azetti Networks not only for supplying so many example memory leaks, but also for their

More information

6.087 Lecture 1 January 11, 2010

6.087 Lecture 1 January 11, 2010 6.087 Lecture 1 January 11, 2010 Introduction to C Writing C Programs Our First C Program 1 What is C? Dennis Ritchie AT&T Bell Laboratories 1972 16-bit DEC PDP-11 computer (right) Widely used today extends

More information

Data Structures using OOP C++ Lecture 1

Data Structures using OOP C++ Lecture 1 References: 1. E Balagurusamy, Object Oriented Programming with C++, 4 th edition, McGraw-Hill 2008. 2. Robert Lafore, Object-Oriented Programming in C++, 4 th edition, 2002, SAMS publishing. 3. Robert

More information

C Primer. Fall Introduction C vs. Java... 1

C Primer. Fall Introduction C vs. Java... 1 CS 33 Intro Computer Systems Doeppner C Primer Fall 2016 Contents 1 Introduction 1 1.1 C vs. Java.......................................... 1 2 Functions 1 2.1 The main() Function....................................

More information

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Chapter 2: Basic Elements of C++ Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates

More information

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013 Oct 4, 2013, p 1 Name: CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013 1. (max 18) 4. (max 16) 2. (max 12) 5. (max 12) 3. (max 24) 6. (max 18) Total: (max 100)

More information

An Incomplete C++ Primer. University of Wyoming MA 5310

An Incomplete C++ Primer. University of Wyoming MA 5310 An Incomplete C++ Primer University of Wyoming MA 5310 Professor Craig C. Douglas http://www.mgnet.org/~douglas/classes/na-sc/notes/c++primer.pdf C++ is a legacy programming language, as is other languages

More information

Measurement of Software Complexity. Testwell CMTJava. with Testwell CMT++ en_software_complexity_metrics

Measurement of Software Complexity. Testwell CMTJava. with Testwell CMT++  en_software_complexity_metrics Measurement of Software Complexity with Testwell CMT++ Testwell CMTJava www.verifysoft.com en_software_complexity_metrics 20121025 Code Complexity Measurements Code Complexity Measurement Tools Testwell

More information

The Fundamentals of C++

The Fundamentals of C++ The Fundamentals of C++ Basic programming elements and concepts JPC and JWD 2002 McGraw-Hill, Inc. Program Organization Program statement Definition Declaration Action Executable unit Named set of program

More information

Model Viva Questions for Programming in C lab

Model Viva Questions for Programming in C lab Model Viva Questions for Programming in C lab Common to: CSE 2 nd sem IT 2 nd sem Title of the Practical: Assignment to prepare general algorithms and flow chart. Q1: What is a flowchart? A1: A flowchart

More information

Threads Scheduling on Linux Operating Systems

Threads Scheduling on Linux Operating Systems Threads Scheduling on Linux Operating Systems Igli Tafa 1, Stavri Thomollari 2, Julian Fejzaj 3 Polytechnic University of Tirana, Faculty of Information Technology 1,2 University of Tirana, Faculty of

More information

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T) Unit- I Introduction to c Language: C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating

More information

JAVA PRIMITIVE DATA TYPE

JAVA PRIMITIVE DATA TYPE JAVA PRIMITIVE DATA TYPE Description Not everything in Java is an object. There is a special group of data types (also known as primitive types) that will be used quite often in programming. For performance

More information

STRINGS. PREPAED BY T.DEVI IT DEPARTMENT Vidya Jyothi Institute of Technology. Hyderabad

STRINGS. PREPAED BY T.DEVI IT DEPARTMENT Vidya Jyothi Institute of Technology. Hyderabad STRINGS PREPAED BY T.DEVI IT DEPARTMENT Vidya Jyothi Institute of Technology. Hyderabad Definition Array of character are called strings. A string is terminated by null character /0. For example: "Vjit

More information

C Compiler Targeting the Java Virtual Machine

C Compiler Targeting the Java Virtual Machine C Compiler Targeting the Java Virtual Machine Jack Pien Senior Honors Thesis (Advisor: Javed A. Aslam) Dartmouth College Computer Science Technical Report PCS-TR98-334 May 30, 1998 Abstract One of the

More information

Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory.

Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. JAVA TYPES BASIC DATA TYPES GENERAL Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. Based on the data type

More information

Outline Basic concepts of Python language

Outline Basic concepts of Python language Data structures: lists, tuples, sets, dictionaries Basic data types Examples: int: 12, 0, -2 float: 1.02, -2.4e2, 1.5e-3 complex: 3+4j bool: True, False string: "Test string" Conversion between types int(-2.8)

More information

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. Exam Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. 1) The JDK command to compile a class in the file Test.java is A) java Test.java B) java

More information

OpenACC 2.0 and the PGI Accelerator Compilers

OpenACC 2.0 and the PGI Accelerator Compilers OpenACC 2.0 and the PGI Accelerator Compilers Michael Wolfe The Portland Group michael.wolfe@pgroup.com This presentation discusses the additions made to the OpenACC API in Version 2.0. I will also present

More information

C programming, the LCD and ADC. Copyright University of Colorado, 2005 ASEN 4519/5519 Lecture #20

C programming, the LCD and ADC. Copyright University of Colorado, 2005 ASEN 4519/5519 Lecture #20 C programming, the LCD and ADC 1 Homework WEDNESDAY 16-NOV-05 (LECTURE) More C programming (LCD and ADC) FRIDAY 18-NOV-05 (LAB) Work on lab 9 (Variable pulse width signal using the ADC) MONDAY 21-NOV-05

More information

Motto: Write once, run anywhere Sun Microsystem

Motto: Write once, run anywhere Sun Microsystem Literature Pecinovský, Rudolf. 2009. Myslíme objektově v jazyku JAVA. 2nd ed. GRADA Publishing. Isaac Rabinovitch, Jacob Royal, Mark Hoeber, Scott Hommel, Sharon Zakhour, Tom Risser. 2007. JAVA 6 Výukový

More information

1) String initialization can be carried out in the following ways, similar to that of an array :

1) String initialization can be carried out in the following ways, similar to that of an array : Strings in C : Overview : String data type is not supported in C Programming. String is essentially a collection of characters to form particular word. String is useful whenever we accept name of the person,

More information

SANKALCHAND PATEL COLLEGE OF ENGINEERING, VISNAGAR ODD/EVEN ACADEMICSEMESTER (2014-15) ASSIGNMENT / QUESTION BANK (2110003) [F.Y.B.E.

SANKALCHAND PATEL COLLEGE OF ENGINEERING, VISNAGAR ODD/EVEN ACADEMICSEMESTER (2014-15) ASSIGNMENT / QUESTION BANK (2110003) [F.Y.B.E. SANKALCHAND PATEL COLLEGE OF ENGINEERING, VISNAGAR ODD/EVEN ACADEMICSEMESTER (2014-15) ASSIGNMENT / QUESTION BANK Subject: Computer Programming and Utilization (2110003) [F.Y.B.E.: ALL BRANCHES] Unit 1

More information

GDB Tutorial. A Walkthrough with Examples. CMSC 212 - Spring 2009. Last modified March 22, 2009. GDB Tutorial

GDB Tutorial. A Walkthrough with Examples. CMSC 212 - Spring 2009. Last modified March 22, 2009. GDB Tutorial A Walkthrough with Examples CMSC 212 - Spring 2009 Last modified March 22, 2009 What is gdb? GNU Debugger A debugger for several languages, including C and C++ It allows you to inspect what the program

More information

2. Names, Scopes, and Bindings

2. Names, Scopes, and Bindings 2. Names, Scopes, and Bindings Binding, Lifetime, Static Scope, Encapsulation and Modules, Dynamic Scope Copyright 2010 by John S. Mallozzi Names Variables Bindings Binding time Language design issues

More information

Comp Arrays and Pointers. Class Notes. T h e G r o u p o f T h r e e

Comp Arrays and Pointers. Class Notes. T h e G r o u p o f T h r e e Comp 2401 Arrays and Pointers Class Notes 2013 T h e G r o u p o f T h r e e Introduction To Arrays: In C programming, one of the frequently problem is to handle similar types of data. For example: if

More information

Reading Assignment. Main Program in C. K.N. King Chapter 2. K.N. King Chapter 3. K.N. King Chapter 4. K.N. King Chapter 7. Our first C program

Reading Assignment. Main Program in C. K.N. King Chapter 2. K.N. King Chapter 3. K.N. King Chapter 4. K.N. King Chapter 7. Our first C program Reading Assignment Main Program in C In C the main program is a function called main The body of the function is enclosed in left ( ) and right ( ) curly braces. K.N. King Chapter 2 K.N. King Chapter 3

More information

Java (12 Weeks) Introduction to Java Programming Language

Java (12 Weeks) Introduction to Java Programming Language Java (12 Weeks) Topic Lecture No. Introduction to Java Programming Language 1 An Introduction to Java o Java as a Programming Platform, The Java "White Paper" Buzzwords, Java and the Internet, A Short

More information

MPLAB TM C30 Managed PSV Pointers. Beta support included with MPLAB C30 V3.00

MPLAB TM C30 Managed PSV Pointers. Beta support included with MPLAB C30 V3.00 MPLAB TM C30 Managed PSV Pointers Beta support included with MPLAB C30 V3.00 Contents 1 Overview 2 1.1 Why Beta?.............................. 2 1.2 Other Sources of Reference..................... 2 2

More information

IS0020 Program Design and Software Tools Midterm, Feb 24, 2004. Instruction

IS0020 Program Design and Software Tools Midterm, Feb 24, 2004. Instruction IS0020 Program Design and Software Tools Midterm, Feb 24, 2004 Name: Instruction There are two parts in this test. The first part contains 50 questions worth 80 points. The second part constitutes 20 points

More information

Lecture 5: Java Fundamentals III

Lecture 5: Java Fundamentals III Lecture 5: Java Fundamentals III School of Science and Technology The University of New England Trimester 2 2015 Lecture 5: Java Fundamentals III - Operators Reading: Finish reading Chapter 2 of the 2nd

More information

Using C to Access Data Stored in Program Space Memory on the TMS320C24x DSP

Using C to Access Data Stored in Program Space Memory on the TMS320C24x DSP Application Report SPRA380 April 2002 Using C to Access Data Stored in Program Space Memory on the TMS320C24x DSP David M. Alter DSP Applications - Semiconductor Group ABSTRACT Efficient utilization of

More information

5 Arrays and Pointers

5 Arrays and Pointers 5 Arrays and Pointers 5.1 One-dimensional arrays Arrays offer a convenient way to store and access blocks of data. Think of arrays as a sequential list that offers indexed access. For example, a list of

More information

FEEG6002 - Applied Programming 5 - Tutorial Session

FEEG6002 - Applied Programming 5 - Tutorial Session FEEG6002 - Applied Programming 5 - Tutorial Session Sam Sinayoko 2015-10-30 1 / 38 Outline Objectives Two common bugs General comments on style String formatting Questions? Summary 2 / 38 Objectives Revise

More information

The Basics of C Programming. Marshall Brain

The Basics of C Programming. Marshall Brain The Basics of C Programming Marshall Brain Last updated: October 30, 2013 Contents 1 C programming 1 What is C?................................. 2 The simplest C program, I........................ 2 Spacing

More information

A Rudimentary Intro to C programming

A Rudimentary Intro to C programming A Rudimentary Intro to C programming Wayne Goddard School of Computing, Clemson University, 2008 Part 4: Strings and Pointers 18 Strings.................................... D1 19 String Functions..............................

More information

Course Name: ADVANCE COURSE IN SOFTWARE DEVELOPMENT (Specialization:.Net Technologies)

Course Name: ADVANCE COURSE IN SOFTWARE DEVELOPMENT (Specialization:.Net Technologies) Course Name: ADVANCE COURSE IN SOFTWARE DEVELOPMENT (Specialization:.Net Technologies) Duration of Course: 6 Months Fees: Rs. 25,000/- (including Service Tax) Eligibility: B.E./B.Tech., M.Sc.(IT/ computer

More information

CS 241 Data Organization Coding Standards

CS 241 Data Organization Coding Standards CS 241 Data Organization Coding Standards Brooke Chenoweth University of New Mexico Spring 2016 CS-241 Coding Standards All projects and labs must follow the great and hallowed CS-241 coding standards.

More information

Coding Rules. Encoding the type of a function into the name (so-called Hungarian notation) is forbidden - it only confuses the programmer.

Coding Rules. Encoding the type of a function into the name (so-called Hungarian notation) is forbidden - it only confuses the programmer. Coding Rules Section A: Linux kernel style based coding for C programs Coding style for C is based on Linux Kernel coding style. The following excerpts in this section are mostly taken as is from articles

More information

13 Classes & Objects with Constructors/Destructors

13 Classes & Objects with Constructors/Destructors 13 Classes & Objects with Constructors/Destructors 13.1 Introduction In object oriented programming, the emphasis is on data rather than function. Class is a way that binds the data & function together.

More information

Simple C++ Programs. Engineering Problem Solving with C++, Etter/Ingber. Dev-C++ Dev-C++ Windows Friendly Exit. The C++ Programming Language

Simple C++ Programs. Engineering Problem Solving with C++, Etter/Ingber. Dev-C++ Dev-C++ Windows Friendly Exit. The C++ Programming Language Simple C++ Programs Engineering Problem Solving with C++, Etter/Ingber Chapter 2 Simple C++ Programs Program Structure Constants and Variables C++ Operators Standard Input and Output Basic Functions from

More information

INTRODUCTION TO FLOWCHARTING

INTRODUCTION TO FLOWCHARTING CHAPTER 1 INTRODUCTION TO FLOWCHARTING 1.0 Objectives 1.1 Introduction 1.2 Flowcharts 1.3 Types of Flowcharts 1.3.1 Types of flowchart 1.3.2 System flowcharts 1.4 Flowchart Symbols 1.5 Advantages of Flowcharts

More information

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void 1. Explain C tokens Tokens are basic building blocks of a C program. A token is the smallest element of a C program that is meaningful to the compiler. The C compiler recognizes the following kinds of

More information

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON PROBLEM SOLVING WITH SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON Addison Wesley Boston San Francisco New York London

More information

Implementation Aspects of OO-Languages

Implementation Aspects of OO-Languages 1 Implementation Aspects of OO-Languages Allocation of space for data members: The space for data members is laid out the same way it is done for structures in C or other languages. Specifically: The data

More information

Common C Errors. Compiled by: Leela Kamalesh Yadlapalli

Common C Errors. Compiled by: Leela Kamalesh Yadlapalli Common C Errors Compiled by: Leela Kamalesh Yadlapalli This document shows some of the common errors and warnings that you may encounter during this class. Always remember to use the Wall option if you

More information

C PROGRAMMING FOR MATHEMATICAL COMPUTING

C PROGRAMMING FOR MATHEMATICAL COMPUTING UNIVERSITY OF CALICUT SCHOOL OF DISTANCE EDUCATION BSc MATHEMATICS (2011 Admission Onwards) VI Semester Elective Course C PROGRAMMING FOR MATHEMATICAL COMPUTING QUESTION BANK Multiple Choice Questions

More information