C-Language Tutorial Introduction The C programming language was originally developed by Dennis Ritchie of Bell Laboratories,and was designed to run on a UNIX operating system Although it was originally intended to run under UNIX, there was a great interest in running it on the IBM PC and compatibles,and other systems. It is an excellent language for this environment because of the simplicity of expression, the compactness of the code, and the wide range of applicability. It allows the programmer a wide range of operations from high level down to a very low level approaching the level of assembly language. There seems to be no limit to the flexibility available. Programming in C is a tremendous asset in those areas where you may want to use Assembly Language, but would rather keep it a simple to write and easy to maintain program. Approximately 75 percent of all new commercial programs introduced for the IBM PC have been written in C, and the percentage is probably growing. Apple Macintosh system software was formerly written in Pascal, but is now almost always written in C. There was no official standard for the C language, but the American National Standards Institute (ANSI) has developed a standard for the language,so it will follow rigid rules. Even though the C language enjoys a good record when programs are transported from one implementation to another, there are differences in compilers, as you will find any time you try to use another compiler.
Over the years several programming languages have been developed to help the programmers to get the computer to performed various tasks. While the programming languages have been varied in terms of key words they have, and the way in which they are written, the basic approach to writing a program has remained more or less the same. For Example: Consider the code written below using three different languages like C,BASIC,and Java * Code Snippet Written In C * * Code Snippet Written In BASIC * #include<stdio.h> 10 Input name$ void main() 20 Print Hello,name$ { 30 END Char name[15]; * Code Snippet Written In Java * Printf( Enter Name : ); Scanf( %s,&name); Printf( Hello %s,name); } Class Hello { Public static void main(string args[]) { String name; System.in.readln(name); System.out.println( Hello +name); } } 2
All the three snippets of code achieve the same task-that of accepting the users name and displaying a welcome message. Note,however,that although the syntax (the way in which the code is written using the keywords of a specific language) differs from one language to the other the logic (steps Followed) behind each of the programs remains the same. The primary reason for choosing C language for learning programming fundamentals is because the structure of C and a lot of keywords in it resemble the ones in java 3
GETTING STARTED WITH C Let us start with the C language. Following are the steps involved in writing a program using C-language 1) Invoking The Turbo C++ editor 2) Writing a new program 3) Saving the program using the given editor 4) Building the program 5) Quitting from turbo c and Executing the program NOTE: You can also write a program in c using Borland C editor 4
STEP 1-Invoking Turbo C++ Editor Click on start, go to run and type cmd to invoke the MS-DOS prompt Locate the directory where the turbo C++ editor is installed (i.e. C,D,etc) Enter the path to invoke the turbo c++ editor as shown in the figure given below 5
The Turbo C++ editor would look as shown in figure given blow 6
STEP 2-Writing a New Program To write a new program, follow the steps given below 1) Press the combination <Alt+F> to invoke the menu File This displays the submenu options. 2) Press down the key to select the option, New. Refer Figure below 7
3) Press <Enter> This opens a new file refer figure below The default name NONAME00.CPP is displayed in the title bar as shown below 4) Now you can type the required code in the Edit Window NOTE: C programs are case sensitive. the instructions need to be always in lower case 8
STEP 3- Saving the Program To save the program, follow the steps given below 5) Press the key combination <Alt+F> to invoke the menu File. 6) Press the down arrow key to select the option Save As... 7) Press <Enter> This displays the dialog box, Save File as Refer figure given below. 9
8) Type a valid filename in the Save File as text box (In this example, we use FIRST as the filename). Refer figure given below 9) Press<Enter> Note that FIRST.C (the name of the file) is displayed in the title bar 10
STEP-4 Building the Program After writing the program, before it can be executed, the program has to go through two stages: Compiling Process- changes the program code written in C to the code that can be understand by the machine 1. 2. Linking Process- stores the machine code in an executable file format The two stages compiling and linking are combined in the build process. To build the program, follow these steps 10) Press the key combination <Alt+C> to invoke the menu Compile This displays its sub menu options as shown in the figure below Continued 11
11) Press the down arrow key to select the option, Build all Selecting this option will compile and link the C program in one go 12
12) Press <Enter> This builds the program. On success, the Linking message box will display the message Success: Press any key to continue as shown in the figure below Notice that the name of the EXE file created by the build process is the same as that of the C file. In our case, the resultant EXE file created is called FIRST.exe 13
In C, when you build a program, the errors and warnings are displayed in a separate window at the bottom. Warnings only point out to inefficiency in the code. Therefore, if any warnings are displayed,the code can still be executed. However,in the case of errors, they have to solved before the program can be executed In this program, the warnings and errors are displayed in a separate window as shown in the given figure 13) Press <Enter> 14
STEP 5- Quitting the Program and Executing the Program To quit turbo c follow these steps 14) Press the key combination <Alt+F> to invoke the menu File 15) Press the down arrow key to select the option Quit 16) Press <Enter> This will take you back to the Dos Prompt To Execute the program follow these steps 1. 2. Press the key combination <Alt+R> to invoke the menu Run Press <Enter> Continued 15
16
Working With C Program Session 1: Concepts- Basic Programming Session 2: Working with Operators and Conditions Session 3: Working with Iteration Constructs Session 4: Using Functions Session 5: Working with Arrays and Structures Session 6: Pointers In C 17
Session 1:Basic Programming Concepts This session includes the basic programming concepts such as What is variable and how it is used in C language What are datatypes and their types used in C language A sample C program Errors in C language Other Important Programming Concepts 18
Defining Variables In C Language A variable can be defined as a named location in memory where a particular value is stored Variables allow us to provide a meaningful name for some locations in memory by which we can access this location. For Example: Consider a program where we are going to accept two numbers and display their sum. The pseudo code for the same is provided in the given example BEGIN DISPLAY Enter 2 Numbers INPUT A,B C=A+B Display C END Pseudo Code Pseudo Code Pseudo code is not the actual code as the name suggests (pseudo=false) but a method which uses a certain standard set of words which makes it resemble code Unlike the actual code, pseudo code cannot be compiled or run 19
Guidelines To Be Followed While Naming Variables Variable names must begin with an alphabet. The First Character can be followed by a sequence of alphanumeric characters and can also include special characters such as underscore. Proper names should be avoided while naming variables. A variable name should be very meaningful and descriptive. It should describe the kind of data in it. Letters such as O and I should be avoided in variable name, as these can be confused with similar characters such as 0 and 1. While naming variables we need to ensure that we do not use one of the keywords as a variable name. 20