Department of Computer Applications

Similar documents
Programming and Data Structures

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

DEPARTMENT OF MATHS, MJ COLLEGE

Computers Programming Course 5. Iulian Năstac

ANSI C Programming Simple Programs

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Computer System and programming in C

UNIT- 3 Introduction to C++

C Programming Multiple. Choice

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

Basics of Programming

Guide for The C Programming Language Chapter 1. Q1. Explain the structure of a C program Answer: Structure of the C program is shown below:

UNIT IV 2 MARKS. ( Word to PDF Converter - Unregistered ) FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING

Pointers. Mr. Ovass Shafi (Assistant Professor) Department of Computer Applications

C - Basic Introduction

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

Preview from Notesale.co.uk Page 6 of 52

Programming in C and Data Structures [15PCD13/23] 1. PROGRAMMING IN C AND DATA STRUCTURES [As per Choice Based Credit System (CBCS) scheme]

UNIT 3 OPERATORS. [Marks- 12]

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Basic Science and Humanities

IECD Institute for Entrepreneurship and Career Development Bharathidasan University, Tiruchirappalli 23.

C: How to Program. Week /Mar/05

PART I. Part II Answer to all the questions 1. What is meant by a token? Name the token available in C++.

A Fast Review of C Essentials Part I

Fundamental of Programming (C)

Work relative to other classes

I Internal Examination Sept Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs.

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

A flow chart is a graphical or symbolic representation of a process.

Fundamentals of Programming

Chapter 2 - Introduction to C Programming

Presented By : Gaurav Juneja

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Lecture 02 C FUNDAMENTALS

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

The component base of C language. Nguyễn Dũng Faculty of IT Hue College of Science

UNIT IV INTRODUCTION TO C

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Data Types and Variables in C language

Chapter 1 & 2 Introduction to C Language

Fundamental of C programming. - Ompal Singh

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

VARIABLES AND CONSTANTS

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

Chapter 7. Basic Types

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Model Viva Questions for Programming in C lab

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Basic Elements of C. Staff Incharge: S.Sasirekha

I BCA[ ] SEMESTER I CORE: C PROGRAMMING - 106A Multiple Choice Questions.

Variables and Operators 2/20/01 Lecture #


C Programming a Q & A Approach

Operators in C. Staff Incharge: S.Sasirekha

Data Types. Data Types. Integer Types. Signed Integers

BLM2031 Structured Programming. Zeyneb KURT

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Types, Operators and Expressions

C Programming Class I

Q 1. Attempt any TEN of the following:

XSEDE Scholars Program Introduction to C Programming. John Lockman III June 7 th, 2012

Computers Programming Course 6. Iulian Năstac

Programming. Elementary Concepts

DECLARATIONS. Character Set, Keywords, Identifiers, Constants, Variables. Designed by Parul Khurana, LIECA.

UIC. C Programming Primer. Bharathidasan University

Unit 3. Operators. School of Science and Technology INTRODUCTION

Introduction to C programming. By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE

Course Outline Introduction to C-Programming

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Java Notes. 10th ICSE. Saravanan Ganesh

ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1

Introduction to C++ with content from

6.096 Introduction to C++ January (IAP) 2009

C Language, Token, Keywords, Constant, variable

Declaration and Memory

Programming in C++ 5. Integral data types

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

Types, Operators and Expressions

SEQUENTIAL STRUCTURE. Erkut ERDEM Hacettepe University October 2010

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

4.1. Structured program development Overview of C language

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

Review of the C Programming Language for Principles of Operating Systems

Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100

P.E.S. INSTITUTE OF TECHNOLOGY BANGALORE SOUTH CAMPUS 1 ST INTERNAL ASSESMENT TEST (SCEME AND SOLUTIONS)

BASIC ELEMENTS OF A COMPUTER PROGRAM

Lecture 3. More About C

Q1. Multiple Choice Questions

CS313D: ADVANCED PROGRAMMING LANGUAGE

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

INTRODUCTION TO C A PRE-REQUISITE

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

C-LANGUAGE CURRICULAM

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

DETAILED SYLLABUS INTRODUCTION TO C LANGUAGE

File Handling in C. EECS 2031 Fall October 27, 2014

Transcription:

