Learning English Language
Communicating with a computer involves speaking the language the computer understands, which immediately rules out English as the language of communication with the computer. The classical method of learning English language is to first
Learning C is similar and easier. Instead of
straight-way learning how to write programs, must first know what alphabets,
numbers and special symbols are used in C, then how using them, constants,
variables and keywords are constructed and finally how are these combined to
form an instruction. A group of instructions would be combined later on to form
a program.
Structure of a Simple C Program
Documentation Section (Or) Comment Section \* a line of single space must be given */ Header Files Section (Or) Preprocessor Directive Section \* a line of single space must be given */ Definition Section \* a line of single space must be given */ Global Declaration Section \* a line of single space must be given */ main() { Declaration Section \* a line of single space must be given */ Input Section \* a line of single space must be given */ Calculation Section \* a line of single space must be given */ Output Section } Subprogram Section (Or) User-defined Function Section Steps to write a simple C program
Step 1: Documentation Section (Or) Comment Section Step 2: Header Files Section Step 3: Definition Section Step 4: Global Declaration Section Step 5: Specification of main function main() Step 6: Body of the main function main() { \* a line of single space must be given */ } Step 7: Within main() function, (a) Declaration Section (b) Input Section (c) Calculation Section (d) Output Section Step 8: Subprogram Section |
Each section has specific type of statements. These statements are building blocks of a program. They represent instructions to the computer to perform a specific task (operations).
instruction: An instruction may contain
|
Documentation Section(Or) Comment Section
Step 1: In dictionary, comment: (n) an observation, remark The comment statements are not compiled and executed and each executable statement should be terminated with semicolon.
Documentation Section
It consist of a set of comment lines giving
Two types of comments available in C. They are namely
Single line Comment:
/* a comment details */ Example: /* This is a single-line comment */ Program: The following simple program shows the use of comment lines. |
Multiple line comment:
/* Comment line-1 Comment line-2 Comment line-3 */ Example: /* This is a multi-line comment */ Note: There must be no spaces between the asterisk and the slash. Appearance of a Comment within a C program
Compiler view about the comment statement
Review Questions
(Only from Documentation Section)
|
About Specification of main function
1. Normally, a function is a self-contained program segment that carries out some specific task whenever it is accessed. 2. main() function is so special. 3. main() function is the first function called when program execution begins. 4. Every C program must include the main() function. 5. Every C program must contain atleast one function which must be a main() function. 6. The main() function is the starting point for the compiler to begin the execution of the program. 7. The main() function will access other functions used in the program. 8. The empty pair of parenthesis followed by main() function indicates that this function does not include any arguments. 9. The main() function should not end with a semicolon. 10. main() is not a keyword 11. main ( ) executes user defined program statements, library functions and user defined functions and all these statements should be enclosed within left and right braces. |
12. The main ( ) function should be written in small (lowercase) letters
|
About Global Variable Declaration Section
|
|
About Within the main() function
1.The main function may or may not contains a declaration section 2.The main function should have an executable statement. (That is, output section) 3.The statements are written within the compound statement main() 4. In all sections, statements end with a semicolon ; compulsorily. 5. In well-written code, main() function contains what is, in essence an outline of what the program does. 6. Any other functions contained in the program must be either directly or indirectly called by main() About Declaration Section
The declaration is a part of C program.
|
About Input Section (that is, step 7: (b))
The user can enter instructions and data into memory through devices such as a keyboard. These devices are called input devices which are used to feed data into the computer. Simply, in this section, all types of data can be accessed. In C, the scanf statement is usually called as the input statement because it accepts the values to be processed by the computer. About Output Section (step 7: (d))
The user can display processed data from memory through devices such as a monitor. These devices are called output devices which are used to display data into the computer. Simply, in this section, all types of data can be displayed. In C, the printf statement is usually called as the output statement because it displays the message or values on the screen. Sub Program or User-defined Functions
Generally,
|
About Curly braces
Every C program should have a pair of curly braces ({, }). These braces can also be used to indicate the user-defined functions beginning and ending. These two braces can also be used in compound statements. 1. The opening brace { specifies that the beginning of the main() function. 2. The ending brace } specifies that the end of the main() function or the logical end of the program. 3. The statements of the program are enclosed within a pair of braces. 4. The statements enclosed within a pair of braces are called as the body of the main() function. |
you should be completely familiar with the following terms before to learn about Header Files Section
Function: It is a self-contained program segment. It carries out some specific well-defined task whenever it is accessed. Library: In dictionary, Library: (n) collection of books, place where books are collected and maintained 1. It is a file. 2. It contains standard functions. 3. The standard functions can use in your program. 4. The standard functions include all I/O operations as well as other useful routines. Standard Library 1. It is not a part of the C language. 2. It is an environment that supports C language. 3. It provides function declarations, type and macro definitions for the standard C language. 4. It does not define any graphic functions. Library Files (or) Header Files In dictionary, Head: (n) the uppermost part of an animal body, leader, origin, intellect, climax 1. They are supplied as a part of the C compiler. 2. Library functions are grouped together as programs in separate files. 3. In order to use a library function, it is necessary to include specific information within the main portion of the program, 4.They contains information about various library functions that are functionally same. General form of a header file or library file: #include <filename> where, filename represents the header file Different header files in C language
|
Library Function
1.It performs commonly used operation. 2.It returns a value of one or zero stating the conditions as true or false respectively. 3.It is similar to a function. 4.C language is accompanied by a number of library functions. 5. In C, library functions that perform standard input operations - scanf(), getc(), getchar(), gets() standard output operations - printf(), putc(), putchar(), puts() operations on characters - tolower(), toupper() etc., operations on strings - strlen(), strcpy(), strcat() etc., mathematical calculations - sin(), cos(), sqrt(), power() etc., 6. All C compilers come with a standard library of functions that perform most commonly needed tasks. 7. Many of the functions are in the standard library. 8. They act as building blocks that you combine. if you write a function that you will use again and again, you can put it into a library. Call a Library Function (or) Linking
When you call a library function, the C compiler "remembers" its name. Later, the linker combines the code you wrote with the object code already found in the standard library. This process is called linking. Some compilers have their own linker, while others use the standard linker supplied by your operating system. Relocatable format The functions in the library are in relocatable format. This means that the memory addresses for the various machine-code instructions have not been absolutely defined-only offset information has been kept. When your program links with the functions in the standard library, these memory offsets are used to the actual addresses used. |
Why C is a Structured Programming?
C language is well situated for structured programming, the user to think of a problem in terms of function modules or blocks. A proper collection of these modules would make a complete program. This modular structure makes debugging, testing and maintenance easier. The 1960s gave birth to structured programming. This is the method of programming championed by the language C, the use of structured programs fairly easily. However, even with structured programming methods, once a project reaches a certain size, its complexity exceeds what a programmer can manage.
Although C is one of the world’s great programming languages, there is a limit to its ability to handle complexity, once a program exceeds between 25,000 and 100,000 lines of code, it become so complex that it is difficult to grasp as a totality. However, as the programs grew larger, even the structured approach failed to show the desired results in terms of bug-free, easy-to-maintain and reusable programs.
|