Ilovepdf Merged
Ilovepdf Merged
UE21CS141B
Introduction
2. Why CPLs ?
3. Intro to Paradigms
4. Levels of Languages
6. Applications of C
7. First Programmer
9. Features of C
PROBLEM SOLVING WITH C
Introduction
● Majority of CPLs come with a lot of features for the Computer Programmers - CP.
● By using basic features of the existing CPL we can simplify things to program a better
option to write resourceful codes.
● There is no compulsion of writing code in a specific way, but rather is the usage of
features and clarity of the concept.
PROBLEM SOLVING WITH C
Introduction
● Imperative
● First do this and next do that
● FORTRAN, Algol, COBOL, Pascal
● Structured programming
● Single entry and single exit. Avoid GOTO
● C
● Procedural
● Subset of imperative
● Use of subroutines
●C
● Declarative
● Declares a set of rules about what outputs should result from which inputs
● Lisp, SQL
PROBLEM SOLVING WITH C
Introduction
Paradigm – Continued..
● Functional
● Function should be the first class citizen. No side effects. Assign Function
name to another, pass function name as an argument, return function itself
● If and recursion. No loops
● Scheme, Haskell, Miranda and JavaScript
● Logical
● Uses predicates
● Extraction of knowledge from basic facts and relations
● ASP, Prolog and Datalog
● Object-Oriented
● User perception is taken into account.
● Data needs protection
● Java, c++, Python
PROBLEM SOLVING WITH C
Introduction
Levels of Languages
PROBLEM SOLVING WITH C
Introduction
TIOBE Index
• The Importance of Being Earnest
• An indicator of the popularity of programming
languages. Updated once in a month
• The ratings are based on the number of skilled engineers
world-wide, courses and third party vendors. Popular
search engines such as Google, Bing, Yahoo!, Wikipedia,
Amazon, YouTube and Baidu are used
• It is not about the best programming language or the
language in which most lines of code have been written.
PROBLEM SOLVING WITH C
Introduction
Applications of C/C++
History.. Continued...
● Development of C
● Martin Richards, around 60’s developed BCPL [Basic Combined Programming
Language]
● Enhanced by Ken Thompson and Introduced B language.
● C is Originally developed between 1969 and 1973 at Bell Labs by Dennis Ritchie and
Kernighan. Closely tied to the development of the Unix operating system
● Standardized by the ANSI [American National Standards Institute] since 1989 and
subsequently by ISO[International Organization for Standardization].
● C Standard
● ANSI C, C89, C99, C11, C17, or C2x – Specifications of C
● C Standard Library
PROBLEM SOLVING WITH C
Introduction
FYK
• Dennis Ritchie
• Brian Kernighan
PROBLEM SOLVING WITH C
Introduction
Features of C Language
THANK YOU
C Programming Environment
3. First Program in C
4. Structure of C Program
6. GCC Insights
7. C Compiler standards
First Program in C
● Case sensitive
#include<stdio.h>
int main()
● Indentation is not a language
{ requirement
printf(“Hello PES\n”);
return 0; ● The main() is the starting point of
} execution
Program Structure
PROBLEM SOLVING WITH C
C Programming Environment
Way 1:
Step1: gcc <filename> // image a.out is the output
Step2: ./a.out OR a.exe
GCC insights
• gcc will not produce any warnings unless they are enabled. gcc –Wall
• Source code which does not produce any warnings by gcc, is said to be compile cleanly.
• The following options are a good choice for finding problems in C and C++ programs
• gcc -ansi -pedantic -Wall -W -Wconversion -Wshadow -Wcast-qual -Wwrite-strings
PROBLEM SOLVING WITH C
C Programming Environment
C Compiler standards
Using c99:
gcc -std=c99 program.c
Using c11:
gcc -std=c11 program.c
Error:
• a mistake
• the state or condition of being wrong in conduct or judgement
• a measure of the estimated difference between the observed or calculated value
of a quantity and its true value
Errors during C program Execution:
• Compile time Error
• Link time Error
• Run time Error
• Logical Error
PROBLEM SOLVING WITH C
C Programming Environment
• Syntax Errors are easy to figure out because the compiler highlights the line of
code that caused the error.
• Coding examples
PROBLEM SOLVING WITH C
C Programming Environment
• Linker is a program that takes the object files generated by the compiler and combines them
into a single executable file.
• Linker Errors are the errors encountered when the executable file of the code can not be
generated even though the code gets compiled successfully.
• This Error is generated when a different object file is unable to link with the main object file.
• We can run into a linked error if we have imported an incorrect header file in the code
• Coding examples
PROBLEM SOLVING WITH C
C Programming Environment
Logical Error
• Sometimes, we do not get the output we expected after the compilation and execution of a
program.
• Even though the code seems error free, the output generated is different from the expected
one.
THANK YOU
1. Introduction
4. Unformatted functions
PROBLEM SOLVING WITH C
Simple Input/Output
Introduction
● Input and output functions are available in the c language to perform the most
common tasks.
● In every c program, three basic functions take place namely accepting of data as
input, the processing of data, and the generation of output
● When a programmer says input, it would mean that they are feeding some data in
the program. Programmer can give this input from the command line or in the form
of any file. This language comes with a set of various built-in functions for reading
the input and then feeding it to the available program as per our requirements.
● When a programmer says output, they mean displaying some data and information
on the printer, the screen, or any other file. ● The c language comes with various
built-in functions for generating the output of the data on any screen or printer, and
also redirecting the output in the form of binary files or text file
PROBLEM SOLVING WITH C
Simple Input/Output
Format string
● % [flags] [field_width] [.precision] conversion_character
where components in brackets [] are optional. The minimum requirement is %
and a conversion character (e.g. %d).
● Coding examples
PROBLEM SOLVING WITH C
Simple Input/Output
Escape sequences
● Represented by two key strokes and represents one character
● Coding examples
PROBLEM SOLVING WITH C
Simple Input/Output
Unformatted functions
● Character input and output functions – getchar() and putchar()
● String input and output functions – gets() and puts() This will be discussed in unit – 2
● Coding examples
THANK YOU
1. Introduction
4. Unformatted functions
PROBLEM SOLVING WITH C
Simple Input/Output
Introduction
● Input and output functions are available in the c language to perform the most
common tasks.
● In every c program, three basic functions take place namely accepting of data as
input, the processing of data, and the generation of output
● When a programmer says input, it would mean that they are feeding some data in
the program. Programmer can give this input from the command line or in the form
of any file. This language comes with a set of various built-in functions for reading
the input and then feeding it to the available program as per our requirements.
● When a programmer says output, they mean displaying some data and information
on the printer, the screen, or any other file. ● The c language comes with various
built-in functions for generating the output of the data on any screen or printer, and
also redirecting the output in the form of binary files or text file
PROBLEM SOLVING WITH C
Simple Input/Output
Format string
● % [flags] [field_width] [.precision] conversion_character
where components in brackets [] are optional. The minimum requirement is %
and a conversion character (e.g. %d).
● Coding examples
PROBLEM SOLVING WITH C
Simple Input/Output
Escape sequences
● Represented by two key strokes and represents one character
● Coding examples
PROBLEM SOLVING WITH C
Simple Input/Output
Unformatted functions
● Character input and output functions – getchar() and putchar()
● String input and output functions – gets() and puts() This will be discussed in unit – 2
● Coding examples
THANK YOU
Language Specifications/Behaviors
1. Language Specification
2. Standards
Language Specification
A documentation that defines a programming language so that users
and implementers can agree on what programs in that language mean.
Are typically detailed and formal, and primarily used by implementers referring
to them in case of ambiguity.
Standards
de Jure: Practices that are legally recognized, regardless of whether the practice
exists in reality.
de Facto: Describes situations that exist in reality, even if not legally recognized
Language may have one or more implementations which acts as deFacto Language may
be implemented and then specified, or vice versa or together.
Languages can exist and be popular for decades without a specification - Perl
After 20 years of usage, specification for PHP in 2014
ALGOL 68 : First (and possibly one of the last) major language for which a full formal
definition was made before it was implemented
PROBLEM SOLVING WITH C
Language Specifications/Behaviors
It is the name of a list of conditions that the program must not meet.
Standard imposes no requirements: May fail to compile, may crash, may generate
incorrect results, may fortunately do what exactly programmer intended
Coding Examples
THANK YOU
1. Identifiers
3. Keywords
4. Data types
PROBLEM SOLVING WITH C
Variables and Data Types
Identifiers
● It is a name used to identify a variable, keyword, function, or any
other user-defined item.
Keywords
PROBLEM SOLVING WITH C
Variables and Data Types
● Has a name, location, type, life, scope and qualifiers, Variable initialization: int a = 10;
● Variable declaration and definition: int a; //An uninitialized variable has some undefined
value. A variable can be assigned a value later in the code
● There is no rule on how long a variable name aka user defined identifier can be. Variable
name may run into problems in some compilers, if the variable name is longer than 31
characters
Keywords
● Are identifiers which have special meaning in C.
● Few here: auto, else, long, switch, break, enum, case, extern, return
char, float, for, void, sizeof, int, double …
Data Types
● The amount of storage to be reserved for the specified variable.
Operators in C
2. Expression
Expression
● An expression consists of Operands
and Operators
● Evaluation of Operands: Order is not
defined
● Evaluation of Operators: Follows the
rules of precedence and rules of
Associativity
http://web.cse.ohio-
state.edu/~babic.1/COperatorPreceden
ceTable.pdf
● L-value and R-values.
● Side effects of Expression
● Coding examples
PROBLEM SOLVING WITH C
Operators in C
Control Structures
1. Selection structures
2. Looping structures
4. Practice Programs
PROBLEM SOLVING WITH C
Control Structures
Selection Structures
If Switch
if (e1) switch (expression)
<block>|<stmt> {
If – else case integral constant: <stmt> break;
if (e1) case integral constant: <stmt> break;
<block>|<stmt> default: <stmt>
else }
<block>|<stmt>
<block>|<stmt>
else if (e2)
<block>|<stmt>
else
<block>|<stmt>
PROBLEM SOLVING WITH C
Control Structures
Looping Structures
for
for(e1; e2; e3)
<block>|<stmt>
while
while(e2)
<block>|<stmt>
do while
do { <block>
}while(e2);
e1,e2,e3 are expressions where e1: initialization, e2: condition, e3: modification
Infinite loop
Coding Examples
PROBLEM SOLVING WITH C
Control Structures
One if may be inside another: inner if is reached only if the Boolean condition of the outer if is
true
Coding Examples
THANK YOU
Control Structures
1. Selection structures
2. Looping structures
4. Practice Programs
PROBLEM SOLVING WITH C
Control Structures
Selection Structures
If Switch
if (e1) switch (expression)
<block>|<stmt> {
If – else case integral constant: <stmt> break;
if (e1) case integral constant: <stmt> break;
<block>|<stmt> default: <stmt>
else }
<block>|<stmt>
<block>|<stmt>
else if (e2)
<block>|<stmt>
else
<block>|<stmt>
PROBLEM SOLVING WITH C
Control Structures
Looping Structures
for
for(e1; e2; e3)
<block>|<stmt>
while
while(e2)
<block>|<stmt>
do while
do { <block>
}while(e2);
e1,e2,e3 are expressions where e1: initialization, e2: condition, e3: modification
Infinite loop
Coding Examples
PROBLEM SOLVING WITH C
Control Structures
One if may be inside another: inner if is reached only if the Boolean condition of the outer if is
true
Coding Examples
THANK YOU
1. Problem Statements
3. Practice Programs
PROBLEM SOLVING WITH C
Programs on Control Structures
• Given the number of rows, print a hollow diamond using star symbol: Ex: Input = 5
• Check whether the given number is divisible by the sum of its digits. Display appropriate message
• Write a C program to find the eligibility of admission for a professional course based on the
following criteria: Eligibility Criteria : Marks in Maths >=65 and Marks in Physics >=50 and Marks in
Chemistry>=55 and Total in all three subjects >=190 or Total in Maths and Physics >=140
PROBLEM SOLVING WITH C
Programs on Control Structures
• Given a number n, find the count of those numbers whose product of digits is equal to n and does
not contain 1. Ex: input: 4 output: 2 (2 x 2 = 4, so count is 2)
PROBLEM SOLVING WITH C
Programs on Control Structures
Demo of solutions
PROBLEM SOLVING WITH C
Programs on Control Structures
Practice Programs
• You are given a number N. Your task is to find if there exist two distinct natural numbers such
that their bitwise XOR is N and bitwise AND is 0. If such numbers exist, then print 1. Otherwise,
print 0. Ex: Input = 0 Output = 0 Input = 5 Output = 1 (Possible numbers are 4 and 1)
• Using Switch case, implement a converter to obtain an integer from a hexadecimal byte
THANK YOU
Counting Problem
Count and display the number of characters, number of words and number of lines in an user input
Count and display the number of characters, number of words and number of lines in a given file
PROBLEM SOLVING WITH C
Counting Problem
Demonstration of C code
THANK YOU