0% found this document useful (0 votes)
31 views55 pages

PIC Lecture 1 Updated

Uploaded by

shreya13
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)
31 views55 pages

PIC Lecture 1 Updated

Uploaded by

shreya13
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/ 55

Programming in C

Programming in C 09/03/2025
Topics for today

• Syllabus in brief
• Marking scheme
• Language/computer language
• Need of computer language
• Why to study C?
• Platform dependency in C

Programming in C 09/03/2025
Introduction

• What is Language?
• Follow set of instructions/ programs

• Application/ software
• What is Computer Language?

Programming in C 09/03/202
5
Introduction: Need of Computer Language
English
Hindi
etc.

Programming in C 09/03/202
5
Introduction: Need of Computer Language
English
Hindi
etc.

communication

Binary
10110…

Programming in C 09/03/202
5
Introduction: Need of Computer Language
English
Hindi
etc.

communicationX

Binary
10110…

Programming in C 09/03/202
5
Introduction: Need of Computer Language
High level languages
English
Hindi C
etc. C++
Java
Python, R
using etc.
communicationX

Binary
10110…

Programming in C 09/03/202
5
Introduction: Need of Computer Language
High level languages
English
Hindi C
etc. C++ Writing programs
Java
Python, R
using etc.
communicationX

Binary
a = 2;
10110… b=6;
c=a + b;
Print c.

Programming in C 09/03/202
5
Introduction: Need of Computer Language
High level languages
English
Hindi C
etc. C++ Writing programs
Java
Python, R
using etc.
communicationX

10110100 a = 2;
Binary
10110… 1000 b=6;
10011 Compiler C=a + b;
111101 Print c.

Programming in C 09/03/202
5
Introduction: Need of Computer Language
High level languages
English
Hindi C
etc. C++ Writing programs
Java
Python, R
using etc.
communicationX

10110100 a = 2;
Binary
10110… 1000 b=6;
10011 Compiler C=a + b;
111101 Print c.

Programming in C 09/03/202
5
What is C?

• It is a very powerful and general-purpose language used in


programming. We can use C to develop software such as databases,
operating systems, compilers, and many more. This programming
language is excellent to learn for beginners in programming.

• The C language is imperative, procedural, and general-purpose in


nature, developed by Dennis M. Ritchie in 1972 at the Bell
Telephone for developing the UNIX OS. As of now, the C language is
one of the most widely used computer languages along with Java,
which is mostly used among modern programmers.

09/03/2025 Programming in C
Why Learn C Programming
Language?
• This language helps users comprehend a computer’s internal architecture. It
assists you in knowing how a computer would store information within and
retrieve it.
• Learning other programming languages becomes easier after learning C, such
as Python, Java, etc.
• A programmer who is well-versed with the C language gets opportunities to
work on various open-source projects. For instance, some of the very popular
projects (open-source) have been written using the C programming language,
such as Python interpreter, Linux kernel, SQLite database, and many more.

09/03/2025 Programming in C
09/03/2025 Programming in C
Applications of a C Program
• We can use the C language in development:
• Language Compilers
• Operating Systems
• Text Editors
• Assemblers
• Network Drivers
• Print Spoolers
• Modern Programs
• Language Interpreters
• Databases
• Utilities
09/03/2025 Programming in C
Disadvantages:
• Steep learning curve: C can be difficult to learn, especially for beginners, due to its
complex syntax and low-level access to system resources.
• Lack of memory management: C does not provide automatic memory management,
which can lead to memory leaks and other memory-related bugs if not handled
properly.
• No built-in support for object-oriented programming: C does not provide built-in
support for object-oriented programming, making it more difficult to write object-
oriented code compared to languages like Java or Python.
• No built-in support for concurrency: C does not provide built-in support for
concurrency, making it more difficult to write multithreaded applications compared to
languages like Java or Go.
• Security vulnerabilities: C programs are prone to security vulnerabilities, such as buffer
overflows, if not written carefully.
09/03/2025 Programming in C
Problem solving skill development: Logical skills, Algorithms and flowcharts

Problem: Print 1 to 20 numbers

Algorithm:

Step 1: Initialize variable “a” = 0;

Step 2: Increment “a”

Step 3: Print “a”

Step 4: Check “a” is less than 20, goto Step 2

Programming in C 09/03/202
5
Problem solving skill development: Logical skills, Algorithms and flowcharts
• A graphical or diagrammatical representation of an algorithm is Flowchart

Problem: Print 1 to 20 numbers Start

a=0
Algorithm:
a=a+1
Step 1: Initialize variable “a” = 0;
Print a
Step 2: Increment “a”

Step 3: Print “a” a < 20?

Step 4: Check “a” is less than 20, goto Step 2


end

Programming in C 09/03/202
5
Compiling & Executing C Program

Programming in C 09/03/202
5
Simple C program structure:

Programming in C 09/03/202
5
Simple C program structure:

