C Programming notes_2012.pptx

download C Programming notes_2012.pptx

of 127

  • date post

  • Category


  • view

  • download


Embed Size (px)

Transcript of C Programming notes_2012.pptx

C Programming

C/C++ Program Compilation BeforeMachine Languages

Assembly Languages

High Level Languages


JUIT Waknaghat2Creating, Compiling and Running Your Program The stages of developing your C program are as follows. Creating the program Create a file containing the complete program, such as myprog.c. You can use any ordinary editor with which you are familiar to create the file. The filename must by convention end ``.c'' (full stop, lower case c), e.g. myprog.c or progtest.c. The contents must obey C syntax. Compilation There are many C compilers around. The cc being the default Sun compiler. The GNU C compiler gcc is popular and available for many platforms. PC users may also be familiar with the Borland bcc compiler. For the sake of compactness in the basic discussions of compiler operation we will simply refer to the Borland bcc compiler -- other compilers can simply be substituted in place of bcc unless otherwise stated.

ContdTo Compile your program simply press the Alt+F9.If there are syntax errors in your program (such as mistyping, misspelling one of the key words or omitting a semi-colon), the compiler will detect and report them. There may, of course, still be logical errors that the compiler cannot detect. You may be telling the computer to do the wrong operations. Running the program The next stage is to actually run your executable program. To run an executable in Windows, you press the Ctrl+F9.This executes your program, printing any results to the screen. At this stage there may be run-time errors, such as division by zero, or it may become evident that the program has produced incorrect output. If so, you must return to edit your program source, and recompile it, and run it again. The C Compilation Model We will briefly highlight key features of the C Compilation model here.

The Preprocessor The Preprocessor accepts source code as input and is responsible for removing comments interpreting special preprocessor directives denoted by #. For example #include -- includes contents of a named file. Files usually called header files. e.g #include -- standard library maths file. #include -- standard library I/O file #define -- defines a symbolic name or constant. Macro substitution. #define MAX_ARRAY_SIZE 100

C Compiler The C compiler translates source to assembly code.

The source code is received from the preprocessor. Assembler The assembler creates object code. On a WINDOWS system you may see files with a .OBJ suffix (on MSDOS) to indicate object code files. Link Editor

If a source file references library functions or functions defined in other source files the link editor combines these functions (with main()) to create an executable file. External Variable references resolved here also. Using Libraries C is an extremely small language. Many of the functions of other languages are not included in C. e.g. No built in I/O, string handling or maths functions.

What use is C then? C provides functionality through a rich set function libraries. As a result most C implementations include standard libraries of functions for many facilities ( I/O etc.). A programmer can also develop his or her own function libraries. C Programming An introduction 16HistoryThe initial development of C occurred at AT&T Bell Labs between 1969 and 1973.It is developed in 1972 by Dennis Ritchie The origin of C is closely tied to the development of the Unix operating system, It was named "C" because many of its features were derived from an earlier language called "B", which according to Ken Thompson was a stripped-down version of the BCPL (Basic Combined Programming Language). JUIT Waknaghat17PhilosophyIn computing, C is a general-purpose, block structured, procedural, imperative computer programming language It was designed to be compiled using a relatively straightforward compiler, to provide low-level access to memory, to provide language constructs that map efficiently to machine instructions, and to require minimal run-time support. Although C was designed for implementing system software, it is also widely used for developing application software.

JUIT Waknaghat18ProgrammingProgramming - scheduling, or performing a task or an event.Computer Programming - creating a sequence of steps for a computer to follow in performing a task. Programming Language - a set of rules, symbols, and special words used to construct a computer program.Programming language rules consist of:Rules of Syntax which specify how valid instructions are written in the language.Rules of Semantics which determine the meaning of the instructions (what the computer will do).


The following program is written in the C programming language.

#include main() {printf ("Programming in C is easy.\n"); } Sample Program Output: Programming in C is easy._ 19JUIT Waknaghat20A NOTE ABOUT C PROGRAMS

In C, lowercase and uppercase characters are very important! All commands in C must be in lowercase. The C programs starting point is identified by the word main() This informs the computer as to where the program actually starts.The brackets that follow the keyword main indicate that there are no arguments supplied to this program (this will be examined later on).

