Module 1
Module 1
Reference Books
E Balaguruswamy, “Programming in ANSI C”.
How to install TDM GCC on windows
2. https://jmeubank.github.io/tdm-gcc/download/
3. Download your window bit exe
4. Open wingw command prompt
5. Go to the directory where your c files are stored
6. gcc filename.c –o outputfilename
Overview of C Language
1) K&R C
In 1978, Brian Kernighan and Dennis Ritchie published the
first edition of The C Programming Language. This book,
known to C programmers as "K&R", served for many years as
an informal specification of the language.
2) ANSI C and ISO C (C89 or C90)
ANSI (American National Standards Institute) is the primary
organization for encouraging the development of technology
standards in the United States.
In 1983, the American National Standards Institute (ANSI)
formed a committee, to establish a standard specification of
C. In 1989, the C standard was rectified as ANSI X3.159-1989
"Programming Language C". This version of the language is
often referred to as ANSI C, Standard C, or sometimes C89.
History of C Standards
void main()
{
}
The above program fails in g++ compiler as the return type
of main is void, but it compiles in Turbo C. How do we decide
whether it is a legitimate C program or not?
Example
Let's say that you have a friend arriving at the airport, and your
friend needs to get from the airport to your house. What are the
algorithms that you might give your friend for getting to your
home:
Possible Solutions?
Ans: Algorithm:
1. Input set of marks
2. Find the average by adding the marks and dividing them by 4
3. if average is less than 40 then display fail otherwise pass
Pseudocode:
1. Input set of 4 marks a,b,c,d
2. Compute average as (a+b+c+d)/4
3. if (average<40) then
display fail
else display pass
Example-2
pseudocode :
Step 1: Input N
Step 2: Sum = 0
Step 3: While (N != 0)
Rem = N % 10;
Sum = Sum + Rem;
N = N / 10;
Step 4: Print Sum
Example-2
Flow Chart
Questions?
#include <stdio.h>
#define SOMETHING "Hello World" Preprocessor
directive
int main()
{
/* Using a macro to print 'Hello World'*/
printf(“Hi”);
printf(SOMETHING);
return 0;
}
Compiling and executing a ‘C’ code
1) Preprocessing: This is the very first stage through which a source code
passes. In this stage the following tasks are done:
Removal of Comments
Expansion of Macros
Expansion of the included files.
Conditional compilation
Analysis:
printf contains now “Hello World” rather than the macro SOMETHING
that’s because macros have expanded.
Comments are stripped off.
#include<stdio.h> is missing instead we see lots of code. So header
files has been expanded and included in our source file.
Compiling and executing a ‘C’ code
Basic Type:
1. Integer Type: Integers are whole numbers that can have
both positive and negative values but no decimal values.
Example: 0, -5, 10
In C programming, keyword int is used for declaring integer
variable. For example: int a;
Here, a is a variable of type integer.
Data types
Different data types also have different ranges upto which they
can store numbers. These ranges may vary from compiler to
compiler. Below is list of ranges along with the memory
requirement and format specifiers on 32 bit gcc compiler.
Online Lecture
Note: A program can have same name for local and global
variables but the value of local variable inside a function will
take preference.
Example:
Constant in C
Integer Constant:
An integer constant is a decimal(base 10), octal(base 8), or
hexadecimal(base 16) number that represents an integer
value.
Integer constants are used to represent integer value that
cannot be changed.
Example: 10, -121, 0, 0256, +14, 0x6, 0xA
Rules for constructing integer constant:
1) An integer constant must have at least one digit.2) It must
not have a decimal point.3) It can either be positive or negative.
4) No commas or blanks are allowed within an integer constant.
5) If no sign precedes an integer constant, it is assumed to be
positive.
Constant in C
Operator Types:
Operators are the symbols which tell the computer to execute
certain mathematical or logical operations.
Arithmetic operators
Relational operators
Logical operators
Assignment operators
Increment and Decrement operators
Conditional operators
Bitwise operators
Special operators
Arithmetic Operators
Relational Operators
Relational operators are used when we have to make comparisons. C
programming offers 6 relational operators.
The shift operators shift their first operand left (<<) or right
(>>) by the number of positions the second operand
specifies.
Both operands must be integral values. These operators
perform the usual arithmetic conversions; the type of the
result is the type of the left operand after conversion.
Bitwise Shift Operators
The shift operators shift their first operand left (<<) or right
(>>) by the number of positions the second operand
specifies.
Left shift is useful in implementing multiplication with
powers of 2 and Right shift is used to implement division
with powers of 2.class Test { public static void main(String
args[]) { int y = 4; System.out.println(y>>1); }
}
We can also check whether a number is even or odd by AND
ing operation with 1. Because if lowest order bit is set
number is odd otherwise it is even.
Increment and decrement operators
if statement
if..else statements
nested if statements
if-else-if ladder
switch statements
if statement
if(condition)
{
// Statements to execute if
// condition is true
}
if(condition)
statement1;
statement2;
.
Flow chart
if-else
if (condition)
{
// Executes this block if
// condition is true
}
else
{
// Executes this block if
// condition is false
}
Example
Example-1
Example-2
Example-2
switch case
Note:
# include <stdio.h>
int main()
{
int a, b, c, big ;
printf("Enter three numbers : ") ;
scanf("%d %d %d", &a, &b, &c) ;
big = a > b ? (a > c ? a : c) : (b > c ? b : c) ;
printf("\nThe biggest number is : %d", big) ;
}
Loops
while loop
while loop can be addressed as an entry control loop. It is
completed in 3 steps.
Variable initialization.( e.g int x=0; )
condition( e.g while( x<=10) )
Variable increment or decrement ( x++ or x-- or x=x+2 )
Loops
do while loop
In some situations it is necessary to execute body of the loop
before testing the condition. Such situations can be handled
with the help of do-while loop. do statement evaluates the
body of the loop first and at the end, the condition is checked
using while statement. It means that for at least one
time ,the body of the loop will be executed, even though the
starting condition inside while is initialized to false. General
format of do-while loop is,
In for loop, initialization, condition and adjustment
statements are all put together in one line which make loop
easier to understand and implement. While in the while loop,
initialization is done prior to the beginning of the loop.
Conditional statement is always put at the start of the loop.
While adjustment can be either combined with condition or
embedded into the body of the loop. For example: