CSC 1107: Structured Programming

Similar documents
Types, Operators and Expressions

Types, Operators and Expressions

File Handling in C. EECS 2031 Fall October 27, 2014

Work relative to other classes

Engineering Computing I

CS1100 Introduction to Programming

CSC 1214: Object-Oriented Programming

Basic Types and Formatted I/O

Room 3P16 Telephone: extension ~irjohnson/uqc146s1.html

XC Specification. 1 Lexical Conventions. 1.1 Tokens. The specification given in this document describes version 1.0 of XC.

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

Lecture 02 C FUNDAMENTALS

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

Programming in C and Data Structures [15PCD13/23] 1. PROGRAMMING IN C AND DATA STRUCTURES [As per Choice Based Credit System (CBCS) scheme]

Programming in C++ 4. The lexical basis of C++

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

C++ is case sensitive language, meaning that the variable first_value, First_Value or FIRST_VALUE will be treated as different.

THE FUNDAMENTAL DATA TYPES

UNIT- 3 Introduction to C++

Computer System and programming in C

Guide for The C Programming Language Chapter 1. Q1. Explain the structure of a C program Answer: Structure of the C program is shown below:

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

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

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

BSM540 Basics of C Language

A Fast Review of C Essentials Part I

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

C Programming Language (Chapter 2 of K&R) Variables and Constants

Java Notes. 10th ICSE. Saravanan Ganesh

Fundamental of C programming. - Ompal Singh

DECLARATIONS. Character Set, Keywords, Identifiers, Constants, Variables. Designed by Parul Khurana, LIECA.

CS313D: ADVANCED PROGRAMMING LANGUAGE

Fundamentals of Programming

Fundamental of Programming (C)

Have the same meaning as variables in algebra Single alphabetic character Each variable needs an identifier that distinguishes it from the others a =

Mechatronics and Microcontrollers. Szilárd Aradi PhD Refresh of C

Fundamentals of Programming

Fundamental of Programming (C)

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

BASIC ELEMENTS OF A COMPUTER PROGRAM

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Variables and literals

Lecture 3. More About C

Goals of C "" The Goals of C (cont.) "" Goals of this Lecture"" The Design of C: A Rational Reconstruction"

Operators and Expressions:

Basic Types, Variables, Literals, Constants

Differentiate Between Keywords and Identifiers

Computers Programming Course 5. Iulian Năstac

Tools : The Java Compiler. The Java Interpreter. The Java Debugger

Data Types and Variables in C language

Scientific Programming in C V. Strings

Programming. Elementary Concepts

CSC Web Programming. Introduction to JavaScript

Chapter 2: Using Data

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Unit 4. Input/Output Functions

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

Basics of Java Programming

Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. C Data Types

The component base of C language. Nguyễn Dũng Faculty of IT Hue College of Science

Reserved Words and Identifiers

Data Representa5on. CSC 2400: Computer Systems. What kinds of data do we need to represent?

Data Representa5on. CSC 2400: Computer Systems. What kinds of data do we need to represent?

Programming in C++ 5. Integral data types

Chapter 2 - Introduction to C Programming

The Design of C: A Rational Reconstruction (cont.)

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used.

Java Basic Datatypees

C++ Programming Basics

JAVA Programming Fundamentals

C: How to Program. Week /Mar/05

CSCI 2132: Software Development. Norbert Zeh. Faculty of Computer Science Dalhousie University. Introduction to C. Winter 2019

COMP327 Mobile Computing Session: Tutorial 2 - Operators and Control Flow

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

LECTURE 3 C++ Basics Part 2

6.096 Introduction to C++ January (IAP) 2009

Chapter 2: Data and Expressions

Declaration and Memory

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

ME240 Computation for Mechanical Engineering. Lecture 4. C++ Data Types

BLM2031 Structured Programming. Zeyneb KURT

Introduction to C Language

ANSI C Programming Simple Programs

EL2310 Scientific Programming

Zheng-Liang Lu Java Programming 45 / 79

C Fundamentals & Formatted Input/Output. adopted from KNK C Programming : A Modern Approach

Chapter 2: Using Data

Visual C# Instructor s Manual Table of Contents

CS61B Lecture #14: Integers. Last modified: Wed Sep 27 15:44: CS61B: Lecture #14 1

INTRODUCTION 1 AND REVIEW

Number Systems, Scalar Types, and Input and Output

Getting started with Java

Structure of this course. C and C++ Past Exam Questions. Text books

C - Basic Introduction

A First Program - Greeting.cpp

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Operators in C. Staff Incharge: S.Sasirekha

Transcription:

CSC 1107: Structured Programming J. Kizito Makerere University e-mail: www: materials: e-learning environment: office: alt. office: jkizito@cis.mak.ac.ug http://serval.ug/~jona http://serval.ug/~jona/materials/csc1107 http://muele.mak.ac.ug block A, level 3, department of computer science institute of open, distance, and elearning Introduction Kizito (Makerere University) CSC 1107 August, 2018 1 / 12

