0% found this document useful (0 votes)
34 views56 pages

1 Module1

Uploaded by

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

1 Module1

Uploaded by

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

Department of Information Science & Engineering

Acharya Institute of Technology

- Nurturing Aspirations & Supporting Growth

C Programming for Problem


Solving-18CPS13/23

C K Marigowda | Associate Professor & HOD | Information Science & Engineering | Acharya Institute of Technology | [email protected]
C K Marigowda - a brief profile
Responsibility
- Head of Information Science and Engineering
- Deputy Director- International Collaborations
- Dean - Alumni Affairs
- SPoC- Infosys Campus Connect
Education and Research:
- Received Bachelor and Masters in Computer Science from PES
- Pursuing Ph.D. in the field of Security in IoT.
- Research interests include Problem solving techniques, Information and Cyber
Security. Security in IoT and Data Science
Accord:
- Establish tie-ups with various international universities
- Coordinating the activities of Alumni Association at Acharya responsible for
coordination of international alumni meets across US and Europe.
- A recipient of Infosys Faculty Excellence award during 2016 from Infosys Ltd.,
Bangalore.
- Research work has published as an article in Cyber Space edition of DECCAN HERALD news paper.
- Member of NAFSA: Association of International Educators, USA
- Mentor for a startup AnyGo - Fitness Aggregator
- Delivered a session on Cyber Security in more than 10 universities in Indonesia.
- Participated in NAFSA 2017 and 2018– Annual Conference & Expo at Los Angeles, and Philadelphia, PA USA
respectively.
Countries Visited as part of International Collaborations
Chicago, California San Jose, LA, New York, Philidelphia, USA, France, Budapest, Hungary, Milan, Italy,
Switzerland, Alicante, Spain
Core Competencies:
Training & Academic Administration, Strategic Planning, Curriculum Development, Establishing tie-ups with
international universities, Start-ups development, Mentoring, Alumni Networking strategy.
The Crisis of Educated Unemployed
Industrial Evolution

4. Industrial
revolution
Based on cyber-physical-
systems

3. Industrial revolution
Through the use of electronics Industry 4.0
and IT further progression in
autonomous production Today

Level of complexity
2. Industrial revolution Industry 3.0
Introducing mass production
lines powered by electric
energy
1. Industrial revolution Industry 2.0 Beginning of the
Introducing mechanical
Beginning of the 70th
production machines powered
by water and steam 20th century
Industry 1.0
End of the 18th century.
Source: DFKI/Bauer IAO
Building Blocks of Industry 4.0
Top 10 skills to be relevant in Industry 4.0
Syllabus
Syllabus
Course Outcomes (CO’s):

After the completion of the course students will be able to

CO1: Illustrate basic concepts of Computer and C programming

CO2: Design the solution for the given problems and develop the same using C programming
language

CO3: Apply the concepts of looping, branching, and decision-making statements for a given
problem

CO4: Demonstrate the ability to write C programs using pointers, structures, unions and arrays

CO5: Develop modular applications using C programming language


Dennis Ritchie

C, Computer
Programming Language
developed in the early
1970s by American
computer scientist
Dennis M. Ritchie at Bell
Laboratories (formerly
AT&T Bell Laboratories)
C Programming Language has following importance:
1.C is robust language and has rich set of built-in functions, data
types and operators which can be used to write any complex
program
2. C has the capabilities of an assembly language (low level
features) with the feature of high level language so it is well suited
for writing both system software and application software
3.C is highly portable language i.e. code written in one machine
can be moved to other which is very important and powerful
feature.
4.C supports low level features like bit level programming and direct
access to memory using pointer which is very useful for managing
resource efficiently.
5.C has high level constructs and it is more user friendly as its
syntaxes approaches to English like language.
Use of C Programming

C language is used to develop System applications that


forms major portion of operating systems such as Windows,
UNIX and Linux.

 Database systems
 Graphics packages
 Word processors
 Spread sheets
 Operating system
development
 Compilers and
Assemblers
 Network drivers
 Interpreters
Application Software V/S System Software
Department of Information Science & Engineering
Acharya Institute of Technology