20JUIT Waknaghat21ContdThe two braces, { and }, signify the begin and end segments of the program. The purpose of the statment : #include is to allow the use of the printf statement to provide program output.Text to be displayed by printf() must be enclosed in double quotes . The program has only one statement printf("Programming in C is easy.\n")21JUIT Waknaghat22printf() is actually a function in C that is used for printing variables and text. Where text appears in double quotes "", it is printed without modification.

There are some exceptions however. This has to do with the \ and % characters. These characters are modifier's, and for the present the \ followed by the n character represents a newline character. Contd22JUIT Waknaghat2223Another important thing to remember is that all C statements are terminated by a semi-colon ; Contd23JUIT Waknaghat24Summary of major points program execution begins at main() keywords are written in lower-case statements are terminated with a semi-colon text strings are enclosed in double quotes C is case sensitive, use lower-case and try not to capitalise variable names \n means position the cursor on the beginning of the next line printf() can be used to display text to the screen the curly braces { } define the beginning and end of a program block

24JUIT Waknaghat25CLASS EXERCISE 1Q1.1. What is the output of following program ?

#include main() { printf("Programming in C is easy.\n");printf("And so is Pascal.\n");

} 25JUIT Waknaghat26ANSWER 1.1Programming in C is easy. And so is Pascal. _ 26JUIT Waknaghat27Q1.2 What is the output of following program ? #include main() { printf("The black dog was big. ");printf("The cow jumped over the moon.\n"); } 27JUIT Waknaghat28ANSWER 1.2The black dog was big. The cow jumped over the moon. _ 28JUIT Waknaghat29Q1.3 Try and work out what the following program displays,

#include main() {printf("Hello...\n..oh my\n...when do i stop?\n"); } 29JUIT Waknaghat30ANSWER 1.3Hello... ..oh my ...when do i stop? _ 30JUIT Waknaghat3131KEYWORDS Keywords are words reserved by C so they cannot be used as variables.

JUIT Waknaghat32

Variables & Data Types

Declaring a variable reserves enough bytes in RAM memory to store value of the declared type.A data type is a set of values and a set of operations on values.The values and operations go together. You can think of them as a unit .The objects of a data type can be variables or constants.32JUIT Waknaghat33ContdC provides the programmer with FOUR basic data types These are:integercharacterfloatdouble

33JUIT Waknaghat34ContdThe basic format for declaring variables is data_type var, var, ... ; where data_type is one of the four basic types, an integer, character, float, or double type.

34JUIT Waknaghat35DATA TYPEDESCRIPTIONMEMORY REQUIRMENTintInteger quantity2 bytes(-32767 32767)Long intInteger quantity4 bytesfloatA number containing a decimal point4 bytes(1E-37 to 1E+37)charAny character1 byte (-127 to 127)doubleA number containing a decimal point but here we can have more significant figures.8 bytes35JUIT Waknaghat36ContdUser defined variables must be declared before they can be used in a program. Get into the habit of declaring variables using lowercase characters. Remember that C is case sensitive, so even though the two variables listed below have the same name, they are considered different variables in C. sumSum36JUIT Waknaghat37ContdThe declaration of variables is done after the opening brace of main(), e.g.#include main() { int sum; sum = 500 + 15; printf("The sum of 500 and 15 is %d\n", sum); } 37JUIT Waknaghat38More about printf()Now lets look more closely at the printf() statement. It has two arguments, separated by a comma. Lets look at the first argument:"The sum of 500 and 15 is %d\n" The % sign is a special character in C. It is used to display the value of variables. When the program is executed, C starts printing the text until it finds a % character. If it finds one, it looks up for the next argument (in this case sum), displays its value, then continues on.

38JUIT Waknaghat39ContdThe d character that follows the % indicates that a decimal integer is expected. So, when the %d sign is reached, the next argument to the printf() routine is looked up (in this case the variable sum, which is 515), and displayed. The \n is then executed which prints the newline character. 39JUIT Waknaghat40Some of the formatters for printf are 1. Cursor Control Formatters \n new line \t tab \r carri