Friday, 10 October 2014

Introduction to Programming and C

What is language?
Language is a way of communication. A characteristic style of speech or writing that interacts between two mediums that is user and machine. A set of characters convention and rules that is used for conveying information is called Language.

What is program?
A well-define set of instructions given to computer is called program. A computer program is written in a programming language.

What is programming?
Programming is the process of writing computer based programs or software. Creating of a computer program is called programming. The steps are
*       Preparing the necessary documentation.
*       Writing the program logic.
*       Developing the program.
*       Assembling, compiling or interpreting the program.
*       Testing and debugging the program.

What is programming language?
A set of words and symbols used to write a program is called programming language. The programming languages are used to write computer program. A programming language is a means of communication between a user and computer. In other words programming language is a complex synthetic mechanism which provides some special features to build/develop any program/software are called programming language. All the computer software is written in a special type of computer software called the programming languages.

What are the types of programming languages?
There are two major types of computer programming languages:
*       Low Level Languages
*       High Level Languages

What are the low level languages?
A low level language that is very closer to the computer hardware. The term low level means closeness to the way in which the machine has been built. Low levels are machine oriented languages and required extensive knowledge of computer hardware and its configuration. The low level languages are divided into machine language and assembly language.

What is the machine language?
A type of language in which instructions are written in binary form called machine language. It is the only language that is directly understood by the computer. It doesn’t need any translator program. We also called it machine code and it is written as string of binary (1s or 0s). It is at the native language of the computer. A program written in machine language can be executed very fast by computer. The computer does not need any translator to understand this language. The programs written in machine language are machine-dependent. Every computer has its own machine language. A program instruction may be like this 100110101. It is not an easy language for you to learn because of its difficult cods and it is also difficult understand. It is efficient for the computer but very inefficient for the programmers.

What is the assembly language?
A programming language that is one step away from machine language means to say that it is first step to improve the programming structure. In assembly language, machine instructions are replaced with English-like word known as mnemonics. It is pronounced as Ne-Monics. Assembly language is mostly used for writing system software. Programs written in assembly language are easier to write and modify than machine language.
Assembly language is requiring translating program to translate assembly language into machine language. The translator program is called “assembler”.

What are the high level languages?
High-level languages are programming languages that are more user friendly, to some extent platform to independent and abstract form low-level languages. In high-level languages we can’t directly interact with the hardware. Assembly and machine language require deep knowledge of computer hardware. Where as in high-level languages you have to know only the instruction in English words and logic of the problem irrespective of the type of computer you are using. High level languages are simple languages that use English and mathematical symbols like +,-,*, / etc for its program creation. In high level languages we use compiler or interpreted to translating our program. The high-level languages enable to the programmer to write the program the English words, but these English words cannot be understand by the computer, so special types of translators (compilers, interpreters) are required that convert the high level language program into the machine readable format (machinery language program) that can be read by the computer without any further translation. There are different types of the high-level languages, such as: BASIC, FORTRAN, COBOL, PL/1, VB, Pascal, ALGOL, Ada, C language etc.

What are the characteristics/advantages of high level languages?
Some important characteristics or advantages of high-level languages are as follows:
*       Easy to Learn: High-level Languages are closer to human languages and far from machine language. These are English like language and are easier to learn.
*       Easy Error Detection: High-level languages are easy to read and modify. It makes it easy to find error in programs written in these languages.
*       Standardized Syntax: The syntaxes of high-level languages are standardized. These languages described a well-defined way of writing programs. Different organizations work to determine standard syntax of these languages. An important organization is American National Standard Institute (ANSI).
*       Deep Hardware Knowledge not Required: These languages do not require deep knowledge of hardware or machine architecture. A programmer can write efficient programs without an in-depth knowledge of hardware. The programmer can concentrate on solving the problem rather than concerning the human machine interaction.
*       Machine Independence: High-level languages provide machine independence. It means that the programs written in high level languages can be executed on different types of computers. For example, a program written in C can be executed on Intel processor and Motorola processor with a little modification.
*       More Programmers: Programming in low-level language is very difficult. But high-level languages are easy to learn. It encourages more people to learn these languages. So many programmers of high-level languages are available.
*       Shorter Programs: Pr       ograms written in high-level languages are shorter than low-level languages. One instruction of high-level languages is equivalent to many instructions of low-level language.

