Scanf Is the C Library’S Counterpart to Printf

Scanf Is the C Library’S Counterpart to Printf

Chapter 1: Introducing C Chapter 1: Introducing C Origins of C • By 1971, Ritchie began to develop an extended version of B. Chapter 1 • He called his language NB (“New B”) at first. • As the language began to diverge more from B, he changed its name to C. Introducing C • The language was stable enough by 1973 that UNIX could be rewritten in C. 1 Copyright © 2008 W. W. Norton & Company. 3 Copyright © 2008 W. W. Norton & Company. All rights reserved. All rights reserved. Chapter 1: Introducing C Chapter 1: Introducing C Origins of C Standardization of C • C is a by-product of UNIX, developed at Bell • K&R C Laboratories by Ken Thompson, Dennis Ritchie, – Described in Kernighan and Ritchie, The C Programming and others. Language (1978) – De facto standard • Thompson designed a small language named B. • C89/C90 • B was based on BCPL, a systems programming – ANSI standard X3.159-1989 (completed in 1988; formally language developed in the mid-1960s. approved in December 1989) – International standard ISO/IEC 9899:1990 • C99 – International standard ISO/IEC 9899:1999 – Incorporates changes from Amendment 1 (1995) 2 Copyright © 2008 W. W. Norton & Company. 4 Copyright © 2008 W. W. Norton & Company. All rights reserved. All rights reserved. Chapter 1: Introducing C Chapter 1: Introducing C C-Based Languages Strengths of C • C++ includes all the features of C, but adds • Efficiency classes and other features to support object- • Portability oriented programming. • Power • Java is based on C++ and therefore inherits many • Flexibility C features. • Standard library • C# is a more recent language derived from C++ and Java. • Integration with UNIX • Perl has adopted many of the features of C. 5 Copyright © 2008 W. W. Norton & Company. 7 Copyright © 2008 W. W. Norton & Company. All rights reserved. All rights reserved. Chapter 1: Introducing C Chapter 1: Introducing C Properties of C Weaknesses of C • Low-level • Programs can be error-prone. • Small • Programs can be difficult to understand. • Permissive • Programs can be difficult to modify. 6 Copyright © 2008 W. W. Norton & Company. 8 Copyright © 2008 W. W. Norton & Company. All rights reserved. All rights reserved. Chapter 1: Introducing C Chapter 2: C Fundamentals Effective Use of C Program: Printing a Pun • Learn how to avoid pitfalls. #include <stdio.h> • Use software tools (lint, debuggers) to make int main(void) { programs more reliable. printf("To C, or not to C: that is the question.\n"); • Take advantage of existing code libraries. return 0; } • Adopt a sensible set of coding conventions. • This program might be stored in a file named pun.c. • Avoid “tricks” and overly complex code. • The file name doesn’t matter, but the .c extension is • Stick to the standard. often required. 9 Copyright © 2008 W. W. Norton & Company. 2 Copyright © 2008 W. W. Norton & Company. All rights reserved. All rights reserved. Chapter 2: C Fundamentals Chapter 2: C Fundamentals Compiling and Linking • Before a program can be executed, three steps are usually necessary: – Preprocessing. The preprocessor obeys commands that Chapter 2 begin with # (known as directives) – Compiling. A compiler translates then translates the C Fundamentals program into machine instructions (object code). – Linking. A linker combines the object code produced by the compiler with any additional code needed to yield a complete executable program. • The preprocessor is usually integrated with the compiler. 1 Copyright © 2008 W. W. Norton & Company. 3 Copyright © 2008 W. W. Norton & Company. All rights reserved. All rights reserved. Chapter 2: C Fundamentals Chapter 2: C Fundamentals Compiling and Linking Using cc The GCC Compiler • To compile and link the pun.c program under • GCC is one of the most popular C compilers. UNIX, enter the following command in a terminal • GCC is supplied with Linux but is available for or command-line window: many other platforms as well. % cc pun.c • Using this compiler is similar to using cc: The % character is the UNIX prompt. % gcc -o pun pun.c • Linking is automatic when using cc; no separate link command is necessary. 4 Copyright © 2008 W. W. Norton & Company. 6 Copyright © 2008 W. W. Norton & Company. All rights reserved. All rights reserved. Chapter 2: C Fundamentals Chapter 2: C Fundamentals Compiling and Linking Using cc Integrated Development Environments • After compiling and linking the program, cc • An integrated development environment (IDE) is leaves the executable program in a file named a software package that makes it possible to edit, a.out by default. compile, link, execute, and debug a program • The -o option lets us choose the name of the file without leaving the environment. containing the executable program. • The following command causes the executable version of pun.c to be named pun: % cc -o pun pun.c 5 Copyright © 2008 W. W. Norton & Company. 7 Copyright © 2008 W. W. Norton & Company. All rights reserved. All rights reserved. Chapter 2: C Fundamentals Chapter 2: C Fundamentals The General Form of a Simple Program Directives • Simple C programs have the form • Before a C program is compiled, it is first edited directives by a preprocessor. • Commands intended for the preprocessor are int main(void) { called directives. statements • Example: } #include <stdio.h> • <stdio.h> is a header containing information about C’s standard I/O library. 8 Copyright © 2008 W. W. Norton & Company. 10 Copyright © 2008 W. W. Norton & Company. All rights reserved. All rights reserved. Chapter 2: C Fundamentals Chapter 2: C Fundamentals The General Form of a Simple Program Directives • C uses { and } in much the same way that some • Directives always begin with a # character. other languages use words like begin and end. • By default, directives are one line long; there’s no • Even the simplest C programs rely on three key semicolon or other special marker at the end. language features: – Directives – Functions – Statements 9 Copyright © 2008 W. W. Norton & Company. 11 Copyright © 2008 W. W. Norton & Company. All rights reserved. All rights reserved. Chapter 2: C Fundamentals Chapter 2: C Fundamentals Functions Statements • A function is a series of statements that have been • A statement is a command to be executed when grouped together and given a name. the program runs. • Library functions are provided as part of the C • pun.c uses only two kinds of statements. One is implementation. the return statement; the other is the function • A function that computes a value uses a return call. statement to specify what value it “returns”: • Asking a function to perform its assigned task is return x + 1; known as calling the function. • pun.c calls printf to display a string: printf("To C, or not to C: that is the question.\n"); 12 Copyright © 2008 W. W. Norton & Company. 14 Copyright © 2008 W. W. Norton & Company. All rights reserved. All rights reserved. Chapter 2: C Fundamentals Chapter 2: C Fundamentals The main Function Statements • The main function is mandatory. • C requires that each statement end with a • main is special: it gets called automatically when semicolon. the program is executed. – There’s one exception: the compound statement. • main returns a status code; the value 0 indicates • Directives are normally one line long, and they normal program termination. don’t end with a semicolon. • If there’s no return statement at the end of the main function, many compilers will produce a warning message. 13 Copyright © 2008 W. W. Norton & Company. 15 Copyright © 2008 W. W. Norton & Company. All rights reserved. All rights reserved. Chapter 2: C Fundamentals Chapter 2: C Fundamentals Printing Strings Comments • When the printf function displays a string • A comment begins with /* and end with */. literal—characters enclosed in double quotation /* This is a comment */ marks—it doesn’t show the quotation marks. • Comments may appear almost anywhere in a • printf doesn’t automatically advance to the program, either on separate lines or on the same next output line when it finishes printing. lines as other program text. • To make printf advance one line, include \n • Comments may extend over more than one line. (the new-line character) in the string to be /* Name: pun.c printed. Purpose: Prints a bad pun. Author: K. N. King */ 16 Copyright © 2008 W. W. Norton & Company. 18 Copyright © 2008 W. W. Norton & Company. All rights reserved. All rights reserved. Chapter 2: C Fundamentals Chapter 2: C Fundamentals Printing Strings Comments • The statement • Warning: Forgetting to terminate a comment may cause printf("To C, or not to C: that is the question.\n"); the compiler to ignore part of your program: could be replaced by two calls of printf: printf("My "); /* forgot to close this comment... printf("cat "); printf("To C, or not to C: "); printf("has "); /* so it ends here */ printf("that is the question.\n"); printf("fleas"); • The new-line character can appear more than once in a string literal: printf("Brevity is the soul of wit.\n --Shakespeare\n"); 17 Copyright © 2008 W. W. Norton & Company. 19 Copyright © 2008 W. W. Norton & Company. All rights reserved. All rights reserved. Chapter 2: C Fundamentals Chapter 2: C Fundamentals Comments in C99 Types • In C99, comments can also be written in the • Every variable must have a type. following way: • C has a wide variety of types, including int and // This is a comment float. • This style of comment ends automatically at the • A variable of type int (short for integer) can end of a line. store a whole number such as 0, 1, 392, or –2553. • Advantages of // comments: – The largest int value is typically 2,147,483,647 but – Safer: there’s no chance that an unterminated comment can be as small as 32,767. will accidentally consume part of a program.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    401 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us