- Nurturing Aspirations & Supporting Growth

Module-1
Overview of C

C K Marigowda | Associate Professor & HOD | Information Science & Engineering | Acharya Institute of Technology | [email protected]
Content

 Basic Structure of C program

 Executing a C Program

 C Tokens

 Constant and Variable

 Data types

 Operators and Expressions


Basic Structure of C Program
#include<stdio.h>
main( )
{
printf("Welcome to CPPS");
}

- Any statement that begins with a # is a pre-processor


directive used for instructing the compiler.
- #include tells the compiler to include standard library
function from a header file that will be used in the program.
- stdio.h is the header file for standard input and output.
This is useful for getting the input from the user( Keyboard )
and output result to the monitor(screen)
.
Program to find area of a circle
#include<stdio.h>
#define PI 3.14
void main( )
{
int r = 10;
printf("Area of circle = %f", PI*r*r);
}

- #define is preprocessor directive, It is used to define a value


for a symbolic constant in the program. Whenever a
symbolic name is encountered, the compiler substitutes the
value associated with the name automatically.

- int r = 10 is used for declaring an integer variable 'r' and


storing a value 10 in it.

- printf is used for printing the area of the circle


Program to find area of a circle using functions

#include<stdio.h> - In this program, the job of


#define PI 3.14 finding area of circle is
float area(int r); entrusted to another
function named area
void main( ) - int area(int r); is used to
{ declare a user defined
int r = 10; function for finding area of
a circle
printf("Area of circle = %f", area( r) );
} - After the main function,
the area function is
float area(int r) defined.
{
return PI*r*r ; - The function area finds the
} area of the circle as PI*r*r
and returns it to the main
function and hence its return
type is float
Program to find area of a circle using functions
and global variables
- In the previous program, the
#include<stdio.h> variable 'r' was declared in the
#define PI 3.14 main function and was a local
int r = 10; variable to the main function.
float area(int r); Local variables are accessible only
in the function in which it is
void main( ) defined, hence it was sent as
{ parameter for the function area to
printf("Area of circle = %f", area(r)); compute the area of the circle.
}
- In this program, the variable 'r' is
declared outside the main function
float area( ) well before it and is a global variable.
{ A global variable is accessible by all
return PI*r*r ; the functions of the program and
} hence it need not be sent as a
parameter.
Basic Structure of a C Program

/*Pgm to find area of a circle*/


#include<stdio.h>
#define PI 3.14
int r = 10;
float area(int r);

void main( )
{
printf("Area of circle = %f", area(r));
}

float area( )
{
return PI*r*r ;
}
Source Code v/s Executable Code
Content

 Basic Structure of C program

 Executing a C Program

 C Tokens

 Constant and Variable

 Data types

 Operators and Expressions


Execution of C Program
Content

 Basic Structure of C program

 Executing a C Program

 C Tokens

 Constant and Variable

 Data types

 Operators and Expressions


C Tokens
A token is a smallest or basic unit of any program.
C Tokens
A token is a smallest or basic unit of any program.
Token Meaning

Keyword The words which have predefined meaning in C language are


called keywords. Ex: int, float, for, while , if, for ….

Constant The constants refer to fixed values that the program may not alter during its
execution (int, float, char, string values)
Ex: pi=3.14, a=5 ….
Identifier A C identifier is a name used to identify a variable, function, or any other
user-defined item. Ex: sum, a, b, area( ), …..
Variables Variables are simply names used to refer to some location in memory
Ex: sum, a, b …..
String Sequence of characters Ex: “Acharya” …..