Programming in C 09/03/202
5
Elements of C program
• Token in C

Programming in C 09/03/202
5
Elements of C program
• Identifiers in C

Programming in C 09/03/202
5
Elements of C program
• Keywords in C

Compiler
specific

Programming in C 09/03/202
5
Elements of C program
• Constants and variables in C
A constant is an entity that doesn’t change whereas a variable is an entity that may change.

Programming in C 09/03/202
5
Elements of C program
• Constants and variables in C
A constant is an entity that doesn’t change whereas a variable is an entity that may change.

Variable Constant

Since the location whose name is var can hold different values at different times, var is
known as a variable. As against this, 10 or 5 do not change, hence are known as constants.

Programming in C 09/03/202
5
Elements of C program
• Types of Constants in C

Programming in C 09/03/202
5
Elements of C program
• Rules for Constructing Integer Constants in C

(a) An integer constant must have at least one digit.


(b) It must not have a decimal point.
(c) It can be either positive or negative.
(d) If no sign precedes an integer constant it is assumed to be positive.
(e) No commas or blanks are allowed within an integer constant.
(f) The allowable range for integer constants is -32768 to 32767.

Programming in C 09/03/202
5
Elements of C program
• Rules for Constructing Character Constants in C

(a) A character constant is a single alphabet, a single digit or a single special symbol
enclosed within single inverted commas. Both the inverted commas should point to the
left.
For example, ’A’ is a valid character constant whereas ‘A’ is not.
(b) The maximum length of a character constant can be 1 character.

Ex.:
'A'
'I'
'5'
'='

Programming in C 09/03/202
5
Elements of C program
• Rules for Constructing Variable Names in C

(a) A variable name is any combination of 1 to 31 alphabets, digits or underscores.


Some compilers allow variable names whose length could be up to 247 characters. Still,
it would be safer to stick to the rule of 31 characters. Do not create unnecessarily long
variable names as it adds to your typing effort.
(b) The first character in the variable name must be an alphabet or underscore.
(c) No commas or blanks are allowed within a variable name.
(d) No special symbol other than an underscore (as in gross_sal) can be used in a
variable name.

Ex.: si_int
m_hra
pop_e_89

Programming in C 09/03/202
5
Elements of C program
• Data types in C

Programming in C 09/03/202
5
Elements of C program
• Data types in C

Programming in C 09/03/202
5
Program to take input of various datatypes in C
• Taking integer as input from user: input and display two numbers at a time

• Taking float as input from user

• Taking character as input from user

Programming in C 09/03/202
5
Program to take input of various datatypes in C

%d and %i, both are used to take numbers as input from the user.
%f is the format specifier to take float as input from the user
%c is the format specifier to take character as input from the user

Programming in C 09/03/202
5
Program to take input of various datatypes in C
Lets try this
int a=25;
float b=5.67;
char ch=‘g’;
char s[]=“Hello”;
printf ( "\n%c %d %f", ch, ch, ch ) ;
printf ( "\n%s %d %f", s, s, s ) ;
printf ( "\n%c %d %f",a ,a, a ) ;
printf ( "\n%f %d\n", b, b ) ;

Programming in C 09/03/202
5
ASCII value table Dec=ASCII value

Programming in C 09/03/202
5
Elements of C program
• Local and Global Variables in C
Local Variable:
The variables which are declared inside the function, compound statement (or block) are
called Local variables.
void function_1()
{
int a, b; // you can use a and b within braces only
}

void function_2()
{
printf("%d\n", a); // ERROR, function_2() doesn't know any variable a
}

Programming in C 09/03/202
5
Elements of C program
• Local and Global Variables in C
Local Variable:
The variables which are declared inside the function, compound statement (or block) are
called Local variables.
int main()
{
int a = 100;

{
int a = 10;
printf("Inner a = %d\n", a);
}

printf("Outer a = %d\n", a);

return 0;
}

Programming in C 09/03/202
5
Elements of C program
• Local and Global Variables in C

Global Variable:
• The variables declared outside any function are called global variables.
• They are not limited to any function.
• Any function can access and modify global variables.
• Global variables are automatically initialized to 0 at the time of declaration.
• Global variables are generally written before main() function.

Programming in C 09/03/202
5
#include<stdio.h>
void func_1();
void func_2();
int a, b = 10; // declaring and initializing global variables
int main()
{
printf("Global a = %d\n", a);
printf("Global b = %d\n\n", b);

func_1();
func_2();
return 0;
}
void func_1()
{
printf("From func_1() Global a = %d\n", a);
printf("From func_1() Global b = %d\n\n", b);
}
void func_2()
{
int a = 5;
printf("Inside func_2() a = %d\n", a);
}
Programming in C 09/03/202
5
Elements of C program
• Constants in C

