Basic Elements of C. Staff Incharge: S.Sasirekha

Similar documents
Data Types and Variables in C language

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

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

VARIABLES AND CONSTANTS

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

C - Basic Introduction

C: How to Program. Week /Mar/05

INTRODUCTION 1 AND REVIEW

Programming and Data Structures

C Language, Token, Keywords, Constant, variable

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

Chapter 2 - Introduction to C Programming

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

Work relative to other classes

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

DEPARTMENT OF MATHS, MJ COLLEGE

Fundamental of Programming (C)

Chapter 1 & 2 Introduction to C Language

6.096 Introduction to C++ January (IAP) 2009

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

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.

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

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

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

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

ANSI C Programming Simple Programs

Java Notes. 10th ICSE. Saravanan Ganesh

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

BLM2031 Structured Programming. Zeyneb KURT

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

Fundamentals of Programming

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

Presented By : Gaurav Juneja

Programming for Engineers Introduction to C

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

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

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

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

LEXICAL 2 CONVENTIONS

A Fast Review of C Essentials Part I

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

Tokens, Expressions and Control Structures

Fundamental of C programming. - Ompal Singh

Fundamentals of Programming Session 4

UNIT- 3 Introduction to C++

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

Number Systems, Scalar Types, and Input and Output

Chapter 2 THE STRUCTURE OF C LANGUAGE

Syntax and Variables

Differentiate Between Keywords and Identifiers

PROGRAMMAZIONE I A.A. 2018/2019

BITG 1233: Introduction to C++

C++ character set Letters:- A-Z, a-z Digits:- 0 to 9 Special Symbols:- space + - / ( ) [ ] =! = < >, $ # ; :? & White Spaces:- Blank Space, Horizontal

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

BASIC ELEMENTS OF A COMPUTER PROGRAM

Introduction to C# Applications

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

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

Fundamentals of Programming. Lecture 3: Introduction to C Programming

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

1.1 Introduction to C Language. Department of CSE

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

Preview from Notesale.co.uk Page 6 of 52

Variables and Literals

BSM540 Basics of C Language

The C++ Language. Arizona State University 1

HP C/iX Reference Manual

Chapter 2, Part I Introduction to C Programming

Lecture 2 Tao Wang 1

UNIT IV 2 MARKS. ( Word to PDF Converter - Unregistered ) FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING

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

Unit-II Programming and Problem Solving (BE1/4 CSE-2)

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

Basic Types, Variables, Literals, Constants

CSc Introduction to Computing

Basics of Programming

Chapter-8 DATA TYPES. Introduction. Variable:

Basic Types and Formatted I/O

Programming in C. What is C?... What is C?

Programming in C UVic SEng 265

Chapter 21: Introduction to C Programming Language

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

ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead.

Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. C Data Types

CHAPTER-6 GETTING STARTED WITH C++

Programming in C. What is C?... What is C?

Lecture 02 C FUNDAMENTALS

Standard 11. Lesson 9. Introduction to C++( Up to Operators) 2. List any two benefits of learning C++?(Any two points)

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

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

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

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

ET156 Introduction to C Programming

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Unit 4. Input/Output Functions

IECD Institute for Entrepreneurship and Career Development Bharathidasan University, Tiruchirappalli 23.

UEE1302 (1102) F10: Introduction to Computers and Programming

Data types, variables, constants

Transcription:

Basic Elements of C Staff Incharge: S.Sasirekha

Basic Elements of C Character Set Identifiers & Keywords Constants Variables Data Types Declaration Expressions & Statements

C Character Set Letters Uppercase A to Z Lowercase a to z E.g.:- 'A', 'B', a', 'b Digits 0 to 9 E.g.:- '0','1 Special Characters Constants, Variables, Operators & Expressions E.g.:- +,?, <, _, %

White Spaces White Spaces are ignored by the compiler until they are a part of string constant. White Space may be used to separate words, but are strictly prohibited while using between characters of keywords or identifiers. Escape Sequences Combination Of Characters E.g.:- 1. Blank Space 2. Horizontal Tab 3. Carriage Return 4. New Line 5. Form Feed Note:@ & $ are included with strings and Comments Characters are small integers (0-255) Character constants are integers that denote corresponding characters ASCII code maps characters to integers A Z (65-90) a z (97-122)

Identifiers & Keyword Identifiers refers to the name of user-defined variables, array and functions. Identifies consists of Letter & digits, in any order, except that 1 st character must be a letter. Both upper and lower case are allowed Underscore(_) can also be included & considered as a letter A identifier should not contain a space. Some implementation can recognize only 8 characters, typically 31 characters are accepted. E.g. Valid X,name,area,SUM_1 Invalid x 4 th

There are certain reserved words called Keywords that have standard predefined meaning in c. Used only for the purpose it is intended for. Cannot be used as identifier Note: Keywords are lowercase, Uppercase keywords can be used as identifiers.

