constants numeric constants integer constants floating point constants character constants...

Download Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational

Post on 03-Jan-2016

212 views

Category:

Documents

0 download

Embed Size (px)

TRANSCRIPT

PowerPoint Presentation

Quantities whose values do not change during the execution of the program are called Constants.BackA Variable is a memory location that is identified by a name to store an item of data of particular types.BackConstants Numeric ConstantsInteger ConstantsFloating Point Constants Character Constants

Constants are quantities whose values do not change during program execution.They may be numeric or character.

Integer Constants are a string of digits that does not include commas, decimal point etc. it can be positive, negative or unsigned. 654, -987, 0r +34 etc.Floating point constants are also real constants. They are used to represent values that are measured. For example 564.78 etc 5.6478 x 102Character constants is one of the symbols in the C character set. For example +, ; , =, - , etc.Variables: are symbolic name used to refer to a quantity. For example A=L X W

A variable is just a named area of storage that can hold a single value (numeric or character). The C language demands that you declare the name of each variable that you are going to use and its type, or class, before you actually try to do anything with it.

The Programming language C has two main variable typesLocal VariablesGlobal Variables

Rules for constructing variable names

1) A Variable name consists of any combination of alphabets, digits and underscores. Some compiler allows variable names whole length could be up to 247 characters. Still it would be safer to stick to the rule of 31 characters. Please avoid creating long variable name as it adds to yourtyping effort.

2) The first character of the variable name must either be alphabet or underscore. It should not start with the digit.

3) No commas and blanks are allowed in the variable name.

4) No special symbols other than underscore are allowed in the variable name.Fundamental data typesWhen programming, we store the variables in our computer's memory, but the computer has to know what kind of data we want to store in them, since it is not going to occupy the same amount of memory to store a simple number than to store a single letter or a large number, and they are not going to be interpreted the same way. The memory in our computers is organized in bytes.

Declaring a variable tells the computer its name and its type.

Declaration of a variable is preceded by its type qualifier for example int, float, char, long, double etc.int a; float c; char lb; etc. %d %f %cNameDescriptionSizeRangecharCharacter or small integer.1 bytesigned: -128 to 127unsigned: 0 to 255short int(short)Short Integer.2 bytessigned: -32768 to 32767unsigned: 0 to 65535intInteger.4 bytessigned: -2147483648 to 2147483647unsigned: 0 to 4294967295long int(long)Long integer.4 bytessigned: -2147483648 to 2147483647unsigned: 0 to 4294967295floatFloating point number.4 bytes+/- 3.4e +/- 38 (~7 digits)double8 bytes+/- 1.7e +/- 308 (~15 digits)long double8 bytes+/- 1.7e +/- 308 (~15 digits)

The following program demonstrates the use of integer variables.It calculates how many apples will be divided amongst children and how many will be left over.

# include void main(void){int apples=20;int children=6;int perchild, leftover;perchild=apples/children;leftover=apples % children;printf(\n each child gets %d apples, perchild);printf(\n we have %d apples left over, leftover);}The following program demonstrates the use of floating point variables to calculate the area of a rectangle.

# include void main(void){float length, width, area;length=6.2;width=4.4;area=length*width;printf(\n the area of rectangle is %6.2f,area);}

%6.2f means there are 6 places maximum reserved for the variable are with two decimal places. ----.--

lengthrectanglewidtharea=length x widthINPUT AND OUTPUT STATEMENTSscanf( ) printf( )Program that reads two numbers from keyboard and prints their sum on screen.

# include void main(void){int num1,num2;printf(\n enter the first number:)scanf(%d, &num1);printf(\n enter the second number:)scanf(%d, &num2);printf(\n\n the sum of two numbers is: %d, num1+num2);}

enter the first number: 6enter the second number:78the sum of two numbers is: 84

A format specifier is used to control what format will be used by printf( ) to print out a particular variable.Format specifierCharacters matched%cany single character%dSigned decimal integer%uUnsigned decimal integer%ooctal integer%xhexa integer%ffloating point number%sString, sequence of characters%eExponential notationWe can modify our last program with by using the input (scanf) statement.

# include void main(void){int apples, children, perchild, leftover;printf(\n enter the no of apples:)scanf(%d, &apples);printf(\n enter the no of children:)scanf(%d, &children);perchild=apples/children;leftover=apples % children;printf(\n each child gets %d apples, perchild);printf(\n we have %d apples left over, leftover);}Program: Write a program that prompts the user to enter weight in kilograms, converts it into pounds and prints result.

# include # include void main(void){float kgs,lbs;clrscr( );printf(\n please enter your weight in kilograms:);scanf(%f, &kgs);lbs=kgs*2.2;printf(%6.2f kilograms = %6.2f pounds, kgs, lbs);getch( );}The following program uses the single character format specifier, %c, for printing characters.

# include void main(void){char ch1, ch2;ch1=a;ch2=b;printf(\n the sample characters are %c and %c, ch1,ch2);}

Outputthe sample characters are a and bToday, C is used for writing all kindsof application programsIf we want to print the above message, we can use the program.

#include void main(void){printf(Today, C is used for writing all kinds);printf(of application programs);}Now the result will not be as desired it will be in single line asToday, C is used for writing all kinds of application programsIf we want to print result in two lines we will have to add an escape sequence \n.printf(Today, C is used for writing all kinds\n);printf(of application programs);Now the result will be as such,Today, C is used for writing all kindsof application programs

The combination of \n is known as an escape sequence.The backslash \ indicates that a special effect is needed and the character following the backslash specifies what to do.The following are commonly used escape sequences.

\nTo issue a new line\tTo issue a tab character, tab over 8 characters\bTo backspace\fTo form feed\To print single quote\To bring double quote\\To print backslashExpressionsExpressions consist of constants and variables combined together with operators. Commonly used operators are:Arithmetic operators+ - * / %Assignment operator= Relational operator== != < > = Logical operators! (not) && (and) (or)Increment and decrement operators ++ - -Arithmetic operators+ - * / %Precedence level is BEDMASAnarithmetic expressionis anexpressionthat results in a numeric value. Parenthesesmay be used to control the order in which the operators are applied. If you don't use parentheses, operations with higherprecedenceare done first.OperatorDescription A=10, B=20Example+Adds two operandsA + B will give 30-Subtracts second operand from the firstA - B will give -10*Multiply both operandsA * B will give 200/Divide numerator by denomenatorB / A will give 2%Modulus Operator and remainder of after an integer divisionB % A will give 0Assignment operator= =Simple assignment operator, Assigns values from right side operands to left side operandC = A + B will assign value of A + B into C+=Add AND assignment operator, It adds right operand to the left operand and assign the result to left operandC += A is equivalent to C = C + A-=Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operandC - = A is equivalent to C = C - AOperatorRelational Operators DescriptionExample==Checks if the value of two operands is equal or not, if yes then condition becomes true.(A == B) is not true.!=Checks if the value of two is equal or not, if values are not equal then condition is true.(A != B) is true.>Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.(A > B) is not true.=Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true.(A >= B) is not true.

Recommended

View more >