Sheikh Ul Alam Memorial Degree College Mr. Ovass Shafi. (Assistant Professor) C Language An Overview (History of C) C programming languages is the only programming language which falls under the category of mid level programming language. Popularly known as mother of all languages, C is a general purpose programming languages and can be used to develop System as well as Application Programs. It was originally developed by Dennis M. Ritche in 1970s at the Bell Labs in California. The idea behind the development of C Language was to rewrite much of the Unix Operating system in some machine independent language in order to port UNIX on to other architecturally dissimilar machines. UNIX was originally written for a DEC PDP-7 computer by Ken Thompson, an electrical engineer and a colleague of Ritchie s at Bells Labs. Thompson had written the first UNIX in a language he used to call B. To rewrite the UNIX using assembly language was out of question, as it was not portable. There arises a need for a language that would permit assembly like operations such as bit manipulation along with the feature of portability. None of the languages then available could have served the purpose. Therefore a new language was developed and hence come into existence C Language. Variable A variable is a named memory location used to store data. The value of the variable may change during the execution of program. To solve real world problems, we often store some data and then retrieve it later for further processing. In order to achieve this goal variables are used. Data Type In C programming language each variable has a specific data type. The data type specifies the number of things to the compiler. It is the data type that specifies what kind of data a variable is going to store, the number of bytes that will be allocated for a

variable, type of operations allowed on that variable, range of values that can assigned to a variable and finally the format specifier for the variable used to display the value of a variable. Datatypes available in C Data Types Integer Types Floating Point / Real Types int char float double Range Modifiers/ Qualifiers Range Modifiers / Qualifiers long long double unsigned signed short long unsigned signed unsigned int signed int short int long int unsigned char signed char

Sheikh Ul Alam Memorial Degree College Variables of Type int Mr. Ovass Shafi. (Assistant Professor) signed int / int In C programming language, before using any variable it must be declared explicitly along with its data type. The syntax for the declaration of variable in C programming is as Data type identifier; In order to define the variable of type int, the declaration statement will look like int num1; int var1, var2, result; The first statement declares an integer variable num1, the second statement declares three integer variables namely var1, var2 and result. If we omit the range modifier/qualifier, then the default modifier is always signed. Therefore the above statements and the given below statements are equal signed int num1; signed int var1, var2, result; We can store the signed integer values (values without fraction) in these variables, i.e. these variables may be either positive or negative integers within the range set for the variables of type int. The range of values that can be assigned to any variable is very much machine dependent. For most compilers for the IBM PC ints are stored in two consecutive bytes and are restricted to the range of -32768 to +32767. However on modern machines ints can be of 4 bytes. We can also define and declare the variable in the single statement like int num1=20;

Sheikh Ul Alam Memorial Degree College signed int var1=10,var2=20,result=var1+var2; Mr. Ovass Shafi. (Assistant Professor) In above statements the variables are declared and defined in single statement and is known as initialization of variable. If we declare a variable but don t assign any value to it, it will not be automatically initialized to zero (0) but will store some unknown value known as garbage value in C terminology. If we want to print the value of variable num1 declared above, we can do it using the printf () function, however if we write the output statement as printf( Value of num1 =num1 ); The above statement will display Value of num1 = num1. But we want to display result as Value of num1 = 20 To display the value of the variable we have to use specific format specifier for the variable and the format specifier for int/signed int is %d. To display value of num1 on screen the output statement will be like printf( Value of num1 =%d,num1); Finally the operations that are allowed to be performed on the variables of type int/signed int are addition, subtraction, multiplication, division and modulus (remainder). The program given below will help to understand most of the concepts about signed int type: /*Prog2a.c*/ #include<stdio.h> #include<conio.h> void main()

{ signed int num1, num2,result; clrscr(); num1=10; num2=3; result=num1+num2; printf("\nsum of %d and %d is %d",num1,num2,result); result=num1-num2; printf("\ndifference of %d and %d is %d",num1,num2,result); result=num1*num2; printf("\nproduct of %d and %d is %d",num1,num2,result); result=num1/num2; printf("\nquotient from %d Divided by %d is \ %d",num1,num2,result); result=num1%num2; printf("\nremainder from %d Divided by %d is \ %d",num1,num2,result); getch(); } The output of above program is: unsigned int Usually in signed integer numbers one bit is always reserved for storing the sign of the number. For positive numbers the sign bit is always 0 and for negative numbers the sign bit is always 1. However if a variable is declared as unsigned int, no sign bit is used and all the bits are used to represent the magnitude of the number. That is why unsigned int