#include <stdio.h>
#define num 25
#define pi 3.14
int main() {
float p, r=2.5;
printf("The value of pi is: %f", pi);
p= 2* pi *r;
printf("The value of perimeter is: %f", p);
return 0;
}

Programming in C 09/03/202
5
Elements of C program
• Input/output functions

There are numerous library functions available for I/O. These can be classified into two broad
categories:

(a) Console I/O functions - Functions to receive input from keyboard and write output to VDU.

(b) File I/O functions - Functions to perform I/O operations on a floppy disk or hard disk.

In this chapter we would be discussing only Console I/O functions.

Programming in C 09/03/202
5
Elements of C program
• Console Input/output functions

Programming in C 09/03/202
5
Elements of C program
• Formatted console Input/output functions

The formatted functions allow the input read from the keyboard or the output displayed on the
VDU to be formatted as per our requirements.

For example, if values of average marks and percentage marks are to be displayed on the screen,
then the details like where this output would appear on the screen, how many spaces would be
present between the two values, the number of places after the decimal points, etc. can be
controlled using formatted functions.

Programming in C 09/03/202
5
Elements of C program
• Formatted console Input/output functions-Format Specifiers
main( )
{
int weight = 63 ;
printf ( "\nweight is %d kg", weight ) ;
printf ( "\nweight is %2d kg", weight ) ;
printf ( "\nweight is %4d kg", weight ) ;
printf ( "\nweight is %6d kg", weight ) ;
printf ( "\nweight is %-6d kg", weight ) ;
}

The output of the program would look like this ...

Programming in C 09/03/202
5
Elements of C program
• Formatted console Input/output functions-Format Specifiers
/* Formatting strings with printf( ) */
main( )
{
char firstname1[ ] = "Sandy" ;
char surname1[ ] = "Malya" ;
char firstname2[ ] = "AjayKumar" ;
char surname2[ ] = "Gurubaxani" ;
printf ( "\n%20s%20s", firstname1, surname1 ) ;
printf ( "\n%20s%20s", firstname2, surname2 ) ;
}

And here’s the output...


Columns

Programming in C 09/03/202
5
Elements of C program
• Formatted console Input/output functions- Escape sequences

Programming in C 09/03/202
5
Elements of C program
• UnFormatted console Input/output functions

Programming in C 09/03/202
5
Elements of C program
• UnFormatted console Input/output functions

main( )
{ main( )
char ch ; {
printf ( "\nPress any key to continue" ) ; char ch = 'A' ;
getch( ) ; /* will not echo the character */ putch ( ch ) ;
putchar ( ch ) ;
printf ( "\nType any character" ) ; fputchar ( ch ) ;
ch = getche( ) ; /* will echo the character typed */ putch ( 'Z' ) ;
printf ( "\nType any character" ) ; putchar ( 'Z' ) ;
fputchar ( 'Z' ) ;
getchar( ) ; /* will echo character, must be followed by enter key */ }
printf ( "\nContinue Y/N" ) ;
fgetchar( ) ; /* will echo character, must be followed by enter key */
}

Programming in C 09/03/202
5
Elements of C program
• UnFormatted console Input/output functions
The limitation of putch( ), putchar( ) and fputchar( ) is that they can output only one character
at a time.

Solution:
gets( ) and puts( )
gets( ) receives a string from the keyboard. Why is it needed?
Because scanf( ) function has some limitations:
main( )
{
char name[50] ;
printf ( "\nEnter name " ) ;
scanf ( "%s", name ) ;
printf ( "%s", name ) ;
}

Programming in C 09/03/202
5
Elements of C program
• UnFormatted console Input/output functions –gets() and puts()
The solution to this problem is to use gets( ) function.

• As said earlier, it gets a string from the keyboard.

• It is terminated when an Enter key is hit. Thus, spaces and tabs are perfectly acceptable as
part of the input string.

• More exactly, gets( ) gets a newline (\n) terminated string of characters from the keyboard
and replaces the \n with a \0.

• The puts( ) function works exactly opposite to gets( ) function. It outputs a string to the
screen.

Here is a program which illustrate

Programming in C 09/03/202
5
Elements of C program
• UnFormatted console Input/output functions –gets() and puts()
main( )
{
char footballer[] ;
puts ( "Enter name" ) ;
gets ( footballer ) ; /* sends base address of array */
puts ( "Happy footballing!" ) ;
puts ( footballer ) ;
}

Programming in C 09/03/202
5
Elements of C program
• Completed first module from syllabus

Programming in C 09/03/202
5
Program to display your complete name, roll no., department, college and
12th percentage entered from the user in C

Programming in C 09/03/202
5
Program to swap two numbers entered by user in C

Input: a= 67, b=32;

Output: a=32,b=67

Programming in C 09/03/202
5
The length & breadth of a rectangle and radius of a circle are input through the
keyboard. Write a program to calculate the area & perimeter of the rectangle, and
the area & circumference of the circle.

Programming in C 09/03/202
5

You might also like