INTRODUCTION TO ‘C’ PROGRAMMING BY Prof. P. PADMANABHAM M.Tech (AE), M.Tech(CS), Ph.D(CS)-FIETE, FIE Director Academics, Bharat Institute Of Engineering.

Slides:



Advertisements
Similar presentations
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
Advertisements

Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Lecture 02 Data Types, Conversions if Statements METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan.
Data types and variables
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
JavaScript, Fourth Edition
Chapter 2 Data Types, Declarations, and Displays
JavaScript, Third Edition
1 Lecture 3  Lexical elements  Some operators:  /, %, =, +=, ++, --  precedence and associativity  #define  Readings: Chapter 2 Section 1 to 10.
Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming.
Variable & Constants. A variable is a name given to a storage area that our programs can manipulate. Each variable in C has a specific type, which determines.
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Objectives You should be able to describe: Data Types
EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Outline Variables 1.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Data representation and Data Types Variables.
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
Variables and Data Types
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
2440: 211 Interactive Web Programming Expressions & Operators.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Sales person receive RM200/week plus 9% of their gross sales for that week. Write an algorithms to calculate the sales person’s earning from the input.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
Chapter 2: Using Data.
CPS120: Introduction to Computer Science
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
Characters and tokens Characters are the basic building blocks in C program, equivalent to ‘letters’ in English language Includes every printable character.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Primitive Variables.
 All C programs are made up of functions that perform operations on variables.  In this lecture we examine variables  Variables are the basic building.
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Topics to be covered  Introduction to C Introduction to C  Characterstics of C Characterstics of C  Characterset Characterset  Keywords Keywords 
Variables Symbol representing a place to store information
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
C Building Block Chapter 2. Variables A variable is a space in the computer’s memory set aside for a certain kind of data and given a name for easy reference.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
Basics of ‘ C ’ By Gaikwad Varsha P. Asst. Prof. Information Technology Dept. Govt. College of Engg. Aurangabad.
BASIC ELEMENTS OF A COMPUTER PROGRAM
Chap. 2. Types, Operators, and Expressions
Tokens in C Keywords Identifiers Constants
ITEC113 Algorithms and Programming Techniques
Data Types, Identifiers, and Expressions
Revision Lecture
What to bring: iCard, pens/pencils (They provide the scratch paper)
Java Programming: From Problem Analysis to Program Design, 4e
By: Syed Shahrukh Haider
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Basics of ‘C’.
Chapter 2: Java Fundamentals
Lectures on Numerical Methods
Lexical Elements & Operators
DATA TYPES There are four basic data types associated with variables:
C Programming Lecture-3 Keywords, Datatypes, Constants & Variables
Programming Fundamental-1
ECE 120 Midterm 1 HKN Review Session.
Presentation transcript:

INTRODUCTION TO ‘C’ PROGRAMMING BY Prof. P. PADMANABHAM M.Tech (AE), M.Tech(CS), Ph.D(CS)-FIETE, FIE Director Academics, Bharat Institute Of Engineering And Technology Mangalpally Village, Ibrahimpatnam Rangareddy District

TOPICS COVERED A Sample ‘C’ Program Entities of a ’C’ Program Variables Constants Data Types Identifiers Expressions

A SAMPLE ‘C’ PROGARM #include void main() { /* This program prints 1 to 10 one number on each line */ int i; clrscr(); for(i=0;i<10;i++) { printf(“%d\n”, i+1); } getch(); }

DIFFERENT ENTITIES IN ‘C’ PROGARAM “#include, #include ” are compiler directives “void main(){ }” is a function written by the program writer “printf()” and “clrscr()” are library functions “int i;” is a declaration and “i” is an integer variable “for( i=0;i<10;i++){ }” is a control structure “i=0 ; i<10 ; i++” are Expressions “/* This program prints 1 to 10 one number on each line */” is a comment

Variables Variables are memory locations in computer's memory to store data. To indicate the memory location, each variable should be given a unique name. Variable names are just the symbolic representation of a memory location. Examples of variable name: a,x, sum, no_of_students, count1 etc.

Rules for writing variable name in C Variable name can be composed of letters (both uppercase and lowercase ), digits and underscore '_' only. The first letter of a variable name should be either a letter or an underscore. But, it is discouraged to start variable name with an underscore though it is legal. There is no rule for the length of a variable. However, the first 31 characters of a variable are discriminated by the compiler. So, the first 31 letters of two variables in a program should be different. A variable name should not be a reserve ( key) word. In C programming, declare a variable before using it in the program

Keywords: Keywords are the reserved words used in programming. Each keyword has fixed meaning and that cannot be changed by user. For example: int money; Here, int is a keyword that indicates, 'money' is of type integer. As, C programming is case sensitive, all keywords must be written in lowercase.

Key(Reserve)words in C Language Autodoubleintstruct Breakelselongswitch Caseenumregistertypedef Charexternreturnunion Continue forsignedvoid Doifstaticwhile Defaultgotosizeofvolatile Constfloatshortunsigned

constants Constants are the entities that can't be changed during the execution of a program. For example: 1, 2.5, "Programming is easy." etc. In C, constants can be classified as: 1.Integer constants 2.Floating point constants 3.Character Constants 4.String constants 5.Enumeration Constants

INTEGER CONSTANTS Integer constants are the numeric constants(constant associated with number) without any fractional part or exponential part. There are three types of integer constants in C language: decimal constant(base 10), octal constant(base 8) and hexadecimal constant(base 16). Decimal digits: Octal digits: Hexadecimal digits: A B C D E F.