can only hold positive integral values. In unsigned int we have one more bit available to store the number and thus the range of values that can be assigned to variables of these types is from 0 to 65535. We use unsigned integer variables when we deal with data that is non negative. The most common use of unsigned int is to represent memory addresses. Just as %d is used to print signed int values, %u is the format specifier for unsigned int variables. If we want to print the value of unsigned int variable, we have to use %u instead of %d. The following programs will help to clearly understand the use of unsigned int data type. Usage: Unsigned int identifier; short int / signed short int The short int declaration may be useful in cases where an integer variable is known beforehand to be small. The variable declared as short int ensures that the range of the variable will not exceed that of signed ints. Usually short modifier is obsolete on most of the modern computers and compilers however on some computers the range may be shorter and may accommodate one byte of memory only and can assume values from - 128 to + 127. The format specifier for signed short int is also same as that of int (%d) and the same operations as that of int can be performed on variables of type short int. unsigned short int The unsigned short int declaration may be useful in cases where an integer variable is known beforehand to be small and non negative. The variable declared as unsigned short int ensures that the range of the variable will not exceed that of unsigned ints. Usually unsigned short modifier is obsolete on most of the modern computers and compilers however on some computers the range may be shorter and may accommodate one byte of memory only and can assume values from 0 to +255. The format specifier for signed

short int also same as that of unsigned int (%u) and the same operations as that of unsigned int can be performed on variables of type unsigned short int. signed long int/long int Syntax: signed long int population; Or long int population; The signed long int variables are required when we use integers larger than the range that is available for int. On most computers long ints are 4 bytes wide and can store the values in the range -2147483648 to +2147483647. They support the same operations that are supported by integer variables. When we assign the value to long int variable the value on right side is immediately followed by l Or L. population=123421345l; The format specifier for the long int is %ld. unsigned long int Syntax: unsigned long int population; Usually in signed long integer numbers one bit is always reserved for storing the sign of the number. For positive numbers the sign bit is always 0 and for negative numbers the sign bit is always 1. However if a variable is declared as unsigned long int, no sign bit is used and all the bits are used to represent the magnitude of the number. That is why unsigned long int can only hold positive integral values. In unsigned long int we have one more bit available to store the number and thus the range of values that can be assigned to variables of these types is from 0 to 4294967295. We use unsigned long integer variables when we deal with data that is non negative. Just as %ld is used to print signed long int values, %lu is the format specifier for unsigned long int variables. If we want to print the value of unsigned long int variable, we have to use %lu instead of %ld.

In the above declarations we can use short form of declarations also as shown: Complete declaration signed int x; Short hand declaration int x unsigned int y; unsigned y; signed long int p; long p; unsigned long int q; unsigned long q; singed short int r; short r; unsigned short int s; unsigned short s; Variables of Type char Sometimes the user is interested in storing character values instead of numeric values. To store character values we can declare variable of type char. Character variable is a named memory location that can hold a single character from the ASCII set. The character variable occupies the least memory for storing data. A single ASCII character can be accommodated in single byte of memory. To assign a character to character variable, the character literal need to be enclosed in single quote. The syntax for declaring and defining character variable is shown below: char var1= a ; char var2; var2= x ; Actually, character data is stored in the form of integers. Each character is assigned an ASCII code. The ASCII code for a is 97 and that of b is 98. The ASCII code for z is 122. For A, the ASCII code is 65 and that of B is 66. The ASCII code for Z is 90. The digits also have ASCII codes; 48 for 0, 49 for 1, 50 for 2 and soon. The ASCII code for 9 is 57.

Therefore in var1 declared above, the value stored in var1 will be 0100001. Therefore character variable are actually the subset of integers. In order to display the character value of a variable, we can use %c as format specifier, however we can also display ASCII/Decimal Code of any character variable using %d specifier. The range of values that can be assigned to signed char variable is -128 to +127 and that of unsigned char variable is 0 to +255. We can perform all the operations on character variables that can be performed on ints. Besides we can assign integer values to character variables also. For example if we want to assign A to character variable var1 it can be done in any of the following ways: char var1= A ; char var1=65; The above two statements will do the same task. Variables of Type float The integer and character variables are used to store decimal and character values. We can store only numeric data without fractional parts in the integer and character variables. However in some applications it is necessary to work on numerals with fractional parts. C provides the facility to the programmers to work with numerals with fractional parts called floats. The numbers with fractional parts are called floats because the user can change the position of the decimal point as per the precision required. The floating point numbers has two parts namely mantissa and exponent. The mantissa part can be fractional and may consist of integer part and fractional part. However the exponent is always integer value and represents the position of the decimal point within the mantissa. The floating point numbers are always signed i.e. the left most bit is always used as sign bit. There is no unsigned floating point concept as in characters and integers. When we assign value to floating point number, the value must be followed immediately by f; the syntax for usage of floating point number is shown below:

Sheikh Ul Alam Memorial Degree College float x=123.332f; Mr. Ovass Shafi. (Assistant Professor) float y=-231.454e04f; float z=232.02e-01f; Variables of Type double The float data type allows us to store values with fractional values and also to perform calculations with fractional values but the limitation of float data type is that it provides single precision arithmetic. But there are situations where large scale scientific or engineering calculations are involved, and thus the double precision floating point computations are required. The facility is available with double data type in C programming language. The variable of type double occupies 8 consecutive bytes in memory and provides capability to produce computational results correct to 14 significant digits. The range of values that can be assigned to variable of type double lies between 1.7E-308 and 1.7E+308. The operations that can be performed on variable of type double are same as that of float variables. The format specifiers used to display the value of double variables are %lf, %le, %lg, however most common format specifier used is %le. All the three format specifiers are counterparts of their single precision (float). Any numeral with decimal point is treated as double even if it lies within the range of float. So in order to treat a number with decimal point as float, it must be followed immediately by f or F. long double Syntax: long double PI; The long double variables are required when we use fractional values larger than the range that is available for double. On most computers long doubles are 10 bytes wide and can store the values in the range 3.4E-4932 to 3.4E+4932. They support the same operations that are supported by float variables.

The format specifier for the long double is %Lf, %Le, %Lg. Most commonly used format specifier is %Le. Identifiers & Keywords An identifier is any user defined word in C program used for naming variables, programs, arrays, functions, structures, enumerations, unions etc. Names of identifiers should be chosen as such that they represent their roles within the program for example to add two numbers, the better variable names would be num1, num2 and sum, instead of x, y, and z. Rules for creating identifiers. The identifier names are sequences of character chosen from the set [A Z, a z, 0-9,_], of which the first character must not be a digit. No two variables must have same names as it would create name conflicts and hence programming errors. Identifiers can be of any suitable length. Generally 8 10 characters should suffice, though certain compilers may allow for very much longer names of upto 63 characters. Two identifiers will be considered different if they differ upto their first 31 characters. The identifier should not be same to that of some keyword. C is a case sensitive language, so num1, Num1 and num1 will be considered three different variable names. Some of the valid identifiers are: Num_1, num1, _hello, hello, HELLO Some of the invalid identifiers are: 1_num, while, 123, num-1, num.1, num 1

Keywords Keywords are the predefined words in any programming language which can t be used in any context other than that predefined in the language. A list of keywords available in C is listed below: auto break case char const continue default do double else enum extern float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while Expressions & Operators One of the most powerful features of C programming language is the versatility of its operators. There are number of operators available in C for arithmetic computation, relational operators for making comparisons, increment and decrement operators, cast operators, assignment operators, logical operators, bitwise operators, conditional operators and many more. We will look on some of the operators in this unit, and the rest will be covered in subsequent chapters. First of all we will define the term operator and operand. An Operator is a symbol that specifies the type of operation that is to be performed like +, -, *,<, > etc. When we use operator, we have to provide the data on which the operation is to be performed. The data on which the specific operation is performed is known as Operand. An operand can be a variable, constant or a literal. Some operators operate on single operand and are known as Unary Operators for example -3, x++ etc. Some operators operate on two operands and