What are the common high level languages?
The following are the high level languages:
COBOL: stands for COmmon Business Oriented Language; it is specially designed for business applications.
BASIC: stands for Beginner All Purpose Symbolic Instruction Code. It was used mainly by students to use the computer for solving simple problems.
FORTRAN: stands for FORmula TRANslation. It has very powerful mathematical capabilities.
Pascal: it is used for both sciencetific and business applications.
Java: it provide strong feature for network programming.
C/C++: it is used to write system software and application software.

What are the differences between assembly and high level languages?
The main difference between assembly and high-level languages are as follows:

Assembly Language
*       Assembly languages are difficult to learn.
*       Assembly languages are far from human languages.
*       Programs in assembly languages are fast in execution.
*       Programs in assembly languages are difficult to modify.
*       Assembly languages provide facility to write programs at hardware level.
*       Knowledge of hardware is required to write any programs.
*       Assembly languages are normally used to write system software.

High-Level Language
*       High-level languages are easy to learn.
*       High-level languages are near to human languages.
*       Programs in high-level languages are slow in execution.
*       Programs is high-level languages are easy to modify.
*       High-level languages do not provide much facility at hardware level.
*       Knowledge of hardware is not required to write any programs.
*       High-level languages are normally used to write application software.

What are differences between Low-Level and High-Level Programming Language.
The main differences between low-level and high-level languages are as follows:

High Level Language
*       High-level languages are easy to learn.
*       High-level languages are near to human languages.
*       Programs in high-level languages are slow in execution.
*       Programs is high-level languages are easy to modify.
*       High-level languages do not provide much facility at hardware level.
*       Deep knowledge of hardware is not required to write any programs.
*       High-level languages are normally used to write application programs.

Low Level Language
*       Low-level languages are difficult to learn.
*       Low-level languages are far from human languages.
*       Programs in low-level languages are fast in execution.
*       Programs in low-level languages are difficult to modify.
*       Low-level languages provide facility to write programs at hardware level.
*       Deep knowledge of hardware is required to write any programs.
*       Low-level languages are normally used to write hardware programs.

What is structured programming language?
In structured programming languages, the entire logic of the program is divided into number of smaller modules or function. Each module is a piece of code that implements a different functionality. The main module calls other modules when they are needed execute. It is a modular method of written programs. It is an easy and simple technique to write program. The programs written in these languages are easy to understand, modify and debug. There is also less possibility of errors in the programs.

What is Unstructured Programming Language?
In unstructured programming languages, the entire logic of the program is implemented in a single module of function. The programs written in these languages are error prone, difficult to understand, modify and debug.

What are the language processors/language translators?
Computer understands only machine language. A program written in high-level or assembly language cannot be run on a computer directly. It must be converted into machine language before execution. Language processor or translator is software that converts these programs into machine language. Every computer language has its own translator. It is a special type of the software use for converting the software from one language to another language. All the computer software is written in a special type of computer software called the programming languages. There are three types of the processors or translators:
*       Assembler
*       Interpreter
*       Compiler

What is assembler?
A program that translates assembly language instructions into machine language instructions is called assembler. Assembly language is translated in to machine language by a piece of software is called assembler.

What is interpreter?
An interpreter is a program that converts one statement of a program into machine at one time. It executes a statement before translating the next statement of source program. If there is an error in the statement, interpreter stops working and display an error message. The advantage of interpreter over compiler is that an error is found immediately. So the programmer can correct errors during program development. The disadvantage of interpreter is that it is not very efficient. The interpreter does not produce an object program. It must convert the program each time it is executed. So we can say that interpreter can check the program line by line. Java, Basic and Visual basic uses interpreter.