Special Symbol Symbols other than the Alphabets and Digits and white-spaces
Ex: @ , { , ) , [ , #, …..

Operators A symbol that represents a specific mathematical or non-mathematical


action Ex: +, *, -, /, &&,||,<,=, ……
Content

 Basic Structure of C program

 Executing a C Program

 C Tokens

 Identifier, Constant and Variable

 Data types

 Operators and Expressions


Identifiers
 A C identifier is a name used to identify a variable, function, or
any other user-defined item

 An identifier starts with a letter A to Z or a to z or an underscore _


followed by zero or more letters, underscores, and digits (0 to 9)

Here are some examples of acceptable identifiers:


 add ( )
 a
 _abc
 a123
Constants
 The constants refer to fixed values that the program may not alter
during its execution
 These fixed values are also called literals
 Constants can be of any of the basic data types like an integer
constant, a floating constant, a character constant, or a string literal.

 Integer Constant
Decimal(0-9) :100,-67,989 etc.,
Octal ( 0-7 with a prefix 0) :010,0777,-065 etc.,
Hexadecimal (0-9 along with A-F) :0XAB, 0XA123 etc.,

 Floating point constant


Fractional form :0.5, -0.99, -6 -9.,+.9
Exponent notation : 634e-5

 Character Constant : ‘9’, ‘a’, ‘m’

 String constant : “Acharya” , “ISE”


Variables
 Variables are simply names used to refer to some memory location
that holds a value
 Before a C program can utilize memory to store a variable it must claim
the memory needed to store the values for a variable
 This is done by declaring variables

int abc;
float _abc;
int abc12;

Rules for defining variables

 The first character is the identifier should be letter or ‘_’


 No extra symbols are allowed
 The length of an identifier should be within 32 character
 Keywords are not allowed
Content

 Basic Structure of C program

 Executing a C Program

 C Tokens

 Identifier, Constant and Variable

 Data types

 Operators and Expressions


Memory Measurement Units
Basic Data types in C

The data type defines the type of data stored in memory location.
It determines how much memory should be allocated for a variable
associated with the data type

All C compilers support 5 primitive data types, namely


 integer (int)
 character (char)
 floating point (float)
 double precision floating point (double)
 void
Data types on a 16 bit Machine

Format Specifier
Data Type Keyword Memory Range for input / output
0 to 216 – 1
0 to 65535 (unsigned)
Integer int 2 bytes
-215 to + 215 -1
%d

-32768 to 32767 (signed)

0 to 28 - 1
1 byte 0 to 255 (unsigned)
Character char %c
-27 to + 27 -1
-128 to 127 (signed)
floating point float 4 bytes 3.4 E -38 to 3.4 E +38 %f
Double precision 1.7 E -308 to 1.7 E +308
double 8 bytes %lf
floating point
Variables
 A variable is a name given to the memory location where the data
can be stored. Using this name the data can be further accessed or
manipulated easily
 The following is the general syntax for declaring variables
data type var1, var2, var3,........, varn;

Example:
int a, b, c; // declaration of 3 integer variables
float x, y; // declaration of 2 floating point variables

 With this declaration, memory for 3 integers and 2 floating point


variables will be allocated by the compiler. The memory map is as
shown below
a b c x y

1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
GV GV GV GV GV
Variables
 Declaration of variables will only allocate memory, however, the
contents inside the memory will be unknown values (also referred to as
garbage values).

 Values to variables can be assigned using assignment operator =

 Example: After the above declaration, values can be assigned like


a = 20;
b = 100;
x = 3.14;

With this initialization, the memory map is as shown below.

a b c x y

1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013

20 100 GV 3.14 GV
Content

 Basic Structure of C program

 Executing a C Program

 C Tokens

 Identifier, Constant and Variable

 Data types

 Input and Output function in C

 Operators and Expressions


Input and Output Functions in C
 Input functions accept the data from input devices (keyboard) and
store them in the memory locations.
Example: scanf(),getchar(), gets() etc.

 Output functions retrieve the data from memory locations and send
them to output devices (monitor, printer).
Example: printf(),putchar(), puts() etc.

Formatted I/O Functions

1) printf( ) function
printf( ) can be used to print only text or text with values

2) To output only text


Write the text within double quotes inside printf.
EX: printf(“Hello 2020 Batch ISE”);
Output Functions in C
 To output values
- Write text (optional) along with format specifiers within double
quotes, followed by a comma, followed by the names of
variables separated by comma inside printf.

- The general syntax of printf ( ) is

printf("control string", var1, var2, var3,...........,varN);

EX: Consider the following declarations,