are known as Binary Operators like 2+3, 4*6, 4<6 etc. Some operators operate on three operands and are known as Ternary Operators. There is only one tertiary operator available in C language and is called conditional operator (?:) which we will see later in this chapter. when we want to perform some computation, we combine the operators and operands. The valid combination of operators and operands forms Expression which is evaluated to get the desired results. Basic Arithmetic Operators and Operations. There are four basic arithmetic operators available in C. These are for addition (+), subtraction (-), multiplication (*) and division (/). Besides we can use modulus operator (%) to calculate the remainder by dividing dividend with a divisor. Among these operators, addition and subtraction operator can act as unary as well as binary operators while as rest three operators can be used as binary operators only. If we want to multiply the two numbers and assign the result to the third variable, we will have to use three operands (multiplier, multiplicand and destination variable) and two operators as shown: x=y*z In the above statement there are two operators. One is multiplication operator (*) and the second one is assignment operator (=). The assignment operator assigns the quantity on its right to the variable on its left. So the assignment operator s associativity is from right to left in contrast to basic arithmetic operators that associates from left to right, as most of the C operators do. Consider the statement given below: i=i+1 One should not be confused from the above expression. In programming i=i+1 should not be read as i is equal to i+1 (LHS = RHS). As i can t be equal to i+1. In programming i=i+1 means replace the previous contents of i by adding 1 to the previous contents of i. Each operator has two properties associated with it. The precedence and associativity. When there is more than one operator occurring in an expression, it is the relative

priorities of the operators with respect to each other that will determine the order in which the expression will be evaluated. The associativity defines the direction left-toright or right-to-left, in which the operator acts upon its operands. The second property priority is also important consideration during the evaluation of expressions. The priority of all the C operators along with their associativity is given in table below: Table Precedence and Associativity of Operators Operators (priority is increasing from top to down Associativity. () [] ->. Left to Right! ~ ++ -- + - * & (type cast) sizeof (all unary operators) Right to Left */ % Left to Right + - Left to Right << >> Left to Right < <= > >= Left to Right ==!= Left to Right & Left to Right ^ Left to Right Left to Right && Left to Right Left to Right? : Left to Right = += -= *= /= %= &= ^= = <<= >>= Right to Left, Left to Right

The operators at the top of the table parenthesis (), array operator [], arrow operator - > and the dot. operators are known a primary operators. All theses operators have same priority among themselves but higher than that of all other operators. After than the unary operators have higher priority than binary operators which in turn has higher priority than ternary operators. If a user is not sure about the priority of the operators, then he can make use of parenthesis to override the normal priority of the operators for example consider the expression x=3*4+2 as per normal rules multiplication has higher priority than addition. So the result of above expression will be 3*4=12 and then 12 + 2=14. Therefore x will be assigned the value of 14. Now consider the same expression but with parenthesis. X=3*(4+2) The presence of parenthesis around the addition operator gives higher priority to addition operator than multiplication. Therefore the result of above expression will be 4+2=6 and then 3*6 =18. Therefore x will be assigned the value of 18. Increment and Decrement operators Most often in programming we need to update the value of the variable by some amount. To increase the value of variable is known as increment and is done by additive operator (+). Similarly sometimes we need to update the value of the variable by decreasing it and is known as decrement. For example x=x+1; /*increment value of x by 1*/ y=y-1; /*decrement value of y by 1*/

There are number of ways to increment and decrement the value of a variable and are shown below: x=x+1; x=x+5; x+=1; x+=3; x++; ++x; x=x-1; x-=1; x=x-5; x-=5; x--; --x; /*increment value of x by 1 using normal arithmetic operator*/ /*increment value of x by 5 using normal arithmetic operator*/ /*increment value of x by 1 using arithmetic assignment operator*/ /*increment value of x by 3 using arithmetic assignment operator*/ /*increment value of x by 1 using post increment operator*/ /*increment value of x by 1 using pre increment operator*/ /*decrement value of x by 1 using normal arithmetic operator*/ /*decrement value of x by 1 using arithmetic assignment operator*/ /*decrement value of x by 5 using normal arithmetic operator*/ /*decrement value of x by 1 using arithmetic assignment operator*/ /* decrement value of x by 1 using post decrement operator*/ /* decrement value of x by 1 using pre decrement operator*/ If the post and pre increment/decrement is used on single variable without embedding in expression then both have the same effect, i.e. increase the value of variable by 1. However when they are used with expressions then they act differently. The Pre increment/decrement operator updates the value of their target variables before the execution of expressions. Once the variable is updated the expression is then evaluated using the new values. With Post increment/decrement operators the expression is evaluated using previous values of variables and after the result of evaluated expression is used, the variables are updated. The effect of increment will not be reflected in the result.

Sheikh Ul Alam Memorial Degree College For example if x=2, and y=3 Mr. Ovass Shafi. (Assistant Professor) z=x++ + y++; After expression evaluation, values will be x=3, y=4 and z=5. As the variables x and y are operated on by post increment, the expression will be evaluated using the original values of x and y i.e. 2 and 3 respectively. So z will get the value 2+3 = 5. After evaluation of expression value of x and y will be incremented by 1. Now consider the same expression but using pre increment using same values x=2 and y=3 z=++x + ++y; In above expression, the value of x and y will be incremented by 1 before the evaluation of expression and after the value of x and y becomes 3 and 4, the expression will be evaluated and z will have the value 3+4=7. getch(), getche(), getchar() functions (Single Character Input) We can read character using getch (), getche () and getchar () functions also. To print character data, we can use putchar () function. The following program illustrates the use of printf (), scanf (), getchar (), getch (), getche () and putchar () functions. /*Prog2l.c*/ #include<stdio.h> #include<conio.h> void main() { int a,b,c; float x,y,z; char ch; clrscr(); printf("\nenter any two integer values:->"); scanf("%d%d",&a,&b);

c=a+b; printf("\nsum of %d and %d is %d",a,b,c); printf("\nenter any two Floating Point values:->"); scanf("%f",&x); scanf("%f",&y); z=x+y; printf("\nsum of %.2f and %.2f is %.2f",x,y,z); printf("\nhit any key on keyboard:->"); fflush(stdin); scanf("%c",&ch); printf("\nyou Hit %c",ch); printf("\nhit any key on keyboard:->"); ch=getch(); printf("\nyou Hit %c",ch); printf("\nhit any key on keyboard:->"); ch=getche(); printf("\nyou Hit %c",ch); printf("\nhit any key on keyboard:->"); fflush(stdin); ch=getchar(); printf("you Hit "); putchar(ch); getch(); } Formatted Input/Output In C language the output is generated using printf () function. The printf () function can be used to output character strings as well as number. So far we have used printf () function to simply print the output without any proper format, however the printf ()