What is compiler?
A compiler is a program that converts the instruction of a high-level language into machine language as a whole. A program written in high-level language is called source program. The compiler converts the source program into machine code. The machine code program is known as object program. The object program can executed many times.
The compiler checks each statement in the source program and generates machine instructions. Compiler also checks syntax errors in program. A source program containing an error cannot be compiled. A compiler can translate the programs of only those languages for which it is written. For example, C compiler can translate only those programs that are written in C language. Compiler can first check the entire program if there are some errors in the whole program than it can show all the possible errors in the program. It means compiler can check the program at whole. C, C++, COBOL and FORTRAN are uses interpreter.

What is a linker?
Linker is a program that combines the object program with additional library files is known as linker. It is a part of compiler. It combines object program and library files and saves the final machine language program as executable file. The extensive of executable files is .exe.

What are the differences between compiler and interpreter?
The main differences between compiler and interpreter are as follows:

Compiler
*       Compiler converts a program into machine code as whole.
*       Compiler create object code file.
*       Compiler converts high-level program that can be executed many times.
*       Program is execution is fast.
*       Compiler display syntax error after compiling the whole program.

Interpreter
*       Interpreter converts a program into machine code statement by statement.
*       Interpreter does not create object code file.
*       Interpreter converts high-level program each time it is executed.
*       Program execution is slow.
*       Interpreter displays the syntax error on each statement of program.

What is source code?
Source code is a computer program written in a high-level programming language like C, C++, Visual Basic or Java. Computer cannot understand the statements of high-level language. The source code cannot be executed by directly. It is converted into machine code and then executed.

What is object code?
An object code is the program that is translated by a language processor. It is also called machine code. Computer understands object code directly.

What is error? Also define its types.
An error is incorrectly written code, usually intentional there are three types of errors.
*       Syntax Errors
*       Logical Errors
*       Run-Time Errors

What is syntax error?
A collection of rules for writing program in a programming language is called as syntax. Syntax error is a type of error that occurs when an invalid statement is written in program. Syntax means any spelling error or any typing error. Because programmer can mention wrong spelling for the same code so it calls syntax error. Syntax errors are detected by compiler. A program containing syntax error cannot be compiled successfully. There can be many causes of syntax errors. Some important causes are as follows:
*       The statement terminator is missing at the end of statement.
*       A variable is used without declaration.
*       Any of the delimiters is missing.
Example: Typing print instead of printf is an example of syntax error.

What is logical error?
A type of error that occurs due to poor logic of the programmer is called as logical error. A logical error occurs when the program follows a faulty algorithm. A statement with logical error may produce unexpected and wrong result. If programmer make any program and then compile them if there are not any error in the program it can run correct without any error but the result can not show’s correctly according to programmer desire then it calls logical error.
Example: If a programmer writes a wrong formula to calculate the result or if a programmer use wrong loop condition or if a programmer use wrong if condition, these are the examples of logical errors.

What is run time error?
A type of error that occurs during the execution of program is called run-time error. It is caused when a statement directs the computer to execute an illegal operation such as dividing a number by zero. The run-time errors are detected and displayed by the computer during execution. Run-time errors are normally caused due to wrong input from the user. The computer stops executing the program and displays error message if a runtime error occurs.
Example: The user may ask the program to open a file that does not exist. Similarly or the user may enter wrong type of data are the examples of run-time errors.

What is the most difficult error?
The most difficult type of error is logical error for the following reasons:
*       It cannot detect by the compiler.
*       It does not crash the program. That is why it is difficult to detect.
*       The user needs to review the whole program to find out logical error(s).
*       It may take a lot of time for detecting logical error.

What is bug?
A small programming error is called bug. A computer bug is an error, fault, flaw, mistake or failure in a computer program that prevents (stop) it from working correctly or produces an incorrect result. Bugs arise from mistakes and errors made by programmer.

