Programming Fundamentals (CS-302 )

Similar documents
Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

ME 172. Lecture 2. Data Types and Modifier 3/7/2011. variables scanf() printf() Basic data types are. Modifiers. char int float double

The syntax of structure declaration is. struct structure_name { type element 1; type element 2; type element n;

BSM540 Basics of C Language

VARIABLES AND TYPES CITS1001

12 CREATING NEW TYPES

Programming in C++ 6. Floating point data types

Principles of C and Memory Management

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

Lab # 02. Basic Elements of C++ _ Part1

M.EC201 Programming language

Procedural Programming & Fundamentals of Programming

Lecture 3 Tao Wang 1

Data Representation and Storage. Some definitions (in C)

CSc Introduction to Computing

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

C programming Lecture 2. Marina Krstic Marinkovic School of Mathematics Trinity College Dublin

Storage class and Scope:

Lecture 5 Tao Wang 1

Week 3 Lecture 2. Types Constants and Variables

VARIABLES AND CONSTANTS

CS242 COMPUTER PROGRAMMING

Programming in C++ 5. Integral data types

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

Downloaded S. from Kiran, PGT (CS) KV, Malleswaram STRUCTURES. Downloaded from

CS2351 Data Structures. Lecture 7: A Brief Review of Pointers in C

M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

Data Structures and Programming with C++

Example: Structure, Union. Syntax. of Structure: struct book { char title[100]; char author[50] ]; float price; }; void main( )

Introduction to C Language (M3-R )

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

CMSC 104 -Lecture 5 John Y. Park, adapted by C Grasso

ESc101 : Fundamental of Computing

CS149: Elements of Computer Science. Fundamental C++ objects

Variables in C. Variables in C. What Are Variables in C? CMSC 104, Fall 2012 John Y. Park

IT 1033: Fundamentals of Programming Data types & variables

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

a data type is Types

Basic Types, Variables, Literals, Constants

C# MOCK TEST C# MOCK TEST II

Sir Syed University of Engineering and Technology. Computer Programming & Problem Solving ( CPPS )

Data Representation and Storage

Computers Programming Course 5. Iulian Năstac

C-LANGUAGE CURRICULAM

Week 8 Lecture 3. Finishing up C

Bil 104 Intiroduction To Scientific And Engineering Computing. Lecture 7

BLM2031 Structured Programming. Zeyneb KURT

Introduction to C/C++ Lecture 5 - String & its Manipulation

C: How to Program. Week /Mar/05

IV Unit Second Part STRUCTURES

Introduction to Programming Using Java (98-388)

Short Notes of CS201

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Variables. Data Types.

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

CS201 - Introduction to Programming Glossary By

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

Unit IV & V Previous Papers 1 mark Answers

Create a Program in C (Last Class)

Chapter 2 - Introduction to C Programming

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Integer Data Types. Data Type. Data Types. int, short int, long int

Understand Computer Storage and Data Types

Chapter-8 DATA TYPES. Introduction. Variable:

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

Student Name: (in Capital Letters) CSE Introduction to Programming for Engineers and Scientists. Final Exam

Assist. Prof. Dr. Caner ÖZCAN

Data Types. Data Types. Integer Types. Signed Integers

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

A Fast Review of C Essentials Part I

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

[0569] p 0318 garbage

An Introduction to Processing

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

Declaration and Memory

Presented By : Gaurav Juneja

C Introduction. Comparison w/ Java, Memory Model, and Pointers

Lab Session # 1 Introduction to C Language. ALQUDS University Department of Computer Engineering

CS 231 Data Structures and Algorithms, Fall 2016

CS240: Programming in C

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

UNIT-V. Structures. The general syntax of structure is given below: Struct <tagname> { datatype membername1; datatype membername2; };

Storage class in C. Automatic variables External variables Static variables Register variables Scopes and longevity of above types of variables.

Lectures 5-6: Introduction to C

Object-Oriented Programming

b. array s first element address c. base address of an array d. all elements of an array e. both b and c 9. An array elements are always stored in a.

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

CPE 101. Overview. Programming vs. Cooking. Key Definitions/Concepts B-1

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

Why Pointers. Pointers. Pointer Declaration. Two Pointer Operators. What Are Pointers? Memory address POINTERVariable Contents ...

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Data Types and Variables in C language

On a 64-bit CPU. Size/Range vary by CPU model and Word size.

Lecture 02 C FUNDAMENTALS

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Transcription:

Programming Fundamentals (CS-302 ) (Structures) Dr. Ihsan Ullah Lecturer Department of Computer Science & IT University of Balochistan 1

Introduction A variable can store one element at a time An array can store a collection of elements with the same type How to handle a collection of mixed types of data? Such as Grades and marks C language provides structures for a collection of elements with different types Structure can be considered as a userdefined data type 2

Structure declaration A structure is declared through the keyword struct The general form is struct <structure name> { } structure element 1 ; structure element 2 ;... Declaration of a structure does not reserve any memory Memory is reserved when a structure variable is defined 3

Example A structure storing marks and grade of a student: struct result { int rno; int marks; char grade; } ; To define variables of type structure result: struct result s1, s2; To access elements of structure variable s1: s1.rno, s1.marks, s1.grade See example struct1.c, strucstr.c Modify struct1.c to take input from the user 4

Data types Integers short, int, long On a 32 bit machine occupy 2,4 and 4 bytes respectively On a 16-bit machine occupy 2,2 and 4 bytes respectively Adding unsigned with all these types increases its storage limit allowing only positive integers The integer range in a two bytes space is -32768 to +32767. Using unsigned integer allows to store values in range from 0 to 65535 Characters To store character type char is used occupying one byte Fractions Float, double & long double occupying 4, 8 and 10 bytes respectively 5

Variable storage classes A variable defined in C language refers to some physical location within the computer Such locations are memory and CPU registers A storage class determines The type of location (memory/register) Default initial value The scope of a variable (visibility in functions) Life of variable 6

Variable storage classes Four storage classes in C Automatic Register Static External 7

Automatic storage class Storage location is memory Its default initial value is unpredictable (garbage) Its scope is within the block in which it is defined Life of automatic variable remains until the control remains within the same block Syntax auto int a; //auto is optional Keyword auto is rarely used since it is the default type 8

Register storage class Storage location is CPU registers Default initial value is garbage Scope is local to the block in which variable is defined life is until the control remains within the block Faster access than the variable stored in memory Syntax: register int a; Processed as auto, if free register is not available 9

Static storage class Static variable is stored in memory Its default initial value is 0 Its scope is local to the block in which it is defined Life is until the control remains in the program (holds its value during function calls) Syntax: static int a; See example staticvar.c 10

External storage class (global) Called global variables Storage location is in memory Its scope is global to the program Life remains throughout the program Defined above all functions and remains visible to all of them Default initial value is zero See example globalvar.c 11

Name input C language does not have a string type Strings are stored in character arrays To input a string %s is used as a format specifier & sign is not required in the scanf() function since the name of array carries its first address See example resultstring.c, resultstringinput.c To copy a string into another strcpy() function is used To check the equality of two strings strcmp() is used 12

Array of structures Just like arrays of int and char, array of structures can be created too An array of structure is defined as struct stucturename varname[array length]; To access elements of an array of structures, varname[index number].field For a complete example see arrstdresult.c 13