PROGRAMMAZIONE I A.A. 2018/2019

Similar documents
Basic Types, Variables, Literals, Constants

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

C: How to Program. Week /Mar/05

In this session we will cover the following sub-topics: 1.Identifiers 2.Variables 3.Keywords 4.Statements 5.Comments 6.Whitespaces 7.Syntax 8.

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

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

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

Chapter 2 - Introduction to C Programming

1.1 Introduction to C Language. Department of CSE

Syntax and Variables

Basic Elements of C. Staff Incharge: S.Sasirekha

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

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

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

Language Basics. Characteristics of C. This chapter describes the basic characteristics and elements of the C programming language.

Data Types and Variables in C language

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

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

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

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

6.096 Introduction to C++ January (IAP) 2009

Procedures, Parameters, Values and Variables. Steven R. Bagley

Programming and Data Structures

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

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

Chapter 1 & 2 Introduction to C Language

Fundamental of Programming (C)

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

COP 3275: Chapter 02. Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA

!"#$% &'($) *+!$ 0!'" 0+'&"$.&0-2$ 10.+3&2),&/3+, %&&/3+, C,-"!.&/+"*0.&('1 :2 %*10% *%7)/ 30'&. 0% /4%./

C Language, Token, Keywords, Constant, variable

Lecture 02 C FUNDAMENTALS

PROGRAMMAZIONE I A.A. 2017/2018

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

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

Programming. C++ Basics

Tokens, Expressions and Control Structures

PROGRAMMAZIONE I A.A. 2017/2018

A Fast Review of C Essentials Part I

Programming for Engineers Introduction to C

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

Introduction to the C Programming Language

BLM2031 Structured Programming. Zeyneb KURT

Variables in C. CMSC 104, Spring 2014 Christopher S. Marron. (thanks to John Park for slides) Tuesday, February 18, 14

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

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

Full file at C How to Program, 6/e Multiple Choice Test Bank

Presented By : Gaurav Juneja

ANSI C Programming Simple Programs

ET156 Introduction to C Programming

ET156 Introduction to C Programming

CMPE-013/L. Introduction to C Programming

Short Notes of CS201

Fundamentals of Programming

Computers Programming Course 5. Iulian Năstac

INTRODUCTION 1 AND REVIEW

XSEDE Scholars Program Introduction to C Programming. John Lockman III June 7 th, 2012

Lecture 02 Summary. C/Java Syntax 1/14/2009. Keywords Variable Declarations Data Types Operators Statements. Functions

Number Systems, Scalar Types, and Input and Output

CS201 - Introduction to Programming Glossary By

Fundamental of C programming. - Ompal Singh

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

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

C Programming Review CSC 4320/6320

VARIABLES AND CONSTANTS

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

UEE1302 (1102) F10: Introduction to Computers and Programming

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

CS102: Variables and Expressions

Basic Types and Formatted I/O

BSM540 Basics of C Language

Lecture 3. More About C

DEPARTMENT OF MATHS, MJ COLLEGE

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

Week 2 C Fundamentals; Formatted Input/Output; int and float Types

HP C/iX Reference Manual

BASIC ELEMENTS OF A COMPUTER PROGRAM

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

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

PES INSTITUTE OF TECHNOLOGY (BSC) I MCA, First IA Test, November 2015 Programming Using C (13MCA11) Solution Set Faculty: Jeny Jijo

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

C-LANGUAGE CURRICULAM

Data types, variables, constants

EEE145 Computer Programming

Introduction to C# Applications

3. Except for strings, double quotes, identifiers, and keywords, C++ ignores all white space.

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

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

Variables and Literals

Computer Programming Unit 3

Fundamentals of Programming Session 4

Formatted Input/Output

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

>B<82. 2Soft ware. C Language manual. Copyright COSMIC Software 1999, 2001 All rights reserved.

Variables. Data Types.

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

Variation of Pointers

THE FUNDAMENTAL DATA TYPES

Transcription:

PROGRAMMAZIONE I A.A. 2018/2019

COMMENTS

COMMENTS There are two ways to insert a comment in C: üblock comments begin with /* and end with */, and üline comments begin with // and end with the next new line character. You can use the /* and */ delimiters to begin and end comments within a line, and to enclose comments of several lines. You can use // to insert comments that fill an entire line, or to write source code in a two-column format, with program code on the left and comments on the right: double pi = 3.1415926536; // Pi is 3,14

