Enumerated Types Sometimes we may wish to have a variable whose values are not numeric, for example, the variable size might have values coming from the.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
Programming III SPRING 2015 School of Computer and Information Sciences Francisco R. Ortega, Ph.D. McKnight Fellow and GAANN Fellow LECTURE #3 Control.
Kernighan/Ritchie: Kelley/Pohl:
POINTER Prepared by MMD, Edited by MSY1.  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference.
Structure of a C program
C Programming Basics Lecture 5 Engineering H192 Winter 2005 Lecture 05
C Language Brief history In 1972, Dennis Ritchie designed C and it was used on PDP-11 mini- computers In 1974, Unix was rewritten in C C++ and C Advantages.
Assignment Operators =, +=, *= A += B means (A+B) --->A or A = (A+B) Similarly true for -=, *=, /=, and %=. The basic rule is from right to left. Never.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
CS 201 Functions Debzani Deb.
CS1061 C Programming Lecture 4: Indentifiers and Integers A.O’Riordan, 2004.
Introduction Computer program: an ordered sequence of instructions whose objective is to accomplish a task. Programming: process of planning and creating.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
CPS120: Introduction to Computer Science Lecture 8.
C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
chap13 Chapter 13 Programming in the Large.
1 Please switch off your mobile phones. 2 Data Representation Instructor: Mainak Chaudhuri
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
C Tokens Identifiers Keywords Constants Operators Special symbols.
CPS120: Introduction to Computer Science Variables and Constants Lecture 8 - B.
CPS120: Introduction to Computer Science
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics Lecture 5.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
C Programming Day 3. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Storage Class Specifiers Every variable in a C.
Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)
#include using namespace std; // Declare a function. void check(int, double, double); int main() { check(1, 2.3, 4.56); check(7, 8.9, 10.11); } void check(int.
Documentation and Style. Documentation and Comments  Programs should be self-documenting.  Use meaningful variable names.  Use indentation and white.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
By Mr. Muhammad Pervez Akhtar
CPS120: Introduction to Computer Science Variables and Constants.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
2. C FUNDAMENTALS. Example: Printing a Message /* Illustrates comments, strings, and the printf function */ #include int main(void) { printf("To C, or.
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
The Machine Model Memory
UNIT 5 C Pointers.
Data types Data types Basic types
C Programming Tutorial – Part I
Functions Separate Compilation
C Basics.
Instructor: Ioannis A. Vetsikas
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
פרטים נוספים בסילבוס של הקורס
6 Chapter Functions.
Introduction to C Programming
Pointers C#, pointers can only be declared to hold the memory addresses of value types int i = 5; int *p; p = &i; *p = 10; // changes the value of i to.
Govt. Polytechnic,Dhangar
Documentation and Style
Module 2 Variables, Data Types and Arithmetic
Variables in C Topics Naming Variables Declaring Variables
Variables in C Topics Naming Variables Declaring Variables
The Three Attributes of an Identifier
Presentation transcript:

Enumerated Types Sometimes we may wish to have a variable whose values are not numeric, for example, the variable size might have values coming from the set small, medium, large. We could create strings for this purpose, but this would require doing string comparisons to determine which value size currently has. An easy solution is to use an integer for each, so small might be represented by 0, medium by 1, and large by 2.

Enumerated Types The problem with this is that the programmer must remember which integer is associated with each value. A solution to this problem is to use enumerated types. enum name{ value1, value2,... }; Enumerated types are special types that allow non-numeric values to have integers associated with them: the first has a value of 0, the second a value of 1, and so on. Enumerated types make it easier to read the source code since the non- numeric names can be used.

Enumerated Types Here is an example of a program without enumerated types: #include int main(void) { /* apple = 0; lemon = 1; orange = 2; pear = 3 */ int tree; tree = 2; /* 2 means orange */ switch(tree) { case 0: printf("You bought an apple tree.\n"); break; case 1: printf("You bought a lemon tree.\n"); break; case 2: printf("You bought an orange tree.\n"); break; case 3: printf("You bought a pear tree.\n"); break; }

Enumerated Types Here is the program with enumerated types: #include enum fruit {apple, lemon, orange, pear}; int main(void) { enum fruit tree; tree = orange; switch(tree) { case apple: printf("You bought an apple tree.\n"); break; case lemon: printf("You bought a lemon tree.\n"); break; case orange: printf("You bought an orange tree.\n"); break; case pear: printf("You bought a pear tree.\n"); break; }

