Presentation is loading. Please wait.

Presentation is loading. Please wait.

DATA TYPES There are four basic data types associated with variables:

Similar presentations


Presentation on theme: "DATA TYPES There are four basic data types associated with variables:"— Presentation transcript:

1 DATA TYPES There are four basic data types associated with variables:
STANDARD DATA TYPES OF C void  - valueless special purpose type Int integer: a whole number. Char - a single character. float - floating point value/a number with a fractional part.

2 VARIABLES Variable are named memory locations, which stores values that are going to change during the execution of a program. Every variable must be declared before assigning a value.

3 Type of variables declaration:
EXAMPLE : short int i,j,k; int m,n; long int p,q; unsigned short int u1,u2; unsigned int m1,m2; unsigned long int v1,v2,v3; char letter1, letter2; float sum,cost,rate; double phi,charge; long double ratio,mass,speed;

4 Integer number variables
An int variable can store a value in the range to No fractional part is allowed. To declare an int use the instruction: int variable name; For example: int a; int a declares that you want to create an int variable called a. To assign a value to our integer variable we would use the following C statement: a=10;

5

6 Character Variables To declare a variable of type character we use the keyword char. A single character stored in one byte. For example: char c; To assign, or store, a character value in a char data type, we use: Example: c='A‘ Notice that you can only store a single character in a char variable. 

7 Variable initialization:
A variable can be assigned a value while declaring them

8 COMMENT STATEMENT Comments are notes describing what a particular portion of your program does and how it does it. Comments make the program more readable and it is an important part of documentation. Text that are written between the starting symbol pair /* and the ending symbol pair */ forms a comment and it will be ignored by the compiler. There should not be any blank space between /and *. The /* and */ form a couple, but they need not be on the same line.

9 Comment Statement Examples:
/* This is a comment */ // This is a comment / *This is not a comment /

10 FORMATED INPUT/OUTPUT FUNCTIONS
A C program accepts data or output the result only through a file. The keyboard is considered as the standard input file. The monitor is considered as the standard output file. When we enter a data on the keyboard, the C program receives them. Everything entered into the C program through the keyboard must be in the form of a sequence of characters. The standard input file is buffered. Ie: The data are stored into a temporary memory location till the return key is pressed. The C program formatting instructions then interpret the sequence of characters into appropriate form.

11 printf() The format string is enclosed in a set of double quotation marks. The format string contains the text data to be printed and instructions for formatting the data. The instructions for formatting the data are specified by field specifiers. Each field specification begins with a percent sign (%).

12 printf (“My age is %d”,21);
Example: printf (“My age is %d”,21); Output: My age is 21

13 scanf() function scanf() is used to read the data supplied by the user and to transfer them to the variable names. The general syntax of scanf function is: scanf(format string, address list); scanf() requires that the variables in the address list be represented by their addresses. To specify an address, the variable name is prefix with the address operator, the ampersand (&). The ‘scanf’ function allows the user to enter the data through the standard input (keyboard), formats the data entered and assign them to variables.

14 scanf() scanf() function is used to format the input sequence of characters. Example: To read the following data: z scanf(“%d %d %d %c”,&a,&b,&c,&d);

15 FIELD SPECIFIER A field specifier must begin with a percentage sign.
A field specifier must have a conversion code. The general form of a format specifier is shown below.

16 EXAMPLE: %d (print as a decimal integer)
%6d (print as a decimal integer with a width of at least 6 wide) %f (print as a floating point) %4f (print as a floating point with a width of at least 4 wide) %.4f (print as a floating point with a precision of four characters after the decimal point) %3.2f (print as a floating point at least 3 wide and a precision of 2)

17

18 Example:

19 CONVERSION CODE The conversion codes are used to interpret the data value keyed in by the users and store them in the variable names. For example, a ‘%c’ field specifier specifies that the input data has to be interpreted as a character.

20

21 ARITHMETIC OPERATORS Integers, floating point numbers, and double precision numbers can be added, subtracted, multiplied or divided using operators called arithmetic operators. Operation Operator Example Addition + 2+3 = 5, = 5.3 = 7.5 Subtraction - 3-2 = 1, 3-2.3= 0.7 =0.7 Multiplication * 3*5=15, 3.1*5= 15.5 3.1*5.0=15.5 Division / 5/2 =2, 4/3=1 5.0/2=2.5 5.0/2.0=2.5 Remainder % 5%2=1 11%6=5

22 An expression contains only integer operands is called an integer expression.
An expression containing only floating-point operands is called a floating-point expression; the result is a double precision value. Although it is usually better not to mix integer and floating-point operands in an arithmetic operation, the data type of each operation is determined by the following rule: If all the operands are integer then the result is integer. If any operand is floating point or double precision then the result is double.

