CS174 ProgramminginC 1
CS174 ProgramminginC 1
Unit V: Points
LECTURE ONE
Unit I: Introduction to C programming Language
Whereas interpreter read only one line of the source code and
convert it to object code.
It checks error, statement by statement i.e. line by line and
hence takes more time.
High level programming language
1 ) Comment line
2) Preprocessor directive
3 ) Global variable declaration
4) main function( )
{
Local variables;
Statements;
}
User defined function
}
}
Structure of C Language program…
Comment line
It indicates the purpose of the program. It is represented as
/*……………………………..*/
Comment line is used for increasing the readability of the
program.
It is useful in explaining the program and generally used for
documentation.
It is enclosed within the decimetres. Comment line can be
single or multiple line but should not be nested.
It can be anywhere in the program except inside string
constant & character constant.
Structure of C Language program…
Preprocessor Directive
#include<stdio.h> tells the compiler to include information
about the standard input/output library.
It is also used in symbolic constant such as #define PI
3.14(value).
The stdio.h (standard input output header file) contains
definition &declaration of system defined function such as
printf( ), scanf( ), pow( ) etc.
Generally printf() function used to display and scanf()
function used to read value
Structure of C Language program…
Global Declaration
This is the section where variable are declared globally so
that it can be access by all the functions used in the
program. And it is generally declared outside the function
Main()
It is the user defined function and every function has one
main() function from
Where actually program is started and it is encloses within the
pair of curly braces.
The main( ) function can be anywhere in the program but in
general practice it is placed in the first position.
Structure of C Language program…
Main() ….
It Syntax :
main()
{
……..
……..
}
The main( ) function return value when it declared by data type
as int main( )
{
return 0
}
Structure of C Language program…
Main() ….
The main function does not return any value when void
(means null/empty) as void main(void ) or void main()
{
printf (“C language”);
}
Output: C language
The program execution start with opening braces and end with
closing brace.
And in between the two braces declaration part as well as
executable part is
mentioned. And at the end of each line, the semi-colon is given
which indicates statement termination
Structure of C Language program…
Step 1
The program that is to be compiled is first typed into a file on the
computer system.
There are various conventions that are used for naming files,
typically be any name provided the last two characters are “.c” or
file with extension .c.
So, the file name prog1.c might be a valid filename for a C
program. A text editor is usually used to enter the C program into
a file.
For example, vi is a popular text editor used on Unix systems. The
program that is entered into the file is known as the source
program because it represents the original form of the program
expressed in the C language.
Steps for Compiling and executing the Programs
Step 2
The compilation process is initiated by typing a special command
on the system. When this command is entered, the name of the
file that contains the source program must also be specified.
For example, under Unix, the command to initiate program
compilation is called cc. If we are using the popular GNU C
compiler, the command we use is gcc.
$ gcc prog1.c or cc prog1.c
In the first step of the compilation process, the compiler examines
each program statement contained in the source program and
checks it to ensure that it conforms to the syntax and semantics of
the language
Steps for Compiling and executing the Programs
Step 2…
If any mistakes are discovered by the compiler during this phase,
they are reported to the user and the compilation process ends
right there.
The errors then have to be corrected in the source program (with
the use of an editor), and the compilation process must be
restarted.
Typical errors reported during this phase of compilation might be
due to an expression that has unbalanced parentheses (syntactic
error), or due to the use of a variable that is not “defined”
(semantic error).
Steps for Compiling and executing the Programs
Step 3
When all the syntactic and semantic errors have been
removed from the program, the compiler then proceeds to
take each statement of the program and translate it into a
“lower” form that is equivalent to assembly language
program needed to perform the identical task.
Steps for Compiling and executing the Programs
Step 4
After the program has been translated the next step in the
compilation process is to translate the assembly language
statements into actual machine instructions.
The assembler takes each assembly language statement
and converts it into a binary format known as object code,
which is then written into another file on the system.
This file has the same name as the source file under Unix,
with the last letter an “o” (for object) instead of a “c”.
Steps for Compiling and executing the Programs
Step 5
After the program has been translated into object code, it is
ready to be linked.
This process is once again performed automatically whenever
the cc or gcc command is issued under Unix.
The purpose of the linking phase is to get the program into a
final form for execution on the computer.
If the program uses other programs that were previously
processed by the compiler, then during this phase the
programs are linked together.
Steps for Compiling and executing the Programs
Step 5…
Programs that are used from the system’s program library are also
searched and linked together with the object program during this phase.
The process of compiling and linking a program is often called building.
The final linked file, which is in an executable object code format, is
stored in another file on the system, ready to be run or executed.
Under Unix, this file is called a.out by default.
Under Windows, the executable file usually has the same name as the
source file, with the c extension replaced by an exe extension
Steps for Compiling and executing the Programs
Step 6
To subsequently execute the program, the command a.out has the effect of
loading the program called a.out into the computer’s memory and initiating its
execution.
When the program is executed, each of the statements of the program is
sequentially executed in turn.
If the program requests any data from the user, known as input, the program
temporarily suspends its execution so that the input can be entered
Or, the program might simply wait for an event, such as a mouse being clicked,
to occur
Steps for Compiling and executing the Programs
Step 6…
Results that are displayed by the program, known as output, appear in a
window, sometimes called the console.
If the program does not produce the desired results, it is necessary to go
back and reanalyze the program’s logic.
This is known as the debugging phase, during which an attempt is made
to remove all the known problems or bugs from the program.
To do this, it will most likely be necessary to make changes to original
source program.
/* Simple program to add two numbers…………………….*/
#include <stdio.h>
int main (void)
{
int v1, v2, sum; //v1,v2,sum are variables and int is data type
declared
v1 = 150;
v2 = 25;
sum = v1 + v2;
printf ("The sum of %i and %i is= %i\n", v1, v2, sum);
return 0;
}
Output:
The sum of 150 and 25 is=175
END OF LECTURE ONE
DISCUSSION QUESTIONS
Unit I: Introduction to C programming Language
LECTURE TWO
Character set
There are certain words reserved for doing specific task, these
words are known as reserved word or keywords. These
words are predefined and always written in lower case or small
letter.
These keywords can’t be used as a variable name as it
assigned with specific meaning.
Some examples are:
int, short, signed, unsigned, default, volatile, float, long,
double, break, continue, typedef, static, do, for, union,
return, while, do, extern, register, enum, case, goto, struct,
char, auto, const etc.
Data types
Numeric constant
Character constant
String constant
Numeric constant
Syntax:
int a; char c; float f;
Variable initialization
When we assign any initial value to variable during the
declaration, is called initialization of variables.
When variable is declared but contain undefined value then it is
called garbage value.
The variable is initialized with the assignment operator such as
All the basic arithmetic operations can be carried out in C. All the
operators have almost the same meaning as in other languages.
Both unary and binary operations are available in C language.
Unary operations operate on a singe operand, therefore the
number 5 when operated by unary – will have the value –5
+ For performing Addition
- For performing Subtraction
/ For performing Division
* For performing Multiplication
% Modulo for finding remainder in division operation
Note: But modulus cannot applied with floating point operand as well as
there are no exponent operator in c.
Arithmetic operator...
Integer Arithmetic
When an arithmetic operation is performed on two whole numbers
or integers then such an operation is called as integer arithmetic. It
always gives an integer as the result. Let x = 27 and y = 5 be 2
integer numbers. Then the integer operation leads to the following
results.
x + y = 32
x – y = 22
x * y = 115
x%y=2
x/y=5
Note: In integer division the fractional part is truncated.
Arithmetic operator...
15/10.0 = 1.
Relational Operators
Logical OR (||)
The logical OR is used to combine 2 expressions or the condition
evaluates to true if any one of the 2 expressions is true.
For Example
a < m || a < n
The expression evaluates to true if any one of them is true or if both
of them are true.
It evaluates to true if a is less than either m or n and when a is less
than both m and n.
Logical Operators…
For Example
! (x >= y)
The NOT expression evaluates to true only if the value of x is
neither greater than or equal to y
Assignment Operators
++variable name and variable name++ mean the same thing when
they form statements independently, they behave differently when
they are used in expression on the right hand side of an assignment
statement: Consider the following
m = 5;
y = ++m; (prefix)
In this case the value of y and m would be 6. Suppose if we rewrite
the above statement as
m = 5;
y = m++; (post fix)
Then the value of y will be 5 and that of m will be 6. A prefix operator
first adds 1 to the operand and then the result is assigned to the
variable on the left. On the other hand, a postfix operator first assigns
the value to the variable on the left and then increments the operand.
Conditional or Ternary Operator
The operator size of gives the size of the data type or variable in
terms of bytes occupied in the memory. The operand may be a
variable, a constant or a data type qualifier.
For Example
m = sizeof (sum);
n = sizeof (long int);
k = sizeof (235L);
The size of operator is normally used to determine the lengths of
arrays and structures when their sizes are not known to the
programmer.
It is also used to allocate memory space dynamically to variables
during the execution of the program. Example program that employs
different kinds of operators. The results of their evaluation are also
shown in comparison
C Programming Input Output (I/O): printf() and
scanf()
Output
Enter a character: g
You entered g.
ASCII value of g is 103.
The ASCII value of character 'g' is 103. When, 'g' is entered, 103 is
stored in variable var1 instead of g.
You can display a character if you know ASCII code of that
character. This is shown by following example.
C Programming Input Output (I/O): printf() and
scanf()…
Output
Character having ASCII value 69 is E.
More on Input/Output of floats and Integers
// Tries to print number right justified to 3 digits but the number is not right adjusted
because there are only 4 numbers
printf("4 digit integer right justified to 3 column: %3d\n", integer);
Output
4 digit integer right justified to 6 column: 9876
4 digit integer right justified to 3 column: 9876
Floating point number rounded to 2 digits: 987.65
Floating point number rounded to 0 digits: 988
Floating point number in exponential form: 9.876543e+02
END OF LECTURE TWO
DISCUSSION QUESTIONS
Unit II: Control Structures
LECTURE ONE
Control Statement
Syntax:-
while(condition)
{
Statement 1;
Statement 2;
}
Or while(test condition)
Statement;
while loop
Example:-
/* Program to print 5 times welcome to C” */
#include<stdio.h>
void main() Output:
Welcome to C
{ Welcome to C
int p=1; Welcome to C
While(p<=5) Welcome to C
Welcome to C
{
printf(“Welcome to C\n”);
P=p+1;
} So as long as condition remains true statements within the
body of while loop will get executed repeatedly
}
While loop
Example:-
/* Program to print 5 times welcome to C” */
#include<stdio.h>
void main()
{
int p=1;
While(p<=5)
{
printf(“Welcome to C\n”);
P=p+1;
}
}
do while loop
This (do while loop) statement is also used for looping. The
body of this loop may contain single statement or block of
statement.
The syntax for writing this statement is:
Do
{
Statement;
}
while(condition);
do while loop
Notes:
Do while loop used rarely when we want to execute a loop
at least once
do while loop
Example:-
#include<stdio.h>
void main()
{
int X=4;
do Output: 4 5 6 7 8 9 10
{
Printf(“%d”,X);
X=X+1;
} while(X<=10);
Printf(“ ”);
}
for loop
Example:-
void main()
{
int i;
for(i=1;i<10;i++)
{
Printf(“ %d ”, i);
}
}
Output:1 2 3 4 5 6 7 8 9
for loop..
break;
Break statement(break)…
void main()
{
int n;
for(n=2; n<=9; n++)
{
if(n==4) Output: 2 3 5 6 7 8 9 out of loop
continue;
printf(“%d”, n);
}
}
Printf(“out of loop”);
}
END OF LECTURE ONE
DISCUSSION QUESTIONS