Program Structure of C Inputoutput Operations 912021 Mr

  • Slides: 21
Download presentation
Program Structure of C & Input/output Operations 9/1/2021 Mr. Mohamed EL. Saied 1

Program Structure of C & Input/output Operations 9/1/2021 Mr. Mohamed EL. Saied 1

History of C q C was evolved by Dennis Ritchie at AT&T Bell Laboratories

History of C q C was evolved by Dennis Ritchie at AT&T Bell Laboratories in early of 1970 s q Successor of: ü ALGOL 60 (1960), ü CPL (Cambridge, 1963), ü BCPL (Martin Richard, 1967), ü B (Ken Thompson, 1970) q Used to develop UNIX. q Traditional C: q The C Programming Language, by Brian Kernighan and Dennis Ritchie, 2 nd Edition, Prentice Hall. K&R 9/1/2021 Mr. Mohamed EL. Saied 2

Standardization q Since it was developed for UNIX, so it was incompatible with any

Standardization q Since it was developed for UNIX, so it was incompatible with any OS or hardware. So need to create an “unambiguous, machine-independent" definition. q. ANSI (American National Standards Institute) created standard in 1989. q. That standard was updated in 1995 (C 95) and finally in 1999 (C 99). C++ &C: C++ extends C to include support for Object Oriented programming and other features that facilitate large software development projects. Because C++ includes C, it is best to master C, then learn C++. 9/1/2021 Mr. Mohamed EL. Saied 3

Characteristics of C C has now become a widely used professional language for various

Characteristics of C C has now become a widely used professional language for various reasons : q Structured language q General purpose programming language q It can handle low-level activities. (hardware programming) q Machine independent. q Has fast speed execution. 9/1/2021 Mr. Mohamed EL. Saied 4

Uses of C C was designed as a system development language because it produces

Uses of C C was designed as a system development language because it produces code that runs as fast as the code written in assembly language. Some examples of the use of C might be: q Operating Systems q Assemblers q Language Compilers q Language Interpreters q Text Editors q Network Drivers q Databases q Utilities 9/1/2021 Mr. Mohamed EL. Saied 5

C Standard Library q. C programs consist of pieces/modules called functions. q C provides

C Standard Library q. C programs consist of pieces/modules called functions. q C provides a set of functions stored in a set of files known as the standard library. q Programmers will often use the C library functions as building blocks. q A programmer can create his own functions. � Advantage: the programmer knows exactly how it works � Disadvantage: time consuming 9/1/2021 Mr. Mohamed EL. Saied 6

Typical C development Environment Phases of C Program: 1. Editor Disk Preprocessor Disk Compiler

Typical C development Environment Phases of C Program: 1. Editor Disk Preprocessor Disk Compiler Disk Linker Disk 2. Preprocess 3. Compile Loader Primary Memory 4. Link Disk 5. Load & Execute(Run) CPU . . . Mr. Mohamed EL. Saied Loader puts program in memory. Primary Memory . . . 9/1/2021 Program is created in the editor and stored on disk. Preprocessor program processes the code. Compiler creates object code and stores it on disk. Linker links the object code with the libraries, creates a. out and stores it on disk CPU takes each instruction and executes it, possibly storing new data values as the program executes. 7

C Program Structure The C program structure is made up of: � Preprocessor directives

C Program Structure The C program structure is made up of: � Preprocessor directives � Function main() � The braces {} � Statement(s) C program looks like this: #include <stdio. h> int main() { statement(s); return 0; } 9/1/2021 Mr. Mohamed EL. Saied 8

Preprocessor directives �C program line begins with # provides an instruction to the C

Preprocessor directives �C program line begins with # provides an instruction to the C preprocessor �It is executed before the actual compilation is done. �Two most common directives : �#include �#define �For example (#include<stdio. h>): Tells C compiler to load contents of the header file <stdio. h> allows standard input/output operations such as scanf() and printf(). 9/1/2021 Mr. Mohamed EL. Saied 9

Function main() & braces 1 -Every C program has a main ( ) function:

Function main() & braces 1 -Every C program has a main ( ) function: �Identify the start of program execution. �'main' is a C keyword, i. e. it must not used for any other variable. �Parenthesis ( ) used to indicate a function �int means that main "returns" an integer value 2 -Every C program has the braces { } � To Identify the start and end of the body of main function. �Also the start and end of the selection or repetition blocks. 9/1/2021 Mr. Mohamed EL. Saied 10

