This is part of the book COMPUTER PROGRAMMING THE C LANGUAGE. by Adriana ALBU. Conspress Publisher, Bucureşti, România, 2013 ISBN:
|
|
- Rafe Cain
- 5 years ago
- Views:
Transcription
1 This is part of the book COMPUTER PROGRAMMING THE C LANGUAGE by Adriana ALBU Conspress Publisher, Bucureşti, România, 2013 ISBN:
2 Chapter 11 User-defined types Besides the standard data types (char, int, float, double), C programming language provides several mechanisms for creating customized data types (user-defined types). Two of these mechanisms are the subject of this chapter: the structure an entity that gathers several variables that are related from a logical point of view, under a single name; the enumeration a list of named integer constants The structure A structure is a collection of variables referred through a single name. Structures are successfully used when complex data must be organized because they allow gathering different variables (that have a certain relation in a given context) and handling them as a unit not as separated entities [BK03]. The variables that belong to a structure are called members or fields and can have different data types. Each member has a name of its own, used in order to select it within the structure Definition and use The keyword struct is used to define a structure. The instruction that performs this action has the following general form [BH92]: struct structure_name{ type1 var11, var12; type2 var2; typen varn; list_of_variables; where: structure_name is an optional label; the new data type can be used to declare variables through this label; var11, var12, var2,, varn are the members of the structure;
3 Computer Programming The C Language type1, type2,, typen represent the types of the structure s members; the variables that have the same type can be defined in a single instruction, separated by comma (,); the variables that don t have the same type are defined in different instructions, separated by semicolon (;); the members are declared within a block enclosed by curly brackets ({); list_of_variables is also optional and can contain one or more variables; the structure s definition ends with semicolon (;), because it is an instruction, on the whole; though both structure_name and list_of_variables are optional, they cannot be omitted in the same time; at least one of them should be present, otherwise the structure s definition has no sense (since neither variables are declared, nor the structure receives a name to be used later). A first example of a structure, quite simply, is represented by the coordinates of a point in a plane. The structure can be defined as: struct point{ int x; int y; ; or as: struct point{ int x, y; ; The members of a structure can have different types, as shown in the following example that defines several features of a student: struct student{ int id; float mark; 2
4 11 User-defined types ; The definition of a structure doesn t allocate memory space. It only describes a new data type that can be considered a pattern. The variables of a structure type can be declared right within the instruction that defines the type. The following example declares two variables A and B of type point: struct point{ int x; int y; A,B; If the structure received a name, then the variables can be declared later in the program, wherever the structure is visible. The name of the structure, preceded by the keyword struct is used when a variable is declared. The following instruction declares a variable using the type student previously declared: struct student stud1; In C++ programming language a variable can also be declared without the keyword struct, the name of the type being enough. Therefore, the previous variable can be declared as: student stud1;. The members of a structure variable can receive initial values right when the variable is declared. The following instruction declares and initializes two points: A with the coordinates (3,4) and B with the coordinates (10,4): struct point A={3,4, B={10,4; Each member of a structure can also be used as an independent variable, but its name is a little bit longer, because it also includes the name of the structure variable that contains the member. The operator., placed between the name of the structure variable and the name of the member variable is used to access the members of a structure: A.x=3; A.y=4; stud1.mark=9.75; strcpy(stud1.name,"ritchie"); printf("%s %.2f",stud1.name,stud1.mark); 3
5 Computer Programming The C Language The name of a member variable can be identical with the name of a usual variable (non-member) without creating conflicts; the difference between the two variables is made by the context where they are accessed [BK03] (the member variable is always preceded by the name of the structure variable that contains it). Based on the same reasoning, a member of a structure can have the same name with a member of another structure (this is not recommended; it could be used only if the elements have a very close meaning for instance, the radius can be a variable that belongs to a structure that describes a circle and also to one that describes an arc). Program is a complete example where the structure student is used. The structure is global (it is defined outside any function) in order to extend its visibility on the entire program (because this program contains a single function main() the structure could also be defined within this function). A variable s of type student is defined and some of its features are read (id, name, exam mark, laboratory mark). The final mark is calculated and displayed. The program is very simple, being only an exercise of defining and using a structure. Program #include <stdio.h> struct student{ int id; float exam, lab, final; ; struct student s; printf("id: "); scanf("%d",&s.id); printf("the name: "); scanf("%s",s.name); printf("the exam mark: "); scanf("%f", &s.exam); printf("the laboratory mark: "); scanf("%f", &s.lab); 4
6 11 User-defined types s.final=(2*s.exam+s.lab)/3; printf("%s has the final mark %.2f",s.name,s.final); The entire information contained in a structure variable can be copied into another variable of the same type through a single assignment (it is not necessary to perform separated assignments for each member variable). The structure point is used in program in order to illustrate this aspect. Two variables A and B are defined using this structure. The coordinates x and y of the variable A are initialized with the values 3 and 4, respectively. Then, the variable A is assigned to the variable B, which is displayed; B(3,4) is printed on the screen. Program #include <stdio.h> struct point{ int x; int y; ; struct point A, B; A.x=3; A.y=4; B=A; printf("b(%d,%d)",b.x,b.y); The members of a structure can have standard types or user-defined types. Therefore, within a structure, variables that have a structure type can be defined. For instance (program ), it can be imagined a structure called circle that contains a member of type point, representing the coordinates of the circle s center, and a member of type float, representing the radius of the circle: struct circle{ struct point center; 5
7 Computer Programming The C Language float radius; c; Three names are required to access the coordinates of the circle s center: the coordinate s name (x), the center s name (center) and the circle s name (c): c.center.x=10; Program #include <stdio.h> struct point{ int x; int y; ; struct circle{ struct point center; float radius; ; struct circle c; c.center.x=10; c.center.y=20; c.radius=5.37; printf("center: (%d,%d)",c.center.x,c.center.y); printf("\nradius: %.2f",c.radius); A structure can also contain an array (with one or more dimensions): struct vector{ int v[10]; int max; v_str; 6
8 11 User-defined types An element of the array (for instance, that with the index 3) is naturally accessed this way: v_str.v[3]; Program uses this structure that contains a vector of integers and a variable that stores the maximum value of the array. The functions of the program (read(), write() and maximum()) operate on a global variable, v_st, of a structure type. Program #include <stdio.h> const n=10; struct vector{ int v[n]; int max; v_st; void read(){ int i; for(i=0;i<n;i++){ printf("v[%d]=",i); scanf("%d",&v_st.v[i]); void write(void){ int i; for(i=0;i<n;i++) printf("%d ",v_st.v[i]); void maximum(void){ int i; v_st.max=v_st.v[0]; for(i=1;i<n;i++) if(v_st.v[i]>v_st.max) v_st.max=v_st.v[i]; 7
9 Computer Programming The C Language read(); write(); maximum(); printf("\nmax: %d", v_st.max); Structures and functions As any other variables, the structure variables can be passed as arguments to functions (by value or by address) or returned from functions. In these cases the structure must have a global declaration in order to be available to many elements of the program. Program illustrates how a structure variable can be returned from a function. The features of a student are read within the function; they are stored in a structure variable that is returned to main() function. This reading is called for two variables of type student. Program #include <stdio.h> struct student{ int id; float mark; ; struct student read(void){ struct student stud; printf("id: "); scanf("%d",&stud.id); printf("name: "); scanf("%s",stud.name); printf("mark: "); 8
10 11 User-defined types scanf("%f", &stud.mark); return stud; struct student s1,s2; s1=read(); s2=read(); printf("%s: %.2f\n",s1.name,s1.mark); printf("%s: %.2f",s2.name,s2.mark); Program is an example of passing by value some structure variables. Two variables A and B of type point are declared and initialized within main(). Then the variables are passed to a function that calculates and returns the distance between these two points (d x B x A y B y A ). Program #include <stdio.h> #include <math.h> struct point{ int x; int y; ; float distance(struct point a, struct point b){ return sqrt(pow((b.x-a.x),2)+pow((b.y-a.y),2)); struct point A={3,4, B={10,4; printf("ab=%.2f", distance(a,b)); 9
11 Computer Programming The C Language Program passes by address a structure variable. A point A is declared and initialized within main(). This point must be moved to other coordinates. In order to have a real movement, the point must be passed by address to the function that performs this movement. The new coordinates are passed by value. On return from the function the new location of the point is displayed to verify that the movement was made. It is found that the values of the structure s variables were changed. Program #include <stdio.h> struct point{ int x; int y; ; void move(struct point *a, int new_x, int new_y){ (*a).x=new_x; (*a).y=new_y; struct point A={3,4; move(&a,10,14); printf("a(%d,%d)", A.x,A.y); Arrays of structures The structure types can be used to create arrays, each element of the array being in fact a structure (a group of variables). Obviously, the structure should be defined before the array. If the number of occurrences of each vowel (a, e, i, o, u) in a text should be counted, then an array of 5 elements with the following structure can be used: struct count_vowels{ 10
12 11 User-defined types char vowel; int counter; ; Program solves this problem. First, the structure count_vowels is defined and then an array v of 5 elements of the previously defined type. The elements of the array are also initialized at declaration: the members vowel with a vowel and the members counter with 0. A text declared as string is read within main() and then it is passed to the function count(). Within this function the vowels are traversed and each vowel v[i].vowel is searched into the text, comparing each letter of the text (text[j]) with the searched vowel. If they are identical, then the counter connected to that vowel (v[i].counter) is incremented. In order to ignore the difference between uppercases and lowercases, each letter of the text is transformed to lowercase (tolower(text[j])), as it is stored into the array of vowels. At the end of the program, a function that displays the vowels and their counters is called. Program #include <stdio.h> #include <string.h> #include <ctype.h> struct count_vowels{ char vowel; int counter; ; struct count_vowels v[5]={ {'a',0, {'e',0, {'i',0, {'o',0, {'u',0; void count(char *text){ int i, j; for(i=0;i<5;i++) for(j=0;j<strlen(text);j++) if(tolower(text[j])==v[i].vowel) 11
13 Computer Programming The C Language v[i].counter++; void display(void){ int i; for(i=0;i<5;i++) printf("\n%c: %d",v[i].vowel,v[i].counter); char text[50]; printf("enter the text: "); gets(text); count(text); display(); Pointers to structures C programming language allows the declaration of pointers to structures as any other pointers are declared. Considering the structure student, created at the beginning of this chapter, the following instruction declares a pointer: struct student *p; The address of a variable of the same structure type can be assigned to a pointer: p=&stud; where stud is a variable of type student. There are two ways of accessing the members of a structure through a pointer: the classic version, through the dereferenced pointer (*p) and using the operator. : (*p).mark;; this version was illustrated when a structure was passed by address; the special version, using the operator -> : p->mark;; program emphasizes this way of accessing a structure. A variable stud and a pointer p are declared using the structure student; the address of the variable 12
14 11 User-defined types stud is assigned to the pointer. Then, the members of the structure receive values through the pointer. At the end, the variable stud is displayed in order to verify if it was correctly accessed. Program #include <stdio.h> #include <string.h> struct student{ int id; float mark; ; struct student stud, *p; p=&stud; p->id=1; strcpy(p->name,"ritchie"); p->mark=10; printf("%s: %.2f",stud.name, stud.mark); 11.2 The enumeration An enumeration is a set of named integer constants that specifies all the legal values that can be taken by a variable of that type [HS98]. The general form of an instruction that defines an enumeration is [BH92]: enum enumeration_name{ const1_name=val1; const2_name=val2; constn_name=valn; list_of_variables; 13
15 Computer Programming The C Language where: enum is the key word used to define the enumeration; enumeration_name is an optional label associated to the enumeration; const1_name, const2_name,, constn_name are the names of the constants that form the enumeration; val1, val2,, valn are the values associated to the constants; these values are optional; if they are present, they should be integers; if they are omitted, then it is considered that the value associated to a constant is the value associated to the previous constant plus 1 and the value 0 is associated by default to the first constant (if nothing else is specified); list_of_variables is an optional way of declaring variables using the enumeration that has just been defined. As example, an enumeration that contains the days of a week is considered: enum day {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday; and two variables of this type: enum day today, tomorrow; The variable of enumeration type can be used in instructions as: today=sunday; if(today==sunday) tomorrow=monday; else tomorrow=today+1; printf("%d", today); //a number between 0 and 6 It is important to emphasize that each constant of an enumeration is associated (by default or not) to an integer value. Therefore, these constants can be used wherever an integer is necessary. The explicitly association of values to the constants of an enumeration can be made as in the following example: enum month {Jan=31, Feb=28, Mar=31, Apr=30, May=31, Jun=30, Jul=31, Aug=31, Sep=30, Oct=31, Nov=30, Dec=31; 14
16 11 User-defined types An enumeration where only some constants have explicitly associated values is also correct. Each of the rest will have a value with a unit greater than the previous constant. In the following example, the constant fifth will have by default the value 5 because it follows the constant fourth that has explicitly associated the value 4: enum perfect_intervals{unison=1, fourth=4, fifth, octave=8; Program defines, within an enumeration, the three colors of the traffic light. The values 0, 1 and 2 are, by default, associated to these colors. An integer number (0, 1 or 2) is randomly generated in main() and then it is passed to the function traffic_light().when submitting, the number is identified with one of the three constants of the enumeration. The function only generates a command (simulated by a text message) according to the color of the traffic light received as argument. The command is returned and displayed by main() function. Program #include <stdio.h> #include <stdlib.h> #include <time.h> #include <string.h> enum color{red,yellow,green; void traffic_light(enum color tl_color){ char command[30]; switch(tl_color){ case RED: strcpy(command,"stop!"); break; case YELLOW: strcpy(command,"stop if possible!"); break; case GREEN: strcpy(command,"pass!"); break; 15
17 Computer Programming The C Language printf("%s",command); randomize(); traffic_light(random(3)); 11.3 Questions and exercises A. Find the error. 1. struct person{ int age; 2. struct circle{ int x; int y; float radius; acircle; ; 3. struct float salary; anemployee; 4. struct{ float salary; ; 5. struct employee{ float salary; ; struct employee anemployee; salary=2500; 16
18 11 User-defined types 6. struct employee{ float salary; ; struct employee anemployee, *apointer; apointer=&anemployee; apointer.salary=2500; 7. enum cardinal_point{east,west,north,south; enum cardinal_point point=east; printf("%s", point); B. Considering the following programs, specify what will be printed on the screen once these programs are executed. 8. #include <stdio.h> struct product{ float quantity, price; ; struct product prod1={"bread",20,1.5, prod2; prod2=prod1; printf("%.2f",prod2.price); 9. #include <stdio.h> #include <string.h> struct polygon{ int sides; ; void modify(struct polygon pol){ strcpy(pol.name,"heptagon"); pol.sides=7; struct polygon p={"square",4; 17
19 Computer Programming The C Language modify(p); printf("the %s has %d sides", p.name, p.sides); 10. #include <stdio.h> #include <string.h> struct polygon{ int sides; ; void modify(struct polygon *pol){ strcpy((*pol).name,"heptagon"); (*pol).sides=7; struct polygon p={"square",4; modify(&p); printf("the %s has %d sides", p.name, p.sides); 11. #include <stdio.h> enum boolean{false,true; enum boolean f; float nota=7.25; if(nota<5) f=false; else f=true; printf("%d",f); C. Choose the correct answer (one only). 12. A structure contains: 18
20 11 User-defined types a) a single variable; b) several variables of the same type; c) several variables gathered under the same name; d) several variables with the same initial value; e) several constants. 13. A structure is defined by the keyword: a) user; b) defined; c) type; d) struct; e) structure. 14. Which of the following operators can be used to access the members of a structure: a).; b) &; c) *; d) #; e) \. 15. In the context of a structure, a member is: a) the data type of the structure; b) a variable of the structure; c) a pointer to the structure; d) an array of structures; e) all answers are wrong. 16. Which of the following instructions accesses in a correct manner a member m of a structure variable s? a) s.m; b) s&m; c) s(m); d) s[m]; e) none of them. 17. Which of the following instructions accesses in a correct manner a member m through a pointer p to a structure? a) p.m; b) p[m]; c) p&m; d) p*m; e) p->m. 18. The enumeration is: a) a list of integer constants; b) a list of variables of the same type; c) a waiting list; d) a list of variables that can have different types; e) all answers are wrong. 19. What will be printed on the screen once the following program is executed? #include <stdio.h> enum season{spring=1,summer,autumn,winter; enum season now=autumn; printf("%d",now); a) autumn; b) 3; c) 2; d) 4; e) the program contains an error. 19
21 Computer Programming The C Language D. Write programs to solve the following problems. 20. A bouquet of flowers must be created. It may contain several types of flowers and several flowers of each type (for example: 5 roses, 3 fern leaves, and a bamboo). Simulate the content of a bouquet. Suggestion: the bouquet can be an array of structure variables, the structure containing two fields: the name of a flower and the number of flowers with that name used for the bouquet. 21. Calculate, using Heron s formula, the surface of a triangle that is described by three points whose coordinates are read from the keyboard. Suggestion: there will be used a structure of type point (with the two coordinates); then structures of type segment (two points) or triangle (three points) can be used. The lengths of the three segments that form the triangle are calculated. Then the surface can be found with Heron s formula A s s a s b s c, where a, b and c are the lengths of the sides and s is the semi-perimeter. 22. Define a structure that describes a date through three fields (year, month, and day). Write a function that compares two dates received as argument. The function will have three versions: a. it returns 0 if the dates are equals and 1 otherwise; b. it returns 0 if the dates are equals, -1 if the first date is less than the second and 1 if the first date is greater than the second; c. it returns the number of years, months and days between the two dates. 23. Imagine a card game with two players. There will be used an enumeration containing the four kinds of cards (club, diamond, heart, spade), each one having associated a value set by the programmer. Each player receives a card that is randomly generated and the player that has the card with a greater value wins Answers to questions and exercises A. 1. The semicolon separator is required after the closing curly bracket. 2. Variable acircle is misplaced; it should be declared between the closing curly bracket and the semicolon separator. 3. The opening curly bracket after the struct is missing. The name of the structure is also missing, but this is not an error, because the instruction that defines the structure also declares a 20
22 11 User-defined types variable of this type, which is enough. 4. Though both the structure s name and the list of variables are optional, they cannot be omitted in the same time. In order to obtain a correct definition for the structure, it is necessary to have either a label for the structure or one or more variables declared at the end of the instruction that defines the structure. 5. Variable salary has no meaning by itself. It is a member of a structure and should be accessed through variable anemployee: anemployee.salary=2500;. 6. The operator -> should be used to access a member of a structure through a pointer; therefore, the correct instruction is: apointer->salary=2500;. 7. The value associated to a constant of an enumeration is an integer, therefore the constant should be printed with the descriptor "%d", not "%s". B. 8. The entire content of variable prod1 was copied to variable prod2, therefore 1.50 is printed on the screen. 9. The polygon is passed by value, therefore the changes that are made within function modify() are not visible in main(); The square has 4 sides is displayed on the screen. 10. This time the polygon is passed by address to function modify(), therefore the changes are visible in main(); The heptagon has 7 sides is displayed on the screen. 11. The value 1 is printed on the screen because the enumeration variable f indicates to the constant true that is associated to the value 1. C. 12. c). 13. d). 14. a). 15. b). 16. a). 17. e). 18. a). 19. b). 21
Darshan Institute of Engineering & Technology for Diploma Studies Unit 5
1 What is structure? How to declare a Structure? Explain with Example Structure is a collection of logically related data items of different data types grouped together under a single name. Structure is
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 informationtypedef int Array[10]; String name; Array ages;
Morteza Noferesti The C language provides a facility called typedef for creating synonyms for previously defined data type names. For example, the declaration: typedef int Length; Length a, b, len ; Length
More informationData Types and Variables in C language
Data Types and Variables in C language Basic structure of C programming To write a C program, we first create functions and then put them together. A C program may contain one or more sections. They are
More informationCompiling and Running a C Program in Unix
CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 95 ] Compiling and Running a C Program in Unix Simple scenario in which your program is in a single file: Suppose you want to name
More informationOBJECT ORIENTED PROGRAMMING USING C++
OBJECT ORIENTED PROGRAMMING USING C++ Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1 Introduction 10.2 Structure Definitions 10.3 Initializing Structures 10.4 Accessing
More informationFundamental of Programming (C)
Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 10 Structures, Unions, Bit Manipulations and Enumerations Department of Computer Engineering
More informationLaboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information
Laboratory 2: Programming Basics and Variables Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information 3. Comment: a. name your program with extension.c b. use o option to specify
More informationHigh Performance Computing
High Performance Computing MPI and C-Language Seminars 2009 Photo Credit: NOAA (IBM Hardware) High Performance Computing - Seminar Plan Seminar Plan for Weeks 1-5 Week 1 - Introduction, Data Types, Control
More informationProgramming for Engineers Structures, Unions
Programming for Engineers Structures, Unions ICEN 200 Spring 2017 Prof. Dola Saha 1 Structure Ø Collections of related variables under one name. Ø Variables of may be of different data types. Ø struct
More informationALQUDS University Department of Computer Engineering
2013/2014 Programming Fundamentals for Engineers Lab Lab Session # 8 Structures, and Enumeration ALQUDS University Department of Computer Engineering Objective: After completing this lab, the students
More informationBLM2031 Structured Programming. Zeyneb KURT
BLM2031 Structured Programming Zeyneb KURT 1 Contact Contact info office : D-219 e-mail zeynebkurt@gmail.com, zeyneb@ce.yildiz.edu.tr When to contact e-mail first, take an appointment What to expect help
More informationVariation of Pointers
Variation of Pointers A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before
More informationIntroduction. Structures, Unions, Bit Manipulations, and Enumerations. Structure. Structure Definitions
Introduction Structures, Unions, Bit Manipulations, and Enumerations In C, we can create our own data types If programmers do a good job of this, the end user does not even have to know what is in the
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 informationTypes, Operators and Expressions
Types, Operators and Expressions CSE 2031 Fall 2011 9/11/2011 5:24 PM 1 Variable Names (2.1) Combinations of letters, numbers, and underscore character ( _ ) that do not start with a number; are not a
More informationOperator overloading: extra examples
Operator overloading: extra examples CS319: Scientific Computing (with C++) Niall Madden Week 8: some extra examples, to supplement what was covered in class 1 Eg 1: Points in the (x, y)-plane Overloading
More informationWriting an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and
Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and #include The Use of printf() and scanf() The Use of printf()
More informationExample: Structure, Union. Syntax. of Structure: struct book { char title[100]; char author[50] ]; float price; }; void main( )
Computer Programming and Utilization ( CPU) 110003 Structure, Union 1 What is structure? How to declare a Structure? Explain with Example Structure is a collection of logically related data items of different
More informationTokens, Expressions and Control Structures
3 Tokens, Expressions and Control Structures Tokens Keywords Identifiers Data types User-defined types Derived types Symbolic constants Declaration of variables Initialization Reference variables Type
More informationStructured programming
Exercises 2 Version 1.0, 22 September, 2016 Table of Contents 1. Simple C program structure................................................... 1 2. C Functions..................................................................
More informationStructures and Pointers
Structures and Pointers Comp-206 : Introduction to Software Systems Lecture 11 Alexandre Denault Computer Science McGill University Fall 2006 Note on Assignment 1 Please note that handin does not allow
More informationC Functions. CS 2060 Week 4. Prof. Jonathan Ventura
CS 2060 Week 4 1 Modularizing Programs Modularizing programs in C Writing custom functions Header files 2 Function Call Stack The function call stack Stack frames 3 Pass-by-value Pass-by-value and pass-by-reference
More informationType Definition. C Types. Derived. Function Array Pointer Structure Union Enumerated. EE 1910 Winter 2017/18
Enum and Struct Type Definition C Types Derived Function Array Pointer Structure Union Enumerated 2 tj Type Definition Typedef Define a new Type Inherits members and operations from a standard or previously
More informationSubject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100
Code: DC-05 Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100 NOTE: There are 11 Questions in all. Question 1 is compulsory and carries 16 marks. Answer to Q. 1. must be written in the space
More informationPayflow Implementer's Guide FAQs
Payflow Implementer's Guide FAQs FS-PF-FAQ-UG-201702--R016.00 Fairsail 2017. All rights reserved. This document contains information proprietary to Fairsail and may not be reproduced, disclosed, or used
More informationIV Unit Second Part STRUCTURES
STRUCTURES IV Unit Second Part Structure is a very useful derived data type supported in c that allows grouping one or more variables of different data types with a single name. The general syntax of structure
More informationArrays III and Enumerated Types
Lecture 15 Arrays III and Enumerated Types Multidimensional Arrays & enums CptS 121 Summer 2016 Armen Abnousi Multidimensional Arrays So far we have worked with arrays with one dimension. Single dimensional
More informationMain Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University
C Programming Notes Dr. Karne Towson University Reference for C http://www.cplusplus.com/reference/ Main Program #include main() printf( Hello ); Comments: /* comment */ //comment 1 Data Types
More informationComputer programming UNIT IV UNIT IV. GPCET,Dept of CSE, P Kiran Rao
UNIT IV COMMAND LINE ARGUMENTS Computer programming UNIT IV It is possible to pass some values from the command line to C programs when they are executed. These values are called command line arguments
More informationCS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation
CS113: Lecture 3 Topics: Variables Data types Arithmetic and Bitwise Operators Order of Evaluation 1 Variables Names of variables: Composed of letters, digits, and the underscore ( ) character. (NO spaces;
More informationFile Handling in C. EECS 2031 Fall October 27, 2014
File Handling in C EECS 2031 Fall 2014 October 27, 2014 1 Reading from and writing to files in C l stdio.h contains several functions that allow us to read from and write to files l Their names typically
More informationFeatures of C. Portable Procedural / Modular Structured Language Statically typed Middle level language
1 History C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC
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 informationTypes, Operators and Expressions
Types, Operators and Expressions EECS 2031 18 September 2017 1 Variable Names (2.1) l Combinations of letters, numbers, and underscore character ( _ ) that do not start with a number; are not a keyword.
More informationRecap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam
Multimedia Programming 2004 Lecture 2 Erwin M. Bakker Joachim Rijsdam Recap Learning C++ by example No groups: everybody should experience developing and programming in C++! Assignments will determine
More informationC Pointers. CS 2060 Week 6. Prof. Jonathan Ventura
CS 2060 Week 6 1 Pointer Variables 2 Pass-by-reference 3 const pointers 4 Pointer arithmetic 5 sizeof 6 Arrays of pointers 7 Next Time Pointers The pointer is one of C s most powerful and important features.
More informationSolved Exercises from exams: Midterm(1)
Solved Exercises from exams: Midterm(1) 1431-1432 1. What is the output of the following program: #include void fun(int *b) *b = *b+10; main() int a=50; fun(&a); cout
More informationHistory. used in early Mac development notable systems in Pascal Skype TeX embedded systems
Overview The Pascal Programming Language (with material from tutorialspoint.com) Background & History Features Hello, world! General Syntax Variables/Data Types Operators Conditional Statements Functions
More informationtoday cs3157-fall2002-sklar-lect05 1
today homework #1 due on monday sep 23, 6am some miscellaneous topics: logical operators random numbers character handling functions FILE I/O strings arrays pointers cs3157-fall2002-sklar-lect05 1 logical
More informationFundamental of C programming. - Ompal Singh
Fundamental of C programming - Ompal Singh HISTORY OF C LANGUAGE IN 1960 ALGOL BY INTERNATIONAL COMMITTEE. IT WAS TOO GENERAL AND ABSTRUCT. IN 1963 CPL(COMBINED PROGRAMMING LANGUAGE) WAS DEVELOPED AT CAMBRIDGE
More informationCS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010
CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011 Lectures 1-22 Moaaz Siddiq Asad Ali Latest Mcqs MIDTERM EXAMINATION Spring 2010 Question No: 1 ( Marks: 1 ) - Please
More informationOBJECTIVE QUESTIONS: Choose the correct alternative:
OBJECTIVE QUESTIONS: Choose the correct alternative: 1. Function is data type a) Primary b) user defined c) derived d) none 2. The declaration of function is called a) function prototype b) function call
More informationA class is a user-defined type. It is composed of built-in types, other user-defined types and
Chapter 3 User-defined types 3.1 Classes A class is a user-defined type. It is composed of built-in types, other user-defined types and functions. The parts used to define the class are called members.
More informationUnit 8. Structures and Unions. School of Science and Technology INTRODUCTION
INTRODUCTION Structures and Unions Unit 8 In the previous unit 7 we have studied about C functions and their declarations, definitions, initializations. Also we have learned importance of local and global
More informationThe syntax of structure declaration is. struct structure_name { type element 1; type element 2; type element n;
Structure A structure is a user defined data type. We know that arrays can be used to represent a group of data items that belong to the same type, such as int or float. However we cannot use an array
More informationChapter 21: Introduction to C Programming Language
Ref. Page Slide 1/65 Learning Objectives In this chapter you will learn about: Features of C Various constructs and their syntax Data types and operators in C Control and Loop Structures in C Functions
More informationData Types. 9. Types. a collection of values and the definition of one or more operations that can be performed on those values
Data Types 1 data type: a collection of values and the definition of one or more operations that can be performed on those values C++ includes a variety of built-in or base data types: short, int, long,
More informationCMIS 102 Hands-On Lab
CMIS 10 Hands-On Lab Week 8 Overview This hands-on lab allows you to follow and experiment with the critical steps of developing a program including the program description, analysis, test plan, and implementation
More informationStructure, Union. Ashishprajapati29.wordpress.com. 1 What is structure? How to declare a Structure? Explain with Example
Structure, Union 1 What is structure? How to declare a Structure? Explain with Example Structure s a collection of logically related data items of different data types grouped together under a single name.
More informationAssoc. Prof. Dr. Tansu FİLİK
Assoc. Prof. Dr. Tansu FİLİK Computer Programming Previously on Bil 200 Midterm Exam - 1 Midterm Exam - 1 126 students Curve: 49,78 Computer Programming Arrays Arrays List of variables: [ ] Computer Programming
More informationC Language, Token, Keywords, Constant, variable
C Language, Token, Keywords, Constant, variable A language written by Brian Kernighan and Dennis Ritchie. This was to be the language that UNIX was written in to become the first "portable" language. C
More informationFloating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions
Announcements Thursday Extras: CS Commons on Thursdays @ 4:00 pm but none next week No office hours next week Monday or Tuesday Reflections: when to use if/switch statements for/while statements Floating-point
More informationComputer Programming 5th Week loops (do-while, for), Arrays, array operations, C libraries
Computer Programming 5th Week loops (do-while, for), Arrays, array operations, C libraries Hazırlayan Asst. Prof. Dr. Tansu Filik Computer Programming Previously on Bil 200 Low-Level I/O getchar, putchar,
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 informationCS349/SE382 A1 C Programming Tutorial
CS349/SE382 A1 C Programming Tutorial Erin Lester January 2005 Outline Comments Variable Declarations Objects Dynamic Memory Boolean Type structs, enums and unions Other Differences The Event Loop Comments
More informationC Structures, Unions, Bit Manipulations, and Enumerations
C Structures, Unions, Bit Manipulations, and Enumerations Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan Outline 10.2 Structure Definitions 10.4
More informationCS201 Spring2009 Solved Sunday, 09 May 2010 14:57 MIDTERM EXAMINATION Spring 2009 CS201- Introduction to Programming Question No: 1 ( Marks: 1 ) - Please choose one The function of cin is To display message
More informationFundamentals of Programming. Lecture 12: C Structures, Unions, Bit Manipulations and Enumerations
Fundamentals of Programming Lecture 12: C Structures, Unions, Bit Manipulations and Enumerations Instructor: Fatemeh Zamani f_zamani@ce.sharif.edu Sharif University of Technology Computer Engineering Department
More informationCS6202 - PROGRAMMING & DATA STRUCTURES UNIT I Part - A 1. W hat are Keywords? Keywords are certain reserved words that have standard and pre-defined meaning in C. These keywords can be used only for their
More informationEC312 Chapter 4: Arrays and Strings
Objectives: (a) Describe how an array is stored in memory. (b) Define a string, and describe how strings are stored. EC312 Chapter 4: Arrays and Strings (c) Describe the implications of reading or writing
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 informationBASIC ELEMENTS OF A COMPUTER PROGRAM
BASIC ELEMENTS OF A COMPUTER PROGRAM CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING LOGO Contents 1 Identifier 2 3 Rules for naming and declaring data variables Basic data types 4 Arithmetic operators
More informationChapter 3: Arrays and More C Functionality
Chapter 3: Arrays and More C Functionality Objectives: (a) Describe how an array is stored in memory. (b) Define a string, and describe how strings are stored. (c) Describe the implications of reading
More informationC: How to Program. Week /Apr/23
C: How to Program Week 9 2007/Apr/23 1 Review of Chapters 1~5 Chapter 1: Basic Concepts on Computer and Programming Chapter 2: printf and scanf (Relational Operators) keywords Chapter 3: if (if else )
More informationCOP 2000 Introduction to Computer Programming Mid-Term Exam Review
he exam format will be different from the online quizzes. It will be written on the test paper with questions similar to those shown on the following pages. he exam will be closed book, but students can
More informationLecture 3. The syntax for accessing a struct member is
Lecture 3 Structures: Structures are typically used to group several data items together to form a single entity. It is a collection of variables used to group variables into a single record. Thus a structure
More informationINFORMATION TECHNOLOGY SPREADSHEETS. Part 1
INFORMATION TECHNOLOGY SPREADSHEETS Part 1 Page: 1 Created by John Martin Exercise Built-In Lists 1. Start Excel Spreadsheet 2. In cell B1 enter Mon 3. In cell C1 enter Tue 4. Select cell C1 5. At the
More informationC Functions. 5.2 Program Modules in C
1 5 C Functions 5.2 Program Modules in C 2 Functions Modules in C Programs combine user-defined functions with library functions - C standard library has a wide variety of functions Function calls Invoking
More informationContract-based Programming: a Route to Finding Bugs Earlier
Contract-based Programming: a Route to Finding Bugs Earlier JSA Research & Innovation February 2018 Subprogram Contracts Type Contracts Contract-based Programming A software development technique, used
More informationStructures Unions and Enumerated Datatypes 224
STRUCTURES, UNIONS AND ENUMERATED DATA TYPES LEARNING OBJECTIVES After reading this chapter, the readers will be able to understand the purpose of the user defined data types Structures, Unions and Enumerated
More informationRecursion Enums. Basics of Programming 1. Department of Networked Systems and Services G. Horváth, A.B. Nagy, Z. Zsóka, P. Fiala, A.
Recursion The enumerated type Recursion Enums Basics of Programming 1 Department of Networked Systems and Services G. Horváth, A.B. Nagy, Z. Zsóka, P. Fiala, A. Vitéz 31 October, 2018 based on slides by
More informationC PROGRAMMING Lecture 4. 1st semester
C PROGRAMMING Lecture 4 1st semester 2017-2018 Structures Structure: Collection of one or more variables a tool for grouping heterogeneous elements together (different types) Array: a tool for grouping
More informationMaterials covered in this lecture are: A. Completing Ch. 2 Objectives: Example of 6 steps (RCMACT) for solving a problem.
60-140-1 Lecture for Thursday, Sept. 18, 2014. *** Dear 60-140-1 class, I am posting this lecture I would have given tomorrow, Thursday, Sept. 18, 2014 so you can read and continue with learning the course
More informationArithmetic Expressions in C
Arithmetic Expressions in C Arithmetic Expressions consist of numeric literals, arithmetic operators, and numeric variables. They simplify to a single value, when evaluated. Here is an example of an arithmetic
More informationChapter 7. Pointers. Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
1 Chapter 7 Pointers Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2 Chapter 7 - Pointers 7.1 Introduction 7.2 Pointer Variable Definitions and Initialization
More informationLab Session # 1 Introduction to C Language. ALQUDS University Department of Computer Engineering
2013/2014 Programming Fundamentals for Engineers Lab Lab Session # 1 Introduction to C Language ALQUDS University Department of Computer Engineering Objective: Our objective for today s lab session is
More informationUNIT-V. Structures. The general syntax of structure is given below: Struct <tagname> { datatype membername1; datatype membername2; };
UNIT-V Structures Structure is a very useful derived data type supported in c that allows grouping one or more variables of different data types with a single name. The general syntax of structure is given
More informationLESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:
LESSON 1 FUNDAMENTALS OF C The purpose of this lesson is to explain the fundamental elements of the C programming language. C like other languages has all alphabet and rules for putting together words
More informationCS102: Variables and Expressions
CS102: Variables and Expressions The topic of variables is one of the most important in C or any other high-level programming language. We will start with a simple example: int x; printf("the value of
More information( Word to PDF Converter - Unregistered ) FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING UNIT V 2 MARKS
( Word to PDF Converter - Unregistered ) http://www.word-to-pdf-converter.net FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING FUNCTIONS AND POINTERS UNIT V Handling of Character Strings User-defined Functions
More informationCSC 1300 Exam 4 Comprehensive-ish and Structs
CSC 1300 Exam 4 Comprehensive-ish and Structs December 8, 2017 Name: Read through the entire test first BEFORE starting Multiple Choice and T/F sections should be completed on the scantron Test has two
More informationFundamentals of Computer Programming Using C
CHARUTAR VIDYA MANDAL S SEMCOM Vallabh Vidyanagar Faculty Name: Ami D. Trivedi Class: FYBCA Subject: US01CBCA01 (Fundamentals of Computer Programming Using C) *UNIT 3 (Structured Programming, Library Functions
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 information2. Numbers In, Numbers Out
COMP1917: Computing 1 2. Numbers In, Numbers Out Reading: Moffat, Chapter 2. COMP1917 15s2 2. Numbers In, Numbers Out 1 The Art of Programming Think about the problem Write down a proposed solution Break
More informationPreview from Notesale.co.uk Page 6 of 52
Binary System: The information, which it is stored or manipulated by the computer memory it will be done in binary mode. RAM: This is also called as real memory, physical memory or simply memory. In order
More informationME 172. Sourav Saha. Md. Mahamudul Hossain Kazi Fazle Rabbi Saddam Hossain Joy Kamruzzaman Lecturer,Dept. of ME,BUET
ME 172 Introduction to C Programming Language Sourav Saha Md. Mahamudul Hossain Kazi Fazle Rabbi Saddam Hossain Joy Kamruzzaman Lecturer,Dept. of ME,BUET Courtesy: Dr. Noor Al-Quddus Cyrus Ashok Arupratan
More informationProgramming in C. What is C?... What is C?
C Programming in C UVic SEng 265 Developed by Brian Kernighan and Dennis Ritchie of Bell Labs Earlier, in 1969, Ritchie and Thompson developed the Unix operating system We will be focusing on a version
More informationTechnical Questions. Q 1) What are the key features in C programming language?
Technical Questions Q 1) What are the key features in C programming language? Portability Platform independent language. Modularity Possibility to break down large programs into small modules. Flexibility
More informationProgramming in C UVic SEng 265
Programming in C UVic SEng 265 Daniel M. German Department of Computer Science University of Victoria 1 SEng 265 dmgerman@uvic.ca C Developed by Brian Kernighan and Dennis Ritchie of Bell Labs Earlier,
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 informationIntroduction to C programming. By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE
Introduction to C programming By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE Classification of Software Computer Software System Software Application Software Growth of Programming Languages History
More informationProgramming in C. What is C?... What is C?
Programming in C UVic SEng 265 C Developed by Brian Kernighan and Dennis Ritchie of Bell Labs Earlier, in 1969, Ritchie and Thompson developed the Unix operating system We will be focusing on a version
More informationStructures, Operators
Structures Typedef Operators Type conversion Structures, Operators Basics of Programming 1 G. Horváth, A.B. Nagy, Z. Zsóka, P. Fiala, A. Vitéz 10 October, 2018 c based on slides by Zsóka, Fiala, Vitéz
More informationAdvanced Pointer Topics
Advanced Pointer Topics Pointers to Pointers A pointer variable is a variable that takes some memory address as its value. Therefore, you can have another pointer pointing to it. int x; int * px; int **
More informationExcel Functions & Tables
Excel Functions & Tables SPRING 2016 Spring 2016 CS130 - EXCEL FUNCTIONS & TABLES 1 Review of Functions Quick Mathematics Review As it turns out, some of the most important mathematics for this course
More informationDECISION CONTROL AND LOOPING STATEMENTS
DECISION CONTROL AND LOOPING STATEMENTS DECISION CONTROL STATEMENTS Decision control statements are used to alter the flow of a sequence of instructions. These statements help to jump from one part of
More informationUNIVERSITY OF LIMERICK OLLSCOIL LUIMNIGH COLLEGE OF INFORMATICS & ELECTRONICS DEPARTMENT OF ELECTRONIC & COMPUTER ENGINEERING
UNIVERSITY OF LIMERICK OLLSCOIL LUIMNIGH COLLEGE OF INFORMATICS & ELECTRONICS DEPARTMENT OF ELECTRONIC & COMPUTER ENGINEERING MODULE CODE: MODULE TITLE: ET4131 Introduction to Computer Programming SEMESTER:
More informationChapter 10 C Structures, Unions, Bit Manipulations
Chapter 10 C Structures, Unions, Bit Manipulations Skipped! Skipped! and Enumerations Skipped! Page 416 In programming languages, Arrays (Chapter 6) allows programmers to group elements of the same type
More informationC-Programming. Claude Fuhrer 5 août 2017
C-Programming Claude Fuhrer (claude.fuhrer@bfh.ch) 5 août 2017 1 Pseudocode The most important step in writing a program, is to understand the problem which should be solved and then establish a strategy
More information