You are on page 1of 11

2nd Week

Lecture – 1
Highlights

SYNTAX : prntf( syntax / keyword / spelling in all conditions it is a syntax error


If you obtain the result / means wrong results then you should be sure that there is a
logical mistake
First program in C continued

Multiple printf(); statements

Multiple Printf(); and make the program more efficient using escape sequence

Concept of Constants and use in the program

Constants with arithmetic operators

Q/A Session
By: Ameen Khowaja
(Lecturer CS)
First Progarm in C Continued
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf(“SMI University”);
getch();
}

Output of Program Single printf();


SMI University
First Progarm in C Continued
#include<stdio.h>
Single printf(); for Multiple text
#include<conio.h>
display using escape sequence
void main()
{ Is that a good practice???
clrscr();
printf(“SMI University \n Alma Mater of Quaid-e-AzaM”);
getch();
}

Output of Program
SMI University
Alma Mater of Quaid-e-AzaM
#include<stdio.h>
Multiple printf() for multiple text
#include<conio.h>
void main()
Who will find the error in the
{
program
clrscr();

\n
Type of error????

printf(“SMI University ”);

printf(“Alma Mater of Quaid-e-Azam “);


getch();
}

Output of Program
SMI University
Alma Mater of Quaid-e-Azam
Multiple printf() for multiple text
#include<stdio.h>
Yes 
#include<conio.h>
void main() \n will be used
{
clrscr();
printf(“SMI University \n”);
printf(“Alma Mater of Quaid-e-Azam “);
getch();
}
Output of Program
SMI University
Alma Mater of Quaid-e-Azam
\n will break the text and set focus on new line
Printf(); with Tab
#include<stdio.h> indentation
#include<conio.h>
\t is used for tab indentation
void main()
{ Normally equal to 5 spaces
clrscr();
printf(“SMI \t University \t”);
printf(“Alma Mater of Quaid-e-Azam \n”);
printf(“MURAD MUHAMMAD”);
getch();
}
Output of Program
SMI University Alma Mater of Quaid-e-Azam
\t will create a gap between two strings
Escape Sequences
 \n for new line
 \t Horizontal tab. Move the cursor to the next tab
 \a Alert – produces a sound or visible alert without
changing the current cursor position.
 \\ Backslash- Insert a backslash character in a string
 \” Double quote – Insert a double-quote character in string.

Programs for understandings


 Declaration of constants
 Adding two constants
 Finding average of two constants
 Use of Format Specifier for integer
Defining constants
 The constant is a value that never changes.
 Constant can be a number (decimal or floating point, character
etc)
 The constant in the memory takes similar number of bytes which
any variable can hold)
 Value of the constant can not be changed during normal execution
in the program.
Declaration of Constants in program
 int a=10, b=20;
 float a=10.4; Program for adding two constant numbers
 char a=‘y’; Program for declaring constants and finding average
Program to use escape sequences.
 char a[20] = “ameen”;
void main() Int a=10;
Constant Declaration and use Float b=20.29;
{ Int c=a+b;
int a=10; C int a int b float
int b=20; 2.0 not accepted
Printf(“%f”,c);
//OR _________________
int a=10,b=20; Integer constant declared in
clrscr(); memory
printf(“Value of A = %d \n”, a); a =10 & b=20

printf(“Value of B = %d \n”,b); %d is format specifier for


printf(“Total of A & B = %d”,a+b); integer type of data.
getch(); Output of Program
} Value of A = 10
Value of B = 20
Total of A & B = 30
Constant Declaration and use
void main() %f is format specifier for
floating point data
{
int a=10; int b=20; %.2f means two values after
//OR decimal point, likewise %.3f,
%.4f etc.
int a=10,b=20;
clrscr();
Output of Program
printf(“Value of A = %d \n”, a);
printf(“Value of B = %d \n”,b);
Value of A = 10
Value of B = 20
printf(“Total of A & B = %d”,a+b/2);
average of A & B = 15
getch();
} Tip: Average can be in pointing data so we should
declare it as floating point data not as integer.
Q/A Session

Any Questions ????

By: Ameen Khowaja


(Lecturer CS)

You might also like