0% found this document useful (0 votes)
12 views23 pages

Lecture 6

psuc material muj

Uploaded by

rishitgupta2019
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views23 pages

Lecture 6

psuc material muj

Uploaded by

rishitgupta2019
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Structure of C

Program, Character
Set, Tokens
C Character set

Character set is a Letters  ‘a’, ‘b’, ‘c’,………..’z’ Or ‘A’, ‘B’, ‘C’,……….’Z’


set of valid
characters that a
language can
recognize.
Digits  0, 1, 2,……………………9
C character set
consists of letters,
digits, special Special characters  ;, ?, >, <, &,{, }, [, ]……
characters, white
spaces.
White spaces  New line (\n), Tab(\t), Vertical Tab(\
v) etc.

2
C Tokens
Tokens

A token is a group of Special


Keywords Identifiers Operators Strings Constants
characters that Symbols
logically belong
together. Break Variable + “hello” 123 ;
The programmer
writes a program by Int Constant * “123” 21.3 ?
using tokens.
C uses the following Function
float % “s” ‘A’ >
name
types of tokens.
Array
‘g’ &
name

“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(“You can learn it easily ”); /*add comment*/

printf(“easily\n”);
}

7
Input: Scanf()
#incude<stdio.h>

Scanf() is used to obtain void main()


the value from the user {
int num;
It is included in stdio.h
printf(“Enter a number = ”);

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

%c Read a single character

%d Read a decimal integer

%f Read a floating point number

%g Read a short int number

%h Read a short int number


C Program Essentials: Format Specifiers
Format specifier Description
%d or %i It is used to print the signed integer value where signed
integer means that the variable can hold both positive and
negative values.
%u It is used to print the unsigned integer value where the
unsigned integer means that the variable can hold only
positive value.
%o It is used to print the octal unsigned integer where octal
integer value always starts with a 0 value.
%x It is used to print the hexadecimal unsigned integer where
the hexadecimal integer value always starts with a 0x value.
In this, alphabetical characters are printed in small letters
such as a, b, c, etc.
%X It is used to print the hexadecimal unsigned integer, but %X
prints the alphabetical characters in uppercase such as A, B,
C, etc. 11
C Program Essentials: Format
Specifiers
Format specifier Description
%e or %E It is used for scientific notation. It is also known as Mantissa
or Exponent.
%g It is used to print the decimal floating-point values, and it
uses the fixed precision, i.e., the value after the decimal in
input would be exactly the same as the value in the output.

%p It is used to print the address in a hexadecimal form.


%c It is used to print the unsigned character.
%s It is used to print the strings.
%ld It is used to print the long-signed integer value.

12
Output: Printf()

C provides the printf() to display the data on the monitor.

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

Syntax errors: violation of programming


language rules (grammar).
• Detected by the compiler
• Eg: printf (“hello world”) // semicolon missing

Logical errors: errors in meaning:


• Programs are syntactically correct but don’t produce the
expected output
• User observes output of running program

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

You might also like