CS1061 2018-2019 Prof J.P.Morrison
C Programming C is the most popular language worldwide. Everything from microcontrollers to operating systems is written in C flexible and versatile, allowing maximum control with minimal commands. C is the predecessor of many modern computer languages. C++, Java, Python,... C is compact and easy to learn. C is a compiled language you need to run it through a compiler to transform the humanreadable form into machine-readable language. We will be using the Dev-CPP Integrated development Environment (IDE) to compose and run our C code. C is efficient and gives the programmer a great deal of control over the machine.
A Little History The language C was developed in the early 1970s at Bell Laboratories, primarily credited to the work of Ken Thompson and Dennis Ritchie. A user-friendly language from writing programs for the UNIX operating system was needed, which at the time required programs written in assembly language. Assembly programs, which speak directly to a computer's hardware, are long and difficult to debug, and they required tedious, time-consuming work to add new features. By 1973, C was stable enough that UNIX itself could be rewritten in C.
Compiling C is a compiled language you have to use a compiler to turn source code into an executable file before you can run it. The source code is written in one or more text files, which you can open, read and edit in any text editor, such as Notepad in Windows, TextEditon a Mac, and geditin Linux. An executable file is something the computer can run (execute). The compiler checks the source code for errors and, if it is error-free, creates an executable file.
A first C program Our first C program: 1. /* Sample program */ 2. #include <stdio.h> 3. int main() 4. { 5. printf("this is output from my first program!\n"); 6. return 0; 7. } When compiled and executed, this program instructs the computer to print out the line "This is output from my first program!" and then stop. Line 1 --This is a comment. Ignored by the compiler, it is for the benefit of the human reader. Line 2 --The #include command tells the compiler to include the standard I/O library functions so that they can be used in your code. Line 3 --This line is the start of the main function. Every C program has a main function. This holds the instructions that are executed first when the program is run. A typical C program will many functions, each describing and solving a specific problem. Lines 4 and 7 --The instructions of a function are enclosed in braces. Some programmers start and end a brace-enclosed block on separate lines as shown here. Others will put the open-brace ({) at the end of the first line of the function definition. Though lines of code in the program don't have to be typed on separate lines, programmers typically put each instruction on a separate line, indented with spaces, to make the code easier to read and edit later. Line 5 --This is a function call to a function named printf. That function is part of the stdio.hlibrary included from Line 1. This is an example of building on the work of other people. Note, Every line in the function must end with a semi-colon. Line 6 --Every function that returns a value must include a return statement like this one. In C, the main function must always have an integer return type, The returned value in sent back to the place from where the program is called.
Common Programming Concepts in C Functions--a function is a block of code representing something the computer should do when the function is called. Variables--a named location that can hold a value of a particular type. Data types--all data values in C has a certain size, measured in binary bits or bytes, and a certain set of rules about what these bits represent. Types include integers, floats, characters,... Operators --Operators work on operand data to produce resultant data: arithmetic operators, Boolean operators, bit-manipulation operators, etc. Control Structures Allowing the programmer to make decisions based on particular values: If, while.