int a = 10;
int b = 20;
float c = 3.14;
float d = 7.25;
Output Functions in C
EX: Consider the following declarations,
int a = 10;
int b = 20;
float c = 3.14;
float d = 7.25;

printf(“%d %d”, a, b); 10 20

printf(“The value of a is %d”, a); The value of a is 10

printf(“The value of a is %d and b is %d”, a, b); The value of a is 10 and b is 20

printf(“%d %d %f %f”, a, b, c, d); 10 20 3.14 7.25

printf(“%f %d %f %d”, c, a, d, b); 3.14 10 7.25 20

printf(“The value of c is %f and a is %d”, c, a); The value of c is 3.14 and a is 10


Input Functions in C

scanf( ) function
 To read values
- Write the format specifiers within double quotes, followed by a
comma, followed by the address of variables separated by comma
inside scanf
EX: scanf(“%d %d %f”, &a, &b, &c);

 The general syntax of scanf ( ) is

scanf("control string", &var1, &var2, &var3,...........,&varN);


Input Functions in C
EX: Consider the following declarations,
int a;
int b;
float c;
float d;

To read a scanf("%d", &a);

To read a and b scanf("%d%d",&a,&b);

To read b and c scanf("%d%f",&b,&c);

To read c followed by a followed by d followed by b scanf("%f%d%f%d",&c,&a,&d,&b);


Simple Programs

1. Write a C program to find the sum of two numbers

2. Write a C program to find the area of a rectangle

3. Write a C program to find Simple Interest

4. Write a C program to swap two numbers

5. Write a C program to swap two numbers without using a

third variable
Content

 Basic Structure of C program

 Executing a C Program

 C Tokens

 Identifier, Constant and Variable

 Data types

 Input and Output function in C

 Operators and Expressions


Operators
Operator : An operator is a symbol that specifies the operation to be
performed on various types of operands.
Example: +, -, * etc., a+b
Operators classification based on number of operands
- Unary Operators: An operator that operates on one operand to
produce a result is called a unary operator
Example: a++, --b, etc

- Binary Operators: An operator that operates on two operands in an


expression to produce a result is called a binary operator
Example: a+b, a*b etc

- Ternary Operators: An operator that operates on three operands to


produce a result is called a ternary operator
Example: a ? b : c

- Special Operators
- comma ( , ), sizeof( ) etc
Operators

Operators classification based on the type of operation


1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Increment / Decrement Operators
6. Conditional Operator
7. Bitwise Operators
8. Special Operators
Arithmetic Operators

- The operators that are used to perform arithmetic operations such as


addition, subtraction, multiplication, division and modulus operations are
called arithmetic operators.
- They are binary operators. Priority 1 is HIGH and Priority 2 is LOW
Arithmetic Operators

If a=8 and b=2 compute the following


1. a+b 6. a+b*a
2. a-b 7. a+b/a
3. a*b 8. a+b/a-b+a
4. a\b
5. a%b
Arithmetic Operators

Evaluate the expression


2 * ( ( a % 5 ) * ( 4 + ( b - 3) / ( c + 2) ) )
assuming a = 8, b = 15 and c = 4
Arithmetic Operators
Evaluate the expression
2 * ( ( a % 5 ) * ( 4 + ( b - 3) / ( c + 2) ) )
assuming a = 8, b = 15 and c = 4
Conversion of Expressions

The expressions used in


mathematics are different
from the expressions that
are used in C language.

The expressions in C must


be written in a single line
Operators

Operators classification based on the type of operation


1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Increment / Decrement Operators
6. Conditional Operator
7. Bitwise Operators
8. Special Operators
Relational Operators
- The operators that are used to find relationship between two operands
are called relational operators
- The result obtained is true ( 1 ) or false ( 0 )
- They are binary operators.

Examples:
3>2 //Output 1 2>5 //Output 0 2==2 //Output 1
3==4 //Output 0 3>=4 //Output 0 3<=4 //Output 1
Relational Operators

Evaluate the expression


100 / 2 < = 10 – 5 + 100 % 10 – 20 == 5 > = 1 != 20

Brackets → Unary → Arithmetic → Relational → Logical


Thank you
[email protected]
www.acharya.ac.in

You might also like