23 UNARY OPERATOR Increment and decrement operators
In C, ++ and -- are called increment and decrement operators respectively. Both of these operators are unary operators. ++ adds 1 to operand and -- subtracts 1 to operand respectively. Example: Let a=5 and b=10 a++; //a becomes 6 a--; //a becomes 5 ++a; //a becomes 6 --a; //a becomes 5

24 Difference between ++ and –- as postfix and prefix
When ++ is used as prefix (eg:++a), ++a will increment the value of a and then return it. if ++ is used as postfix (eg: a++), operator will return the value of operand first and then only increment it.

25 Example 1: /* program in c to implement postfix ++ operator */
#include <stdio.h> int main(void) { int a; a = 10; printf(“The value of a is %d \n”,a); printf(“The value of a++ is %d\n”, a++); printf(“The new value of a is %d\n”,a); return 0; } Results: 10 11

26 Example 2: /* program in c to implement prefix ++ operator */
#include <stdio.h> int main(void) { int a; a = 10; printf(“The value of a is %d\n”,a); printf(“The value of ++a is %d\n”, ++a); printf(“The new value of a is %d\n”,a); return 0; } Results: 10 11

27 RELATIONAL EXPRESSION
A relational expression consists of a relational operator connecting using two variables and/or constant operands. RELATIONAL OPERATOR OPERAND age >

28 Relational Operators Relational Operator Meaning Example < less than age <30 > greater than ht > 4.5 <= less than or equal to age <=3 >= greater than or equal to ht>=4 == equal to g ==2 != not equal to s != 3 Relational expressions are evaluated to yield only an integer value of ‘1’ or ‘0’. A condition true evaluates to ‘1’ false evaluates ‘0’

29 Example: if (grade >=60) printf (“passed”); if( !(gate1>=40))
printf("Block the gate");

30 (age <31) && (age>41)
LOGICAL OPERATORS: Logical Operator Meaning Example && and (age <31) && (age>41) || or (age > 21) || (sex ==1) ! not !(age <=3) Logical Operators Truth values: i) AND Function (&&) [Precedence Level : 5] X Y R

31 Truth Table for AND Function
X Y R = X&&Y 1 Representation :R = X && Y ii) OR Function (||) [Precedence Level : 4] X Y R

32 Truth Table for OR Function
X Y R = X|| y 1 Representation : R = X || Y iii) NOT Function: (!) [Precedence Level : 15] Truth Value of NOT Function X R = ! X 1 Representation : R = ! X

33 int i = 7; f loat f = 5.5; ================================================= Expression Interpretation Value f > 5 true !(f > 5) false i <= 3 false !(i <= 3) true i > f true !(i > f+1) false

34 Example: /* This is a program to calculate the value of IR sensors for mobile robot*/ #include<stdio.h> int main() { int sensor1 = 30; int sensor2 = 40; if (sensor1>=40 || sensor2>=40) printf(“mobile robot turn right"); if(sensor1>=40 && sensor2>=40) printf (“mobile robot move forward"); if( !(sensor1>=40 && sensor2>=40)) printf(“mobile robot stop"); return(0); }

35 sizeof( ) operator Though sizeof( ) looks like a function , it is an operator. The sizeof operator returns the number of bytes of the data type included in the parenthesis. The sizeof operator is an integral part of the C language. #include<stdio.h> int main(void) { printf(“The size of char is %d\n”,sizeof(char)); printf(“The size of int is %d\n”,sizeof(int)); printf(“The size of float is %d \n”, sizeof(float)); printf(“The size of double is %d\n”, sizeof(double)); return 0; } Result: 1 2 4 8

36 SELECTION STRUCTURE

37 Selection Selection is the second construct of a structured programming language. We perform an action depending upon the prevailing conditions. Selection allows you to make some decisions and choose between two or more alternatives. We make some decision and select a particular choice. Your program reflects the real world problem and it should have the capability of making a decision and choose between two or more alternatives

38 The ‘if’ statement  The if statement is sometimes called a conditional statement. The operation of a if statement is governed by a conditional test. If the conditional test is true, one or more actions are executed. The syntax of a simplest if statement is shown below: if(expression) statement; NOTE: In C, an expression is true; it is evaluated to a nonzero value. If the expression is zero, it is false.

39 /* Program to demonstrate a if statement */
#include<stdio.h> int main(void) { int number; printf(“Enter a number “); scanf(“%d”,&number); if (number%2 == 0) printf(“The number %d is an even number\n”, number); return 0; }


Download ppt "DATA TYPES There are four basic data types associated with variables:"

Similar presentations


Ads by Google