function can be used to deliver the formatted output of numbers of variables and expressions of any type. As specified earlier, the printf () function used two arguments. The first one is control string, and second is the list of variables. If we specify the control string and list of variables, the output will be generated in unformatted manner. However if we want output to be formatted, the control string is provided with Format specifications. The Format specifications controls how the variables or expressions should be displayed on the screen. To illustrate the unformatted output consider the following program segment. /*Prog3g.c*/ #include<stdio.h> #include<conio.h> void main() { long int p1,p2,p3; clrscr(); printf("\ncountry\tpopulation:->"); p1=123451234; p2=12323; p3=12312334; printf("\nindia\%d",p1); printf("\nafghanistan\%d",p2); printf("\namerica\%d",p3); getch(); } As seen from the output, the output is jumbled and not in proper format. It is hard to read and understand. Now the same program can be rewritten using formatted output as below:

/*Prog3h.c*/ #include<stdio.h> #include<conio.h> void main() { long int p1,p2,p3; clrscr(); printf("\n%20s%20s","country","population"); p1=123451234; p2=12323; p3=12312334; printf("\n%20s%20d","india",p1); printf("\n%20s%20d","afghanistan",p2); printf("\n%20s%20d","america",p3); getch(); } gets() and puts() functions: For reading strings at runtime we can use either scanf() or gets() function. using scanf (), we have to use %s as format specifier but scanf() function can read single word string. When user inputs space character, the scanf() function terminates input process. Another function that can be used to input strings is gets() function. This function accepts one argument, i.e. string to be read. This function can read multiple word string and terminates input when enter key is pressed, i.e. when newline is inserted. We can also use puts(string) for printing the string on screen. The string input/output didn t require loops for printing individual character of string as in integer, float, double, and character arrays. The reason for this is that the format specifier starts printing elements of string from the base address till it encounters null character. In other types of arrays there is no

null character and hence we need to run loop from 0 to the last element to process all the elements of array. The following program illustrates the process of string input: /*Prog8f.c String Input at Runtime*/ #include<stdio.h> #include<conio.h> void main() { char str1[100]; char *str2; /*dynamic string*/ clrscr(); printf("\nenter Any Single Word String\n"); scanf("%s",str1); printf("\nenter Any Multiple Word String\n"); fflush(stdin); gets(str2); printf("\nstring I is %s",str1); printf("\nstring II is %s",str2); puts(str1); puts(str2); getch(); }