Overview 1 Introduction 2 Types 3 Variables 4 Operators 5 Conversions and Casting 6 Arrays Kizito (Makerere University) CSC 1107 August, 2018 2 / 12

Introduction Introduction The C Programming Language A computer program (or just a program) is a sequence of instructions, writtem to perform a specific task with a computer A programming language is a formal constructed language designed to communicate instructions to a machine, particularly a computer Programming languages can be used to create programs The only way to learn a new programming language is by writing programs in it First C Program #include <stdio.h> main() { printf("hello, world\n"); } Output: Hello, world Kizito (Makerere University) CSC 1107 August, 2018 3 / 12

Types Types Data Types and Sizes Basic Data Types Qualifiers char: A single byte (8 bits), capable of holding one character in the local character set int (either 16 or 32 bits no longer than long): An integer, typically reflecting the natural size of integers on the host machine float: Single-precision floating point double: Double-precision floating point short: Often 16 bits long no longer than int long: At least 32 bits when applied to int unsigned: Always positive or zero, and obey the laws of arithmetic modulo 2 n, where n is the number of bits in the type signed: Default Kizito (Makerere University) CSC 1107 August, 2018 4 / 12

Types Types Constants Variables and constants are the basic data objects manipulated in a program Type Example Notes int 1234 037 Leading zero means integer in octal (31 10 ) 0x1F Leading 0x or 0X means integer in hexadecimal long int 123456789L Written with a terminal l (ell) or L unsigned Written with a terminal u or U unsigned long Written with the suffix ul or UL 0xFUL 15 in decimal double 123.4 Contain a decimal point or an exponent or 1e-2 both float 123.4F Written with the suffix f or F long double 123.4L Written with the suffix l or L char A One character within single quotes (65 10 ) Kizito (Makerere University) CSC 1107 August, 2018 5 / 12

Types Constants Escape Sequences Recall the \n in printf("hello, world\n"); of our first program... \a alert (bell) character \\ backslash \b backspace \? question mark \f formfeed \ single quote \n newline \ double quote \r carriage return \ooo octal number \t horizontal tab \xhh hexadecimal number \v vertical tab Kizito (Makerere University) CSC 1107 August, 2018 6 / 12

Variables Variables Variable Declarations Variable names are made up of letters and digits The first character must be a letter The underscore counts as a letter Library routines often begin variable names with underscore All variables must be declared before use Examples: 1 int lower, upper = MAXLINE+1, step; 2 char c, line[1000]; 3 char esc = \\ ; 4 float eps = 1.0e-5; 5 const double e = 2.71828182845905; 6 const char msg[] = "warning: "; Kizito (Makerere University) CSC 1107 August, 2018 7 / 12

Operators Operators An operator takes one or more operands and produces a result An operator is just a function, but with a different syntax Most of C s operators can be divided into: Arithmetic: +,, *, /, % Relational and Logical: >, >=, <, <=, &&,,!, ==,!= Increment and Decrement: ++, Bitwise: &,, ˆ, <<, >>, Assignment: =, op= Ternary:?: Example (get n bits of x from position p). Does it work for n > p + 1? int b = (x >> (p+1-n)) & ( 0 << n); Kizito (Makerere University) CSC 1107 August, 2018 8 / 12

Operators Operators Precedence and Associativity Highest Lowest Operators Associativity () [] >. left to right! ++ + (type) sizeof right to left / % left to right + left to right << >> left to right < <= > >= left to right ==! = left to right & left to right ˆ left to right left to right && left to right left to right? : right to left = + = = = / = % = & = ˆ= = <<= >>= right to left, left to right Kizito (Makerere University) CSC 1107 August, 2018 9 / 12

Conversions and Casting Example Type Conversions Type Conversion and Casting Operands of different types in the same expression are converted to a common type Narrowing conversions may draw a warning, but they are not illegal Recall: a char is a small integer Conversions take place across assignments Explicit type conversions can be forced with the operator called a cast (type-name) expression int i, n = 0, m; char s[]; n = 10 * n + (s[i] - 0 ); m = (unsigned int)(n*1234567) % 32; Kizito (Makerere University) CSC 1107 August, 2018 10 / 12

Arrays Arrays An array is a group of like-type variables that are referred to by a common name Arrays can be of any type Arrays may have one or more dimensions The simplest kind is a linear array also called one-dimensional array A specific element in an array is accessed by its key or index Kizito (Makerere University) CSC 1107 August, 2018 11 / 12

Arrays Arrays Declaring and Indexing Arrays General form: type var-name[size]; Examples 1 int month days[12]; 2 int mdays[] = {31,28,31,30,31,30,31,31,30,31,30,31}; 3 int x; char daytab[2][13] = { {0,31,28,31,30,31,30,31,31,30,31,30,31} {0,31,29,31,30,31,30,31,31,30,31,30,31} }; x = daytab[0][2]; // x equals 28 Indexing the array 1 month days[1] = 28; 2 printf("%d", month days[3]); 3 printf("april has %d days", mdays[3]); Strings as arrays of characters (terminated by the character \0 ) Kizito (Makerere University) CSC 1107 August, 2018 12 / 12