490 likes | 707 Views
What is C?. C is a programming language developed at AT& T’s Bell Laboratories of USA in 1972. It was designed written by a Dennis Ritchie. C seems so popular it is reliable, simple & easy to use. C has been already superceded by languages like C++,C# and Java. CHARACTERISTICS OF C.
E N D
What is C? C is a programming language developed at AT& T’s Bell Laboratories of USA in 1972. It was designed written by a Dennis Ritchie. C seems so popular it is reliable, simple & easy to use. C has been already superceded by languages like C++,C# and Java.
CHARACTERISTICS OF C • C has a variety of language characteristics, Some of the most significant characteristics of the languages are listed below: 1. SIZE OF LANGUAGE:C is an externally small language does not have any built-in input/output capabilities . C implementations include standard libraries for input/output, string manipulations and arithmetic operations is characterized by the ability to write concise source programs program use and extensive usage of function calls programs are highly portable. 2.MODERN CONTROL STRUCTURES:C contains all the control structures expected in a modern language. For loops, if-else constructs, case (switch) statements, and while loops are all part of the language. 3. BITWISE CONTROL: To perform systems programming, it is frequently necessary to manipulate objects at the bit level provides variety of operators for bitwise manipulation of data.
CURRENT USES OF C • C is used for system applications and it is language of choice most UNIX users. • System programs include: Operating System, Interpreters,Editors,Assembly Programs, Compilers. • The C language is used for developing • Database Systems • Graphics Packages • Spread Sheets • Word Processors • Office Automations • Scientific & Engineering Applications
Getting Started with C • Learning C is simple & easier. • How to write a program in C,we must know alphabets, numbers and special symbols are used in C. Steps in learning English Language: Steps in learning C: Alphabets Words Sentences Paragraphs Alphabets Digits Special Symbol Constants,Variables,keywords Instructions Program
Structure of C Program • Every C program consists of one or modules called functions. One of the functions must be called main. • The program will always begin by executing the main function, which may access other functions. • Each function must contain: 1.A function heading, which consists of the function name, followed by optional list of arguments, enclosed in parentheses. 2.A list of argument declarations, if arguments are included in the heading. 3.A compound statement, which comprises the remainder of the function.
The format of C program given below, void main() { variable declaration; program statements; } Variable Declaration • All variable used in C language programs must declared C variable declarations include the name of the variable and its types. Program Statements • Program statements or executable statements must have variable declarations. An executable statement is an expression followed by semicolon or a control construct such as IF or an WHILE statement. • Most program and supporting functions contain program comments whose purpose the program more understandable. Program comments may contain any message starting with the charter sequence “/* and ending with the sequence “*/”.
C CHARACTER SET • C uses the uppercase letters A to Z. • the lowercase letters a to z • the digit 0 to 9 • some special characters to form basic program elements(e.g., constants variables, operators, expression) • The special characters are listed below: ! * + \ ” < # ) = ; } >^ ( ] , { ? & - [ : ‘ /% _ = = ~ . (blank)<>. • C. also uses some combinations of these characters such as \t, \n to represent special conditions such as horizontal tab and newline Respectively. • character combinations are called escape sequences. Each escape sequence represents a single character, it is a combination of two or more characters.
IDENTIFIERS AND KEYWORDS • Identifiers are names given to various elements of a program, such as variables, functions and arrays. • Identifiers consist of digits and letters, in any order but the rule is first character should be a letter. • Anyone can use both uppercase and lowercase letters. But uppercase and lowercase letters are not having same meaning e.g. RAM & ram are not same. • The underscore character (_) can also be included and is considered to be a letter. • Identifier can start with underscore character, though this is not a good programming practice. • valid identifiers: She, Z02, computer_02, _wind, Classes, areal, interest_rate, PERIOD. • not valid identifiers:2nd, “y”, ship-no, flag error.
KEYWORDS • Unlike identifiers, keywords are certain reserved words that have, standard, predefined meanings in C. • These keywords can be used only for their intended purpose, they cannot be used as programmer-defined identifiers. • The standard keywords are listed below: auto extern size of break float static case for struct const typedef if switch char goto unsigned default long continue int union void do register volatile double return while else short signed enum.
CONSTANTS • Constant in C refers to fixed values that do not change during the execution of a program. • C supports four types of constants. They are integer, character, string & floating-point constants. • The numeric-type constants must follow the following rules: 1.Commas and blank spaces cannot be included within the constant. 2.The constant can be preceded by a minus(-) sign if desired. 3.The value of a constant cannot exceed specified minimum and maximum bounds. For each type of constant, these bounds will vary from one C compiler to another.
An integer constant refers to a sequence of digits. There are three types of integers, namely, decimal, octal and hexadecimal. • A decimal integer constant can consist of any combination of digits taken from the set 0 through 9, proceeded by an optional - or + sign. • Various examples of decimal integer constants are: 123, – 345, 0, 5324, +62 • An octal integer constant can consist of any combination of digits taken from the set 0 through 7, with a leading 0. • Some examples of octal integers are: 0, 01, 0456, 05555. • A hexadecimal integer constant must begin with either 0x or 0x.It can then be followed by any combination of digits taken from the sets 0 through 9 and a through 7. • Some examples of hexadecimal integers are: 0x2, 0xgf, 0xbc5, 0x
Character constant is a single character, enclosed in apostrophes (single quotation marks). • Some of the valid character constants are: ‘A’, ‘y’ ‘4’ ‘$’. • Each character constant has its equivalent ASCII value like ‘A’ has 65, ‘y’ has 121 ‘4’ has 52 & so on. • Several character constants, expressed in terms of escape sequences are:‘\n’ ‘\t’ ‘\b’ ‘\\’ ‘\’. • The escape sequence \f represents the null character which is used to indicate the end of a string. • String constants consist of any number of consecutive characters enclosed in double quotation marks. Some of the string constants are “Red’’, “Mary Marry quite contrary”, “2*(J+3)/J” and ‘‘ ’’.
Constants Numeric Constants Character Constants Single Character Constants String Constants Real Constants Integer Constants
DATA TYPES • C supports different types of data, each of which may be represented differently within the computer’s memory. Data type Description Typical memory requirements int integers quantity 2 bytes or 1 word char simple character 1 byte float floating-point number 1 word (4 bytes) double double-precision floating 2 words (8 bytes) -point number • Some basic data types can be augmented by using the data type qualifiers short, long, signed & unsigned. For example, integer quantities be defined as short int,long int or unsigned int. • Floating point number sometimes are referred to as real numbers.
Differences between floating point numbers and integers: 1.Integer include only whole numbers, but floating point numbers can be either whole or fractional. 2. Integers are always exact, whereas floating point numbers sometimes can lead to loss of mathematical precision 3. Floating point operations are slower in execution and often occupy more memory than integer operations. 4. The char type is used to represent individual characters. char data type will permit a range of values extending from 0 to 255.
Data Types Character Data Type Integer Data Type Floating Point Data Type
VARIABLES • Variable is a name that can be used to store data value. A variable can have only one value assigned to it any given time during the execution of the program. EXAMPLE:MARKS,AVERAGE,TOTAL Rules for declaring variables • Identifiers are used to name of variable • A variable name consists of a sequence of letters or digits • First letter start with alphabet Valid variable name:student_name,emp_name,mark Invalid variable name:3a,salary#,student-name
DECLARATION OF VARIABLES • Declaration of variables in 2 manners, 1. Primary type declaration 2. User-defined type declaration SYNTAX Data-type variable name1,variable name2…variable namen; EXAMPLE:intmarks;,ntrollno,average;,double exam-fees; • User-defined Type Declaration C supports a feature known as “type definition” that permit users to define an identifier represent an existing data type. SYNTAX: typedef type identifer Where, typedef->keyword Type->refers to an data type Identifier->refers to the name of variable name EXAMPLE typedef float average; typedefint block; They can be classified into further manner as, block unit1,unit2; • The main advantage of typedef is can create meaningful data type for increasing the readability of the program.
A SAMPLE C PROGRAM #include<stdio.h> void main() { printf(“Wecome to C Programming:\n”); } Sample Program Output Welcome to C Programming
EXAMPLE 2 #include<stdio.h> void main() { printf(“Programming in C is easy \n”); printf(“and also Programming in C++.”); } Sample Program Output Programming in C is easy and also Programming in C++.
EXAMPLE 3 #include<stdio.h> void main() { printf(“Hello…\n..oh my \n when do I stop?. \n”); } Sample Program Output Hello… ..oh my …when do I stop?
OPERATORS • C supports different type of operators. In operators is a symbol that operates certain mathematical or logical operations. • These operators are being: 1. Arithmetic operators 2. Unary operators 3. Relational operators 4. Logical operators 5. Assignment operators 6. Increment and Decrement operators 7. Conditional operators 8. Bitwise operators 9. Comma operators 10. Special operators
ARITHMETIC OPERATORS • The arithmetic operators can perform arithmetic operations and can be classified into unary and binary arithmetic operators. Unary Arithmetic Operators • The use of unary ± does not serve any purpose by default. However it can be used as follows: A=+50 • The unary minus operator can be used to negate the value of variable. It is also used specify a negative number. Here a minus sign (-) is prefixed to the number. EXAMPLE int x=10 int y=-x The value of y=10
EXAMPLE If a and b two integer variables whose values 5 and 10 respectively, then
UNARY OPERATORS • Unary operators are another type of operators that act upon a single operand to produce a value. • The unary minus is minus sign which precedes a numerical constant a value or an expression.Examples are -5,-(a+b),-0.5,-root2.
RELATIONAL OPERATORS • The relational operators are used to compare arithmetic, logical and character expressions. • We can compare two similar values and depending on their relation take some decision. These comparisons can be done with the help of relational operators. There are six relational operators in C
LOGICAL OPERATORS • C language logical operators allow a programmer to combine simple relational expression to form complex expressions by using AND, OR and NOT. • Logical AND: logical AND when two expressions are added the resulting expression will be true only if both sub expressions are true. EXAMPLE:if(number<0 && number>100) • Logical OR:A logical expression is evaluated from any one of the sub expressions are true. EXAMPLE: if(number<0 || number>100) • Logical NOT The logical !(NOT) operator takes single expression and evaluates to true(1) if the expression is false(zero) and evaluates to false(zero) if the if the expression is true.
ASSIGNMENT OPERATORS • In C language assignment operator = (Equal Sign). • Assignment expressions are written in the form Identifier=Expression • The arithmetic operators are =, +=,-=,*=,/,/=,%=. EXAMPLE x+=y is equal to x=x+y INCREMENT AND DECREMENT OPERATORS • C provides two operators for incrementing and decrementing variables. • The increment (++) and decrement (--) operator may be used either as prefix operators (before the variable, as in ++x),or postfix operators(after the variable++). EXAMPLE x=5 then x=x+1, sets x o 5
CONDITIONAL OPERATORS • Conditional operator (? :) is ternary operator to construct conditional expressions, replace sing if-else construct. Syntax:Expression1? Expression2:Expression3 • The conditional (?) works as done similar to the ternary operator. EXAMPLE a=20; b=25; x= (a>b)? a:b BITWISE OPERATORS • Bitwise operator operators on each bit of data. These operators are used for testing the bits or shifting them either left or right.
Bitwise AND: Consider the statement c=a+b a:0000 0000 0000 1010 b:0000 0000 0000 0101 a+b:0000 0000 0000 0000 Bitwise OR: Consider the statement c=a|b a:0000 0000 0000 1010 b:0000 0000 0000 0101 a+b:0000 0000 0000 1111 Bitwise XOR:Consider the statement c=a^b a:0000 0000 0000 1010 b:0000 0000 0000 0101 a+b: 0000 0000 0000 1111 Left shift operator: The left shift operator to use the symbol << is a binary operator. EXAMPLE c=a<<5 To apply left shift << for a, a: 0000 0000 0000 0101 After executing the left shift operator one zero are inserted in to right.
Right shift operator: The right shift operator to use the symbol >> is a binary operator. Example:=a>>2 To apply left shift >> for a, a: 0000 0000 0000 0010 after right shift by 1 places a,a>>5 a:0000 0000 0000 0001 after executing right shift operator one zero are inserted into left. Bitwise complement operator • The bitwise complement operator (~) is a unary operator. It gives the value complementing each bit of the operand. Consider the statement c=~b • Bitwise complements (~) are, b: 0000 0000 0000 0101 • after bitwise complement that is ~b c:1111 1111 1111 1010
COMMA OPERATORS • The set of expressions separated by using the operator are called comma operator. • If any expression are represented are evaluated from left to right. SPECIAL OPERATORS • The sizeof() operator is denoted as special operators when used an operand it returns the number of bytes the operand occupies. EXAMPLE x=sizeof(m1) y=sizeof(float) z=sizeof(#ol)
EXPRESSION • In C language the expressions are evaluated using assignment operators. • When expression is executed that is evaluated in right side and then the evaluated result is stored in left-side of variable name. EXAMPLE x=(a*b)/b y=(a*b)+(b*a) z=(m/n)/(x+a-b)
PRECEDENCE OF ARITHMETIC OPERATORS • C operators in order of precedence (highest to lowerest) is listed given below, (i)In Arithmetic expression is represented without parentheses will be evaluated from left to right. (ii)In expression are represented with two or more parentheses appear the expression is evaluated with left-most parentheses of expression after it evaluated with right-most. (iii)The next highest priority are *,/ and %. (iv)The last priority that is lowerest priority +.-.
TYPE CONVERSIONS IN EXPRESSIONS In C support two types of conversions expressions they are, 1.Implicit type conversion 2. Explicit type conversion
Implicit Type Conversion • During evaluation automatic type conversion are known as implicit type conversion. • In C support automatically type conversion without any intermediate value that is can be evaluated without any loosing of expression. • Automatic conversion by the compiler. Explicit Type Conversion • The Explicit type conversion which explicitly defined with a program (instead of being done by a complier for implicit type conversion). (type-name) expression where, type-name->is the data types expression->it may be constant, variable or expression EXAMPLE 1 x=(int)3.4
OPERATOR PRECEDENCE AND ASSOCIATIVEITY • In C contains many operators operator precedence is the order in which compiler groups operands with operators. • The operator are given the higher level of precedence are evaluated first. The operator of the same precedence is evaluated either from ‘left to right’ or ‘right to left’ depending on the level. This is known as the associatively property of an operator.
Hierarchy of operators in ‘C are summarized below: 1.Any expression within parenthesis is first evaluated, if more than one pair of parenthesis are present, the inner parenthesis is evaluated first. 2.Unary operators are evaluated first in an expression. 3.Thrn priority is given for multiplication and division. 4.Then subtraction and addition are performed. 5.Ten relational operations are performed. 6.Then equality checking is performed. 7.Then logical operations are performed. 8.Then the conditions are checked. 9.Finally the assignment operation is carried out.
EXAMPLE • Consider the variable x,y and z are integer variables having the values 2,3 and 4 respectively. x*=-2*(y+z)/3 The given expression, x=x*(-2*(y+z)/3) Step 1 y+z=3+4=7 Step 2 -2*(y+z)=-2*7=-14 Step 3 -2*(y+z)/3=-14/3=-4 Step 4 2*-4=-8 x=8
LIBRARY FUNCTIONS • C language provides buildin functions or intrinsic functions called Library functions. The complier itself evaluates these functions. • Table shows some popular library functions.
Example 1: The absolute value of ‘x’ is the value of ‘x’ without sign. The abs() function evaluates the absolute value of an integer quantity. abs(-75) Here the absolute value of -75 is evaluated as 75. Example 2: Where the argument ‘x’ may be integer or float. This evaluates the ceiling of the value of x.Ceiling of a number ‘x’ is the smallest integer greater than or equal to ‘x’. Example 3: This evaluates the trigonometric sine value of a real or integer quantity given in radian measure. sin(30) Evaluates 0.5
Data Input Output Functions • The following are the formatted Input/Output statements: scanf() Function The scanf() function is used to read information from the standard input device(keyboard),scanf() function starts with a string argument and may contain additional arguments.