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

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

Introduction to C Programming
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
The scanf Function The scanf function reads input from the standard input device into one or more variables Example: scanf(“%lf”, &miles); Reads a real.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Introduction to C Programming
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Introduction to programming Language C, Data Types, Variables, Constants. Basics of C –Every program consists of one or more functions and must have main.
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
 Input and Output Functions Input and Output Functions  OperatorsOperators Arithmetic Operators Assignment Operators Relational Operators Logical Operators.
2440: 211 Interactive Web Programming Expressions & Operators.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
1 CSC103: Introduction to Computer and Programming Lecture No 6.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Chapter 2: Using Data.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
Week 1 Algorithmization and Programming Languages.
C++ Programming: Basic Elements of C++.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
Types of C Variables:  The following are some types of C variables on the basis of constants values it has. For example: ○ An integer variable can hold.
Module B - Computation1/61 Module-B-Computation Variables Basic Memory Operations Expressions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Programming Fundamentals. The setw Manipulator setw changes the field width of output. The setw manipulator causes the number (or string) that follows.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
 Most C programs perform calculations using the C arithmetic operators (Fig. 2.9).  Note the use of various special symbols not used in algebra.  The.
C Building Block Chapter 2. Variables A variable is a space in the computer’s memory set aside for a certain kind of data and given a name for easy reference.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Lecture2.
Zhang Hongyi CSCI2100B Data Structures Tutorial 3
BASIC ELEMENTS OF A COMPUTER PROGRAM
INSPIRING CREATIVE AND INNOVATIVE MINDS
Chapter 2 - Introduction to C Programming
Tokens in C Keywords Identifiers Constants
Input/output.
Revision Lecture
Chapter 2 - Introduction to C Programming
By: Syed Shahrukh Haider
Introduction to C Programming
Operators and Expressions
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Introduction to C++ Programming
Chapter 2 - Introduction to C Programming
CS111 Computer Programming
Introduction to C Programming
Chapter 2 - Introduction to C Programming
Lecture3.
Introduction to Java Applications
Chapter 2 - Introduction to C Programming
Introduction to C Programming
Programming Fundamental-1
Getting Started With Coding
Presentation transcript:

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.

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.

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;

Integer number variables An int variable can store a value in the range -32768 to +32767. 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;

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. 

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

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.

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

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.

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 (%).

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

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.

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

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.

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)

Example:

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.

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, 2.3+3 = 5.3 2.3+5.2= 7.5 Subtraction - 3-2 = 1, 3-2.3= 0.7 3.0-2.3=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

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.

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

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.

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

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

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

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’

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

(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

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

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

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

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); }

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

SELECTION STRUCTURE

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

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.

/* 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; }