1 / 51

PROGRAM STRUCTURE

PROGRAM STRUCTURE. Presentation by:- Sukhwinder Kaur Lect. I.T. G.P.C.G. PATIALA. Input /Output Statements.

mahina
Télécharger la présentation

PROGRAM STRUCTURE

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PROGRAM STRUCTURE Presentation by:- Sukhwinder Kaur Lect. I.T. G.P.C.G. PATIALA

  2. Input /Output Statements Reading ,processing and writing of data are the three essential functions of a computer program.Most programs take some data as input and display the processed data, often known as information or results on a suitable medium.So far we have used one method of providing value to the program users. In this method value assigned to variables through the assignment statements such as X=5 and Y=11.

  3. Types of I/O functions I/O functions can be classified into two Categories: • Unformatted I/O functions • Formatted I/O functions

  4. Formatted Input function The formatted input functions is scanf( ) Every scanf statement contains a control string that describes the format of the data to be input. Syntax: scanf(“control string”,arg1,arg2…,argn); The control string specifies the field format in which the data is to be entered also known as format specifier. The Arguments arg1,arg2,…,argn specify the address of locations where the data is stored. Control string may include: Field specification, consisting of conversion character %, data type character.

  5. Formatted Output function Formatted output function is printf( ) Output data can be written from the computer onto a standard output device using the library function printf. Syntax: printf(“control,string”,arg1,arg2,...argn);

  6. Formatted Output function Control string consists of three types of items:- • Characters that will be printed on the screen as they appear. • Format specification that define the output format for display of each item. • Escape sequence characters such as \n (new line)and\t(tab) . Arg1, arg2,…….argn are the variables whose values are formatted and printed acording to the specification of control string.

  7. Unformatted functions • There are several library functions under this categories: those that can deal with a single character and those that can deal with a string of characters. Single Character Input functions: getchar( ), getche( ), getch( ). Single Character Output:- putch( ) and putchar( ).

  8. Unformatted Input functions getchar( ) Syntax :- variable_ name=getchar( ); Reading a single character can be done by this function. getch() Syntax:- getch(); This function returns a character that has been recently typed . There is no need to press enter key after typing a character.

  9. Unformatted Input functions getche( ) Syntax:- variable_name = getche(); The typed character is echoed on the screen. The user is not required to press enter key after typing the character. For getchar( ), getche( ) and getch( ) functions header file <conio.h> must be included .

  10. Unformatted Output functions putchar( ) like getchar, there is an analogous function putchar for writing or printing one character at a time on the screen. • Syntax: putchar(variable_name); Where variable_name is a char type variable containing a character. This statement displays the character contained in the variable, name on the screen.

  11. Unformatted Output functions Example:- answer =‘Y’; putchar (answer); will display the character Y on the screen. The statement putchar(‘\n’); would cause the cursor on the screen to move to the beginning of the next line.

  12. Constants These are the tokens whose value do not change during the program execution. The following rules apply to all numeric type constants:- -Commas and blank spaces cannot be included within the constants. -The constant can be preceded by a minus(-) sign if desired. -The value of a constants cannot exceed minimum and maximum bounds.

  13. Types of constants • Integer constants:- an integer constants refers to a sequence of digits. e.g. 345, -127, +834, -82. • octal integerconstant I made of any combination of digits from 0-7 both inclusive with a leading zero. eg. 043, 026, 057 • hexadecimal integer constant is formed by combination of digits 0-9 and letter A-F or a-f. • Character constants:-a character represented within single quotes denotes a character constants. Escape sequence are also character constant. eg. ‘c’, ‘4’, ‘X’, ‘\n’ • Floating constants:- integer numbers are inadequate to r4epresnt certain quantities that vary continuously, such as distances, etc. e.g. 647.43, +0.321. • String constants: a string constant is asequence of alphanumeric characters enclosed in double quotation mark whose maximum length is 255 characters. e.g. ”welcome to this world” “$4375”

  14. Variables Variable is a storage location and an associated symbolic name which contains some known or unknown quantity or information, a value. • Declaration <<Data type>> <<variable name>>; int a; • Definition <<varname>>=<<value>>; a=10; • Usage <<varname>> a=a+1; //increments the value of a by 1

  15. Variable names- Rules • Should not be a reserved word like int etc.. • Should start with a letter or an underscore(_) • Can contain letters, numbers or underscore. • No other special characters are allowed including space • Variable names are case sensitive A and a are different.

  16. Data types in C • Primary(fundamental) data types • int, float, double, char • Derived data types • Arrays • Pointers • functions • User defined data types • Structures, union and enumerated

  17. Fundamental data types Fundamental data types are the built in data types and can be classified as follows:- • Integer type: an integer is an integral whole number without a decimal point. They are of nine type: short int, unsigned short int, signed int, unsigned int, signed int, long int, unsigned long int, signed long int.

  18. Fundamental data types • Character type: char is one of the integer type. It is used because when variable of this type are input or output, they are interpreted as characters • Floating type: A number having sectional part is afloating point number. E.g. 45.67 • Void type: The void type has no value. It specifies an empty set of values.

  19. Derived Data Types Array – An array is collection of identical data objects which are stored in consecutive memory locations under a common heading or variable name.

  20. Arrays • Arrays fall under aggregate data type • Aggregate – More than 1 • Arrays are collection of data that belong to same data type • Arrays are collection of homogeneous data • Array elements can be accessed by its position in the array called as index

  21. Arrays • Array index starts with zero • The last index in an array is num – 1 where num is the no of elements in a array • int a[5] is an array that stores 5 integers • a[0] is the first element where as a[4] is the fifth element • We can also have arrays with more than one dimension • float a[5][5] is a two dimensional array. It can store 5x5 = 25 floating point numbers • The bounds are a[0][0] to a[4][4]

  22. Derived data types Pointer – pointer is a variable that holds the address of data item such as variable or an array

  23. Pointers Pointers that store address of a double, char and float are called as double pointer, character pointer and float pointer respectively. char *cp float *fp double *dp; Assigning value to a pointer int *ip = &a; //a is an int already declared

  24. Pointers • Pointer is a special variable that stores address of another variable • Addresses are integers. Hence pointer stores integer data • Size of pointer = size of int • Pointer that stores address of integer variable is called as integer pointer and is declared as int *ip;

  25. User defined Data Types Structure A collection of related variables of the same and/or different data types. The structure is called a record and the variables in the record are called members or fields.

  26. Uses of structure struct student { Char name; int rollno; float marks; }; // End struct Struct student s1;

  27. User defined data types Union A union is same as structure it follows the same syntax as structure. The basic difference between structure and union is in terms of storage. In structure each member is assigned a unique storage area where as in union all the members in share the same storage area with in the computer memory.

  28. Operators Operators operate on some data to give the result. Various operators in C are:- • Arithmetic (+,-,*,/,%) • Relational (<,>,<=,>=,==,!=) • Logical (&&,||,!) • Increment/Decrement operators(++, --) • Bitwise (&,|) • Assignment (=) • Compound assignment(+=,*=,-=,/=,%=,&=,|=) • Shift (right shift >>, left shift <<)

  29. ARITHMETIC OPERATORS Arithmetic operators are the simplest and basic operators that do arithmetic operations. These operators require two variables and are called binary operators. The various arithmetic operators in C are:

  30. Use of arithmetic operators let a=5, b=7 and a, b are integers a + b = 5+7 = 12 a – b = 5-7 = -2 There is no exponentiation operator in C. however there is library function (pow) to carry out exponentiation.

  31. Arithmetic operators When operands are of different data type , then type conversion take place and data type of the value returned by an expression is of highest priority • Operation between int and float will result in float. • Operation between float and double will result in double. • Operation between int and char will result in int. • Operation between float and char will result in float. • Operation between int and double will result in double.

  32. ASSIGNMENT OPERATORS • Assignment operators are used to assign the result of an expression to a variable. The most commonly used assignment operator is =. Assignment expression that make use of this operator are written in the form :- identifier = expression e.g. X = 5 X=Y A=5.42 Sum= X+Y The automatic type conversion can result in an alteration of the data being assigned. E.g. • A floating-point value may be truncated if assigned to an integer identifier. • Double-precision value may be rounded if assigned to a floating-point (single-precision) identifier. • An integer quantity may be altered if assigned to a shorter integer identifier or to a character identifier (some high order bits may be lost).

  33. Relational Operators Relational operators are used for comparing two values depending upon their relation , take certain decision.

  34. Logical Operators Logical expression or a compound relational expression- An expression that combines two or more relational expressions Ex:- if (a==b && b==c)

  35. Truth Table

  36. Increment & Decrement Operators C supports 2 useful operators namely Increment ++ The ++ operator adds a value 1 to the operand ++a or a++ Decrement –- The –- operator subtracts 1 from the operand --a or a--

  37. Rules for ++ & -- operators These require variables as their operands 1.When postfix either ++ or – is used with the variable in a given expression, the expression is evaluated first and then it is incremented or decremented by one 2.When prefix either ++ or – is used with the variable in a given expression, it is incremented or decremented by one first and then the expression is evaluated with the new value

  38. Conditional operators Syntax: exp1 ? exp2 : exp3 Where exp1,exp2 and exp3 are expressions Working of the ? Operator: Exp1 is evaluated first, if it is nonzero(1/true) then the expression2 is evaluated and this becomes the value of the expression, If exp1 is false(0/zero) exp3 is evaluated and its value becomes the value of the expression Ex: m=2; n=3 r=(m>n) ? m : n;

  39. Bitwise operators These operators allow manipulation of data at the bit level

  40. Special operators • Comma operator ( ,) • sizeof operator – sizeof( ) • Pointer operators – ( & and *) • Member selection operators – ( . and ->)

  41. Expressions An expressions represents a single data item, such as a a number or a character. The expression may consist of a single entity, such as a constant, a variable, an array element or a reference to a function. Expressions can also represent logical condition that are either true or false. e.g. c=a+b x= =y i= i+1

  42. Use of header files • The header files are the files having .h extension. Header files provide function prototype, definition of library function, declaration of data types and constant used with library function, declaration of data types and constant used with library function . For performing input, output and other function, header files stdio.h is required. • In C programming, the use of header is to add functionality. Header files are basically saying put code in that header file here so you don’t have to type that many lines of codes. e.g. #include<stdio.h> # include<conio.h> #include<stdlib.h> #include<string.h>

  43. Header files • The files that are specified in the include section is called as header file • These are pre-compiled files that has some functions defined in them • We can call those functions in our program by supplying parameters • Header file is given an extension .h • C Source file is given an extension .c

  44. Tokens A token is a collection of characters whose significance is its collectivity rather than individuality. • Identifiers • Keywords • Literals or Constants • Operators • Punctuators or Special Symbols • Strings

  45. Keywords • auto • break • Case • Char • Continue • Default • Delete • Do • Double • Else • Float • For • Friend • Goto • If • Int • Long • Private

  46. Writing the first program • #include<stdio.h> • int main() • { • printf(“Hello”); • return 0; • } • This program prints Hello on the screen when we execute it

  47. Running a C Program • Type a program • Save it • Compile the program – This will generate an exe file (executable) • Run the program (Actually the exe created out of compilation will run and not the .c file) • In different compiler we have different option for compiling and running. We give only the concepts.

  48. Comments in C • Single line comment • // (double slash) • Termination of comment is by pressing enter key • Multi line comment • /*…. • …….*/ • This can span over to multiple lines

  49. Input and Output • Input • scanf(“%d”,&a); • Gets an integer value from the user and stores it under the name “a” • Output • printf(“%d”,a) • Prints the value present in variable a on the screen

More Related