Keywords Description Keyword auto break case char const continue default do double else enum extern float for goto Description Storage class specifier Statement Statement Type specifier Storage class modifier Statement Label Statement Type specifier Statement Type specifier Storage class specifier Type specifier Statement Statement

Keywords Description if int long register return short signed sizeof static struct switch typedef Union unsigned void volatile while Statement Type specifier Type specifier Storage class specifier Statement Type specifier Type specifier Operator Storage class specifier Type specifier Statement Statement Type specifier Type specifier Type specifier Storage class modifier Statement

Constants A constant value is the one which does not change during the execution of a program. C supports several types of constants. Numeric Constants 1. Integer Constants 2. Real Constants Character Constants 3. Single Character Constants 4. String Constants Numeric Type Constants Follow Rules to be applied Commas & blank spaces cannot be included The Constants can be preceded by a minus(-) sign if desired The value of the Const cannot exceed specific Min & Max Bounds

Integer Constants An integer constant is a sequence of digits & integer valued number. There are 3 types of integers namely Decimal integer (base 10) Octal integers (base 8) Hexadecimal integer (base 16) Decimal Integers consists of a set of digits 0 to 9 preceded by an optional + or - sign. Spaces, commas and non digit characters are not permitted between digits. Example for valid decimal integer constants are» 123» -31» 0» 562321» + 78 Some examples for invalid integer constants are» 15 750» 20,000» Rs. 1000