What is debugging?
Debugging is a methodical (logical) process of finding and reducing the number of bugs. The process of finding and removing bugs is called debugging.

What are the testing and debugging in programming languages?
When any software company completes any software, after completion of the same testing department can check all the software according to the user requirements to make software free of errors. For this purposes he can check/test all the software thoroughly, separately all modules of the software, and if found any error in the software then correct them. Phase in which team checks software called Testing and the phase where a team correct error is called Debugging. So this all procedure called testing and debugging.

What is C language?
C is a popular high-level programming language. It was developed at AT&T’s Bell Laboratory of USA in 1972 and it was designed and written by a man named Dennis Ritchie. C was invented by Dennis Ritchie of AT&T Bell Laboratories to provide a high-level language, in which the UNIX operating system could be programmed. It is now widely used for many other applications as well. C is especially popular with systems programmers because it allows programs to be expressed simply and concisely. The definitive work describing C is the programming Language by Kernighan and Ritchie (1978). Bolon (1986), Gemani (1984), Hancock and Krieger (1986), Harbison and Steele (1984), and numerous others have also written books about C. C is a high level language (C is often called a "Middle Level" programming language) created by Dennis Ritchie at the AT&T’s Bell Telephone Laboratories of USA in 1972. The language was created for a specific purpose: to design the UNIX operating system (which is used on many computers). Because C is such a powerful and flexible language, its use quickly spread beyond Bell Labs. Programmers everywhere began using it to write all sorts of programs. Soon, however, different organizations began utilizing their own versions of C, and subtle differences between implementations started to cause programmers headaches. In response to this problem, the American National Standards Institute (ANSI) formed a committee in 1983 to establish a standard definition of C, which became known as ANSI Standard C. With few exceptions, every modern C compiler has the ability to adhere to this standard.

Define brief history of C language.
In 1960’s hundreds of languages had come into existence, almost each for a specific purpose. For example: COBOL was being used for commercial applications, FORTRAN for engineering and Scientific Applications and so on. At this stage people started thinking that instead of learning and using so many languages, each for a different purpose, why not use only one language, which can program all possible applications. Therefore, an international committee was set up to develop such a language. This committee came out with a language called ALGOL in 1960. However, ALGOL 60 never really becomes popular because it seemed too abstract, too general. To reduce this abstractness and generality, a new language called Common Programming Language (CPL) was developed at Cambridge University in 1963. CPL was an attempt to bring ALGOL 60 down to earth. However, CPL turned out to be so big, having so many features, that it was hard to learn and difficult to implement. Basic Combined Programming Language (BCPL) developed by Martin Richards at Cambridge University in 1967 aimed to solve this problem bringing CPL down to its basic good features. But unfortunately it turned out to be too less powerful and too specific. Around the same time Ken Thompson at AT&T Bell Lab’s as further simplification of CPL wrote a language called B. But unlike BCPL, the B language too turned out to be very specific. Dennis Ritchie inherited the features of B and BCPL, added some of his own and developed C language.

Year
Language Development
1960
ALGOL 60
1963
CPL
1967
BCPL
1970
B
1972
C

Short history in points
The milestones in C's development as a language are listed below:
*       UNIX developed in 1969: DEC PDP-7 Assembly Language
*       BCPL: a user friendly OS providing powerful development tools developed from BCPL. Assembler tedious long and error prone.
*       A new language called B a second attempt in 1970.
*       A totally new language C a successor to B in 1972.
*       By 1973 UNIX OS almost totally written in C.

Where C stands for?
As we know all the programming language can be divided in to two categories.
*       Low Level Language (Machine Oriented Language)
*       High Level Language (Problem Oriented Language)
C stand in between these two categories that’s why it is often called a middle level language, since it was designed to have both is relatively good programming efficiency (as compared to machine oriented language) and relatively good machine efficiency (as compared to problem oriented language).

