Getting-Started-in-C-Programming.pdf

Full Transcript

GETTING STARTED IN C PROGRAMMING G E R RY R I T Z R. D E N S I N G Adapted from A First Book of ANSI C, Fourth Edition CC 112 / CC 112L TOPICS COVERED Introduction to C Programming Simple Input / Out...

GETTING STARTED IN C PROGRAMMING G E R RY R I T Z R. D E N S I N G Adapted from A First Book of ANSI C, Fourth Edition CC 112 / CC 112L TOPICS COVERED Introduction to C Programming Simple Input / Output Operations Programming Style INTRODUCTION TO C PROGRAMMING C provides a comprehensive set of functions – Stored in a set of files known as the standard library – The standard library consists of 15 header files IDENTIFIERS Identifiers in C consist of three types: – Reserved words – Standard identifiers – Programmer-created identifiers Reserved word: word that is predefined by the programming language for a special purpose and can only be used in a specified manner for its intended purpose – Also referred to as keywords in C Standard identifiers: words predefined in C Most of the standard identifiers are the names of functions that are provided in the C standard library It is good programming practice to use standard identifiers only for their intended purpose Programmer-created identifiers: selected by the programmer – Also called programmer-created names – Used for naming data and functions – Must conform to C’s identifier rules – Can be any combination of letters, digits, or underscores (_) subject to the following rules: First character must be a letter or underscore (_) Only letters, digits, or underscores may follow the initial character Blank spaces are not allowed Cannot be a reserved word Examples of invalid C programmer-created names: – 4ab7 – calculate total – while All uppercase letters used to indicate a constant A function name must be followed by parentheses An identifier should be descriptive: degToRadians() – Bad identifier choices: easy, duh, justDoIt C is a case-sensitive language – TOTAL, and total represent different identifiers Identifiers THE main ( ) FUNCTION Sometimes referred to as a driver function SIMPLE INPUT / OUTPUT OPERATIONS INPUT OPERATIONS Inputs: any data entered into a program scanf() one of the commonly used function to take input from the user reads formatted input from the standard input such as keyboards Syntax: set of rules for formulating statements that are “grammatically correct” for the language Syntax for scanf(): scanf(“%d”, &variable); THE printf ( ) FUNCTION printf() formats data and sends it to the standard system display device (i.e., the monitor) Inputting data or messages to a function is called passing data to the function – printf("Hello there world!"); Messages are known as strings in C – A string of characters is surrounded by double quotes Syntax for printf(): printf("Hello there world!"); Function arguments Comment Preprocessor command Header file Invoking or calling the printf() function PROGRAMMING ST YLE PROGRAMMING STYLE: INDENTATION Except for strings, function names, and reserved words, C ignores all white space – White space: any combination of one or more blank spaces, tabs, or new lines In standard form: – A function name is placed, with the parentheses, on a line by itself starting at the left-hand corner – The opening brace follows on the next line, under the first letter of the function name – The closing function brace is placed by itself at the start of the last line of the function Within the function itself, all program statements are indented two spaces – Indentation is another sign of good programming practice, especially if the same indentation is used for similar groups of statements Don’t do this: int main ( ){printf ("Hello there world!" );return 0;} PROGRAMMING STYLE: COMMENTS Comments help clarify what a program does, what a group of statements is meant to accomplish, etc. The symbols designate the end of a comment Comments can be placed anywhere within a program and have no effect on program execution Under no circumstances may comments be nested invalid */ COMMON PROGRAMMING ERRORS Omitting the parentheses, (), after main Omitting or incorrectly typing the opening brace, {, that signifies the start of a function body Omitting or incorrectly typing the closing brace, }, that signifies the end of a function Misspelling the name of a function; for example, typing print() instead of printf() Forgetting to close a string passed to printf() with a double quote symbol Omitting the semicolon at the end of each executable statement Forgetting to include \n to indicate a new line SUMMARY A C program consists of one or more functions A function is a C language description of an algorithm Many functions are supplied in a standard library of functions provided with each C compiler Simple C programs consist of the single function named main() An executable statement causes some specific action to be performed when the program is executed All executable C statements must be terminated by a semicolon The printf() function displays text or numerical results

Use Quizgecko on...
Browser
Browser