COMMENTS 2 Inside the quotation marks that delimit a character constant or a string literal, the characters /* and // do not start a comment. For example, the following statement contains no comments: printf( "Comments in C begin with /* or //.\n" ); The first */ will terminate the opening of the multiline comment: not possible to nest block comments You can insert /* and */ to comment out part of a program that contains line comments: /* Temporarily removing two lines: double pi = 3.1415926536; // Pi is 3.14 area = pi * r * r; // Calculate the area Temporarily removed up to here */

EXAMPLE #include<stdio.h> int main(){ int x= 4; /* int y= 10; /* Comment starts here printf("x= %d\n", x); */ printf("y= %d\n", y); */ Comment closed here

ALL ABOUT COMMUNICATION We write code for two audiences. First: for the compiler. It does not judge the style of the code We write code to be executed by a computer, but to be read by humans. You write code for: üyou right now, as you re writing it. The code has to be crystal clear so you don t make implementation mistakes. üyou, a few weeks (or months) later as you prepare the software for release. üother people on your team who have to integrate their work with this code. üthe maintenance programmer (which could be you or another programmer) years later, when investigating a bug in an old release.

COMMUNICATION It can also look pretty, but be unreasonably hard to maintain. /****************************************************** * This is a pretty comment. * * Note that there are asterisks on the * * righthand side of the box. Wow; it looks neat. * * Hope I never have to fix this tiypo. * ******************************************************/ It s cute, but it s not easy to maintain. If you want to change the comment text, you ll have to manually rework the right-hand line of comment markers.

BAD COMMENTING EXAMPLES Stupid, redundant comments range from the classic example of byte wastage: i=i+1; // increment i // loop 5 times and add input values together int sum= 0, count= 0, n; while (count < 5) { scanf( %d, &n); sum= sum + n; count= count+1;

GOOD AND BAD COMMENTING It s also common to see old code that has been surgically removed by commenting it out. Do not remove code by commenting it out. It confuses the reader and gets in the way. You can do it when you are still in the debugging phase, to check how different encodings work if you have a runtime error for instance. Often, a clarification comment is a code smell. It tells you that your code is too complex. You should strive to remove clarification comments and simplify the code instead because, good code is self-documenting.

CHARACTER SETS

CHARACTER SETS Accordingly, C defines two character sets: the source character set is the set of characters that may be used in C source code, and the execution character set is the set of characters that can be interpreted by the running program. In most implementation they are the same. UTF-8 (more on Linux, MacOS) or UTF-16 (more on Windows)

BASIC SOURCE AND EXECUTION CHARACTER SETS PLUS

SPECIAL CHARACTERS The basic execution character set also includes four nonprintable characters: üthe null character, which acts as the termination mark in a character string; üalert; übackspace; and ücarriage return. To represent these characters in character and string literals, type the corresponding escape sequences beginning with a backslash: \0 for the null character, \a for alert, \b for backspace, and \r for carriage return.

WHITESPACE CHARACTERS Whitespace charaters are ü \f form feed; ü \n newline; ü \t horizontal tab; ü \v vertical tab.

IDENTIFIERS

IDENTIFIERS The term identifier refers to the names of variables, functions, macros, structures and other objects defined in a C program. Identifiers can contain the following characters: üthe letters in the basic character set, a z and A Z. Identifiers are case-sensitive. üthe underscore character, _. üthe decimal digits 0 9, although the first character of an identifier must not be a digit.

RESERVED KEYWORDS The following 44 keywords are reserved in C, each having a specific meaning to the compiler, and must not be used as identifiers: auto extern break float case for char goto const if continue inline default int do long double register else restrict enum return short signed sizeof static struct switch typedef union unsigned void volatile while _Alignas _Alignof _Atomic _Bool _Complex _Generic _Imaginary _Noreturn _Static_assert _Thread_local.

EXAMPLES The following examples are valid identifiers: üx üdollar üwhile üerror_handler üscale64 The following are not valid identifiers: ü1st_rank switch y/n x-ray

MORE ON IDENTIFIERS When choosing identifiers in your programs, remember that many identifiers are already used by the C standard library. These include the names of standard library functions, which you cannot use for functions you define or for global variables. There is no limit on the length of identifiers. However, most compilers consider only a limited number of characters in identifiers to be significant. To conform to the C standard, a compiler must treat at least the first 31 characters as significant in the names of functions and global variables (that is, identifiers with external linkage), and at least the first 63 characters in all other identifiers.