What are the characteristics/advantages of the C language?
Some important advantages of C language are as follows:
Convenient Language
C is very convenient language. It provides many facilities in easier way that are difficult to use in low-level languages. Programmers can write complex programs more easily as compared to low-level languages.
Well-Structured Language
C is a well-structured language. Its syntax is very easy to understand. The programs written in C language is easy to maintain and modify.
Machine Independence
C language provides machines independence. It means that the programs written in C language can be executed on different types of computers. For example, a program written in C can be executed on Intel processors and Motorola processors with a little modification. That is why it is preferable to write program in C rather then machine language.
Modularity
C language provides the facility of modular programming. It means that you can divide your program into small modules. These modules can be developed and compiled independent.
Case Sensitivity
C is a case sensitive language. It means that it can differentiate uppercase and lowercase words. All keywords are written in lowercase. This feature makes it easier to maintain the source code.
Hardware Control
C language provides close control on hardware. You can write efficient programs to control hardware components of computer system.
Small Language
C is a small language. It has a small number of keywords and programming controls. But still it is every powerful for developing different types of programs.
Fast Code Generation
The compilers of C language generate very fast code. The code executes very efficiently. So the programs take less time to execute.

Some more points on Characteristics of C
*       C is case sensitive language
*       C is a structured language
*       C is a language of few words (Small size)
*       C has high-level constructs
*       C can also handle low-level activities
*       C can produce efficient programs
*       C can be compiled on a variety of computers
*       C can extensive use of function calls
*       C language program has a extension .c
*       C statements are terminated with semicolon

What are the features of the C Language?
*       Ability to write very concise source codes in spite of having a large number of operators.
*       It has a small instruction set
*       It has library functions that enhance the basic instruction.
*       The features and capabilities can be easily extended b the users as it encourages users to write additional library functions
*       C Compliers are compact and generate smaller and highly efficient object code compared to other high level languages.
*       C Programs are highly portable as its library set avoids all to computer dependent features.
*       C Compliers are compact and generate smaller and highly efficient object code compared to other high level languages.
*       C Programs are highly portable as its library set avoids all to computer dependent features.

What are the program characteristics of c language?
The program characteristics are as follows:
*       Integrity
*       Clarity
*       Simplicity
*       Efficiency
*       Modularity
*       Generality

What are C character sets?
*       Uppercase A - Z
*       Lowercase a - z
*       Digits 0 – 9
*       Special characters
These are used as building blocks to form basic program elements.

What is the Turbo C++ IDE?
The compiler used for C language is Turbo C++. It is the implementation for Borland International for C language. Turbo C also provides a complete Integrated Development Environment (IDE) known as TC editor. It is used to create, edit and save programs. It also provides a powerful debugger. The debugger helps users in detecting and removing errors in programs. The process of writing programs in Turbo C editor is very easy. The user can type TC on DOS prompt or double click TC shortcut to start IDE. The menu bar of IDE contains menus to create, edit, compile, execute and debug a C program. The user can open a menu by clicking on it with the mouse. The user can also press a combination of ALT key and the first highlighted character of the name of menu. For example ALT+F is used to open File menu.

