what is c?

47
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.

Upload: deana

Post on 25-Feb-2016

35 views

Category:

Documents


2 download

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: What is C?

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.

Page 2: What is C?

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.

Page 3: What is C?

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

Page 4: What is C?

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

Page 5: What is C?

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.

Page 6: What is C?

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 “*/”.

Page 7: What is C?

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 newlineRespectively. character combinations are called escape sequences. Each escape

sequence represents a single character, it is a combination of two or more characters.

Page 8: What is C?

IDENTIFIERS AND KEYWORDS Identifiers are names given to various elements of a program, suchas 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 uppercaseand 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.

Page 9: What is C?

KEYWORDSUnlike 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 ifswitch char goto unsigned default long continue int union void do register volatile double return while else short signed enum.

Page 10: What is C?

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.

Page 11: What is C?

An integer constant refers to a sequence of digits. There are threetypes 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, +62An 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.Itcan then be followed by any combination of digits taken from thesets 0 through 9 and a through 7.Some examples of hexadecimal integers are:

0x2, 0xgf, 0xbc5, 0x

Page 12: What is C?

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’ has65, ‘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 usedto 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 ‘‘ ’’.

Page 13: What is C?

Constants

Numeric Constants Character Constants

Integer Constants Real Constants Single Character

Constants

String Constants

Page 14: What is C?

DATA TYPES

C supports different types of data, each of which may be representeddifferently within the computer’s memory.Data type Description Typical memory requirementsint integers quantity 2 bytes or 1 wordchar simple character 1 bytefloat 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.

Page 15: What is C?

Differences between floating point numbers and integers:1.Integer include only whole numbers, but floating point numberscan be either whole or fractional.2. Integers are always exact, whereas floating point numbers sometimescan lead to loss of mathematical precision3. 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.

Page 16: What is C?

Data Types

Character Data Type Integer Data Type Floating Point Data Type

Page 17: What is C?

VARIABLESVariable 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,TOTALRules 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,markInvalid variable name:3a,salary#,student-name

Page 18: What is C?

DECLARATION OF VARIABLES

Declaration of variables in 2 manners,1. Primary type declaration2. User-defined type declarationSYNTAX Data-type variable name1,variable name2…variable namen;

EXAMPLE:int marks;,nt rollno,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 identiferWhere, typedef->keyword Type->refers to an data type Identifier->refers to the name of variable nameEXAMPLE typedef float average; typedef int 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.

Page 19: What is C?

A SAMPLE C PROGRAM

#include<stdio.h>void main(){

printf(“Wecome to C Programming:\n”);}

Sample Program OutputWelcome to C Programming

Page 20: What is C?

EXAMPLE 2

#include<stdio.h>void main(){

printf(“Programming in C is easy \n”);printf(“and also Programming in C++.”);

} Sample Program OutputProgramming in C is easyand also Programming in C++. 

Page 21: What is C?

EXAMPLE 3 #include<stdio.h>void main(){

printf(“Hello…\n..oh my \n when do I stop?. \n”);} Sample Program OutputHello…..oh my…when do I stop?

Page 22: What is C?

OPERATORSC supports different type of operators. In operators is a symbol that operates certain mathematical or logical operations. These operators are being:

1. Arithmetic operators2. Unary operators3. Relational operators4. Logical operators5. Assignment operators6. Increment and Decrement operators7. Conditional operators8. Bitwise operators9. Comma operators10. Special operators

Page 23: What is C?

ARITHMETIC OPERATORSThe arithmetic operators can perform arithmetic operations and can be classified into unary and binary arithmetic operators.Unary Arithmetic OperatorsThe use of unary ± does not serve any purpose by default. However it can be used as follows:

A=+50The 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

Page 24: What is C?

Operator Meaning+ Addition

- Subtraction

* Multiplication

/ Division

% Modulus operators(Remainder after integer division)

EXAMPLEIf a and b two integer variables whose values 5 and 10 respectively, then

Expression Valuea+b 15

a-b 7

a*b 50

a/b 2

a%b 1

Page 25: What is C?

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.Operator Meaning

- Unary minus

++ Increment operator

-- Decrement operator

sizeof Size of the operand

Page 26: What is C?

RELATIONAL OPERATORSThe 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

Operator Meaning

< Less than

> Greater than

<= Less than or equal to

>= Greater than or equal to

!= Not equal to

Page 27: What is C?

LOGICAL OPERATORSC 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 NOTThe 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.

Operator Meaning

&& Logical AND

|| Logical OR

