




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





























































































What is C?
C is a computer programming language. That means that you can use C to create lists of instructions for a computer to follow. C is one of thousands of program- ming languages currently in use. C has been around for several decades and has won widespread acceptance because it gives programmers maximum control and efficiency. C is an easy language to learn. It is a bit more cryptic in its style than some other languages, but you get beyond that fairly quickly. C is what is called a compiled language. This means that once you write your C program, you must run it through a C compiler to turn your program into an exe- cutable that the computer can run (execute). The C program is the human-readable form, while the executable that comes out of the compiler is the machine-readable and executable form. What this means is that to write and run a C program, you must have access to a C compiler.
We will start at the beginning with an extremely simple C program and build up from there. I will assume that you are using the Linux command line and gcc as your environment for these examples; if you are not, all of the code will still work fine – you will simply need to understand and use whatever compiler you have available.
The simplest C program, I
Let’s start with the simplest possible C program and use it both to understand the basics of C and the C compilation process. Type the following program into a standard text editor. Then save the program to a file named samp.c. If you leave off .c, you will probably get some sort of error when you compile it, so make sure you remember the .c. Also, make sure that your editor does not automatically append some extra characters (such as .txt) to the name of the file. Here’s the first program:
i n t main ( v o i d ) {
p r i n t f ( " T h i s i s o u t p u t from my f i r s t p ro g ram! \ n " ) ; r e t u r n 0 ; }
Spacing and indentation
When you enter this program, position #include so that the pound sign is in col- umn 1 (the far left side). Otherwise, the spacing and indentation can be any way you like it. On some Linux systems, you will find a program called indent, which will format code for you. The spacing and indentation shown above is a good example to follow.
Compilation and run
When executed, this program instructs the computer to print out the line “This is output from my first program!” – then the program quits. You can’t get much simpler than that!
To compile this code on a Linux machine, type
g cc samp. c −o samp
This line invokes the C compiler called gcc, asks it to compile samp.c and asks it to place the executable file it creates under the name samp. To run the program, type
. / samp
You should see the output “This is output from my first program!” when you run the program.
If you mistype the program, it either will not compile or it will not run. If the program does not compile or does not run correctly, edit it again and see where you went wrong in your typing. Fix the error and try again.
Variables
As a programmer, you will frequently want your program to “remember” a value. For example, if your program requests a value from the user, or if it calculates a value, you will want to remember it somewhere so you can use it later. The way your program remembers things is by using variables. For example:
i n t b ;
This line says, “I want to create a space called b that is able to hold one integer value.” A variable has a name (in this case, b) and a type (in this case, int, an integer). You can store a value in b by saying something like:
b = 5 ;
You can use the value in b by saying something like:
p r i n t f ("%d " , b ) ;
In C, there are several standard types for variables:
We will see examples of these other types as we go along.
Here is an explanation of the different lines in this program:
Let’s look at some variations to understand printf completely. Here is the simplest printf statement:
p r i n t f ( " H e l l o " ) ;
This call to printf has a format string that tells printf to send the word “Hello” to standard out. Contrast it with this:
p r i n t f ( " H e l l o \ n " ) ;
The difference between the two is that the second version sends the word “Hello” followed by a carriage return to standard out. The following line shows how to output the value of a variable using printf.
p r i n t f ("%d " , b ) ;
The %d is a placeholder that will be replaced by the value of the variable b when the printf statement is executed. Often, you will want to embed the value within some other words. One way to accomplish that is like this:
p r i n t f ( " The t e m p e r a t u r e i s " ) ; p r i n t f ("%d " , b ) ; p r i n t f ( " d e g r e e s \ n " ) ;
An easier way is to say this:
p r i n t f ( " The t e m p e r a t u r e i s %d d e g r e e s \ n " , b ) ;
You can also use multiple %d placeholders in one printf statement:
p r i n t f ("%d + %d = %d \ n " , a , b , c ) ;
In the printf statement, it is extremely important that the number of operators in the format string corresponds exactly with the number and type of the variables following it. For example, if the format string contains three %d operators, then it must be followed by exactly three parameters and they must have the same types in the same order as those specified by the operators.
You can print all of the normal C types with printf by using different placeholders:
The scanf function allows you to accept input from standard in, which for us is generally the keyboard. The scanf function can do a lot of different things, but it is generally unreliable unless used in the simplest ways. It is unreliable because it does not handle human errors very well. But for simple programs it is good enough and easy-to-use.
The simplest application of scanf looks like this:
s c a n f ("%d " , &b ) ;
The program will read in an integer value that the user enters on the keyboard (%d is for integers, as is printf, so b must be declared as an int) and place that value into b.
The scanf function uses the same placeholders as printf:
You must put & in front of the variable used in scanf. The reason why will become clear once you learn about pointers. It is easy to forget the & sign, and when you forget it your program will almost always crash when you run it.
In general, it is best to use scanf as shown here – to read a single value from the keyboard. Use multiple calls to scanf to read multiple values. In any real program, you will use the gets or fgets functions instead to read text a line at a time. Then you will “parse” the line to read its values. The reason that you do that is so you can detect errors in the input and handle them as you see fit.
The printf and scanf functions will take a bit of practice to be completely under- stood, but once mastered they are extremely useful.
Programming exercise
i n t main ( v o i d ) { i n t a , b , c ; p r i n t f ( " E n t e r t h e f i r s t v a l u e : " ) ; s c a n f ("%d " , &a ) ; p r i n t f ( " E n t e r t h e s e c o n d v a l u e : " ) ; s c a n f ("%d " , &b ) ; c = a + b ; p r i n t f ("%d + %d = %d \ n " , a , b , c ) ; r e t u r n 0 ; }
Boolean expression evaluates to True, then C executes the single line immediately following the if statement (or a block of lines within braces immediately following the if statement). If the Boolean expression is False, then C skips the line or block of lines immediately following the if statement. Here’s slightly more complex example:
i n t main ( v o i d ) { i n t b ; p r i n t f ( " E n t e r a v a l u e : " ) ; s c a n f ("%d " , &b ) ; i f ( b < 0 ) { p r i n t f ( " The v a l u e i s n e g a t i v e \ n " ) ; } e l s e i f ( b == 0 ) { p r i n t f ( " The v a l u e i s z e r o \ n " ) ; } e l s e { p r i n t f ( " The v a l u e i s p o s i t i v e \ n " ) ; } r e t u r n 0 ; }
In this example, the else if and else sections evaluate for zero and positive values as well.
Boolean expressions
Here is a more complicated Boolean expression:
i f ( ( x == y ) && ( j > k ) ) { z = 1 ; } e l s e { q = 1 0 ; }
This statement says, “If the value in variable x equals the value in variable y, and if the value in variable j is greater than the value in variable k, then set the variable z to 1, otherwise set the variable q to 10.” You will use if statements like this throughout your C programs to make decisions. In general, most of the decisions you make will be simple ones like the first example; but on occasion, things get more complicated.
Notice that C uses == to test for equality, while it uses = to assign a value to a variable. The && in C represents a Boolean AND operation.
Here are all of the Boolean operators in C:
e q u a l i t y == l e s s t h a n < G r e a t e r t h a n > <= <= >= >= i n e q u a l i t y != and && o r | | n o t!
= vs == in boolean expressions
The == sign is a problem in C because every now and then you may forget and type just = in a Boolean expression. This is an easy mistake to make, but to the compiler there is a very important difference. C will accept either = and == in a