—Aristophanes Welcome to Teach Yourself C in 24 Hours. In this first lesson you' ll learn the following: What C is Why you need to learn C The ANSI. th St., Indianapolis, Indiana, USA. Bradley L. Jones and. Peter Aitken. C. in21Days. Teach Yourself. SIXTH EDITION. vi. Sams Teach Yourself Objective-C in 24 Hours. HOUR 2: Object-Oriented Programming with Objective-C. Object-Oriented Programming in the Objective-C.
|Language:||English, Spanish, Indonesian|
|Genre:||Fiction & Literature|
|ePub File Size:||26.82 MB|
|PDF File Size:||17.46 MB|
|Distribution:||Free* [*Regsitration Required]|
Learning About the Python Development Environment Shell. Creating Exploring the tkinter Widgets Python Programming for. Sams Teach Yourself C in 24 Hours. (Imprint: Sams) (Publisher: Macmillan Computer Publishing) Author: Tony Zhang ISBN: West rd St., Indianapolis, Indiana, USA. Teach Yourself in Hours. Tony Zhang. C. SECOND EDITION.
Read more. Q Why do you need a linker? So, here I described full guideline step by step. Part II. Q Is the unary minus operator - the same as the subtraction operator -? Also, it's very easy to read or write in C because C is a high-level programming language that is close to human languages, especially English. Incrementing or Decrementing by One The increment and decrement operators are very handy to use when you want to add or subtract 1 from a variable.
All programs in this book can be compiled by any compilers that support the ANSI standard. If you're interested in a specific compiler, you can learn the compiler-specific functions from the compiler's reference manual.
Assumptions About You No previous programming experience is required for you to learn the C language from this book, although some knowledge of computers helps.
Also, it's up to you to determine how quickly to go through the 24 hours of this book: You could sit up with a big pot of coffee and power through the book in a sitting or you could take an hour a day for 24 days.
Setting Up Your System Basically, you need a computer and a C compiler in order to compile and run your own C programs or the C programs from this book.
The recommended hardware and software are listed in the following sections. Hardware Any type of computer that has or can access a C compiler is fine. More likely, you may have a PC on your desktop. For a Windows-based C compiler, you must have a bigger hard drive and add more memory to your computer. Check your compiler vendor for more details. Software If you're using a UNIX-based workstation, you might already have a C compiler loaded on your machine, or at least you might be able to access a C compiler on a server machine.
On a UNIX-based machine, you should know how to use a text editor, such as vi and emacs, to write C programs. If you have a PC, you need to install a C compiler and a text editor on it. Most C compilers come with an editor. You can also use a text editor that is already installed on your machine.
Both of them provide an integrated development environment IDE , which you can use to edit your C programs, along with other fancy features for advanced Windows programming.
You shouldn't have any problems installing a C compiler on your machine if you read the manuals that come with the compiler and follow the installation instructions correctly. I have both Windows 95 and Windows NT on my machine. However, many of those features are not needed for running the C programs in this book. I set up my development environment in such a way that all C programs in this book can be compiled and made into DOS applications.
Summary In this first lesson you've learned the following: In the next lesson you'll learn to write your first C program. A The computer's machine language is the lowest because the machine language, made up of 0s and 1s, is the only language that the computer can understand directly. Q What are the advantages of high-level programming languages?
A Readability, maintainability, and portability are the main advantages of high-level programming languages.
Q What is C, anyway? A C is a general-purpose programming language. It's a high-level language that has advantages such as readability, maintainability, and portability. Also, C allows you to get down to the hardware to increase the performance speed if needed. A C compiler is needed to translate programs written in C into machine-understandable code.
The portability of C is realized by recompiling the C programs with different C compilers specified for different types of computers. Q Can I learn C in a short time? A Yes. C is a small programming language. There are not many C keywords or commands to remember. Also, it's very easy to read or write in C because C is a high-level programming language that is close to human languages, especially English.
You can learn C in a relatively short time. Workshop To help solidify your understanding of this hour's lesson, you are encouraged to answer the quiz questions provided in the Workshop before you move to the next lesson. What are the lowest-level and highest-level languages mentioned in this book? Can a computer understand a program written in C? What do you need to translate a program written in C into the machine-understandable code that is, binary code?
If needed, can a C program be reused in another C program? Later in this lesson you're going to write your own C program for the first time. TYPE Listing 2. A simple C program. This is my first C program. Note that the name of a C program file must have an extension of. If you've installed a C compiler and set up the proper development environment, you should be able to compile this C program and make it into an executable file. I set up my development environment in such a way that all C programs in this book can be compiled and made into DOS-based applications.
For instance, 02L Note that. Also, on my machine, I save all the executable files made from the C programs in this book into a dedicated directory called C: The following output is a copy from the screen: The first line contains a comment: The C compiler ignores everything between the opening comment mark and closing comment mark.
That means the comment in the first line of Listing 2. This is my first C program, is ignored by the compiler. The only purpose of including comments in your C program is to help you document what the program or some specific sections in the programs do.
Remember, comments are written for programmers like you. For example, when you read someone's code, the comments in the code help you to understand what the code does, or at least what the code intends to do. You don't need to worry about the size or performance speed of your C program if you add many comments into it. Adding comments into a C program does not increase the size of the binary code of the program that is, the executable file , although the size of the program itself that is, the source code may become larger.
Also, the performance speed of the executable file made from your C program is not affected by the comments inside your C program. Most C compilers allow you to write a comment that crosses more than one line. For instance, you can write a comment in C like this: The comment ends at the end of the line. One thing that needs to be pointed out is that the ANSI standard does not support nested comments, that is, comments within comments.
For instance, the following is not allowed by the ANSI standard: The C compiler will ignore the statements you comment out. Later, you can always restore the previously commented-out statements simply by removing the opening comment and closing comment marks.
In this way, you don't need to erase or rewrite any statements during the testing and debugging. The include Directive Let's now move to line 2 in the C program of Listing 2. In C, include forms a preprocessor directive that tells the C preprocessor to look for a file and place the contents of the file in the location where the include directive indicates. The preprocessor is a program that does some preparations for the C compiler before your code is compiled.
You may guess that the file the include directive asks for is something called stdio. You are exactly right! Here, the include directive does ask the C preprocessor to look for and place stdio.
The name of the stdio. The stdio. In other words, C is a case-sensitive language. For instance, stdio. H are different filenames in C. Likewise, main and Main are two different function names.
Header Files The files that are required by the include directive, like stdio. Actually, the extension name of. You may be wondering what the angle brackets do. In C, the angle brackets ask the C preprocessor to look for a header file in a directory other than the cur- rent one.
For instance, the current directory containing the 02L C file is called C: If you want to let the C preprocessor look into the current directory first for a header file before it starts to look elsewhere, you can use double quotes to surround the name of the header file. For instance, when the C preprocessor sees "stdio. Normally, the header files are saved in a subdirectory called include.
Then the path to access the header files becomes C: The main Function In line 4 of Listing 2. Every C program must have a main function, and every C program can only have one main function. However, the execution of your program always starts with the main function. In Listing 2. Because this is a very simple program, the main function is the only function defined in the program. Within the main function body, a C library function, printf , is called in order to print out a greeting message.
See line 6. More details about printf are covered in Hour 5. One more important thing about main is that the execution of every C program ends with main.
A program ends when all the statements within the main function have been executed. Usually suffixed at the end of a message, the newline character tells the computer to generate a carriage-return and line-feed sequence so that anything printed out after the message will start on the next new line on the screen.
Exercise 3 in this lesson gives you a chance to use the newline character to break a one-line message into two lines. The return Statement All functions in C can return values. For instance, when you make a function to add two numbers, you can make such a function that returns to you the value of the addition. The main function itself returns a value. By default, main returns an integer. In C, integers are decimal numbers without fraction portions.
Therefore, in line 7 of Listing 2. A nonzero value returned by the return statement tells the operating system that an error has occurred. The bigger the return value, the more severe the error. The exit Function There is also a C library function, exit , that can be used to cause a program to end. Because the exit function is defined in a header file, stdlib. Unlike main , the exit function itself does not return any values, but the argument to exit indicates whether the program is terminated normally.
A nonzero argument to the exit function tells the operating system that the program has terminated abnormally. Actually, you can replace return 0; in line 7 of Listing 2. Note that return and exit can also be used in other functions. You'll see more examples in the rest of the book. Listing 2. A C program with exit. When a void is placed prior to a function name, it indicates that the function does not return a value. As you have learned, the exit function does not return any values, but, by default, the main function does.
Therefore, as shown in line 5 of Listing 2. Let's have a look at how a C program is compiled and translated into an executable file binary code. As shown in Figure 2. Figure 2. Make an executable file by the compiler and linker. First, a program written in C, called source code, is made.
Then the source code is compiled by a C compiler, which creates a new file. The new file is an object file.
In the UNIX operating system, the name of an object file ends with the extension. You cannot execute the object file because there is some function code missing. You have to finish the next step: Linking is done by invoking a special program called a linker, which normally comes with the compiler package. A linker is used to link together the object file, the ANSI standard C library, and other user-generated libraries to produce an executable file—the binary code. In this stage, the binary code of the library functions that are called in the source code is combined with the object file; the result is saved into a new file—an executable file.
As you learned in the first hour of this book, the name of an executable file usually ends with the extension. In UNIX, it's not necessary to include such an extension to an executable filename.
Later, you'll learn that in many cases, there may be several object files that have to be linked together in order to make an executable program.
Note that the object file and executable file are both machine-dependent. You cannot simply move an executable file, without recompiling the source code, from the current computer platform to another one that is operated by a different operating system even though the source code of the executable file, presumably written in ANSI C, is machine independent that is, portable.
What's Wrong with My Program? When you finish writing a C program and start to compile it, you might get some error or warning messages. Don't panic when you see error messages. We're human beings. Everybody makes mistakes. Actually, you should appreciate that your compiler catches some errors for you before you go any further.
Usually, your compiler can help you check the grammar of your C program and make sure you've followed the C programming rules properly. For instance, if you forget to put the ending brace on the main function in line 8 of Listing 2. Also, the linker will issue an error message if it cannot find the missing code for a needed function in the libraries. For instance, if you misspell printf as pprintf in the program of Listing 2.
All errors found by the compiler and linker must be fixed before an executable file binary code can be made. Debugging Your Program In the computer world, program errors are also called bugs.
In many cases, your C compiler and linker do not find any errors in your program, but the result generated by running the executable file of the program is not what you expect.
In order to find those "hidden" errors in your program, you may need to use a debugger. Normally, your C compiler vendor already includes a debugger software program in the C compiler package.
The debugger can execute your program one line at a time so that you can watch closely what's going on with the code in each line, or so that you can ask the debugger to stop running your program on any line.
For more details about your debugger, refer to the manuals made by your C compiler vendor.
Later in this book, you'll learn that debugging is a very necessary and important step in writing software programs. You can put comments anywhere you like in your programs. The program execution starts and ends with the main function. The exit function terminates a program; the argument to the function indicates the error status, too. Applying void to main tells the operating system that the main function does not return any value after termination.
Debugging is a very important step in your program design and coding. In the next lesson you'll learn more about the essentials of C programs. A Comments help us document what a program, or a special portion of a program, does. Especially when a program becomes very complex, we need to write comments to mark different parts in the program.
Q Why is the main function needed in your program? A The execution of a C program starts and ends with the main function. Without the main function, the computer does not know where to start to run a program. Q What does the include directive do? A The include directive is used to include header files that contain the declarations to the functions used in your C program. In other words, the include directive tells the C preprocessor to look into directories and find the specified header file.
Q Why do you need a linker? A After compiling, some function code may still be missing in the object file of a program.
Workshop To help solidify your understanding of this hour's lesson, you are encouraged to answer the quiz questions and finish the exercises provided in the Workshop before you move to the next lesson. The answers and hints to the questions and exercises are given in Appendix E, "Answers to Quiz Questions and Exercises. Can a C compiler see the comments within your C program?
What kinds of files does a C compiler actually produce? Does the exit function return a value? How about the return statement? What is a header file? Exercises 1. It's time for you to write your own first program. Referring to the program in Listing 2. It's fun to write my own program in C. Update the program in Listing 2. You should see two lines of the message on the screen after running the updated executable file: Howdy, neighbor! What warning or error messages will you get when you're trying to compile the following program?
What error messages will you get when you're trying to compile the following program? You also learned about the basic structure of a C program.
You know that a program written in C has to be compiled before it can be executed. These elements are discussed in the following sections. But first, you need to learn two smaller but important elements, constant and variable, which make up expressions.
Constants and Variables As its name implies, a constant is a value that never changes. A variable, on the other hand, can be used to present different values. A variable is more like an audio cassette: You can always update the contents of the cassette by simply overwriting the old songs with new ones. You can see many examples in which constants and variables are in the same statement.
For instance, consider the following: In other words, i contains the value of 1 after the statement is executed. Because i can contain different values, it's called a variable in the C language. Expressions An expression is a combination of constants, variables, and operators that are used to denote computations. For instance, the following: The final result of the expression is Here are some other examples of expressions: Expression Description 6 An expression of a constant.
In the C language, these symbols are called arithmetic operators. Table 3. C arithmetic operators. Among the arithmetic operators, the multiplication, division, and remainder operators have a higher precedence than the addition and subtraction operators. As you might know, you can put parentheses around an addition or subtraction to force the addition or subtraction to be performed before a multiplication, division, or modulus computation.
In many cases, you can turn an expression into a statement by simply adding a semicolon at the end of the expression. Here are some other examples of statements: A statement block is treated as a single statement by the C compiler.
For instance, the following for. Here for is a keyword in C that determines the statement block. Many C keywords can only control one statement.
If you want to put more than one statement under the control of a C keyword, you can add those statements into a statement block so that the block is considered one statement by the C keyword. Anatomy of a C Function Functions are the building blocks of C programs. Besides the standard C library functions, you can also use some other functions made by you or another programmer in your C program.
In Hour 2 you saw the main function, as well as two C library functions, printf and exit. Now, let's have a closer look at functions. As shown in Figure 3. The anatomy of a function in the C language. The six parts of a function are explained in the following sections.
Determining a Function's Type The function type is used to signify what type of value a function is going to return after its execution.
In Hour 2, for instance, you learned that the default function type of main is integer. You also learned how to change the function type of main to void so that the main function does need to return any value. In C, int is used as the keyword for the integer data type. In the next hour, you'll learn more about data types. Giving a Function a Valid Name A function name is given in such a way that it reflects what the function can do. For instance, the name of the printf function means "print formatted data.
The following are examples of illegal function names in C: Illegal Name The Rule 2 digit A function name cannot start with a digit.
Some samples of valid function names are as follows: For example, in Listing 2. Pieces of information passed to functions are known as arguments. The argument of a function is placed between the parentheses that immediately follow the function name.
The number of arguments to a function is determined by the task of the function. If no information needs to be passed to a function, you just leave the argument field between the parentheses blank. For instance, the main function in Listing 2. The Beginning and End of a Function As you may have already figured out, braces are used to mark the beginning and end of a function. As mentioned earlier, the braces are also used to mark the beginning and end of a statement block. You can think of it as a natural extension to use braces with functions because a function body can contain several statements.
The Function Body The function body in a function is the place that contains variable declarations and C statements. The task of a function is accomplished by executing the statements inside the function body one at a time.
Listing 3. TYPE Listing 3. A function that adds two integers. In line 2, you see that the int data type is prefixed prior to the function name. Here int is used as the function type, which signifies that an integer should be returned by the function. The argument list contains two arguments, int x and int y, in line 2, where the int data type specifies that the two arguments are both integers.
Line 4 gives the variable declaration of result, whose value is specified by the int data type as an integer. The statement in line 5 adds the two integers represented by x and y and assigns the computation result to the result variable. The return statement in line 6 then returns the computation result represented by result.
TIP When you create a function in your C program, don't assign the function too much work. If a function has too much to do, it will be very difficult to write and debug. And try your best to make sure that each function has just one task to do. An example of such a program is demonstrated in Listing 3. A C program that calculates an addition and prints the result to the screen.
After this program is compiled and linked, an executable file for 03L On my machine, the executable file is named 03L The following is the output printed on the screen after I run the executable from a DOS prompt on my machine: As you learned in Hour 2, the include directive in line 2 includes the stdio.
The main function, prefixed with the int data type, starts in line Lines 12 and 18 contain the opening brace and closing brace for the main function, respectively. An integer variable, sum, is declared in line You first saw the C standard library function printf in Hour 2. Here you may find something new added to the function in line You're right. This time, there are two arguments that are passed to the printf function. The second argument is the integer variable sum.
You're right again. Summary In this lesson you've learned the following: A variable, on the other hand, can present different values.
An expression is used to denote different computations. An argument list contains two or more arguments that are separated by commas. Usually, a function should accomplish just one task. In the next lesson you'll learn more about data types in the C language. A The major difference is that the value of a constant cannot be changed, while the value of a variable can. You can assign different values to a variable whenever it's necessary in your C program. Q Why do you need a statement block?
A Many C keywords can only control one statement. A statement block provides a way to put more than one statement together and put the statement block under the control of a C keyword.
Then, the statement block is treated as a single statement. Q Which arithmetic operators have a higher precedence? A Among the five arithmetic operators, the multiplication, division, and remainder operators have a higher precedence than the addition and subtraction operators. Q How many parts does a function normally have? A A function normally has six parts: In the C language, is 74 a constant?
How about ? Are the following function names valid? What is wrong with the following function? Write a C function that can multiply two integers and return the calculated result. Write a C program that calls the C function you just wrote in exercise 4 to calculate the multiplication of 3 times 5 and then print out the return value from the function on the screen. That which we call a rose By any other name would smell as sweet. Also in this hour you're going to learn about the four data types of the C language in detail: Those reserved words are sometimes called C keywords.
You should not use the C keywords as variable, constant, or function names in your program. The following are the 32 reserved C keywords: In the rest of the book, you'll become more familiar with them and start to use many of the keywords through examples and exercises. Note that all C keywords are written in lowercase letters.
As I've mentioned, C is a case-sensitive language. Therefore, int, as shown in the list here, is considered as a C keyword, but INT is not. The char Data Type An object of the char data type represents a single character of the character set used by your computer. For example, A is a character, and so is a. But 7 is a number. But a computer can only store numeric code. Therefore, characters such as A, a, B, b, and so on all have a unique numeric code that is used by computers to represent the characters.
Usually, a character takes 8 bits that is, 1 byte to store its numeric code. The original ASCII character set has only characters because it uses the lower 7 bits that can represent 27 that is, characters.
On IBM-compatible PCs, however, the character set is extended to contain a total of that is, 28 characters. Character Variables A variable that can represent different characters is called a character variable. You can set the data type of a variable to char by using the following declaration format: If you have more than one variable to declare, you can either use the following format: You'll see later in the book that the double quote is used to indicate a string of characters.
Therefore, given x as a character variable, for instance, the following two assignment statements are equivalent: Besides the newline character, several other special characters exist in the C language, such as the following: Printing Out Characters You already know that the printf function, defined in the C header file stdio.
Let's first have a look at the program in Listing 4. TYPE Listing 4. Printing out characters on the screen. On my machine, the executable file is named as 04L The following is the output printed on the screen after I run the executable from a DOS prompt: Convert the value of c2 to character: When the two statements in lines 11 and 12 are executed, two characters are formatted and output to the screen, based on the numeric values contained by c1 and c2, respectively.
Now look at the program shown in Listing 4. Converting numeric values back to characters. The character that has the numeric value of 97 is: Note that in lines 9 and 10 of Listing 4. As you know, 65 is the numeric value decimal of the A character in the ASCII character set; 97 is the numeric value of a. The A and a characters are then printed out on the screen. The int Data Type You saw the integer data type in Hour 3.
The int keyword is used to specify the type of a variable as an integer. Integer numbers are also called whole numbers, which have no fractional part or decimal point. Therefore, the result of an integer division is truncated, simply because any fraction part is ignored. Depending on the operating system and the C compiler you're using, the length of an integer varies.
Declaring Integer Variables You also saw the declaration of an integer in Hour 3. The following shows the basic declaration format: You might recall that in line 16 of Listing 3. In this section you're going to study a program, shown in Listing 4. Showing the numeric values of characters. The numeric value of a is: As a matter of fact, I simply copied the source code from Listing 4. The float Data Type The floating-point number is another data type in the C language.
Unlike an integer number, a floating-point number contains a decimal point. For instance, 7. A floating-point number is also called a real number. A floating-point number is specified by the float keyword in the C language. Floating-point numbers can be suffixed with f or F to specify float. A floating-point number without a suffix is double by default. The double data type is introduced later in this lesson.
Like an integer number, a floating-point number has a limited range. The ANSI standard requires that the range be at least plus or minus 1. Normally, a floating-point number is represented by taking 32 bits. Therefore, a floating-point number in C is of at least six digits of precision. That is, for a floating-point number, there are at least six digits or decimal places on the right side of the decimal point.
Not like an integer division from which the result is truncated and the fraction part is discarded, a floating-point division produces another floating-point number. A floating-point division is carried out if both the divisor and the dividend, or one of them, are floating-point numbers. For instance, So do Declaring Floating-Point Variables The following shows the declaration format for a floating-point variable: Listing 4.
Printing out results of integer and floating-point divisions. Integer vs. Therefore, you just see the integer 3 in both the third and fifth lines of the output. The double Data Type In the C language, a floating-point number can also be represented by another data type, called the double data type.
In other words, you can specify a variable by the double keyword, and assign the variable a floating-point number. The difference between a double data type and a float data type is that the former normally uses twice as many bits as the latter. Therefore, a double floating-point number is of at least 10 digits of precision, although the ANSI standard does not specify it for the double data type.
In Hour 8, "More Operators," you'll learn to use the sizeof operator to obtain the length of a data type, such as char, int, float, or double, specified on your computer system.
Using Scientific Notation The C language uses scientific notation to help you write lengthy floating-point numbers.
In scientific notation, a number can be represented by the combination of the mantissa and the exponent. The format of the notation is that the mantissa is followed by the exponent, which is prefixed by e or E. Here are two examples: For instance, can be represented by 5e3 in scientific notation. Likewise, can be represented by -3e2, and 0.
In this section, let's focus on naming variables. Function names and variable names are both identifiers in C.
The following are all the characters you can use to make a valid variable name: Now, let's see what you cannot use in variable naming: Summary In this lesson you've learned about the following: A floating-point number without suffix is double by default. A Characters are stored in computers in the form of bits. The combinations of bits can be used to represent different numeric values.
A character has to have a unique numeric value in order to distinguish itself.
Many computer systems support the ASCII character set, which contains a set of unique numeric values for up to characters. Q How can you declare two character variables?
A There are two ways to do the declaration. A These are format specifiers. Q What are the main differences between the int data type integer and the float data type floating- point? A First, an integer does not contain any fraction parts, but a floating-point number does.
A floating-point number must have a decimal point. In C, the float data type takes more bits than the int data type. In other words, the float data type has a larger range of numeric values than the int data type.
Also, the integer division truncates the fraction part. How can you represent the following numbers in scientific notation? Are the following variable names valid? Write a program that prints out the numeric values of characters Z and z. Given two numeric values, 72 and , write a program to print out the corresponding two characters.
For a bit integer variable, can you assign the variable with an integer value of ? In this lesson you're going to learn more about printf , as well as about the following functions, which are necessary to receive the input from the user or print the output to the screen: Because all characters and numbers are represented in bits on computers, the C language treats a file as a series of bytes.
A series of bytes is also called a stream. In fact, the C language treats all file streams equally, although some of the file streams may come from a disk or tape drive, from a terminal, or even from a printer. Additionally, in C, there are three file streams that are pre-opened for you: Usually, the standard input stdin file stream links to your keyboard, while the standard output stdout and the standard error stderr file streams point to your terminal screen.
Also, many operating systems allow you to redirect these files' streams. In fact, you've already used stdout. When you executed the printf function in the last lesson, you were in fact sending the output to the default file stream, stdout, which points to your screen.
You'll learn more on stdin and stdout in the following sections. The header file stdio. Therefore, always include the header file stdio. Getting the Input from the User In these days, typing from keyboard is still the de facto standard way to input information into computers. The C language has several functions to direct the computer to read the input from the user typically through the keyboard.
In this lesson the getc and getchar functions are introduced. Using the getc Function The getc function reads the next character from a file stream, and returns the character as an integer. The function returns the numeric value of the character read. If an end-of-file or error occurs, the function returns EOF. At this moment, don't worry about the FILE structure. Part II. NOTE Defined in the header file stdio.
EOF stands for end-of-file. Usually, the value of EOF is But keep using EOF, instead of -1, if you need an end-of-file indicator, in case a compiler uses a different value. Listing 5. TYPE Listing 5. Reading in a character entered by the user. H The character you just entered is: Line 8 prints out a piece of message that asks the user to enter one character from the keyboard. As I mentioned earlier in this lesson, the printf function in line 8 uses the default standard output stdout to display messages on the screen.
In line 9, the standard input stdin is passed to the getc function, which indicates that the file stream is from the keyboard.
After the user types in a character, the getc function returns the numeric value that is, an integer of the character. You see that, in line 9, the numeric value is assigned to the integer variable ch. Then, in line 10, the character entered by the user is displayed on the screen with the help of printf. Using the getchar Function The C language provides another function, getchar , to perform a similar operation to getc. More precisely, the getchar function is equivalent to getc stdin. The program in Listing 5.
Reading in a character by calling getchar. Hi The first character you just entered is: H The second character you just entered is: The statement in line 6 declares two integers, ch1 and ch2. Line 8 displays a message asking the user to enter two characters together. Then, the getc and getchar functions are called in lines 9 and 10, respectively, to read in two characters entered by the user. Note that in line 10, nothing is passed to the getchar function. This is because, as mentioned earlier, getchar has its default file stream—stdin.
You can replace the getchar function in line 10 with getc stdin , because getc stdin is equivalent to getchar. Lines 11 and 12 send two characters kept by ch1 and ch2, respectively to the screen. Printing the Output on the Screen Besides getc and getchar for reading, the C language also provides two functions, putc and putchar , for writing. The following two sections introduce these functions. Using the putc Function The putc function writes a character to the specified file stream, which, in our case, is the standard output pointing to your screen.
If successful, putc returns the character written; otherwise, it returns EOF.
In this lesson the standard output stdout is used to be the specified file stream in putc. The putc function is used in Listing 5. Putting a character on the screen. The integer variable, ch, declared in line 6, is assigned the numeric value of 65 in line 8.
You may remember that 65 is the numeric value of character A. Then, the putc function in line 10 puts character A on the screen. Note that the first argument to the putc function is the integer variable ch that contains 65, and the second argument is the standard output file stream, stdout. Another Function for Writing: The only difference between the two functions is that putchar needs only one argument to contain the character. You don't need to specify the file stream, because the standard output stdout is the default file stream to putchar.
The function returns EOF if an error occurs; otherwise, it returns the character that has been written. An example of using putchar is demonstrated in Listing 5. Outputting characters with putchar. There is no variable declared in the program. As you might have figured out, 65, 66, and 67 are, respectively, the numeric values of characters A, B, and C. Therefore, respectively, lines 6 and 7 put character A on the screen and cause the computer to start at the beginning of the next line. Likewise, line 8 puts B on the screen, and line 9 starts a new line.
Then, line 10 puts C on the screen, and line 11 starts another new line. Accordingly, A, B, and C, are put at the beginnings of three consecutive lines, as shown in the output section. Revisiting the printf Function The printf function is the first C library function you used in this book to print out messages on the screen.
The number of expressions is determined by the number of the format specifiers inside the first argument. The function returns the numbers of expressions formatted if it succeeds. It returns a negative value if an error occurs. For the time being, consider the first argument to the printf function as a series of characters surrounded with double quotes with some format specifiers inside. Figure 5. Note that the format specifiers and the expressions are matched in order from left to right.
The relation between the format string and the expressions in printf. Please remember that you should use exactly the same number of expressions as the number of format specifiers within the format string.
The following are all the format specifiers that can be used in printf: Several others are explained later in this book. Converting to Hex Numbers The difference between a decimal number and a hexadecimal number is that the hexadecimal is a base numbering system. A hexadecimal number can be represented by four bits.
Hexadecimal is often written as hex for short. The hexadecimal numbers 0 through 9 use the same numeric symbols founded in the decimal numbers 0 through 9. Similarly, in lowercase, a, b, c, d, e, and f are used to represent these hex numbers. Converting to hex numbers.
In fact, the program in Listing 5. The printf function in line 6 prints out a headline that contains three fields: Hex uppercase , Hex lowercase , and Decimal. Sixteen printf functions are called to accomplish the job.
Each of the printf functions has a format string as the first argument followed by three integers as three expressions. In reality, nobody would write a program like the one in Listing 5. Instead, a loop can be used to call the printf function repeatedly. From where you can learn C computer programming. The demand for C programming now and in the future. So, keep reading. The C programming language is very necessary because, without it, the system program Windows, Mac, Linux can not build efficiently.
So, C languag…. Read more. Top techniques to increase your concentration and focus while programming February 19, When you code for several hours at a time, you feel uncomfortable, and also you lose your focus due to many factors. However, after reading this article, you will recognize Why do you lose your concentration and how to prevent this problem by applying some easy techniques. Continuous programming without rest is one of the main factors to lose concentration.
Hence the question is that how do you stay focused while coding. For that, you should recognize Concentration and focus increasing techniques. The techniques are given below- Find out encumber Destruction You can improve your concentration and focus by apprehending What things distract your attention.
Then remove questionable things that distract you. Smartphone or Gaming console is the major dis-tractor. So, promise yourself that after completing your work you will use it or put your smartphone in a desk drawer.
Internet connection may also decrease your focus. For this, if possible to do your work without the web, then I ….