Day 2 Lecture Notes
Day 2 Lecture Notes
Introduction to C
It is a middle level language (in between 2GL and 3GL) developed by Denis Ritchie
in the year 1972 at AT & T’s Bell Lab.
Why it is middle level language because it allows manipulation of bits, bytes and
addresses. Mainly it is used for system side programming.
It is a structured language.
Structure of a C Program
<Header files>
<Declaration of global variables>
main( )
{
<Declaration of local variables>
[Input part]
Calculation part
Display part
}
<sub program – function blocks>
Example:
/*Addition of two number*/
#include<stdio.h>
main()
{
int n1,n2,sum;
printf(“Enter two nos:”);
scanf(“%d%d”, &n1,&n2);
sum=n1+n2;
printf(“Sum of two nos=%d”,sum);
}
Explanation:
Comment line: Non-executable lines.
Header files: Header files commonly contain forward declarations of classes,
subroutines, variables, and other identifiers.
Global variable: Variables which can be used anywhere in the program. They are
declared before main( ) function
main(): It is a special function from where the execution starts. Every program must
have a main() function. Here main doesn’t have any arguments.
{: Beginning of main function.
}: End of main function.
Function body: Lines between the ‘{‘and ‘}’.
Local variable: Can be used in the same function. n1, n2, sum are global variables.
Variable declaration: All variables should be declared to indicate what type of data
that variable could store.
Library Function: printf(), scanf().
Input: Input can be given by some predefined library function like scanf(), getchar()
etc.
Calculation: Normally calculation is carried out after input.
Output: Normally output is printed at the end of the function by using some library
function like printf().
User defined Function: Defined after defination of main() function.
Semicolon(;): Every statement of C ends with a semicolon.
Compilation Process:
Source code: Program written in any programming language is known as source code.
Source code is written in an editor.
Compilation: It is the process of translating source program into computer
understandable form (binary form). A system software known as compiler does this
compilation. In this stage, the compiler first checks the syntax of the program. If
everything (syntax and semantics) is all right, then it translates the source code and
stores it in another file. If there is some error, it shows the error message. In case of
error, we have to do the correction and again compile it.
Object code: After translation of the source code, the file created is known as object
code. In Linux Operating System if the source file is “fact.c” then the object code would
be “fact.o”.
Linking: It is the process of putting together other program files and functions that are
required by the program. Linker is software, which does this. For example if the
program uses a mathematical function “pow()” then the object code of this function
should be brought from the math library.
Executable code: The compiled and linked code is known as executable code. In Linux
the executable code is store automatically in a file known as “a.out”.
Loading: Loader is a program, which accepts the object program, prepares it for
execution & initiates the execution. It allocates the space in the main memory for
loading the program.
Execution: To execute the executable code we have to just type the name of the
executable file “a.out”. During this stage it asks for the input. We provide the input
and it gives us the output.
Steps in Learning English:
Alphabets------Words-------Statements------Paragraph
Steps in Learning C:
Character Set (Alphabets, Digits, Symbols)------Constants, Variables, Keywords----
---Instruction------Program.