Octal Integers Constant consists of any combination of digits from 0 through 7. First digit must begin with a O.( for identification Some examples of octal integers are O26 O O347 O676 Some Invalid example 743 05280 077.77 Hexadecimal integer Constant is preceded by OX or Ox, They may contain alphabets from A to F or a to f. The alphabets A to F refers to 10 to 15 in decimal digits. Example of valid hexadecimal integers are OX2 OX8C OXbcd Ox

Real Constants Real Constants consists of a fractional part in their representation. Integer constants are inadequate to represent quantities that vary continuously. These quantities are represented by numbers containing fractional parts like 26.082. Example of real constants are 0.0026-0.97 435.29 +487.0 Real Numbers can also be represented by exponential notation. The general form for exponential notation is mantissa e exponent The mantissa is either a real number expressed in decimal notation or an integer. e x mean multiply by 10 2 The exponent is an integer number with an optional plus or minus sign.(+ means shift right,- means shift left)

Character Constants Single Character Constant represent a single character which is enclosed in a pair of quotation symbols. Example for character constants are» '5» 'x» ';» ' ' All character constants have an equivalent integer value which are called ASCII Values String Constants A string constant is a set of characters enclosed in double quotation marks. The characters in a string constant sequence may be a alphabet, number, special character and blank space. Example of string constants are» "VISHAL» "1234» "God Bless» "!...?"S

Backslash Character Constants [Escape Sequences] Backslash character constants are special characters used in output functions. Although they contain two characters they represent only one character. Given below is the table of escape sequence and their meanings. Escape Sequence \a \b \f \n \r \t \v \' \" \? \\ \0 Alert Name Backspace Form Feed New Line Carriage Return Horizontal Tab Vertical Tab Meaning Produces an audible or visible alert. Moves the cursor back one position (non-destructive). Moves the cursor to the first position of the next page. Moves the cursor to the first position of the next line. Moves the cursor to the first position of the current line. Moves the cursor to the next horizontal tabular position. Moves the cursor to the next vertical tabular position. Produces a single quote. Produces a double quote. Produces a question mark. Produces a single backslash. Produces a null character.

Example /* Special Characters */ #include <stdio.h> main () { printf ("Beep! \7 \n"); printf ("ch = \'a\' \n"); printf (" <- Start of this line!! \r"); } Output of this program is: Beep! (and the BELL sound ) ch = 'a' <- Start of this line!! and the text cursor is left where the arrow points.

Variables A variable is a value that can change any time. It is a memory location used to store a data value. A variable name should be carefully chosen by the programmer so that its use is reflected in a useful way in the entire program. Variable names are case sensitive. Example of variable names are» Sun» Number» Salary» Emp_name» Average1

Any variable declared in a program should confirm to the following 1.They must always begin with a letter, although some systems permit underscore as the first character. 2.The length of a variable must not be more than 8 characters. 3.White space is not allowed and 4.A variable should not be a Keyword 5.It should not contain any special characters. Examples of Invalid Variable names are» 123» (area)» 6 th» %abc

Data Types C Support different types of data, each of which may represented differently within the computer memory C language data types can be broadly classified as Primary data type Derived data type User-defined data type

Primary data type All C Compilers accept the following fundamental data types The size and range of each data type is given in the table below

The basic data types can be augmented by the use of data type qualifiers (Storage) Short Half or same the amount of storage as regular int numbers Long To increase the range of values( double the amount) These can be: Signed ordinary data type Unsigned All the bits for the magnitude of the numbers are always positive E.g. Integer qualifiers: Short int Long int (4 bytes) Signed int (2 bytes) Unsigned int(2 bytes) Won t use left most bit for sign(0 to 65536)

Void Type : Using void data type, we can specify the type of a function. It is a good practice to use a void functions that does not return any values to the calling function

Declaration of Variables Every variable used in the program should be declared to the compiler. The declaration does two things. 1. Tells the compiler the variables name. 2. Specifies what type of data the variable will hold. The general format of any declaration datatype v1, v2, v3,.. vn; Where v1, v2, v3 are variable names. Variables are separated by commas. A declaration statement must end with a semicolon. Example: Int sum; Int number, salary; Double average, mean;

User defined type declaration In C language a user can define an identifier that represents an existing data type. The user defined datatype identifier can later be used to declare variables. The general syntax is typedef type identifier; here type represents existing data type and identifier refers to the row name given to the data type. Example: typedef int salary; typedef float average; Here salary symbolizes int and average symbolizes float. They can be later used to declare variables as follows: salary dept1, dept2; Average section1, section2; Therefore dept1 and dept2 are indirectly declared as integer datatype and section1 and section2 are indirectly float data type.

The second type of user defined datatype is enumerated data type which is defined as follows. Enum identifier {value1, value2. Value n}; The identifier is a user defined enumerated datatype which can be used to declare variables that have one of the values enclosed within the braces. After the definition we can declare variables to be of this new type as below. enum identifier V1, V2, V3, Vn The enumerated variables V1, V2,.. Vn can have only one of the values value1, value2.. value n

The enum Data Type enum is short for enumerated. The enumerated data type can be used to declare named integer constants. Syntax of the enum data type declaration enum tag_name {enumeration_list} variable_list; tag_name name of the enumeration. variable_list list of variable names that are of the enum data type. enumeration_list enumerated names that are used to represent integer constants. (Both tag_name and variable_list are optional.)

The enum Data Type - Example enum automobile {sedan, pick_up, sport_utility}; Given this, you can define enum variables like this: enum automobile domestic, foreign; Here the two enum variables, domestic and foreign, are defined. You can always declare and define a list of enum variables in a single statement, as shown in the general form of the enum declaration. enum automobile {sedan, pick_up, sport_utility} domestic, foreign;

The enum Data Type Assigning Values to enum Names By default, the integer value associated with the leftmost name in the enumeration list field, surrounded by the braces ({ and }), starts with 0, and the value of each name in the rest of the list increases by one from left to right. Therefore, in the previous example, sedan, pick_up, and sport_utility have the values of 0, 1, and 2, respectively. You can assign integer values to enum names. Considering the previous example, you can initialize the enumerated names like this: enum automobile {sedan = 60, pick_up = 30, sport_utility = 10};

Example program for enum /* Defining enum data types */ #include <stdio.h> main() /* main() function */ { enum language {human=100,animal=50,computer}; enum days{sun, MON,TUE,WED,THU,FRI,SAT}; printf("human: %d, animal: %d, computer: %d\n", human, animal, computer); printf("sun: %d\n", SUN); printf("mon: %d\n", MON); printf("tue: %d\n", TUE); printf("wed: %d\n", WED); printf("thu: %d\n", THU); printf("fri: %d\n", FRI); printf("sat: %d\n", SAT); return 0; }

Example program for enum OUTPUT human: 100, animal: 50, computer: 51 SUN: 0 MON: 1 TUE: 2 WED: 3 THU: 4 FRI: 5 SAT: 6

Declaration of Storage Class Variables in C have not only the data type but also storage class that provides information about their location and visibility. The storage class divides the portion of the program within which the variables are recognized. auto : It is a local variable known only to the function in which it is declared. Auto is the default storage class. static : Local variable which exists and retains its value even after the control is transferred to the calling function. extern : Global variable known to all functions in the file register : Social variables which are stored in the register.

Defining Symbolic Constants A symbolic constant value can be defined as a preprocessor statement and used in the program as any other constant value. The general form of a symbolic constant is # define symbolic_name value of constant Valid examples of constant definitions are : # define marks 100 # define total 50 # define pi 3.14159 These values may appear anywhere in the program, but must come before it is referenced in the program. It is a standard practice to place them at the beginning of the program.

Example #define PASSPERCENT 65 int main(int argc, char **argv) { /* PASSPERCENT macro being used in code */ printf ( Pass percentage is: %d\n, PASSPERCENT); /* Return a success code to Operating System */ return 0; } A preprocessed code looks like this: int main(int argc, char **argv) { printf ( Pass percentage is: %d\n, 65); return 0; }

Declaring Variable as Constant The values of some variable may be required to remain constant through-out the program. We can do this by using the qualifier const at the time of initialization. Example: Const int class_size = 40; The const data type qualifier tells the compiler that the value of the int variable class_size may not be modified in the program

Volatile Variable A volatile variable is the one whose values may be changed at any time by some external sources. Example: volatile int num; The value of data may be altered by some external factor, even if it does not appear on the left hand side of the assignment statement. When we declare a variable as volatile the compiler will examine the value of the variable each time it is encountered to see if an external factor has changed the value.