C Programming Language Introduction PDF
Document Details
Uploaded by DelightedCarbon5219
Tags
Summary
This document provides an introduction to the C programming language. It covers fundamental concepts like data types, operators, and expressions. The document also touches upon the structure of a C program and the various types of statements used in C.
Full Transcript
UNIT –II Chapter -2 Introduction to C Language Dennis M Ritchie Introduced C Language in 1972 Salient Features of C It is Robust – Long lasting, strong & powerful. C is a high-level language. C consists of rich variety of datatypes and powerful operators. This makes it m...
UNIT –II Chapter -2 Introduction to C Language Dennis M Ritchie Introduced C Language in 1972 Salient Features of C It is Robust – Long lasting, strong & powerful. C is a high-level language. C consists of rich variety of datatypes and powerful operators. This makes it more efficient and fast. It is Platform independent – It can be used with all type of systems such as Laptops, desktops , palmtops etc. It is highly portable- it can be run on almost all type of operating system.. It is a structured language wherein the programs can be sub-divided into modules. Easy to Debug. Applications of C C Can be used to develop software such as Translators Operating System DBMS Word Processors Spreadsheets CAD/CAM Scientific and Engineering Applications Components of C( Building Blocks) 1. Character Set The C-character set is a collection of Alphabets- C language allows both lower case and upper case letters. But both are having different significances. [A-Z] , [a-z] Digits- C language allows 0 – 9 decimal digits. Special symbols- C allows special symbols like ; , @ , # ,$ ,% , ^, z etc. White spaces - C allows tabs, space etc.. 2. C Tokens- The Smallest individual unit of a C program is known as tokens. The 6 types of C tokens are:- -> Keywords ->Identifiers ->Constants ->Variables ->Operators ->Special Symbols Keywords: Keywords are the reserve words which is having a specific meaning in C language. All keywords have predefined meaning. Keywords are in lowercase letters only. Eg: break case char int float double Identifiers Identifier is a name given to a variable. They are made up of sequence of characters..Eg: Salary, Sum,Average Rules 1. The first character must be an alphabet. 2. Identifiers must consist of only alphabets, digits or underscore. Eg: Total_Salary , Student_Marks1 etc 3. Special characters are not allowed. 4. The maximum length of an identifier in turbo C is 32 characters 5. Keywords should not be used as identifiers. 6. Identifier should be a single word. No blank spaces are allowed. Eg: Salary of a person(invalid) 7. Identifiers can be both uppercase and lower case letters. Constants: “A physical quantity which does not change its value during the execution of a program is known as constant”. Numeric Constants: Constants with only numbers are known as Numeric constants. Eg: 100, 45667 Numeric Constants is divided into two types they are: Integer constants & Real Constants Integer Constants: Integer constant is a whole number. It has a sequence of digits with out having a decimal point. Eg: 10, 2456 Types of Integer Constants Decimal 0-9 eg: 89 Octal 0-7 eg: 67 Hexa Decimal- 0-9 and A-F eg: 89A, BAD Real Constants This type of constant has a sequence of digits having decimal point.eg: 345.67 Character Constants:- Constants which is made up of only characters or letters. Eg: “Hello” Character Constants is of two types they are: Single Character Constants: A character constant is a single character which is enclosed with in a pair of single quote. Ex: ‘a’, ’d’ String Constants or Strings: A string is a sequence of characters which is enclosed with in double quotes. Ex: “welcome”, “hello” etc. Variables “The quantity which can change its value during the execution of a program is known as variable.” Rules for variables The first character must be an alphabet. Variables must consist of only alphabets, digits or underscore. Special characters are not allowed. Eg: num, salary All variables are in lowercase letters. C Keywords cannot be used as variables. Max length of the variable should not exceed 32 characters. Basic Structure of C-Program Documentation Section- Comments Pre-processor section- Header file Global Declaration section main() { Declaration part ; Execution part; } Documentation Section- This section consists of a set of comment lines useful for the program. Comments are non-executable statements in C. It helps the users to understand the program better. Pre-processor section- Header file This section contains header files which begins with # symbol and extended with.h as its extension. Eg: #include They are also known as preprocessor directives which is used to include the header files in a C program. Eg: #include #include Global Declaration section This section is used to define variables that would be used in more than one function. Such variables are called global variables and they must be declared before the main() function. main() This section is the main part of the C program. Every C program must have a main() function which is the starting point of the program execution. There should be only one main() function in a program. Braces { } All the C program must have a set of curly braces { }. Execution of the program begins at the open braces { of the main function and ends at its closing braces }. Declaration part & Execution part Declaration part is used to declare the variables which are used in the C program. Executable Part consists of a set of statements such as input or output statements, arithmetic statements etc which are all considered as the executable statements in a program. #include #include main() { printf(“ welcome to C program”); } *Escape Sequences Special backslash character constants denoted by backslash followed by a particular character. Eg:- \n \t etc… **Data types in C The data types are used to inform the type of value that can be stored in a variable. In C data types are classified into 2 ways. They are Primary data type - Built in data types Secondary Data type- User defined data types Built-in Data types Built-in data types: They are in built data types, also called as primary data type or the basic data types. ***C has 4 basic data types. They are Integer Floating point Double Char Integer Data type integer : (int) All integer data types holds integer numbers without a decimal point. All these numbers can be preceded either + or –ve sign. Ex: 100 , -65 etc It is represented by int. The size of this data type will be 2 bytes. For this we use the format specifier %d. The range of this data type will be -32768 to +32767. Floating point: (float) Float is a key word used to indicate floating point numbers. These are numbers with decimal point. Float data type occupies 4 bytes of memory. The range of this data type is 3.4 *10-38 to 3.4 * 10 +38. For this data type we will use %f format specifier. Ex:20.345 Double This data type is used to declare the variables to hold the large floating point numbers. When higher precession numbers are required, instead of using float double data type is used. For this %lf format specifier will be used. It occupies 8 bytes of memory. The range of this data type will be 1.7 * 10 -308 to 1.7 * 10 +308. Ex: +123.566788 etc Character Data type Char: This means that a variable is a character.. It occupies 1 byte (8bits) of memory. For this we are used %c format specifier. The range of this data type will be -128 to +127. Ex:- Name Variable Declaration All variables must be declared before they are used in program. “the purpose of declaring variables is to reserve amount of memory required for those variables”. General format or syntax of declaring a variable: Data type variable1,Variable2; Ex: int a, b,c ; Float average,sum; We can declare one or more variables on single line by using comma operator. Assigning value to variable: “The process of assigning values to variables is known as initialization”. Here we are used operator (=). Syntax: Data type variable1=value; variable 2= value..; Ex: int a=10, b=20 ; Symbolic Constant A symbolic constant is a name that substitutes a numeric constant in our program. Ex: area = 3.14 * r* r; In the above example numeric constant will be 3.14 this can be replaced by symbolic constant “PI”. Then we are using one pre-processor directive that is “# define”. “#define is a pre-processor directive and is used to declare a symbolic constants in C program”. The above example can be rewrite as # define PI 3.142 Operators in C Operators An Operator is a symbol which is used to perform specific operations in a programming language. An Operand the quantity on which an operation is to be done. Unary Operators Unary operator: are operators that act upon a single operand to produce a new value.. Operand : The quantity on which an operation is to be done. Types of unary operators: unary minus(-) unary plus(+) increment(++) decrement(--) Logical NOT(!) Addressof operator(&) sizeof() Unary Minus(-) The minus operator changes the sign of a quantity. A positive quantity becomes negative. int a = 10; int b = -a; o/p b=-10 Unary plus(+) The plus operator changes the sign of a negative quantity to positive. int a = -10; int b = +a; o/p b= +10 or 10 **Increment(++) It is used to increase the value of the variable by 1. Eg: a++ means a = a+1 i++ means i=i+1 The increment can be done in two ways: pre increment post increment **Pre increment In this method, the operator precedes the operand. (e.g: ++a means same as a = a+1) Program eg: int a = 2; int b = ++a; ie, a= 2+1; a=3; therefore b=3; O/p is b = 3 Post Increment In this method, the operator follows the operand (e.g., a++) Program example int a = 1; int b = a++; ------- a=a+1 ie, a=2 int c = a; ---------- ie, c=2; Therefore the o/p is c=2 Decrement(--) It is used to decrease the value of the variable by 1. Eg: a-- means a = a-1 The increment can be done in two ways: Pre-decrement Post decrement Pre decrement In this method, the operator preceeds the operand (e.g., --a) Program example int a = 1; int b = --a; ----- a= a-1 ie, a=1-1=0; o/p is b = 0 Post decrement In this method, the operator follows the operand (e.g., a- - which means a = a-1) Program example int a = 1; int b = a--; -------- a=a-1 = 1-1 int c = a; --------- c=0 Therefore o/p is c = 0 Logical NOT(!) It is used to reverse the logical state of an operand. If a condition is true, then Logical NOT operator will make it false. Eg: If x is true, then !x is false If x is false, then !x is true Address of operator(&) It gives an address of a variable. It is used to return the memory address of a variable. Eg: ptr = &a; o/p will be : address of a is copied to the location ptr. sizeof() This operator returns the size of its operand, in bytes. Eg: float n = 0; printf(“ The size of the variable n is:”, sizeof(n)); Output is n =4; because the size of float is 4 Binary Operators Binary Operator: are operators that act upon two operands to produce a new value. Eg: 10a+12b Types of Binary Operators Arithmetic Operators Relational Operators Logical Operators Assignment Operators Bitwise Operators Arithmetic Operators These are the operators used to perform arithmetic/mathematical operations on operands. There are five arithmetic operators, +, -, *, I, and %, which respectively represent the processes of addition, subtraction, multiplication, division, and modulus. The modulus operator (%) gives the remainder when one integer is divided by another integer. Types of Arithmetic operators Addition Subtraction Multiplication Division Modulus - to perform remainder *Relational Operators Relational operators are also known as comparison operators used to checking the relation between two operands. They are used to compare two operands. The result of a relational operation is a Boolean value that can only be true or false according to the result of the comparison. Types of Relational Operators *== Equal to operator: Represented as ‘==’, It is used to check for equality. The equal to operator checks whether the two given operands are equal or not. If so, it returns true. Otherwise it returns false. For example, 5==5 will return true. != Not equal to operator: Represented as ‘!=’, the not equal to operator checks whether the two given operands are equal or not. If not, it returns true. Otherwise it returns false. It is the exact boolean complement of the ‘==’ operator. For example, 5!=5 will return false. > Greater than operator: Represented as ‘>’, the greater than operator checks whether the first operand is greater than the second operand or not. If so, it returns true. Otherwise it returns false. For example, 6>5 will return true. < Less than operator: Represented as ‘=’, the greater than or equal to operator checks whether the first operand is greater than or equal to the second operand. If so, it returns true else it returns false. For example, 5>=5 will return true. 5>=6