Typedef We can use the typedef keyword to give an alternate name to a type. Example: #include typedef double DBL; /* typedefs don’t have to be capitalized, but that is the convention. */ int main(void) { DBL num = 2.3; /* creates a variable of type double */ printf("the size of DBL is %d\n", sizeof(DBL) ); printf("num is %f\n", num); } produces the size of DBL is 8 num is

Typedef typedef can be used with structures; in fact, FILE is a structure for which a typedef was used. Example: #include typedef struct { char name[20]; int age; } PERSON; int main(void) { PERSON John = {"John Smith", 25}; PERSON *ptr = &John; printf("%s is %d years old.\n", John.name, ptr->age); }

static Variables The variables that we have used so far are called automatic variables. Variables created inside functions only exist as long as the function is executing. The values of these variables are lost after the function completes its execution. The keyword for declaring an automatic variable is auto: auto int x; Local variables are automatic variables by default.

Static Variables Static variables: Remain in memory until the program terminates. If a static variable is created in a function it will still exist with its last value the next time the function is called. Static variables are automatically initialized to zero. The keyword for declaring a static variable is static. See example-static.c on the course website.

Defining Constants Sometimes we may have a value that is constant and used in several places in a program. For this, we may wish to use a preprocessor directive to define the constant. We use the following syntax for this: #define name value Then in our program, we use name in each place where we would have used value.

Defining Constants #include #define PI double calcArea(double); double calcCircumference(double); int main(void) { double r = 1.5; printf("The area of a circle with a radius of %5.2f is %f\n", r, calcArea(r) ); printf("Its circumference is %f\n", calcCircumference(r) ); } double calcArea(double r) { return PI*r*r; } double calcCircumference(double r) { return 2*PI*r; }

Global Variables All of the variables that we have declared in our programs have been local variables. That is, they were known only within the function in which they were declared. Variables declared outside of any function are called global variables. Global variables are static by default.

Global Variables You may look at this and see global variables as the solution to having to pass variables to functions. Global variables are generally considered bad practice. They can make code more difficult to debug since it is unclear which function changed a variable’s value. This becomes more the case as the number of functions in a program grows, especially if functions are in multiple source files to make them reusable.

Multiple Source Files The source code for large programs is typically split over many different files. This has the advantage of making the re-use of functions easy. Function declarations for functions defined outside a file are placed in a separate header file. The header file needs to be included in each file where one of the declared functions is called.

Multiple Source Files – GCC If we have our source code split among the files mainfile.c, myfunctions.c, and myfunctions.h, we can compile it using gcc mainfile.c myfunctions.c -o mainfile to produce mainfile.

Bits and Bytes Recall that the smallest storage unit is a bit, which we typically think of having a value of 0 or 1. C allows us to make changes at the bit level. However, we can’t do so directly, but there are indirect methods to change bits. Some uses for bit operations are systems programming, encryption, compression, and image encoding.

Binary Here are the first 16 numbers in decimal, hexadecimal and binary (assumes an 8-bit representation): Decimal Binary Hexadecimal A B C D E F

Bit Operators Symbol Name ~bitwise-negation & bitwise-and | bitwise-or ^ bitwise-xor >> shift right << shift left Note: These can only be used with the integer types.

Bitwise-negation The bitwise-negation reverses the bit pattern for a number. Example 1: If x has a bit pattern of 0001, then x has a bit pattern of Example 2: We use it like this: y = ˜x; or x = ˜x;

Bitwise-and The bitwise-and compares the bit patterns of two numbers to produce a new bit pattern. When the two compared bits are a 1, the resulting bit will be a 1. Otherwise, it will be a 0. Example 1: Example 2: We use it like this: z = x & y; or x &= y;

Bitwise-or The bitwise-or compares the bit patterns of two numbers to produce a new bit pattern. When either of the compared bits is a 1, the resulting bit will be a 1. Otherwise, it will be a 0. Example 1: Example 2: We use it like this: z = x | y; or x |= y;

Bitwise-xor The bitwise-xor (where xor is exclusive or) compares the bit patterns of two numbers to produce a new bit pattern. When the compared bits differ, the resulting bit will be a 1. Otherwise, it will be a 0. Example 1: Example 2: We use it like this: z = x ^ y; or x ^= y;

Shift Left and Shift Right The shift operators allow us to shift the bit patterns to the left or right. Example 1: If x has a bit pattern of 0001, then x << 1 has a bit pattern of Example 2: We use them like this: x = y >> 2; or y >>= 2;

More Examples See example-bitwise.c on the course website.