EXAMPLES FOR INTEGER CONSTANTS You can use lowercase a, b, c, d, e, f instead of uppercase letters while writing a hexadecimal constant. Every octal constant starts with 0 and hexadecimal constant starts with 0x in C programming. Decimal constants: 0, -9, 22 etc Octal constants: 021, 077, 033 etc Hexadecimal constants: 0x7f, 0x2a, 0x521 etc

FLOATING POINT CONSTATNS Floating point constants are the numeric constants are in fractional and/or in exponential form. For example: E-5 Here, E-5 represents Thus, -0.22E-5 = We can use either “E” or “e”.

CHARACTER CONSTANTS Character constants are the constant which use single quotation around characters. For example: 'a', 'l', 'm', 'F' etc. However, all character constants are internally represented in ASCII. Sometimes, it is necessary to use newline(enter), tab, quotation mark etc. in the program which either cannot be typed or has special meaning in C programming. In such cases, escape sequence are used

Different Escape Sequences

String constants

Enumeration constants

Identifiers In C programming, identifiers are names given to C entities, such as variables, functions, structures etc. Identifier are created to give unique name to C entities to identify it during the execution of program. For example: int money; int gcd(int n1,int n2); Here, money is a identifier which denotes a variable of type integer. gcd is an identifier which is a function name which returns an integer and takes two integers as paramaters. The rules for choosing an identifier name are same as applicable to variable name (As discussed earlier)

In C, Different data types have different storage allocation and /or different format of storage in order to store different data used in a program. Variables that hold data should be declared before they can be used in program along with their data type by using the keywords(such as int,float,double,char etc.,) which are used for assigning a data type to a variable. DATA TYPES

Data types in C 1. Fundamental Data Types – Integer types – Floating Type – Character types 2. Derived Data Types – Arrays – Pointers – Structures – Enumeration Syntax for declaration of a variable data_type variable_name;

Integer data types Keyword int is used for declaring the variable with integer type. For example: int var1; Here, var1 is a variable of type integer. The size of int is either 2 bytes or 4 bytes. If you consider an integer having size of 4 byte( equal to 32 bits), it has a range as: to Similarly, int of 2 bytes, it has a range as: to If you try to store numbers larger than the ranges specified above, program will not run correctly. Some compilers refer 4 byte int as long and 2 byte int as short If “unsigned” qualifier is used before the declaration of int it stores 0 to or 0 to 2 16 depending on int being 2 bytes or 4 bytes

Floating point types Variables of floating points types can hold real values(numbers) such as: 2.34, etc. Keywords either float or double is used for declaring floating type variable. For example: float var2; double var3; Here, both var2 and var3 are floating type variables. In C, floating values can be represented in exponential form as well. For example: float var3=22.442e2

Difference between float and double Generally the size of float(Single precision float data type) is 4 bytes and that of double(Double precision float data type) is 8 bytes. Floating point variables has a precision of 6 digits whereas the precision of double is 14 digits. Precision describes the number of significant decimal places that a floating values carries.

Character types Keyword char is used for declaring the variable of character type. For example: char var4='h'; Here, var4 is a variable of type character which is storing a character 'h'. The size of char is 1 byte. The character data type is represented in ASCII internally. Each character is given a specific ASCII value For example: 'a’ =97,’b’ =98, 'A’=65, ‘0’=48, '2’=50

EXPRESSIONS In general an expression in C is constructed using variables and/or constants For Example: x+y*3.5 where x,y are variables +,* are operators and 3.5 is a floating point constant. The type of expression depends on variables and operators chosen. The above is an example of arithmetic expression. For example x>y is a relational expression

TYPES OF OPERATORS IN C Arithmetic Assignment Relational Logical Increment and Decrement Conditional operators Bitwise and others

HOW EXPRESSIONS ARE EVALUATED All expressions in C language are evaluated based on 1. Precedence of the operator 2. Associativity The operator with higher precedence is evaluated first and with in the same precedence operators are evaluated based on associativity

operator Meaning of operatorAssociativity () [] ->. Braces to indicate higher priority Array element reference indirect member selection ( through pointer) Direct member selection Left to right ! ~ & * sizeof (type ) Logical negation Bitwise(1 's) complement Unary plus Unary minus Increment Decrement Dereference Operator(Address) Pointer reference Returns the size of an object Type cast(conversion) Right to left (unary operators) */%*/% Multiply Divide Remainder Left to right

OperatorMeaning of operatorAssociativity +-+- Binary plus(Addition) Binary minus(subtraction) Left to right > Left shift Right shift Left to right >= Less than Less than or equal Greater than Greater than or equal Left to right == != Equal to Not equal to Left to right & Bitwise ANDLeft to right ^ Bitwise exclusive ORLeft to right | Bitwise ORLeft to right && Logical ANDLeft to right || Logical ORLeft to right ?: Conditional OperatorLeft to right

EXAMPLES OF EXPRESSIONS *5 is evaluated as 17 and not as (2+3)*5 is evaluated as x+y>z*3 is evaluated to 1 if x+y is grater than z*3 else to 0 4. x=y+=3 current value of y is incremented by 3 and that is assigned to x 5. x<<=3 the current value of x is multiplied by 8 6.~x+1 is evaluated to -x

More examples 8>6>5 X=a>b ? a :b X=a>b&&a>c?a:b>c?b:c X=a<0?-a:a

Thank you For further doubts send your question to my mail