Lecture 6
Lecture 6
Program, Character
Set, Tokens
C Character set
2
C Tokens
Tokens
“hello” {
3
Simple C program
5
Hello world program
Structure of C program
#incude<stdio.h>
void main()
{
printf(“I love programming\n”); /*new line*/
printf(“easily\n”);
}
7
Input: Scanf()
#incude<stdio.h>
scanf(“%d”, &num);
Eg: scanf(“%d”, &integer1);
printf(“The number is = %d”, num);
}
8
Data Types and its Storage Requirements and
its Range
Format
Specifier
%c
%c
%d
%u
%h
%hu
%ld
%lu
%f
%lf
%Lf
9
Character Meaning
Group
12
Output: Printf()
It is included in stdio.h
Examples are:
• Printf(“programming is an art”;
• Printf(“%d”, number);
• Printf(“%f%f”, p,q);
13
#include <stdio.h>
Adding two int main( void )
integers {
int sum; /* variable in which sum will be stored */
int integer1; /* first number to be input by user */
int integer2; /* second number to be input by user */
printf( "Enter first integer\n" );
scanf( "%d", &integer1 ); /* read an integer */
printf( "Enter second integer\n" );
scanf( "%d", &integer2 ); /* read an integer */
sum = integer1 + integer2; /* assign total to sum */
printf( "Sum is %d\n", sum ); /* print sum */
return 0; /* indicate that program ended successfully */
} /* end function main */
Syntax and Logical errors
15
C Compiler:
• For Window, you need to install MinGW for GCC or you can use Turbo C
compiler.
• The most frequently used and free available compiler is the GNU C/C++
compiler for Linux
• For MAC, the easiest way to obtain GCC is to download the Xcode
development environment from Apple's web site and follow the simple
installation instructions.
What to do when a C program produces different results in two different
compilers? Your program must follow C standard i.e. ISO/IEC 9899:2011
(also known as C11)
16
Compilation Process
• The compilation is a process of converting the source code into
object code.
• The compilation process can be divided into four steps, i.e.,
Pre-processing, Compiling, Assembling, and Linking.
The extension of the object
file in DOS is '.obj,' and in
UNIX, the extension is 'o'.
17
Thank You!!
18
A program for Finding an Even and
Odd
#include<stdio.h>
main()
{
int a,x;
printf("Enter a number\n");
scanf("%d",&a);
x=a%2;
if(x==0)
printf("Number a=%d is even\n",a);
else
printf("Number a=%d is odd\n",a);
}
19
Swap of Two Numbers, Using Two
Variables
#include<stdio.h>
main()
{
int a=5, b=6;
printf("Swap of Numbers\n");
a=a+b; printf("a=%d\n",a);
b=a-b; printf("b=%d\n",b);
a=a-b; printf("a=%d\n",a);
///b=c;
printf("Final a=%d and b=%d",a,b);
} 20
Interest Calculation Program
#include<stdio.h>
#define PERIOD 10
#define PRINCIPAL 5000.00
void main()
{
int year;
float amount, value, Inrate;
amount=PRINCIPAL;
inrate=0.11;
year=0;
while(year<=PERIOD)
{
printf("year=%2d and amount=%8.2f\n",year, amount);
value=amount=amount+inrate*amount;
year=year+1;
amount=value;
}
21
}
Subroutine Call Program
#include<stdio.h>
main()
{
int a, b, c;
a=5; b=6;
c=mul(a,b);
printf("multplcaton of a=%d and b=%d is = %d",a,b,c);
}
int mul(int x,int y)
{
int p;
{
p=x*y;
return p;
}
22
}
Compilation Process
Example
23