What are the debugging features of Turbo C++?
Turbo C++ provides many useful debugging features. The debugging features of Turbo C++ are available in Debug menu. Some important debugging features of Turbo C++
are as follows:
Single Stepping: A program may not execute even if it has been compiled successfully. The debugger provides the facility to find errors by executing one line of a program at one time. It enables the programmer to detect the exact place of error. The following procedure is used to execute one step at a time with single stepping Select Run> Trace into or press F7.
Watches: The watch or watch expression is used to check the value of a variable as the program executes. It indicates how the values of variables change during program execution. It is normally used in combination with single stepping. The following procedure is sued to use watch or watch expression:
*       Place the cursor on the variable whose value is to be checked.
*       Select Debug > Watches. A submenu will appear.
*       Select Add Watch from the submenu. OR press CTRL+F7. A dialog box will appear.
*       The selected variable will appear in Watch expression field.
*       Click OK OR press Enter. A new window will appear indicating that the selected symbol is undefined.
*       Select Run > Trace into OR press F7 to execute single stepping. The value of the selected variable will appear in Watch window.
Breakpoints: A breakpoint is used to mark a part of program where program execution will stop. The program executes all statements up to the breakpoint and then stops. The user can check the values of a variable at this point by using Watch window by single stepping remaining part of the program. The following procedure is used to insert a breakpoint in the program:
*       Place the cursor on the line on which the breakpoint is to be inserted.
*       Select Debug > Toggle breakpoint or press CTRE+F8. The breakpoint will be inserted and the line will be highlighted.
Evaluate/Modify Window: The evaluate/modify window is used to change the value of variable during program execution. It can be useful if the user is single stepping the program and wants to change the value of a certain variable. The following procedure is used to use evaluate/modify window:
*       Select Debug > Evaluate/Modify. A new window will appear with three fields.
*       Enter the name of the variable whose value is to be modified in Expression field.
*       Enter the new value for the variable in New Value field. The value of the third field Result will also change automatically.

How to Write, Edit, Compile, Link and Run the C Program?
We use Turbo IDE that provides the central location where the entire C program that can be written, compile, link and run. Following steps are required for working with Turbo C++ IDE.
Step 1: Select the icon/shortcut TC.EXE from the BIN folder present in the TC.
C:    TC    BIN    TC.EXE
This action will open the Turbo C++ IDE, where we can perform all the relevant activities associated with the C program. After properly opening the Turbo C++ IDE, a program with blue color will be appeared on the screen. It is the place where we do our programming work.

Step2: Now write the following program in Turbo C++ IDE.

Simple C Program
#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
printf("I am Javed Iqbal.");
getch();
}
Output
I am Javed Iqbal.

Step 3: Now save the file with the name javed.c by pressing F2 or by selecting the Save option from the File Menu.
Step 4: Now compile the file by pressing the Alt + F9 or by selecting the Compile option of the compile menu (after the successful compilation of .c and object file .obj will be generated).
Step 5: Now link the file by selecting the link option of the Compile menu. After linking the file the .exe will be generated.
Step 6: Instead of Using the Option 4 and 5 directly press F9 or use the Make option of the of the Compiler menu, that which the program is compile and link with the single command.
Step 7: For running the program press Ctrl+F9 or select the Run option from the Run menu. After selecting this option the Turbo C++ IDE will run the program but before running the IDE compile and link the file automatically so instead of using the options 4 to 6 simply press the Run or Ctrl + F9 by which the compile, linking and running process will be perform in a single tern.
Step 8: If we don’t use getch(); function so we cannot view the output directly. Without using getch(); function we displaying the output by pressing the Alt+F5.
Step 9: Press Alt+F3 or select the Close option from the Window menu, to close the active file.
Step 10: Press Alt + X or select the Quit option from the File menu to exit from the Turbo C++ IDE.

What is the basic structure of the C program?
The format of writing C program is called its structure. The basic structure of a C program is very flexible. It increases the power of language. It consists of the following:
*       Preprocessor Directive (#)
*       Main() function
*       Body of the program (C Statements)


In the above program
*       First line is preprocessor directive to include a header file stdio.h. The preprocessor directives are the commands that give instructions to C, preprocessor. Preprocessor is a program that modifies C source program before compilation. The preprocessor directives start with hash symbol #.
*       Second line is also preprocessor directive to include a header file conio.h.
*       The third line is main function. The word void before the function main() means that this function does not return a value and the second void inside the bracket means it does not have any argument. The main() function is the place where execution of a C program starts. When the program is executed, the control enters main() function and starts executing its statements.
*       The statements of the program are written in curly braces. The curly brace { is called opening brace and } is called closing brace. The braces are also known as delimiters. These statements are collectively known as the body of the program.
*       Every statement in C program is terminated with a semicolon (;). The compiler generates an error if any statement is not terminated by semi colon (;).

What is the #include<stdio.h>?
The # sign indicates that this is an instruction for the C compiler and includes information about how to use the functions from the <stdio.h> stand for Standard Device Input/Output header file. Usually the standard input device is your keyboard and the standard output device a terminal which is displayed on your monitor. One item included in this header is a declaration of the function printf(). This library is very widely used.
What is the #include <conio.h>?
This line tells C to include information about how to use the functions from the Common Input Output library.
What is void main()?
main() is a program building block called function. C programs contain one or more functions, exactly one of which must be main(). The keyword void to the left of the main indicated that main can not return any value. The parentheses after main indicates that main is a program building block called function. { the left braces begin the body of every of every function and a corresponding right brace } must means end the body of each function.
What is clrscr();?
It is a built in function for clear screen and as ; (semicolon) at the end of line its means that the statement end with ; all the statements in C are terminated ; (semicolon) called terminator.
What is printf(“Hello! Students.”);?
It is also a built in function of C to interact compiler for print message in console output screen, which are mention with parentheses and double quote.
What is getch();?
It is also a built in function, this function can get any character from user on run time, and mostly this function is used for stop any program on any specific location when user can enter any key then program goes on.

What is Preprocessor?
The preprocessor is used to modify your program according to the preprocessor directives in your source code. Preprocessor directives (such as #define) give the preprocessor specific instructions on how to modify your source code. The preprocessor reads in all of your include files and the source code you are compiling and creates a preprocessed version of your source code. This preprocessed version has all of its macros and constant symbols replaced by their corresponding code and value assignments. If your source code contains any conditional preprocessor directives (such as #if), the preprocessor evaluates the condition and modifies your source code accordingly. The preprocessor contains many features that are powerful to use such as creating macros, performing conditional compilation, inserting predefined environment variables into your code and turning compiler features on and off. For the professional programmer in-depth knowledge of the features of the preprocessor can be one of the keys to creating fast efficient programs.

What are the preprocessor directives?
The preprocessor directives are commands that give instructions to C preprocessor. It is also known as compiler directive. The preprocessor is a, program that modifies C source program before compilation. The preprocessor directives start with hash symbol #. These directives are written at the start of program. Many necessary actions such as input and output are not defined directly in C programs. These actions are defined in the form of function. These functions are stored in C libraries. Each library has a standard header file. A header file always has .h extension. The file stdio.h is a header file. It contains definition of standard input/output functions.

What are the types of preprocessor directives?
Two types of preprocessor directives used in C language are as follows:
*       Include preprocessor directive
*       Define preprocessor directive

What is the include preprocessor directive?
The "include" preprocessor directive enables a program to access a library. It allows the preprocessor to insert definitions from a standard header file into C program before compilation. It is used to include header files in the program.
Syntax: The syntax of using this directive is as follows: #include <standard header file>.
Example #include <stdio.h> , #include <math.h>
The above statement tells preprocessor to include the file stdio.h in the source program before compiling it. The include directive tells the compiler where to find the meanings of standard identifiers such as printf. The meanings are described in standard header files. The header file stdio.h contains information about standard input and output functions such as scanf and printf. The math.h contains information about common mathematical functions.

What is the define preprocessor directive?
The define preprocessor directive is used to define constant macro.
Syntax: The syntax of using this directive is as follows:
#define Macro _Name Expression
The expression can be a constant, arithmetic expression or a string. C preprocessor replaces each occurrence of the identifier Macro Name with the value of the expression before compilation. The expression or the identifier Macro Name cannot be changed during program execution.
Example: #define a 10
The above line defines an identifier a that represents the value 10.

What is a Header File?
Header files contain declarations to functions and variables and have a .h extension. Examples of header files include stdio.h, string.h and strlib.h. Header files have the extension .h and the full filename follows from the #include directive. They contain declarations to certain functions that you may or may not have used in your program. For example, the stdio.h file is required if you have used functions like printf and scanf in your program. There are two ways to include a header file: #include "stdio.h" and #include <stdio.h>. If you use the double quote marks it means that the directory you're currently in, will be searched for first for the header file before any other directories are searched. If you use the angled brackets directories other than the one you're currently in will be searched for the header file. Usually this will be the default directory for header files specified in your compiler so you'll probably be using square brackets all the time.
#include<stdio.h> The # sign indicates that this is an instruction for the C compiler and includes information about how to use the functions from the <stdio.h> stand for Standard Device Input/Output header file. Usually the standard input device is your keyboard and the standard output device a terminal which is displayed on your monitor. One item included in this header is a declaration of the function printf(). This library is very widely used.
#include <conio.h> This line tells C to include information about how to use the functions from the Common Input Output library.

What is main function?
A function is like a subroutine where a group of instructions are evaluated when the function is called. All C programs must have a main function. You can only have one but you can place it anywhere within the code. The program always starts with the main function and ends when the end of main is reached. Functions RETURN a value too in other words passes back a value to the program. If a function returns nothing its return type is of type void i.e. nothing is returned. The main function is special as it returns an integer by default which is why you'll see me write return 0; at the end of the program. Zero is usually returned to indicate error-free function termination. Another way to terminate a program is to use the exit function, there's an example.
#include <stdio.h>
int main()
{
  clrscr();
  printf("Hello Javed!\n");
  return 0;
}

What is a local block?
A local block is any portion of a C program that is enclosed by the left brace ({) and the right brace (}). A C function contains left and right braces and therefore anything between the two braces is contained in a local block. An if statement or a switch statement can also contain braces so the portion of code between these two braces would be considered a local block.

What are memory models?
The compiler uses a memory model to determine how much memory is allocated to the program. The PC divides memory into blocks called segments of size 64 KB. Usually, program uses one segment for code and a second segment for data. A memory model defines the number of segments the compiler can use for each. It is important to know which memory model can be used for a program. If we use wrong memory model, the program might not have enough memory to execute. The problem can be solved using larger memory model. However, larger the memory model, slower is your program execution. So we must choose the smallest memory model that satisfies our program needs. Most of the compilers support memory models like tiny, small, medium, compact, large and huge.

What is the purpose of the statement #include<stdio.h> in a C program?
The purpose of #include<stdio.h> is include usually the standard device input/output is your keyboard and the standard output device a terminal which is displayed on your monitor. That item included in this header is a declaration of the function printf(), scanf() etc. almost any program needs to perform input/output operations. Therefore, most of the C program must be preceded by this instruction. This library is very widely used. The # sign indicates that this is an instruction for the C compiler and includes information about how to use the functions from the <stdio.h> stand for Standard Device Input/Output header file. Assembly language is translated by a translator program called as Assembler. High-level language is translated by translators program "Compiler or Interpreter".

What is a function?
A function is a named block of code that performs some action. The statements written in a function are executed when it is called by its name. Each function has a unique name. A function is a piece of code i.e. designed to perform a specific task. It is a complete and independent program. It is executed by the main function or any other function of the program. Functions are the building blocks of C programs.

What is main() function?
The main() function is the place where execution of a C program starts. When the program is executed, the control enters main() function and starts executing its statements.

Write a program that will print the message "I Love Computers" and compile and run it on the computer.

Simple C Program
#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
printf("I Love Computers");
getch();
}
Output
I Love Computers


After writing the above program we will compile it with the help of pressing Alt+F9 or by selecting the compile option from the compile menu (after the successful compilation of .c and object file .obj will be generated). For running the program we will press Ctrl+F9 or select Run option from the run menu. The effect is that the string “I Love Computers” is printed on the screen when the program runs.

No comments:

Post a Comment