! Logical NOT

Page 28: What is C?

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+yINCREMENT 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

Page 29: What is C?

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:bBITWISE OPERATORS Bitwise operator operators on each bit of data. These operators are used for testing

the bits or shifting them either left or right.Operator Meaning

& Bitwise AND

| Bitwise Or

^ Bitwise XOR

<< Shift Left

>> Shift Right

~ Bitwise Complement

Page 30: What is C?

Bitwise AND: Consider the statement c=a+b

a:0000 0000 0000 1010 b:0000 0000 0000 0101

a+b:0000 0000 0000 0000Bitwise OR: Consider the statement c=a|b

a:0000 0000 0000 1010 b:0000 0000 0000 0101 a+b:0000 0000 0000 1111Bitwise XOR:Consider the statement c=a^b a:0000 0000 0000 1010

b:0000 0000 0000 0101a+b: 0000 0000 0000 1111Left shift operator: The left shift operator to use the symbol << is a binary operator.EXAMPLE c=a<<5To apply left shift << for a, a: 0000 0000 0000 0101After executing the left shift operator one zero are inserted in to right.

Page 31: What is C?

Right shift operator: The right shift operator to use the symbol >> is a binary operator.Example:=a>>2To apply left shift >> for a,  a: 0000 0000 0000 0010after 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 operatorThe bitwise complement operator (~) is a unary operator. It gives the value complementing each bit of the operand.Consider the statement c=~bo Bitwise complements (~) are, b: 0000 0000 0000 0101o after bitwise complement that is ~b c:1111 1111 1111 1010 

Page 32: What is C?

COMMA OPERATORSThe set of expressions separated by using the operator are called comma operator. If any expression are represented are evaluated from left to right.SPECIAL OPERATORSThe 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)

Page 33: What is C?

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)/by=(a*b)+(b*a)z=(m/n)/(x+a-b)

Page 34: What is C?

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 +.-. 

Page 35: What is C?

TYPE CONVERSIONS IN EXPRESSIONS

In C support two types of conversions expressions they are,1.Implicit type conversion

2. Explicit type conversion

Page 36: What is C?

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

Page 37: What is C?

OPERATOR PRECEDENCE AND ASSOCIATIVEITYIn 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.

Page 38: What is C?

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.

Page 39: What is C?

Operator & Operation Associatively PriorityFunction call ()

Square brackets []Structure Operator ->

Dot Operator .

Left to Right 1

Unary plus +Unary minus –Increment ++Decrement –

Not Operator !Complement ~

Point Operator *Address Operator &

sizeof Operator sizeofType cast type

Right to Left 2

Multiplication *Division /

Modulo division %Left to Right 3

Page 40: What is C?

Operator & Operation Associatively PriorityAddition +

Subtraction - Left to Right 4

Left Shift <<Right Shift >> Left to Right 5

Less than <Less than or equal to <=

Greater than >Greater than or equal to >=

Left to Right 6

Equality ==Inequality != Left to Right 7

Bitwise AND & Left to Right 8

Bitwise XOR ^ Left to Right 9

Bitwise OR | Left to Right 10

Logical AND && Left tot Right 11

Logical OR || Left to Right 12

Conditional Operator ?: Right to Left 13

Assignment Operator { =,*=,-=,+=,^=,!=,<<=,>>=}

Right to Left 14

Comma Operator , Left to Right 15

Page 41: What is C?

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 

Page 42: What is C?

LIBRARY FUNCTIONSC language provides buildin functions or intrinsic functions called Library functions. The complier itself evaluates these functions.Table shows some popular library functions.

Functions Meaning

Sqrt(x) X

log(x) logex

abs(x) |x|

fabs(x) |x|

exp(x) Ex

pow(x,y) xy

ceil(x) rounding x to the next integer value

fmod(x,y) Returns the remainder of x/y

Page 43: What is C?

                        rand() generates  a positive random integer

srand(v) To initialize the random number generator

sin(x) sin x

cos(x) cos x

tan(x) tan x

toascii(x) Returns the integer value for the particular character

tolower(x) To convert the character to lowercase

toupper(x) To convert the character to upper case

Page 44: What is C?

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

Page 45: What is C?

UNIT II

Page 46: What is C?

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.

     Input                    Output

scanf() printf()

fscanf() fprintf()

Page 47: What is C?

Syntax scanf(“control string”,&var1,var2…&varn)Description The control String consists of character groups. Each

character group must begin with a percentage sign ‘%’.

Example int n;Scanf(“%d”,&n);