1 Objectives • To understandthe constructs of C Language. • To develop C Programs using basic programming constructs • To develop C programs using arrays and strings • To develop modular applications in C using functions • To develop applications in C using pointers and structures • To do input/output and file handling in C C PROGRAMMING
3 Background • C wasoriginally developed in the 1970s, by Dennis Ritchie at Bell Telephone Laboratories, Inc. • C is a High level , general purpose structured programming language. Instructions of C consists of terms that are very closely same to algebraic expressions, consisting of certain English keywords such as if, else, for, do and while • C contains certain additional features that allows it to be used at a lower level , acting as bridge between machine language and the high level languages. • This allows C to be used for system programming as well as for applications programming
4.
4 Background C is a Cis a structured programming language. It is structured programming language. It is considered a high-level language considered a high-level language because it allows the because it allows the programmer to concentrate on the problem at hand programmer to concentrate on the problem at hand and not worry about the machine that the program and not worry about the machine that the program will be using. That is another reason why it is used by will be using. That is another reason why it is used by software developers whose applications have to run on software developers whose applications have to run on many different hardware platforms. many different hardware platforms.
5.
5 C Programs It's timeto write your first C program. It's time to write your first C program. Structure of a C Program Your First C Program Comments The Greeting Program Topics discussed in this section: Topics discussed in this section:
12 Applications of CProgramming • Operating Systems. The first operating system to be developed using a high-level programming language was UNIX, which was designed in the C programming language. • Embedded Systems • GUI • New Programming Platforms • Google • Mozilla Firefox and Thunderbird. • MySQL. • Compiler Design.
13.
13 Most Important Featuresof C Language • Simple and Efficient. • Fast. • Portability. • Extensibility. • Function-Rich Libraries. • Dynamic Memory Management. • Modularity With Structured Language. • Mid-Level Programming Language
14.
Computer Science: AStructured Programming Approach Using C 14 • C language consist of some characters set, numbers and some special symbols. The character set of C consist of all the alphabets of English language. C consist of • Alphabets a to z, A to Z • Numeric 0,1 to 9 • Special Symbols {,},[,],?,+,-,*,/,%,!,;,and more • The words formed from the character set are building • blocks of C and are sometimes known as tokens. These tokens represent the individual entity of language. The following different types of token are used in C 1) Identifiers 2)Keywords 3)Constants 4) Operators 5)Punctuation Symbols The Character set of ‘C’
15.
15 Identifiers One feature presentin all computer languages is the One feature present in all computer languages is the identifier. Identifiers allow us to name data and other identifier. Identifiers allow us to name data and other objects in the program. Each identified object in the objects in the program. Each identified object in the computer is stored at a unique address. computer is stored at a unique address.
Identifiers A 'C'program consist of two types of elements , user defined and system defined. Idetifiers is nothing but a name given to these elements. An identifier is a word used by a programmer to name a variable , function, or label. identifiers consist of letters and digits, in any order, except that the first charecter or lable. Identifiers consist of letters and digits if any order,except that the first charecter must be letter. Both Upper and lowercase letters can be used
Keywords Keywords arenothing but system defined identifiers. Keywords are reserved words of the language. They have specific meaning in the language and cannot be used by the programmer as variable or constant names C is case senitive, it means these must be used as it is 32 Keywords in C Programming auto double int struct break else long switch case enum register typedef char extern return union const float short unsigned continue for signed void default goto sizeof volatile do if static while
21.
Variables A variableis nothing but a name given to a storage area that our programs can manipulate. Each variable in C has a specific type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable. The name of a variable can be composed of letters, digits, and the underscore character. It must begin with either a letter or an underscore. Upper and lowercase letters are distinct because C is case- sensitive. There are following basic variable types − Type Description char Typically a single octet(one byte). This is an integer type. int The most natural size of integer for the machine. float A single-precision floating point value. double A double-precision floating point value. void Represents the absence of type.
22.
Constants A constantis a value or an identifier whose value cannot be altered in a program. For example: 1, 2.5, As mentioned, an identifier also can be defined as a constant. eg. const double PI = 3.14 Here, PI is a constant. Basically what it means is that, PI and 3.14 is same for this program. Integer constants A integer constant is a numeric constant (associated with number) without any fractional or exponential part. There are three types of integer constants in C programming: decimal constant(base 10) octal constant(base 8) hexadecimal constant(base 16)
23.
Constants Floating-point constants Afloating point constant is a numeric constant that has either a fractional form or an exponent form. For example: 2.0,0.0000234,-0.22E-5 Character constants A character constant is a constant which uses single quotation around characters. For example: 'a', 'l', 'm', 'F' String constants String constants are the constants which are enclosed in a pair of double-quote marks. For example: "good" ,"x","Earth is roundn"
24.
Escape Sequences Sometimes, itis necessary to use characters which cannot be typed or has special meaning in C programming. For example: newline(enter), tab, question mark etc. In order to use these characters, escape sequence is used. For example: n is used for newline. The backslash ( ) causes "escape" from the normal way the characters are interpreted by the compiler.Escape Sequences Character b Backspace f Form feed n Newline r Return t Horizontal tab v Vertical tab Backslash ' Single quotation mark " Double quotation mark ? Question mark 0 Null character
33 Variables Variables are namedmemory locations that have a type, Variables are named memory locations that have a type, such as integer or character, which is inherited from such as integer or character, which is inherited from their type. The type determines the values that a variable their type. The type determines the values that a variable may contain and the operations that may be used with may contain and the operations that may be used with its values. its values. Variable Declaration Variable Initialization Topics discussed in this section: Topics discussed in this section:
Computer Science: AStructured Programming Approach Using C 37 When a variable is defined, it is not initialized. We must initialize any variable requiring prescribed data when the function starts. Note Note
38.
Computer Science: AStructured Programming Approach Using C 38 PROGRAM 2-2 Print Sum of Three Numbers
39.
Computer Science: AStructured Programming Approach Using C 39 PROGRAM 2-2 Print Sum of Three Numbers (continued)
40.
Computer Science: AStructured Programming Approach Using C 40 PROGRAM 2-2 Print Sum of Three Numbers (continued)
41.
41 Constants Constants are datavalues that cannot be changed Constants are data values that cannot be changed during the execution of a program. Like variables, during the execution of a program. Like variables, constants have a type. In this section, we discuss constants have a type. In this section, we discuss Boolean, character, integer, real, complex, and string Boolean, character, integer, real, complex, and string constants. constants. Constant Representation Coding Constants Topics discussed in this section: Topics discussed in this section:
Computer Science: AStructured Programming Approach Using C 45 FIGURE 2-13 Some Strings
46.
Computer Science: AStructured Programming Approach Using C 46 FIGURE 2-14 Null Characters and Null Strings
47.
Computer Science: AStructured Programming Approach Using C 47 Use single quotes for character constants. Use double quotes for string constants. Note Note
48.
Computer Science: AStructured Programming Approach Using C 48 PROGRAM 2-3 Memory Constants
49.
Computer Science: AStructured Programming Approach Using C 49 PROGRAM 2-3 Memory Constants (continued)
50.
Operators in C:Anoperator is a symbol which operates on a value or a variable. For example: + is an operator to perform addition. C programming has wide range of operators to perform various operations. For better understanding of operators, these operators can be classified as: Arithmetic Operators Increment and Decrement Operators Assignment Operators Relational Operators Logical Operators Conditional Operators Bitwise Operators Special Operators
51.
Arithmetic Operator Operator Meaningof Operator + addition or unary plus - subtraction or unary minus * multiplication / division % remainder after division ( modulo division)
52.
Increment and Decrement Operators 1.C programming has two operators increment ++ and decrement -- to change the value of an operand (constant or variable) by 1. 2. Increment ++ increases the value by 1 whereas decrement -- decreases the value by 1. 3. These two operators are unary operators, meaning they only operate on a single operand. eg. int a=10, b=100 ++a = 11 --b = 99
53.
C Assignment Operators An assignment operator is used for assigning a value to a variable. The most common assignment operator is = Operator Example Same as = a = b a = b += a += b a = a+b -= a -= b a = a-b *= a *= b a = a*b /= a /= b a = a/b %= a %= b a = a%b
54.
C Relational Operators A relational operator checks the relationship between two operands. If the relation is true, it returns 1; if the relation is false, it returns value 0. Relational operators are used in decision making and loops. Operator Meaning of Operator Example == Equal to 5 == 3 returns 0 > Greater than 5 > 3 returns 1 < Less than 5 < 3 returns 0 != Not equal to 5 != 3 returns 1 >= Greater than or equal to 5 >= 3 returns 1 <= Less than or equal to 5 <= 3 return 0
55.
55 C Relational Operators <,<=, > >=, ==, != are the relational operators. The expression operand1 relational-operator operand2 takes a value of 1(int) if the relationship is true and 0(int) if relationship is false. Example int a = 25, b = 30, c, d; c = a < b; d = a > b; value of c will be 1 and that of d will be 0.
56.
Logical Operators Computer Science:A Structured Programming Approach Using C 56 &&, || and ! are the three logical operators. expr1 && expr2 has a value 1 if expr1 and expr2 both are nonzero. expr1 || expr2 has a value 1 if expr1 and expr2 both are nonzero. !expr1 has a value 1 if expr1 is zero else 0. Example If(marks >= 40 && attendance >= 75 ) grade = ‘P’ if(marks < 40 || attendance < 75 ) grade = ‘N’
59 Type Conversions Theoperands of a binary operator must have a the same type and the result is also of the same type. Integer division: c = (9 / 5)*(f - 32) The operands of the division are both int and hence the result also would be int. For correct results, one may write c = (9.0 / 5.0)*(f - 32) In case the two operands of a binary operator are different, but compatible, then they are converted to the same type by the compiler. The mechanism (set of rules) is called Automatic Type Casting. c = (9.0 / 5)*(f - 32) It is possible to force a conversion of an operand. This is called Explicit Type casting. c = ((float) 9 / 5)*(f - 32)
60.
60 Automatic Type Casting 1.char and short operands are converted to int 2. Lower data types are converted to the higher data types and result is of higher type. 3. The conversions between unsigned and signed types may not yield intuitive results. 4. Example float f; double d; long l; int i; short s; d + f f will be converted to double i / s s will be converted to int l / i i is converted to long; long result Hierarchy Double float long Int Short and char
61.
61 Explicit Type Casting The general form of a type casting operator is (type-name) expression It is generally a good practice to use explicit casts than to rely on automatic type conversions. Example C = (float)9 / 5 * ( f – 32 ) float to int conversion causes truncation of fractional part double to float conversion causes rounding of digits long int to int causes dropping of the higher order bits.
5-83 Repetition in Programs •In most software, the statements in the program may need to repeat for many times. – e.g., calculate the value of n!. – If n = 10000, it’s not elegant to write the code as 1*2*3*…*10000. • Loop Loop is a control structure that repeats a group of steps in a program. – Loop body Loop body stands for the repeated statements. • There are three C loop control statements: – while while, for for, and do-while do-while.
#26 wchar_t is a wide character: The increased datatype size allows for the use of larger coded character sets. Width is compiler specific (not portable).