Statement �A statement is the action to be taken by the computer when the

Statement �A statement is the action to be taken by the computer when the program executes. �All statements in C must end with semicolon (; ) �Statement has two types : �Declaration statements � Program lines that tells the compiler the names of memory cells in a program �Executable statements � Program lines that are converted to machine language instructions and executed by the computer 9/1/2021 Mr. Mohamed EL. Saied 11

A Simple C Program: Printing a Line of Text �The output of this program:

A Simple C Program: Printing a Line of Text �The output of this program: Welcome to C! 9/1/2021 Mr. Mohamed EL. Saied 12

In this example: �printf( "Welcome to C!n" ); �This line is called a statement,

In this example: �printf( "Welcome to C!n" ); �This line is called a statement, So it ends with a semicolon (; ) �This statement instructs computer to perform an action. (It prints the string of characters within quotes (" ") �Escape character () � Indicates that printf should do something out of the ordinary n is the newline character �return 0; �A way to exit a function �return 0, indicates that the program ended successfully. 9/1/2021 Mr. Mohamed EL. Saied 13

Printing a Line of text: Cont. � The output of this program: Welcome to

Printing a Line of text: Cont. � The output of this program: Welcome to C! 9/1/2021 Mr. Mohamed EL. Saied 14

Input / Output �Input operation �It is an instruction that copies data from an

Input / Output �Input operation �It is an instruction that copies data from an input device such as (keyboard) into memory �A C function that performs an input operation is predefined in the header file <stdio. h> such as : scanf() �Output operation �It is an instruction that displays information stored in memory to the output devices (such as the monitor screen) �A C function that performs output operation is predefined in the header file <stdio. h> such as : printf() 9/1/2021 Mr. Mohamed EL. Saied 15

The scanf ()function �Used to read data from the standard input device (keyboard) and

The scanf ()function �Used to read data from the standard input device (keyboard) and store it in a variable in the memory. �General format (syntax): �scanf(“format string”, & variable); (&) operator : � Address operator and called “ampersand”. � it passes the address of the variable instead of the variable itself � tells the scanf() where to find the variable to store the new value Format string is a combination of conversion specifier s. 9/1/2021 Mr. Mohamed EL. Saied 16

The scanf () function cont… �Example-1: printf(“Please enter your height : ”); scanf(“%f”, &height);

The scanf () function cont… �Example-1: printf(“Please enter your height : ”); scanf(“%f”, &height); In example-1: computer asks /wants the user to enter value for height. �Example-2 printf(“Please enter your height and weight : ”); scanf(“%f%f”, &height, &weight); In example-2: computer asks /wants the user to enter more than one value. 9/1/2021 Mr. Mohamed EL. Saied 17

The printf ()function �Used to send data to the standard output (monitor) to be

The printf ()function �Used to send data to the standard output (monitor) to be printed according to specific format. �General format: �printf(“string literal”); �A sequence of any number of characters surrounded by double quotation marks. �printf(“format string”, variable); � Format string is a combination of text, conversion specifier and escape sequence. 9/1/2021 Mr. Mohamed EL. Saied 18

The printf ()function cont… �Example: �printf(“Thank youn”); Thank you Press any key to continue

The printf ()function cont… �Example: �printf(“Thank youn”); Thank you Press any key to continue �printf (“Total sum is: %dn”, sum); Total sum is: 50 Press any key to continue Assuming that the value of sum is 50 � %d is a format string (conversion specifier) � marks the display position for a type integer variable � Common Conversion Identifier used in printf function. � n is an escape sequence � moves the cursor to the new line 9/1/2021 Mr. Mohamed EL. Saied 19

Conversion specifiers: �Common Conversion specifier used in printf( ) and scanf( ) functions. 9/1/2021

Conversion specifiers: �Common Conversion specifier used in printf( ) and scanf( ) functions. 9/1/2021 Mr. Mohamed EL. Saied 20

Escape sequences: Sequence a b n t \ ' " 9/1/2021 Meaning Bell (alert)

Escape sequences: Sequence a b n t \ ' " 9/1/2021 Meaning Bell (alert) Backspace Newline Horizontal tab Backslash Single quote Double quotation Mr. Mohamed EL. Saied 21