EXAMPLE #include <stdio.h> int main() { int firstnumber, secondnumber, sumoftwonumbers; printf("enter two integers: "); // Two integers entered by user is stored using scanf() function scanf("%d %d", &firstnumber, &secondnumber); // sum of two numbers in stored in variable sumoftwonumbers sumoftwonumbers = firstnumber + secondnumber; // Displays sum printf("%d + %d = %d", firstnumber, secondnumber, sumoftwonumbers); return 0;

SCOPE AND BINDING

BINDING A binding is an association between two things, such as a name and the thing it names. Binding time is the time at which a binding is created or, more generally, the time at which any implementation decision is made. The textual region of the program in which a binding is active is its scope. In C the scope of a binding is determined statically, that is, at compile time

IDENTIFIER SCOPE The scope of an identifier refers to that part of the translation unit in which the identifier is meaningful. Or to put it another way, the identifier s scope is that part of the program that can see that identifier. The type of scope is always determined by the location at which you declare the identifier (except for labels, which always have function scope). Different kinds of scope are possible: üfile scope: If you declare an identifier outside all blocks and parameter lists (functions), then it has file scope. You can then use the identifier anywhere after the declaration and up to the end of the translation unit.

IDENTIFIER SCOPE 2 Block scope: identifiers declared within a block have block scope. You can use such an identifier only from its declaration to the end of the smallest block containing that declaration. The smallest containing block is often, but not necessarily, the body of a function definition.

IDENTIFIER SCOPE 3 The scope of an identifier generally begins after its declaration. However, the type names, or tags, of structure, union, and enumeration types and the names of enumeration constants are an exception to this rule: their scope begins immediately after their appearance in the declaration, so that they can be referenced again in the declaration itself.

IDENTIFIER It is not possible to have two variables with the same identifier with the same scope int main() { int a= 5; float a= 6.5; int main() { int a= 5; float a= 6.5; int c= a + 1; // which a?

EXAMPLE 1 int p; int fun2 (int r) { int s = r; while( r!= s) { r--; return r; File scope Block scope p, fun2 r,s

EXAMPLE 2 int main() x = 10, y = 20 { x = 11, y = 41 { x = 11, y = 20 int x = 10, y = 20; { printf("x = %d, y = %d\n", x, y); { int y = 40; x++; y++; printf("x = %d, y = %d\n", x, y); printf("x = %d, y = %d\n", x, y); return 0;

EXAMPLE 3 int main() { int x = 1, y = 2, z = 3; printf(" x = %d, y = %d, z = %d \n", x, y, z); { int x = 10; float y = 20; printf(" x = %d, y = %f, z = %d \n", x, y, z); { int z = 100; printf(" x = %d, y = %f, z = %d \n", x, y, z); return 0; x = 1, y = 2, z = 3 x = 10, y = 20.000000, z = 3 x = 10, y = 20.000000, z = 100

SO It is possible to use an identifier again in a new declaration nested within its existing scope. If you do so, then the new declaration must have block scope, and the block must be a subset of the outer scope. In such cases, the new declaration of the same identifier hides the outer declaration, so that the variable or function declared in the outer block is not visible in the inner scope.

EXAMPLE int main() { { int x = 10; { printf("%d", x); return 0; Error: x is not accessible here error: 'x' undeclared (first use in this function)

EXERCISE #include <stdio.h> int f1( void ) ; int f2( int x, int a ) ; int a ; int main() { int a, b, c ; a = 7 ; b = f1() ; c = f2( a, b ) ; printf( "%d %d %d\n", a, b, c ) ; int f1( void ) { a = 12 ; printf( "%d ", a ) ; return( a + 5 ) ; int f2( int x, int a ) { printf( "%d ", a ) ; return( x * a ) ; What is the output? 12 17 7 17 119

HOW TO CHOOSE NAMES OF IDENTIFIERS A name conveys the identity of an object; it describes the thing, indicates its behaviour and intended use. A misnamed variable can be very confusing. Do not create unnecessarily long variable name Choose different names for different scopes in order to avoid confusion See the difference üa = b * c; üweekly_pay = hours_worked * pay_rate;