Date published 


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.

Language:English, Spanish, Dutch
Country:South Africa
Published (Last):25.05.2016
Distribution:Free* [*Registration needed]
Uploaded by: MEGGAN

75980 downloads 126507 Views 28.56MB PDF Size Report

Learn C In 24 Hours Pdf

—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.

Skip to main content. Log In Sign Up. In this first lesson you'll learn the following: C is a programming language. Ritchie called his newly developed language C simply because there was a B programming language already. As a matter of fact, the B language led to the development of C. C is a high-level programming language.

The site advanced search for [ title: teach, yourself, hours, since: and found such books. Of the top ten, nine are programming books the other is about bookkeeping. Similar results come from replacing "teach yourself" with "learn" or "hours" with "days. Felleisen et al. Idiots can learn it in 21 days, even if they are dummies. In short, you won't have time to learn much. So the book can only be talking about a superficial familiarity, not a deep understanding.

As Alexander Pope said, a little learning is a dangerous thing. So what's the point? Alan Perlis once said: "A language that doesn't affect the way you think about programming, is not worth knowing". But then you're not learning how to program; you're learning to accomplish that task. The key is deliberative practice: not just doing it again and again, but challenging yourself with a task that is just beyond your current ability, trying it, analyzing your performance while and after doing it, and correcting any mistakes.

Then repeat. And repeat again. There appear to be no real shortcuts: even Mozart, who was a musical prodigy at age 4, took 13 more years before he began to produce world-class music. In another genre, the Beatles seemed to burst onto the scene with a string of 1 hits and an appearance on the Ed Sullivan show in But they had been playing small clubs in Liverpool and Hamburg since , and while they had mass appeal early on, their first great critical success, Sgt. Peppers, was released in Malcolm Gladwell has popularized the idea, although he concentrates on 10, hours, not 10 years.

Henri Cartier-Bresson had another metric: "Your first 10, photographs are your worst. True expertise may take a lifetime: Samuel Johnson said "Excellence in any department can be attained only by the labor of a lifetime; it is not to be downloadd at a lesser price. As Prof. Anders Ericsson puts it, "In most domains it's remarkable how much time even the most talented individuals need in order to reach the highest levels of performance.

After reading this article, you will understand-The significance of "C" language. 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. 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.

Teach Yourself Programming in Ten Years

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.

Teach Yourself Programming in Ten Years

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.

Learn Objective-C in 24 Days

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. The integer is called the minimum field width specifier because it specifies the minimum field width and ensures that the output reaches the minimum width. The example in Listing 5. Specifying the minimum field width. Without using any minimum field width specifiers, lines 10 and 11 print out the two integers by calling the printf function.

You can see in the output section that the output made by the statements in line 10 is 12, which takes two character spaces, while the output, , from line 11 takes five character spaces. The output from line 12 therefore takes five character spaces, with three blank spaces plus two character spaces of See the third output line in the output section. This means that when the minimum field width is shorter than the width of the output, the latter is taken, and the output is still printed in full.

Aligning Output As you might have noticed in the previous section, all output is right-justified. In other words, by default, all output is placed on the right edge of the field, as long as the field width is longer than the width of the output. You can change this and force output to be left-justified. To do so, you need to prefix the minimum field specifier with the minus sign -. Left- or right-justified output.

Note that all the printf functions have the same first argument: The combination of the period. The precision specifier is another important specifier you can use to determine the number of decimal places for floating-point numbers, or to specify the maximum field width or length for integers or strings. Strings in C are introduced in Hour 13, "Manipulating Strings.

Remember, the default number of decimal places is 6. Using precision specifiers. Lines 9 and 10 assign and Therefore, you see that five zeros are padded prior to the integer in the second line of the output. Note here that the left-justification is also specified by the minus sign - in the floating-point format specifier.

The floating-point number Therefore, In the next lesson you'll learn about some important operators in C. A In C, a file is treated as a series of bytes that is called file stream. Q How much is the hex number 32? A Hexadecimal, or hex for short, is a base numerical system. Q Are getc stdin and getchar equivalent?

A Because the getchar function reads from the file stream stdin by default, getc stdin and getchar are equivalent. Here the first numeric value of 12 is going to be printed out in integer format, while the second 12 in the expression section will be displayed in the hex format. Generally speaking, the number of format specifiers in the format section should match the number of expressions in the expression section.

Can you align your output at the left edge, rather than the right edge, of the output field? What is the difference between putc and putchar? What does getchar return? Write a program to put the characters B, y, and e together on the screen. Display the two numbers and Given three integers—15, , and —write a program that prints the integers on the screen in the hex format. Write a program that uses getchar and putchar to read in a character entered by the user and write the character to the screen.

If you compile the following C program, what warning or error messages will you get? Carroll You can think of operators as verbs in C that let you manipulate data.

Additionally, the assignment statement itself returns the same value that is assigned to the left- hand-operand. After the execution of the statement, both a and b contain the value of 5. Given two integer variables, x and y, how do you assign the sum of x and y to another integer variable, z? Now, consider the same example again. This time, instead of assigning the result to the third variable, z, let's write the sum back to the integer variable, x: TYPE Listing 6.

Using arithmetic assignment operators. On my machine, this executable file is named as 06L Line 11 then prints out the initial values assigned to x, y, and z. The statement in line 13 uses the one addition operator and one assignment operator to add the values contained by x and y, and then assigns the result to x.

Line 14 displays the result on the screen. Similarly, lines 17 and 18 do the same addition and display the result again, after the variable x is reset in line Also, line 16 in Listing 6.

The value of x is reset again in line The printf function in line 22 displays the result, 13, on the screen. The two results are actually the same that is, 40 , because the two computations in lines 25 and 29 are equivalent. Getting Negations of Numeric Numbers If you want to change the sign of a numeric number, you can put the minus operator - right before the number. For instance, given an integer of 7, you can get its negation by changing the sign of the integer like this: Here, - is the minus operator.

Precisely, - is called the unary minus operator in C. This is because the operator takes only one operand. The type of the operand can be any integer or floating-point number. You can apply the unary minus operator to an integer or a floating-point variable as well. For instance, the following statement: 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.

The decrement operator is Actually, there are two versions of the increment operator and of the decrement operator. Likewise, in the --x; statement, the pre-decrement operator first subtracts 1 from x and then gets the value of x. Similarly, in x--, the decrement operator is called the post-decrement operator.

In other words, the post-increment operator makes a copy of the original value of x and stores the copy in a temporary location. Then, x is increased by 1. The post-decrement operator has a similar story. This operator returns a copy of the original value of a variable, rather than the current value of the variable which has been decreased by 1.

The program in Listing 6. Using pre- or post-increment and decrement operators.