Download presentation
Presentation is loading. Please wait.
Published byFrederick Randall Modified over 9 years ago
1
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
2
TOPICS COVERED A Sample ‘C’ Program Entities of a ’C’ Program Variables Constants Data Types Identifiers Expressions
3
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(); }
4
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
5
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.
6
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
7
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.
8
Key(Reserve)words in C Language Autodoubleintstruct Breakelselongswitch Caseenumregistertypedef Charexternreturnunion Continue forsignedvoid Doifstaticwhile Defaultgotosizeofvolatile Constfloatshortunsigned
9
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
10
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: 0 1 2 3 4 5 6 7 8 9 Octal digits: 0 1 2 3 4 5 6 7 Hexadecimal digits: 0 1 2 3 4 5 6 7 8 9 A B C D E F.
11
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
12
FLOATING POINT CONSTATNS Floating point constants are the numeric constants are in fractional and/or in exponential form. For example: -2.0 0.0000234 -0.22E-5 Here, E-5 represents 10 -5. Thus, -0.22E-5 = - 0.0000022. We can use either “E” or “e”.
13
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
14
Different Escape Sequences
15
String constants
16
Enumeration constants
17
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)
18
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
19
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;
20
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: -2 31 to 2 31 -1 Similarly, int of 2 bytes, it has a range as: -2 15 to 2 15 -1. 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 2 32 -1 or 0 to 2 16 depending on int being 2 bytes or 4 bytes
21
Floating point types Variables of floating points types can hold real values(numbers) such as: 2.34, -9.382 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
22
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.
23
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
24
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
25
TYPES OF OPERATORS IN C Arithmetic Assignment Relational Logical Increment and Decrement Conditional operators Bitwise and others
26
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
27
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
28
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
30
EXAMPLES OF EXPRESSIONS 1. 2+3*5 is evaluated as 17 and not as 25 2. (2+3)*5 is evaluated as 25 3. 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
31
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
32
Thank you For further doubts send your question to my mail ppadmanabham@yahoo.com
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.