SlideShare a Scribd company logo
1 of 73
Programming for
Problem Solving
Arnab Gain
28-07-2022
15:08
1
Programming, Algorithm, Flowchart,
Pseudocode
• Programming is the process of creating a set of instructions that tell a computer
how to perform a task.
• An Algorithm is a finite sequence of instructions, each of which has a clear
meaning and can be performed with a finite amount of effort in a finite length of
time.
• A flowchart is simply a graphical representation of steps. It shows steps in
sequential order and is widely used in presenting the flow of algorithms, workflow
or processes.
• Pseudocode is an artificial and informal language that helps programmers to
develop algorithms.
28-07-2022 15:08 2
A sample C programming language
#include<stdio.h>
int main()
{
int hours=5;
int rate=4;
int pay;
pay= hours*rate;
printf("%d", pay);
return 0;
}
28-07-2022 15:08 3
Algorithm
• Step 1: Start
• Step 2: Read hours, rate
• Step 3: Calculate pay=hours*rate
• Step 4: Print or display pay
• Step 5: Stop
28-07-2022 15:08 4
Flowchart and Pseudocode: Example
28-07-2022 15:08 5
C Keywords and Identifiers
• Character set
• A character set is a set of alphabets, letters and some special characters that are valid
in C language.
• Alphabets
• C accepts both lowercase and uppercase alphabets as variables and functions.
• Digits
• White space Characters
• Blank space, newline, horizontal tab, carriage return and form feed.
28-07-2022 15:08 6
Special Characters
28-07-2022 15:08 7
C Keywords
• Keywords are predefined, reserved words used in programming that have
special meanings to the compiler. Keywords are part of the syntax and they
cannot be used as an identifier. For example:
• int pay;
• Here, int is a keyword that indicates pay is a variable of type int (integer).
• As C is a case sensitive language, all keywords must be written in lowercase.
Here is a list of all keywords allowed in ANSI C.
28-07-2022 15:08 8
28-07-2022 15:08 9
C Identifiers
• Identifier refers to name given to entities such as variables, functions,
structures etc.
• Identifiers must be unique. They are created to give a unique name to an
entity to identify it during the execution of the program. For example:
• int pay;
• Here pay is an identifier
• Also remember, identifier names must be different from keywords.
28-07-2022 15:08 10
Rules for naming identifiers
• A valid identifier can have letters (both uppercase and lowercase letters),
digits and underscores.
• The first letter of an identifier should be either a letter or an underscore.
• You cannot use keywords like int, while etc. as identifiers.
• There is no rule on how long an identifier can be. However, you may run
into problems in some compilers if the identifier is longer than 31 characters.
28-07-2022 15:08 11
C Variables, Constants and Literals
• Variables
• In programming, a variable is a container (storage area) to hold data.
• To indicate the storage area, each variable should be given a unique name (identifier).
Variable names are just the symbolic representation of a memory location.
• For example:
• int playerScore = 95;
• Here, playerScore is a variable of int type. Here, the variable is assigned an integer value
95.
• The value of a variable can be changed, hence the name variable.
• char ch = 'a';
• // some code
• ch = 'l';
28-07-2022 15:08 12
Rules for naming a variable
• A variable name can only have letters (both uppercase and lowercase letters),
digits and underscore.
• The first letter of a variable should be either a letter or an underscore.
• There is no rule on how long a variable name (identifier) can be. However,
you may run into problems in some compilers if the variable name is longer
than 31 characters.
28-07-2022 15:08 13
Strongly typed language
• C is a strongly typed language. This means that the variable type cannot be changed
once it is declared. For example:
• int number = 5; // integer variable
• number = 5.5; // error
• double number; // error
• Here, the type of number variable is int. You cannot assign a floating-point
(decimal) value 5.5 to this variable. Also, you cannot redefine the data type of the
variable to double. By the way, to store the decimal values in C, you need to declare
its type to either double or float.
28-07-2022 15:08 14
Literals
• Literals are data used for representing fixed values. They can be used directly
in the code. For example: 1, 2.5, 'c' etc.
• Here, 1, 2.5 and 'c' are literals. Why? You cannot assign different values to
these terms.
28-07-2022 15:08 15
Integers
• An integer is a numeric literal(associated with numbers) without any fractional or
exponential part. There are three types of integer literals in C programming:
• decimal (base 10)
• octal (base 8)
• hexadecimal (base 16)
• For example:
• Decimal: 0, -9, 22 etc
• Octal: 021, 077, 033 etc
• Hexadecimal: 0x7f, 0x2a, 0x521 etc
• In C programming, octal starts with a 0, and hexadecimal starts with a 0x
28-07-2022 15:08 16
Floating-point Literals
• A floating-point literal is a numeric literal that has either a fractional form or
an exponent form. For example:
• -2.0
• 0.0000234
• -0.22E-5
28-07-2022 15:08 17
Characters
• A character literal is created by enclosing a single character inside single
quotation marks. For example: 'a', 'm', 'F', '2', '}' etc.
28-07-2022 15:08 18
Escape Sequences
• Sometimes, it is necessary to use characters that cannot be typed or has
special meaning in C programming. For example: newline(enter), tab,
question mark etc.
• In order to use these characters, escape sequences are used.
28-07-2022 15:08 19
28-07-2022 15:08 20
String Literals
• A string literal is a sequence of characters enclosed in double-quote marks.
For example:
• "good" //string constant
• "" //null string constant
• " " //string constant of six white space
• "x" //string constant having a single character.
• "Earth is roundn" //prints string with a newline
28-07-2022 15:08 21
Constants
• If you want to define a variable whose value cannot be changed, you can use
the const keyword. This will create a constant. For example,
• const double PI = 3.14;
• Notice, we have added keyword const.
• Here, PI is a symbolic constant; its value cannot be changed.
• const double PI = 3.14;
• PI = 2.9; //Error
28-07-2022 15:08 22
C Data Types
• In C programming, data types are declarations for variables. This determines
the type and size of data associated with variables. For example,
• int myVar;
• Here, myVar is a variable of int (integer) type. The size of int is 4 bytes.
28-07-2022 15:08 23
Basic types
28-07-2022 15:08 24
int
• Integers are whole numbers that can have both zero, positive and negative
values but no decimal values. For example, 0, -5, 10
• We can use int for declaring an integer variable.
• int id;
• Here, id is a variable of type integer.
• You can declare multiple variables at once in C programming. For example,
• int id, age;
• The size of int is usually 4 bytes (32 bits). And, it can take 232 distinct states
from -2147483648 to 2147483647.
28-07-2022 15:08 25
float and double
• float and double are used to hold real numbers.
• float salary;
• double price;
• In C, floating-point numbers can also be represented in exponential. For
example,
• float normalizationFactor = 22.442e2;
• What's the difference between float and double?
• The size of float (single precision float data type) is 4 bytes. And the size of
double (double precision float data type) is 8 bytes.
28-07-2022 15:08 26
char and void
• Keyword char is used for declaring character type variables. For example,
• char test = 'h';
• The size of the character variable is 1 byte.
• void is an incomplete type. It means "nothing" or "no type". You can think
of void as absent.
• For example, if a function is not returning anything, its return type should be
void.
• Note that, you cannot create variables of void type.
28-07-2022 15:08 27
short and long
• If you need to use a large number, you can use a type specifier long. Here's how:
• long a;
• long long b;
• long double c;
Here variables a and b can store integer values. And, c can store a floating-point number.
• If you are sure, only a small integer ([−32,767, +32,767] range) will be used, you can use
short.
• short d;
• You can always check the size of a variable using the sizeof() operator.
28-07-2022 15:08 28
signed and unsigned
• In C, signed and unsigned are type modifiers. You can alter the data storage of a
data type by using them. For example,
• unsigned int x;
• int y;
• Here, the variable x can hold only zero and positive values because we have used the
unsigned modifier.
• Considering the size of int is 4 bytes, variable y can hold values from -231 to 231-1,
whereas variable x can hold values from 0 to 232-1.
• Other data types defined in C programming are:
• bool Type
• Enumerated type
• Complex types
28-07-2022 15:08 29
Derived Data Types
• Data types that are derived from fundamental data types are derived types.
For example: arrays, pointers, function types, structures, etc.
28-07-2022 15:08 30
C Input Output (I/O)
• C Output
• In C programming, printf() is one of the main output function. The function
sends formatted output to the screen.
28-07-2022 15:08 31
Example 1: C Output
• #include <stdio.h>
• int main()
• {
• // Displays the string inside quotations
• printf("C Programming");
• return 0;
• }
• Output
• C Programming
28-07-2022 15:08 32
How does this program work?
• All valid C programs must contain the main() function. The code execution
begins from the start of the main() function.
• The printf() is a library function to send formatted output to the screen. The
function prints the string inside quotations.
• To use printf() in our program, we need to include stdio.h header file using
the #include<stdio.h> statement.
• The return 0; statement inside the main() function is the "Exit status" of the
program. It's optional.
28-07-2022 15:08 33
Example 2: Integer Output
• #include <stdio.h>
• int main()
• {
• int testInteger = 5;
• printf("Number = %d", testInteger);
• return 0;
• }
• Output
Number = 5
• We use %d format specifier to print int types. Here, the %d inside the quotations
will be replaced by the value of testInteger.
28-07-2022 15:08 34
Example 3: float and double Output
• #include <stdio.h>
• int main()
• {
• float number1 = 13.5;
• double number2 = 12.4;
• printf("number1 = %fn", number1);
• printf("number2 = %lf", number2);
• return 0;
• }
• Output
• number1 = 13.500000
• number2 = 12.400000
• To print float, we use %f format specifier. Similarly, we use %lf to print double values.
28-07-2022 15:08 35
Example 4: Print Characters
• #include <stdio.h>
• int main()
• {
• char chr = 'a';
• printf("character = %c", chr);
• return 0;
• }
• Output
• character = a
• To print char, we use %c format specifier.
28-07-2022 15:08 36
C Input
• In C programming, scanf() is one of the commonly used function to take
input from the user.
• The scanf() function reads formatted input from the standard input such as
keyboards.
28-07-2022 15:08 37
Example 5: Integer Input/Output
• #include <stdio.h>
• int main()
• {
• int testInteger;
• printf("Enter an integer: ");
• scanf("%d", &testInteger);
• printf("Number = %d",testInteger);
• return 0;
• }
• Output
• Enter an integer: 4
• Number = 4
28-07-2022 15:08 38
Example 5: Integer Input/Output
• Here, we have used %d format specifier inside the scanf() function to take
int input from the user. When the user enters an integer, it is stored in the
testInteger variable.
• Notice, that we have used &testInteger inside scanf(). It is because
&testInteger gets the address of testInteger, and the value entered by the
user is stored in that address.
28-07-2022 15:08 39
Example 6: Float and Double
Input/Output
#include <stdio.h>
int main()
{
float num1;
double num2;
printf("Enter a number: ");
scanf("%f", &num1);
printf("Enter another number: ");
scanf("%lf", &num2);
printf("num1 = %fn", num1);
printf("num2 = %lf", num2);
return 0;
}
• Output
• Enter a number: 12.523
• Enter another number: 10.2
• num1 = 12.523000
• num2 = 10.200000
• We use %f and %lf format specifier for float and double respectively.
28-07-2022 15:08 40
Example 7: C Character I/O
#include <stdio.h>
int main()
{
char chr;
printf("Enter a character: ");
scanf("%c",&chr);
printf("You entered %c.", chr);
return 0;
}
• Output
• Enter a character: g
• You entered g
28-07-2022 15:08 41
Example 7: C Character I/O
• When a character is entered by the user in the above program, the character
itself is not stored. Instead, an integer value (ASCII value) is stored.
• And when we display that value using %c text format, the entered character
is displayed. If we use %d to display the character, it's ASCII value is printed.
28-07-2022 15:08 42
Example 8: ASCII Value
#include <stdio.h>
int main()
{
char chr;
printf("Enter a character: ");
scanf("%c", &chr);
// When %c is used, a character is displayed
printf("You entered %c.n",chr);
// When %d is used, ASCII value is displayed
printf("ASCII value is %d.", chr);
return 0;
}
• Output
• Enter a character: g
• You entered g.
• ASCII value is 103.
28-07-2022 15:08 43
I/O Multiple Values
Here's how you can take multiple inputs from the user and display them.
#include <stdio.h>
int main()
{
int a;
float b;
printf("Enter integer and then a float: ");
// Taking multiple inputs
scanf("%d%f", &a, &b);
printf("You entered %d and %f", a, b);
return 0;
}
• Output
• Enter integer and then a float: -3
• 3.4
• You entered -3 and 3.400000
28-07-2022 15:08 44
Format Specifiers for I/O
• As you can see from the above examples, we use
• %d for int
• %f for float
• %lf for double
• %c for char
• Here's a list of commonly used C data types and their format specifiers.
28-07-2022 15:08 45
Data Type Format Specifier
int %d
char %c
float %f
double %lf
short int %hd
unsigned int %u
long int %li
long long int %lli
unsigned long int %lu
unsigned long long int %llu
signed char %c
unsigned char %c
long double %Lf
28-07-2022 15:08 46
C Programming Operators
• An operator is a symbol that operates on a value or a variable. For example:
+ is an operator to perform addition.
• C has a wide range of operators to perform various operations.
28-07-2022 15:08 47
C Arithmetic Operators
• An arithmetic operator performs mathematical operations such as addition,
subtraction, multiplication, division etc. on numerical values (constants and
variables).
Operator Meaning of Operator
+ addition or unary plus
- subtraction or unary minus
* multiplication
/ division
% remainder after division (modulo division)
28-07-2022 15:08 48
C Increment and Decrement Operators
• C programming has two operators increment ++ and decrement -- to change
the value of an operand (constant or variable) by 1.
• Increment ++ increases the value by 1 whereas decrement -- decreases the
value by 1. These two operators are unary operators, meaning they only
operate on a single operand.
28-07-2022 15:08 49
Example 2: Increment and Decrement
Operators
// Working of increment and decrement operators
#include <stdio.h>
int main()
{
int a = 10, b = 100;
float c = 10.5, d = 100.5;
printf("++a = %d n", ++a);
printf("--b = %d n", --b);
printf("++c = %f n", ++c);
printf("--d = %f n", --d);
return 0;
}
Output
++a = 11
--b = 99
++c = 11.500000
--d = 99.500000
28-07-2022 15:08 50
C Assignment Operators
• An assignment operator is used for assigning a value to a variable. The most
common assignment operator is =
Operator Example Same as
= a = b a = b
+= a += b a = a+b
-= a -= b a = a-b
*= a *= b a = a*b
/= a /= b a = a/b
%= a %= b a = a%b
28-07-2022 15:08 51
C Relational Operators
• A relational operator checks the relationship between two operands. If the
relation is true, it returns 1; if the relation is false, it returns value 0.
Operator Meaning of Operator Example
== Equal to 5 == 3 is evaluated to 0
> Greater than 5 > 3 is evaluated to 1
< Less than 5 < 3 is evaluated to 0
!= Not equal to 5 != 3 is evaluated to 1
>= Greater than or equal to 5 >= 3 is evaluated to 1
<= Less than or equal to 5 <= 3 is evaluated to 0
28-07-2022 15:08 52
Example 4: Relational Operators
// Working of relational operators
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10;
printf("%d == %d is %d n", a, b, a == b);
printf("%d == %d is %d n", a, c, a == c);
printf("%d > %d is %d n", a, b, a > b);
printf("%d > %d is %d n", a, c, a > c);
printf("%d < %d is %d n", a, b, a < b);
printf("%d < %d is %d n", a, c, a < c);
printf("%d != %d is %d n", a, b, a != b);
printf("%d != %d is %d n", a, c, a != c);
printf("%d >= %d is %d n", a, b, a >= b);
printf("%d >= %d is %d n", a, c, a >= c);
printf("%d <= %d is %d n", a, b, a <= b);
printf("%d <= %d is %d n", a, c, a <= c);
return 0;
}
28-07-2022 15:08 53
Example 4: Relational Operators output
Output
5 == 5 is 1
5 == 10 is 0
5 > 5 is 0
5 > 10 is 0
5 < 5 is 0
5 < 10 is 1
5 != 5 is 0
5 != 10 is 1
5 >= 5 is 1
5 >= 10 is 0
5 <= 5 is 1
5 <= 10 is 1
28-07-2022 15:08 54
C Logical Operators
• An expression containing logical operator returns either 0 or 1 depending
upon whether expression results true or false.
Operator Meaning Example
&&
Logical AND. True only if all
operands are true
If c = 5 and d = 2 then,
expression ((c==5) && (d>5))
equals to 0.
||
Logical OR. True only if either
one operand is true
If c = 5 and d = 2 then,
expression ((c==5) || (d>5))
equals to 1.
!
Logical NOT. True only if the
operand is 0
If c = 5 then, expression !(c==5)
equals to 0. 28-07-2022 15:08 55
Example 5: Logical Operators
// Working of logical operators
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;
result = (a == b) && (c > b);
printf("(a == b) && (c > b) is %d n", result);
result = (a == b) && (c < b);
printf("(a == b) && (c < b) is %d n", result);
result = (a == b) || (c < b);
printf("(a == b) || (c < b) is %d n", result);
result = (a != b) || (c < b);
printf("(a != b) || (c < b) is %d n", result);
result = !(a != b);
printf("!(a != b) is %d n", result);
result = !(a == b);
printf("!(a == b) is %d n", result);
return 0;
}
28-07-2022 15:08 56
• Output
(a == b) && (c > b) is 1
(a == b) && (c < b) is 0
(a == b) || (c < b) is 1
(a != b) || (c < b) is 0
!(a != b) is 1
!(a == b) is 0
28-07-2022 15:08 57
C Bitwise Operators
• During computation, mathematical operations like: addition, subtraction,
multiplication, division, etc are converted to bit-level which makes processing
faster and saves power.
• Bitwise operators are used in C programming to perform bit-level
operations.
28-07-2022 15:08 58
C Bitwise Operators: Contd.
Operators Meaning of operators
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
~ Bitwise complement
<< Shift left
>> Shift right
28-07-2022 15:08 59
sizeof operator
• The sizeof operator
• The sizeof is a unary operator that returns the size of data (constants, variables,
array, structure, etc).
#include <stdio.h>
void main()
{
char a;
printf("Size of char=%lu bytesn",sizeof(a));
}
• Output
• Size of char = 4 bytes
28-07-2022 15:08 60
Conditional or Ternary Operator (?:)
Here, Expression1 is the condition to be evaluated. If the condition(Expression1) is True then
Expression2 will be executed and the result will be returned. Otherwise, if the
condition(Expression1) is false then Expression3 will be executed and the result will be returned.
28-07-2022 15:08 61
Example: Program to Store the greatest of the
two Number.
#include <stdio.h>
int main()
{
int a=2,b=3,c;
c=a>b?a:b;
printf("%d is greater ",c);
return 0;
}
• Output
• 3 is greater
28-07-2022 15:08 62
Errors in C
• Error is an illegal operation performed by the user which results in abnormal
working of the program.
• Programming errors often remain undetected until the program is compiled
or executed.
• Some of the errors inhibit the program from getting compiled or executed.
• Thus errors should be removed before compiling and executing.
28-07-2022 15:08 63
Syntax errors
• Errors that occur when you violate the rules of writing C syntax are known
as syntax errors.
• Most frequent syntax errors are:
• Missing Parenthesis (})
• Printing the value of variable without declaring it
• Missing semicolon
28-07-2022 15:08 64
Run-time Errors
• Errors which occur during program execution(run-time) after successful
compilation are called run-time errors
• One of the most common run-time error is division by zero also known as
Division error
28-07-2022 15:08 65
Linker Errors
• These error occurs when after compilation we link the different object files
with main’s object
• These are errors generated when the executable of the program cannot be
generated.
• This may be due to wrong function prototyping, incorrect header files.
• One of the most common linker error is writing Main() instead of main().
28-07-2022 15:08 66
Logical Errors
• On compilation and execution of a program, desired output is not obtained
when certain input values are given
• These types of errors which provide incorrect output but appears to be error
free are called logical errors
28-07-2022 15:08 67
Logical Errors : Example
int main()
{
int i = 0;
// logical error : a semicolon after loop
for(i = 0; i < 3; i++);
{
printf("loop ");
continue;
}
getchar();
return 0;
}
28-07-2022 15:08 68
Semantic errors
• This error occurs when the statements written in the program are not meaningful to
the compiler.
• Example:
// C program to illustrate
// semantic error
void main()
{
int a, b, c;
a + b = c; //semantic error
}
28-07-2022 15:08 69
Object Code
• Object code generally refers to the output, a compiled file, which is
produced when the Source Code is compiled with a C compiler.
• The object code file contains a sequence of machine-readable instructions
that is processed by the CPU in a computer.
28-07-2022 15:08 70
Executable code
• Executable (also called the Binary) is the output of a linker after it
processes the object code.
• A machine code file can be immediately executable (i.e., runnable as a
program), or it might require linking with other object code files (e.g.
libraries) to produce a complete executable program.
28-07-2022 15:08 71
Linker
• In computer science, a linker is a computer program that takes one or more
object files generated by a compiler and combines them into one, executable
program.
28-07-2022 15:08 72
Loader
• In computer systems a loader is the part of an operating system that is
responsible for loading programs and libraries.
28-07-2022 15:08 73

More Related Content

What's hot (20)

Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C Programming
 
Preprocessor directives in c language
Preprocessor directives in c languagePreprocessor directives in c language
Preprocessor directives in c language
 
Variables in C and C++ Language
Variables in C and C++ LanguageVariables in C and C++ Language
Variables in C and C++ Language
 
C++ programming
C++ programmingC++ programming
C++ programming
 
Loop(for, while, do while) condition Presentation
Loop(for, while, do while) condition PresentationLoop(for, while, do while) condition Presentation
Loop(for, while, do while) condition Presentation
 
Role-of-lexical-analysis
Role-of-lexical-analysisRole-of-lexical-analysis
Role-of-lexical-analysis
 
Operators in C++
Operators in C++Operators in C++
Operators in C++
 
Pre processor directives in c
Pre processor directives in cPre processor directives in c
Pre processor directives in c
 
Python Spyder IDE | Edureka
Python Spyder IDE | EdurekaPython Spyder IDE | Edureka
Python Spyder IDE | Edureka
 
Read & write
Read & writeRead & write
Read & write
 
Computer organisation -morris mano
Computer organisation  -morris manoComputer organisation  -morris mano
Computer organisation -morris mano
 
Basics of c++ Programming Language
Basics of c++ Programming LanguageBasics of c++ Programming Language
Basics of c++ Programming Language
 
Arithmetic operator
Arithmetic operatorArithmetic operator
Arithmetic operator
 
Strings in C language
Strings in C languageStrings in C language
Strings in C language
 
Unit 4 sp macro
Unit 4 sp macroUnit 4 sp macro
Unit 4 sp macro
 
Storage classes in c language
Storage classes in c languageStorage classes in c language
Storage classes in c language
 
ASSIGNMENT STATEMENTS AND
ASSIGNMENT STATEMENTS ANDASSIGNMENT STATEMENTS AND
ASSIGNMENT STATEMENTS AND
 
Unit 5. Control Statement
Unit 5. Control StatementUnit 5. Control Statement
Unit 5. Control Statement
 
Operator in c programming
Operator in c programmingOperator in c programming
Operator in c programming
 
pushdown automata
pushdown automatapushdown automata
pushdown automata
 

Similar to C programming

Data structure & Algorithms - Programming in C
Data structure & Algorithms - Programming in CData structure & Algorithms - Programming in C
Data structure & Algorithms - Programming in Cbabuk110
 
programming week 2.ppt
programming week 2.pptprogramming week 2.ppt
programming week 2.pptFatimaZafar68
 
C programming tutorial for Beginner
C programming tutorial for BeginnerC programming tutorial for Beginner
C programming tutorial for Beginnersophoeutsen2
 
C Programming Lecture 3 - Elements of C.pptx
C Programming Lecture 3 - Elements of C.pptxC Programming Lecture 3 - Elements of C.pptx
C Programming Lecture 3 - Elements of C.pptxMurali M
 
CSE 1201: Structured Programming Language
CSE 1201: Structured Programming LanguageCSE 1201: Structured Programming Language
CSE 1201: Structured Programming LanguageZubayer Farazi
 
Programming for Problem Solving Unit 2
Programming for Problem Solving Unit 2Programming for Problem Solving Unit 2
Programming for Problem Solving Unit 2Dhiviya Rose
 
Chap_________________1_Introduction.pptx
Chap_________________1_Introduction.pptxChap_________________1_Introduction.pptx
Chap_________________1_Introduction.pptxRonaldo Aditya
 
Data Type in C Programming
Data Type in C ProgrammingData Type in C Programming
Data Type in C ProgrammingQazi Shahzad Ali
 
LESSON1-C_programming (1).GRADE 8 LESSONpptx
LESSON1-C_programming (1).GRADE 8 LESSONpptxLESSON1-C_programming (1).GRADE 8 LESSONpptx
LESSON1-C_programming (1).GRADE 8 LESSONpptxjoachimbenedicttulau
 
C++ unit-1-part-7
C++ unit-1-part-7C++ unit-1-part-7
C++ unit-1-part-7Jadavsejal
 
C PROGRAMMING LANGUAGE.pptx
 C PROGRAMMING LANGUAGE.pptx C PROGRAMMING LANGUAGE.pptx
C PROGRAMMING LANGUAGE.pptxAnshSrivastava48
 
COM1407: Variables and Data Types
COM1407: Variables and Data Types COM1407: Variables and Data Types
COM1407: Variables and Data Types Hemantha Kulathilake
 
Data types slides by Faixan
Data types slides by FaixanData types slides by Faixan
Data types slides by FaixanٖFaiXy :)
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAAiman Hud
 

Similar to C programming (20)

Data structure & Algorithms - Programming in C
Data structure & Algorithms - Programming in CData structure & Algorithms - Programming in C
Data structure & Algorithms - Programming in C
 
programming week 2.ppt
programming week 2.pptprogramming week 2.ppt
programming week 2.ppt
 
C PROGRAMING.pptx
C PROGRAMING.pptxC PROGRAMING.pptx
C PROGRAMING.pptx
 
C programming tutorial for Beginner
C programming tutorial for BeginnerC programming tutorial for Beginner
C programming tutorial for Beginner
 
C Programming Lecture 3 - Elements of C.pptx
C Programming Lecture 3 - Elements of C.pptxC Programming Lecture 3 - Elements of C.pptx
C Programming Lecture 3 - Elements of C.pptx
 
CSE 1201: Structured Programming Language
CSE 1201: Structured Programming LanguageCSE 1201: Structured Programming Language
CSE 1201: Structured Programming Language
 
Cp presentation
Cp presentationCp presentation
Cp presentation
 
Introduction to C
Introduction to CIntroduction to C
Introduction to C
 
Programming for Problem Solving Unit 2
Programming for Problem Solving Unit 2Programming for Problem Solving Unit 2
Programming for Problem Solving Unit 2
 
lec 2.pptx
lec 2.pptxlec 2.pptx
lec 2.pptx
 
Chap_________________1_Introduction.pptx
Chap_________________1_Introduction.pptxChap_________________1_Introduction.pptx
Chap_________________1_Introduction.pptx
 
Data Type in C Programming
Data Type in C ProgrammingData Type in C Programming
Data Type in C Programming
 
LESSON1-C_programming (1).GRADE 8 LESSONpptx
LESSON1-C_programming (1).GRADE 8 LESSONpptxLESSON1-C_programming (1).GRADE 8 LESSONpptx
LESSON1-C_programming (1).GRADE 8 LESSONpptx
 
C language
C languageC language
C language
 
C++ unit-1-part-7
C++ unit-1-part-7C++ unit-1-part-7
C++ unit-1-part-7
 
C PROGRAMMING LANGUAGE.pptx
 C PROGRAMMING LANGUAGE.pptx C PROGRAMMING LANGUAGE.pptx
C PROGRAMMING LANGUAGE.pptx
 
COM1407: Variables and Data Types
COM1407: Variables and Data Types COM1407: Variables and Data Types
COM1407: Variables and Data Types
 
All C ppt.ppt
All C ppt.pptAll C ppt.ppt
All C ppt.ppt
 
Data types slides by Faixan
Data types slides by FaixanData types slides by Faixan
Data types slides by Faixan
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 

More from AsifRahaman16

Asif_Rahaman_8_MATHCA1.pptxjgjgjhgiyuki7uihmn
Asif_Rahaman_8_MATHCA1.pptxjgjgjhgiyuki7uihmnAsif_Rahaman_8_MATHCA1.pptxjgjgjhgiyuki7uihmn
Asif_Rahaman_8_MATHCA1.pptxjgjgjhgiyuki7uihmnAsifRahaman16
 
34900721068_Asif Rahaman_MD.pptx
34900721068_Asif Rahaman_MD.pptx34900721068_Asif Rahaman_MD.pptx
34900721068_Asif Rahaman_MD.pptxAsifRahaman16
 
MSF Report(for PR).pdf
MSF Report(for PR).pdfMSF Report(for PR).pdf
MSF Report(for PR).pdfAsifRahaman16
 
Asif Rahaman_34900721068.pptx
Asif Rahaman_34900721068.pptxAsif Rahaman_34900721068.pptx
Asif Rahaman_34900721068.pptxAsifRahaman16
 
Energy Method ppt.pptx
Energy Method ppt.pptxEnergy Method ppt.pptx
Energy Method ppt.pptxAsifRahaman16
 
34900721009_Arnab Hazra_BS-BIO301.pptn
34900721009_Arnab Hazra_BS-BIO301.pptn34900721009_Arnab Hazra_BS-BIO301.pptn
34900721009_Arnab Hazra_BS-BIO301.pptnAsifRahaman16
 
ssppt-170414031953.pptx
ssppt-170414031953.pptxssppt-170414031953.pptx
ssppt-170414031953.pptxAsifRahaman16
 
Engineering Mechanics.
Engineering Mechanics.Engineering Mechanics.
Engineering Mechanics.AsifRahaman16
 

More from AsifRahaman16 (15)

Asif_Rahaman_8_MATHCA1.pptxjgjgjhgiyuki7uihmn
Asif_Rahaman_8_MATHCA1.pptxjgjgjhgiyuki7uihmnAsif_Rahaman_8_MATHCA1.pptxjgjgjhgiyuki7uihmn
Asif_Rahaman_8_MATHCA1.pptxjgjgjhgiyuki7uihmn
 
34900721068_Asif Rahaman_MD.pptx
34900721068_Asif Rahaman_MD.pptx34900721068_Asif Rahaman_MD.pptx
34900721068_Asif Rahaman_MD.pptx
 
MSF Report(for PR).pdf
MSF Report(for PR).pdfMSF Report(for PR).pdf
MSF Report(for PR).pdf
 
yutu.pptx
yutu.pptxyutu.pptx
yutu.pptx
 
Asif Rahaman_34900721068.pptx
Asif Rahaman_34900721068.pptxAsif Rahaman_34900721068.pptx
Asif Rahaman_34900721068.pptx
 
Energy Method ppt.pptx
Energy Method ppt.pptxEnergy Method ppt.pptx
Energy Method ppt.pptx
 
34900721009_Arnab Hazra_BS-BIO301.pptn
34900721009_Arnab Hazra_BS-BIO301.pptn34900721009_Arnab Hazra_BS-BIO301.pptn
34900721009_Arnab Hazra_BS-BIO301.pptn
 
34900721068.pptx
34900721068.pptx34900721068.pptx
34900721068.pptx
 
ssppt-170414031953.pptx
ssppt-170414031953.pptxssppt-170414031953.pptx
ssppt-170414031953.pptx
 
EC-304.pptx
EC-304.pptxEC-304.pptx
EC-304.pptx
 
NUMBER SYSTEM.pptx
NUMBER  SYSTEM.pptxNUMBER  SYSTEM.pptx
NUMBER SYSTEM.pptx
 
PN Junction.pptx
PN Junction.pptxPN Junction.pptx
PN Junction.pptx
 
Engineering Mechanics.
Engineering Mechanics.Engineering Mechanics.
Engineering Mechanics.
 
Method of sections
Method of sections Method of sections
Method of sections
 
Analysis of Truss
Analysis of TrussAnalysis of Truss
Analysis of Truss
 

Recently uploaded

Kieran Salaria Graphic Design PDF Portfolio
Kieran Salaria Graphic Design PDF PortfolioKieran Salaria Graphic Design PDF Portfolio
Kieran Salaria Graphic Design PDF Portfolioktksalaria
 
Call Girls in Okhla Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Okhla Delhi 💯Call Us 🔝8264348440🔝Call Girls in Okhla Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Okhla Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
Bus tracking.pptx ,,,,,,,,,,,,,,,,,,,,,,,,,,
Bus tracking.pptx ,,,,,,,,,,,,,,,,,,,,,,,,,,Bus tracking.pptx ,,,,,,,,,,,,,,,,,,,,,,,,,,
Bus tracking.pptx ,,,,,,,,,,,,,,,,,,,,,,,,,,bhuyansuprit
 
Best VIP Call Girls Noida Sector 44 Call Me: 8448380779
Best VIP Call Girls Noida Sector 44 Call Me: 8448380779Best VIP Call Girls Noida Sector 44 Call Me: 8448380779
Best VIP Call Girls Noida Sector 44 Call Me: 8448380779Delhi Call girls
 
VIP Kolkata Call Girl Gariahat 👉 8250192130 Available With Room
VIP Kolkata Call Girl Gariahat 👉 8250192130  Available With RoomVIP Kolkata Call Girl Gariahat 👉 8250192130  Available With Room
VIP Kolkata Call Girl Gariahat 👉 8250192130 Available With Roomdivyansh0kumar0
 
SCRIP Lua HTTP PROGRACMACION PLC WECON CA
SCRIP Lua HTTP PROGRACMACION PLC  WECON CASCRIP Lua HTTP PROGRACMACION PLC  WECON CA
SCRIP Lua HTTP PROGRACMACION PLC WECON CANestorGamez6
 
The_Canvas_of_Creative_Mastery_Newsletter_April_2024_Version.pdf
The_Canvas_of_Creative_Mastery_Newsletter_April_2024_Version.pdfThe_Canvas_of_Creative_Mastery_Newsletter_April_2024_Version.pdf
The_Canvas_of_Creative_Mastery_Newsletter_April_2024_Version.pdfAmirYakdi
 
A level Digipak development Presentation
A level Digipak development PresentationA level Digipak development Presentation
A level Digipak development Presentationamedia6
 
VIP Call Girls Service Mehdipatnam Hyderabad Call +91-8250192130
VIP Call Girls Service Mehdipatnam Hyderabad Call +91-8250192130VIP Call Girls Service Mehdipatnam Hyderabad Call +91-8250192130
VIP Call Girls Service Mehdipatnam Hyderabad Call +91-8250192130Suhani Kapoor
 
PORTFOLIO DE ARQUITECTURA CRISTOBAL HERAUD 2024
PORTFOLIO DE ARQUITECTURA CRISTOBAL HERAUD 2024PORTFOLIO DE ARQUITECTURA CRISTOBAL HERAUD 2024
PORTFOLIO DE ARQUITECTURA CRISTOBAL HERAUD 2024CristobalHeraud
 
WAEC Carpentry and Joinery Past Questions
WAEC Carpentry and Joinery Past QuestionsWAEC Carpentry and Joinery Past Questions
WAEC Carpentry and Joinery Past QuestionsCharles Obaleagbon
 
Fashion trends before and after covid.pptx
Fashion trends before and after covid.pptxFashion trends before and after covid.pptx
Fashion trends before and after covid.pptxVanshNarang19
 
Design Portfolio - 2024 - William Vickery
Design Portfolio - 2024 - William VickeryDesign Portfolio - 2024 - William Vickery
Design Portfolio - 2024 - William VickeryWilliamVickery6
 
SD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptxSD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptxjanettecruzeiro1
 
Cosumer Willingness to Pay for Sustainable Bricks
Cosumer Willingness to Pay for Sustainable BricksCosumer Willingness to Pay for Sustainable Bricks
Cosumer Willingness to Pay for Sustainable Bricksabhishekparmar618
 
Raj Nagar Extension Call Girls 9711199012 WhatsApp No, Delhi Escorts in Raj N...
Raj Nagar Extension Call Girls 9711199012 WhatsApp No, Delhi Escorts in Raj N...Raj Nagar Extension Call Girls 9711199012 WhatsApp No, Delhi Escorts in Raj N...
Raj Nagar Extension Call Girls 9711199012 WhatsApp No, Delhi Escorts in Raj N...ankitnayak356677
 
如何办理(UVa毕业证书)弗吉尼亚大学毕业证毕业证(文凭)成绩单原版一比一定制
如何办理(UVa毕业证书)弗吉尼亚大学毕业证毕业证(文凭)成绩单原版一比一定制如何办理(UVa毕业证书)弗吉尼亚大学毕业证毕业证(文凭)成绩单原版一比一定制
如何办理(UVa毕业证书)弗吉尼亚大学毕业证毕业证(文凭)成绩单原版一比一定制didi bibo
 
NATA 2024 SYLLABUS, full syllabus explained in detail
NATA 2024 SYLLABUS, full syllabus explained in detailNATA 2024 SYLLABUS, full syllabus explained in detail
NATA 2024 SYLLABUS, full syllabus explained in detailDesigntroIntroducing
 
Kindergarten Assessment Questions Via LessonUp
Kindergarten Assessment Questions Via LessonUpKindergarten Assessment Questions Via LessonUp
Kindergarten Assessment Questions Via LessonUpmainac1
 

Recently uploaded (20)

Kieran Salaria Graphic Design PDF Portfolio
Kieran Salaria Graphic Design PDF PortfolioKieran Salaria Graphic Design PDF Portfolio
Kieran Salaria Graphic Design PDF Portfolio
 
Call Girls in Okhla Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Okhla Delhi 💯Call Us 🔝8264348440🔝Call Girls in Okhla Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Okhla Delhi 💯Call Us 🔝8264348440🔝
 
Bus tracking.pptx ,,,,,,,,,,,,,,,,,,,,,,,,,,
Bus tracking.pptx ,,,,,,,,,,,,,,,,,,,,,,,,,,Bus tracking.pptx ,,,,,,,,,,,,,,,,,,,,,,,,,,
Bus tracking.pptx ,,,,,,,,,,,,,,,,,,,,,,,,,,
 
Best VIP Call Girls Noida Sector 44 Call Me: 8448380779
Best VIP Call Girls Noida Sector 44 Call Me: 8448380779Best VIP Call Girls Noida Sector 44 Call Me: 8448380779
Best VIP Call Girls Noida Sector 44 Call Me: 8448380779
 
VIP Kolkata Call Girl Gariahat 👉 8250192130 Available With Room
VIP Kolkata Call Girl Gariahat 👉 8250192130  Available With RoomVIP Kolkata Call Girl Gariahat 👉 8250192130  Available With Room
VIP Kolkata Call Girl Gariahat 👉 8250192130 Available With Room
 
SCRIP Lua HTTP PROGRACMACION PLC WECON CA
SCRIP Lua HTTP PROGRACMACION PLC  WECON CASCRIP Lua HTTP PROGRACMACION PLC  WECON CA
SCRIP Lua HTTP PROGRACMACION PLC WECON CA
 
The_Canvas_of_Creative_Mastery_Newsletter_April_2024_Version.pdf
The_Canvas_of_Creative_Mastery_Newsletter_April_2024_Version.pdfThe_Canvas_of_Creative_Mastery_Newsletter_April_2024_Version.pdf
The_Canvas_of_Creative_Mastery_Newsletter_April_2024_Version.pdf
 
A level Digipak development Presentation
A level Digipak development PresentationA level Digipak development Presentation
A level Digipak development Presentation
 
VIP Call Girls Service Mehdipatnam Hyderabad Call +91-8250192130
VIP Call Girls Service Mehdipatnam Hyderabad Call +91-8250192130VIP Call Girls Service Mehdipatnam Hyderabad Call +91-8250192130
VIP Call Girls Service Mehdipatnam Hyderabad Call +91-8250192130
 
PORTFOLIO DE ARQUITECTURA CRISTOBAL HERAUD 2024
PORTFOLIO DE ARQUITECTURA CRISTOBAL HERAUD 2024PORTFOLIO DE ARQUITECTURA CRISTOBAL HERAUD 2024
PORTFOLIO DE ARQUITECTURA CRISTOBAL HERAUD 2024
 
WAEC Carpentry and Joinery Past Questions
WAEC Carpentry and Joinery Past QuestionsWAEC Carpentry and Joinery Past Questions
WAEC Carpentry and Joinery Past Questions
 
Fashion trends before and after covid.pptx
Fashion trends before and after covid.pptxFashion trends before and after covid.pptx
Fashion trends before and after covid.pptx
 
Design Portfolio - 2024 - William Vickery
Design Portfolio - 2024 - William VickeryDesign Portfolio - 2024 - William Vickery
Design Portfolio - 2024 - William Vickery
 
SD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptxSD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptx
 
Cosumer Willingness to Pay for Sustainable Bricks
Cosumer Willingness to Pay for Sustainable BricksCosumer Willingness to Pay for Sustainable Bricks
Cosumer Willingness to Pay for Sustainable Bricks
 
Raj Nagar Extension Call Girls 9711199012 WhatsApp No, Delhi Escorts in Raj N...
Raj Nagar Extension Call Girls 9711199012 WhatsApp No, Delhi Escorts in Raj N...Raj Nagar Extension Call Girls 9711199012 WhatsApp No, Delhi Escorts in Raj N...
Raj Nagar Extension Call Girls 9711199012 WhatsApp No, Delhi Escorts in Raj N...
 
如何办理(UVa毕业证书)弗吉尼亚大学毕业证毕业证(文凭)成绩单原版一比一定制
如何办理(UVa毕业证书)弗吉尼亚大学毕业证毕业证(文凭)成绩单原版一比一定制如何办理(UVa毕业证书)弗吉尼亚大学毕业证毕业证(文凭)成绩单原版一比一定制
如何办理(UVa毕业证书)弗吉尼亚大学毕业证毕业证(文凭)成绩单原版一比一定制
 
NATA 2024 SYLLABUS, full syllabus explained in detail
NATA 2024 SYLLABUS, full syllabus explained in detailNATA 2024 SYLLABUS, full syllabus explained in detail
NATA 2024 SYLLABUS, full syllabus explained in detail
 
Kindergarten Assessment Questions Via LessonUp
Kindergarten Assessment Questions Via LessonUpKindergarten Assessment Questions Via LessonUp
Kindergarten Assessment Questions Via LessonUp
 
young call girls in Pandav nagar 🔝 9953056974 🔝 Delhi escort Service
young call girls in Pandav nagar 🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Pandav nagar 🔝 9953056974 🔝 Delhi escort Service
young call girls in Pandav nagar 🔝 9953056974 🔝 Delhi escort Service
 

C programming

  • 1. Programming for Problem Solving Arnab Gain 28-07-2022 15:08 1
  • 2. Programming, Algorithm, Flowchart, Pseudocode • Programming is the process of creating a set of instructions that tell a computer how to perform a task. • An Algorithm is a finite sequence of instructions, each of which has a clear meaning and can be performed with a finite amount of effort in a finite length of time. • A flowchart is simply a graphical representation of steps. It shows steps in sequential order and is widely used in presenting the flow of algorithms, workflow or processes. • Pseudocode is an artificial and informal language that helps programmers to develop algorithms. 28-07-2022 15:08 2
  • 3. A sample C programming language #include<stdio.h> int main() { int hours=5; int rate=4; int pay; pay= hours*rate; printf("%d", pay); return 0; } 28-07-2022 15:08 3
  • 4. Algorithm • Step 1: Start • Step 2: Read hours, rate • Step 3: Calculate pay=hours*rate • Step 4: Print or display pay • Step 5: Stop 28-07-2022 15:08 4
  • 5. Flowchart and Pseudocode: Example 28-07-2022 15:08 5
  • 6. C Keywords and Identifiers • Character set • A character set is a set of alphabets, letters and some special characters that are valid in C language. • Alphabets • C accepts both lowercase and uppercase alphabets as variables and functions. • Digits • White space Characters • Blank space, newline, horizontal tab, carriage return and form feed. 28-07-2022 15:08 6
  • 8. C Keywords • Keywords are predefined, reserved words used in programming that have special meanings to the compiler. Keywords are part of the syntax and they cannot be used as an identifier. For example: • int pay; • Here, int is a keyword that indicates pay is a variable of type int (integer). • As C is a case sensitive language, all keywords must be written in lowercase. Here is a list of all keywords allowed in ANSI C. 28-07-2022 15:08 8
  • 10. C Identifiers • Identifier refers to name given to entities such as variables, functions, structures etc. • Identifiers must be unique. They are created to give a unique name to an entity to identify it during the execution of the program. For example: • int pay; • Here pay is an identifier • Also remember, identifier names must be different from keywords. 28-07-2022 15:08 10
  • 11. Rules for naming identifiers • A valid identifier can have letters (both uppercase and lowercase letters), digits and underscores. • The first letter of an identifier should be either a letter or an underscore. • You cannot use keywords like int, while etc. as identifiers. • There is no rule on how long an identifier can be. However, you may run into problems in some compilers if the identifier is longer than 31 characters. 28-07-2022 15:08 11
  • 12. C Variables, Constants and Literals • Variables • In programming, a variable is a container (storage area) to hold data. • To indicate the storage area, each variable should be given a unique name (identifier). Variable names are just the symbolic representation of a memory location. • For example: • int playerScore = 95; • Here, playerScore is a variable of int type. Here, the variable is assigned an integer value 95. • The value of a variable can be changed, hence the name variable. • char ch = 'a'; • // some code • ch = 'l'; 28-07-2022 15:08 12
  • 13. Rules for naming a variable • A variable name can only have letters (both uppercase and lowercase letters), digits and underscore. • The first letter of a variable should be either a letter or an underscore. • There is no rule on how long a variable name (identifier) can be. However, you may run into problems in some compilers if the variable name is longer than 31 characters. 28-07-2022 15:08 13
  • 14. Strongly typed language • C is a strongly typed language. This means that the variable type cannot be changed once it is declared. For example: • int number = 5; // integer variable • number = 5.5; // error • double number; // error • Here, the type of number variable is int. You cannot assign a floating-point (decimal) value 5.5 to this variable. Also, you cannot redefine the data type of the variable to double. By the way, to store the decimal values in C, you need to declare its type to either double or float. 28-07-2022 15:08 14
  • 15. Literals • Literals are data used for representing fixed values. They can be used directly in the code. For example: 1, 2.5, 'c' etc. • Here, 1, 2.5 and 'c' are literals. Why? You cannot assign different values to these terms. 28-07-2022 15:08 15
  • 16. Integers • An integer is a numeric literal(associated with numbers) without any fractional or exponential part. There are three types of integer literals in C programming: • decimal (base 10) • octal (base 8) • hexadecimal (base 16) • For example: • Decimal: 0, -9, 22 etc • Octal: 021, 077, 033 etc • Hexadecimal: 0x7f, 0x2a, 0x521 etc • In C programming, octal starts with a 0, and hexadecimal starts with a 0x 28-07-2022 15:08 16
  • 17. Floating-point Literals • A floating-point literal is a numeric literal that has either a fractional form or an exponent form. For example: • -2.0 • 0.0000234 • -0.22E-5 28-07-2022 15:08 17
  • 18. Characters • A character literal is created by enclosing a single character inside single quotation marks. For example: 'a', 'm', 'F', '2', '}' etc. 28-07-2022 15:08 18
  • 19. Escape Sequences • Sometimes, it is necessary to use characters that cannot be typed or has special meaning in C programming. For example: newline(enter), tab, question mark etc. • In order to use these characters, escape sequences are used. 28-07-2022 15:08 19
  • 21. String Literals • A string literal is a sequence of characters enclosed in double-quote marks. For example: • "good" //string constant • "" //null string constant • " " //string constant of six white space • "x" //string constant having a single character. • "Earth is roundn" //prints string with a newline 28-07-2022 15:08 21
  • 22. Constants • If you want to define a variable whose value cannot be changed, you can use the const keyword. This will create a constant. For example, • const double PI = 3.14; • Notice, we have added keyword const. • Here, PI is a symbolic constant; its value cannot be changed. • const double PI = 3.14; • PI = 2.9; //Error 28-07-2022 15:08 22
  • 23. C Data Types • In C programming, data types are declarations for variables. This determines the type and size of data associated with variables. For example, • int myVar; • Here, myVar is a variable of int (integer) type. The size of int is 4 bytes. 28-07-2022 15:08 23
  • 25. int • Integers are whole numbers that can have both zero, positive and negative values but no decimal values. For example, 0, -5, 10 • We can use int for declaring an integer variable. • int id; • Here, id is a variable of type integer. • You can declare multiple variables at once in C programming. For example, • int id, age; • The size of int is usually 4 bytes (32 bits). And, it can take 232 distinct states from -2147483648 to 2147483647. 28-07-2022 15:08 25
  • 26. float and double • float and double are used to hold real numbers. • float salary; • double price; • In C, floating-point numbers can also be represented in exponential. For example, • float normalizationFactor = 22.442e2; • What's the difference between float and double? • The size of float (single precision float data type) is 4 bytes. And the size of double (double precision float data type) is 8 bytes. 28-07-2022 15:08 26
  • 27. char and void • Keyword char is used for declaring character type variables. For example, • char test = 'h'; • The size of the character variable is 1 byte. • void is an incomplete type. It means "nothing" or "no type". You can think of void as absent. • For example, if a function is not returning anything, its return type should be void. • Note that, you cannot create variables of void type. 28-07-2022 15:08 27
  • 28. short and long • If you need to use a large number, you can use a type specifier long. Here's how: • long a; • long long b; • long double c; Here variables a and b can store integer values. And, c can store a floating-point number. • If you are sure, only a small integer ([−32,767, +32,767] range) will be used, you can use short. • short d; • You can always check the size of a variable using the sizeof() operator. 28-07-2022 15:08 28
  • 29. signed and unsigned • In C, signed and unsigned are type modifiers. You can alter the data storage of a data type by using them. For example, • unsigned int x; • int y; • Here, the variable x can hold only zero and positive values because we have used the unsigned modifier. • Considering the size of int is 4 bytes, variable y can hold values from -231 to 231-1, whereas variable x can hold values from 0 to 232-1. • Other data types defined in C programming are: • bool Type • Enumerated type • Complex types 28-07-2022 15:08 29
  • 30. Derived Data Types • Data types that are derived from fundamental data types are derived types. For example: arrays, pointers, function types, structures, etc. 28-07-2022 15:08 30
  • 31. C Input Output (I/O) • C Output • In C programming, printf() is one of the main output function. The function sends formatted output to the screen. 28-07-2022 15:08 31
  • 32. Example 1: C Output • #include <stdio.h> • int main() • { • // Displays the string inside quotations • printf("C Programming"); • return 0; • } • Output • C Programming 28-07-2022 15:08 32
  • 33. How does this program work? • All valid C programs must contain the main() function. The code execution begins from the start of the main() function. • The printf() is a library function to send formatted output to the screen. The function prints the string inside quotations. • To use printf() in our program, we need to include stdio.h header file using the #include<stdio.h> statement. • The return 0; statement inside the main() function is the "Exit status" of the program. It's optional. 28-07-2022 15:08 33
  • 34. Example 2: Integer Output • #include <stdio.h> • int main() • { • int testInteger = 5; • printf("Number = %d", testInteger); • return 0; • } • Output Number = 5 • We use %d format specifier to print int types. Here, the %d inside the quotations will be replaced by the value of testInteger. 28-07-2022 15:08 34
  • 35. Example 3: float and double Output • #include <stdio.h> • int main() • { • float number1 = 13.5; • double number2 = 12.4; • printf("number1 = %fn", number1); • printf("number2 = %lf", number2); • return 0; • } • Output • number1 = 13.500000 • number2 = 12.400000 • To print float, we use %f format specifier. Similarly, we use %lf to print double values. 28-07-2022 15:08 35
  • 36. Example 4: Print Characters • #include <stdio.h> • int main() • { • char chr = 'a'; • printf("character = %c", chr); • return 0; • } • Output • character = a • To print char, we use %c format specifier. 28-07-2022 15:08 36
  • 37. C Input • In C programming, scanf() is one of the commonly used function to take input from the user. • The scanf() function reads formatted input from the standard input such as keyboards. 28-07-2022 15:08 37
  • 38. Example 5: Integer Input/Output • #include <stdio.h> • int main() • { • int testInteger; • printf("Enter an integer: "); • scanf("%d", &testInteger); • printf("Number = %d",testInteger); • return 0; • } • Output • Enter an integer: 4 • Number = 4 28-07-2022 15:08 38
  • 39. Example 5: Integer Input/Output • Here, we have used %d format specifier inside the scanf() function to take int input from the user. When the user enters an integer, it is stored in the testInteger variable. • Notice, that we have used &testInteger inside scanf(). It is because &testInteger gets the address of testInteger, and the value entered by the user is stored in that address. 28-07-2022 15:08 39
  • 40. Example 6: Float and Double Input/Output #include <stdio.h> int main() { float num1; double num2; printf("Enter a number: "); scanf("%f", &num1); printf("Enter another number: "); scanf("%lf", &num2); printf("num1 = %fn", num1); printf("num2 = %lf", num2); return 0; } • Output • Enter a number: 12.523 • Enter another number: 10.2 • num1 = 12.523000 • num2 = 10.200000 • We use %f and %lf format specifier for float and double respectively. 28-07-2022 15:08 40
  • 41. Example 7: C Character I/O #include <stdio.h> int main() { char chr; printf("Enter a character: "); scanf("%c",&chr); printf("You entered %c.", chr); return 0; } • Output • Enter a character: g • You entered g 28-07-2022 15:08 41
  • 42. Example 7: C Character I/O • When a character is entered by the user in the above program, the character itself is not stored. Instead, an integer value (ASCII value) is stored. • And when we display that value using %c text format, the entered character is displayed. If we use %d to display the character, it's ASCII value is printed. 28-07-2022 15:08 42
  • 43. Example 8: ASCII Value #include <stdio.h> int main() { char chr; printf("Enter a character: "); scanf("%c", &chr); // When %c is used, a character is displayed printf("You entered %c.n",chr); // When %d is used, ASCII value is displayed printf("ASCII value is %d.", chr); return 0; } • Output • Enter a character: g • You entered g. • ASCII value is 103. 28-07-2022 15:08 43
  • 44. I/O Multiple Values Here's how you can take multiple inputs from the user and display them. #include <stdio.h> int main() { int a; float b; printf("Enter integer and then a float: "); // Taking multiple inputs scanf("%d%f", &a, &b); printf("You entered %d and %f", a, b); return 0; } • Output • Enter integer and then a float: -3 • 3.4 • You entered -3 and 3.400000 28-07-2022 15:08 44
  • 45. Format Specifiers for I/O • As you can see from the above examples, we use • %d for int • %f for float • %lf for double • %c for char • Here's a list of commonly used C data types and their format specifiers. 28-07-2022 15:08 45
  • 46. Data Type Format Specifier int %d char %c float %f double %lf short int %hd unsigned int %u long int %li long long int %lli unsigned long int %lu unsigned long long int %llu signed char %c unsigned char %c long double %Lf 28-07-2022 15:08 46
  • 47. C Programming Operators • An operator is a symbol that operates on a value or a variable. For example: + is an operator to perform addition. • C has a wide range of operators to perform various operations. 28-07-2022 15:08 47
  • 48. C Arithmetic Operators • An arithmetic operator performs mathematical operations such as addition, subtraction, multiplication, division etc. on numerical values (constants and variables). Operator Meaning of Operator + addition or unary plus - subtraction or unary minus * multiplication / division % remainder after division (modulo division) 28-07-2022 15:08 48
  • 49. C Increment and Decrement Operators • C programming has two operators increment ++ and decrement -- to change the value of an operand (constant or variable) by 1. • Increment ++ increases the value by 1 whereas decrement -- decreases the value by 1. These two operators are unary operators, meaning they only operate on a single operand. 28-07-2022 15:08 49
  • 50. Example 2: Increment and Decrement Operators // Working of increment and decrement operators #include <stdio.h> int main() { int a = 10, b = 100; float c = 10.5, d = 100.5; printf("++a = %d n", ++a); printf("--b = %d n", --b); printf("++c = %f n", ++c); printf("--d = %f n", --d); return 0; } Output ++a = 11 --b = 99 ++c = 11.500000 --d = 99.500000 28-07-2022 15:08 50
  • 51. C Assignment Operators • An assignment operator is used for assigning a value to a variable. The most common assignment operator is = Operator Example Same as = a = b a = b += a += b a = a+b -= a -= b a = a-b *= a *= b a = a*b /= a /= b a = a/b %= a %= b a = a%b 28-07-2022 15:08 51
  • 52. C Relational Operators • A relational operator checks the relationship between two operands. If the relation is true, it returns 1; if the relation is false, it returns value 0. Operator Meaning of Operator Example == Equal to 5 == 3 is evaluated to 0 > Greater than 5 > 3 is evaluated to 1 < Less than 5 < 3 is evaluated to 0 != Not equal to 5 != 3 is evaluated to 1 >= Greater than or equal to 5 >= 3 is evaluated to 1 <= Less than or equal to 5 <= 3 is evaluated to 0 28-07-2022 15:08 52
  • 53. Example 4: Relational Operators // Working of relational operators #include <stdio.h> int main() { int a = 5, b = 5, c = 10; printf("%d == %d is %d n", a, b, a == b); printf("%d == %d is %d n", a, c, a == c); printf("%d > %d is %d n", a, b, a > b); printf("%d > %d is %d n", a, c, a > c); printf("%d < %d is %d n", a, b, a < b); printf("%d < %d is %d n", a, c, a < c); printf("%d != %d is %d n", a, b, a != b); printf("%d != %d is %d n", a, c, a != c); printf("%d >= %d is %d n", a, b, a >= b); printf("%d >= %d is %d n", a, c, a >= c); printf("%d <= %d is %d n", a, b, a <= b); printf("%d <= %d is %d n", a, c, a <= c); return 0; } 28-07-2022 15:08 53
  • 54. Example 4: Relational Operators output Output 5 == 5 is 1 5 == 10 is 0 5 > 5 is 0 5 > 10 is 0 5 < 5 is 0 5 < 10 is 1 5 != 5 is 0 5 != 10 is 1 5 >= 5 is 1 5 >= 10 is 0 5 <= 5 is 1 5 <= 10 is 1 28-07-2022 15:08 54
  • 55. C Logical Operators • An expression containing logical operator returns either 0 or 1 depending upon whether expression results true or false. Operator Meaning Example && Logical AND. True only if all operands are true If c = 5 and d = 2 then, expression ((c==5) && (d>5)) equals to 0. || Logical OR. True only if either one operand is true If c = 5 and d = 2 then, expression ((c==5) || (d>5)) equals to 1. ! Logical NOT. True only if the operand is 0 If c = 5 then, expression !(c==5) equals to 0. 28-07-2022 15:08 55
  • 56. Example 5: Logical Operators // Working of logical operators #include <stdio.h> int main() { int a = 5, b = 5, c = 10, result; result = (a == b) && (c > b); printf("(a == b) && (c > b) is %d n", result); result = (a == b) && (c < b); printf("(a == b) && (c < b) is %d n", result); result = (a == b) || (c < b); printf("(a == b) || (c < b) is %d n", result); result = (a != b) || (c < b); printf("(a != b) || (c < b) is %d n", result); result = !(a != b); printf("!(a != b) is %d n", result); result = !(a == b); printf("!(a == b) is %d n", result); return 0; } 28-07-2022 15:08 56
  • 57. • Output (a == b) && (c > b) is 1 (a == b) && (c < b) is 0 (a == b) || (c < b) is 1 (a != b) || (c < b) is 0 !(a != b) is 1 !(a == b) is 0 28-07-2022 15:08 57
  • 58. C Bitwise Operators • During computation, mathematical operations like: addition, subtraction, multiplication, division, etc are converted to bit-level which makes processing faster and saves power. • Bitwise operators are used in C programming to perform bit-level operations. 28-07-2022 15:08 58
  • 59. C Bitwise Operators: Contd. Operators Meaning of operators & Bitwise AND | Bitwise OR ^ Bitwise exclusive OR ~ Bitwise complement << Shift left >> Shift right 28-07-2022 15:08 59
  • 60. sizeof operator • The sizeof operator • The sizeof is a unary operator that returns the size of data (constants, variables, array, structure, etc). #include <stdio.h> void main() { char a; printf("Size of char=%lu bytesn",sizeof(a)); } • Output • Size of char = 4 bytes 28-07-2022 15:08 60
  • 61. Conditional or Ternary Operator (?:) Here, Expression1 is the condition to be evaluated. If the condition(Expression1) is True then Expression2 will be executed and the result will be returned. Otherwise, if the condition(Expression1) is false then Expression3 will be executed and the result will be returned. 28-07-2022 15:08 61
  • 62. Example: Program to Store the greatest of the two Number. #include <stdio.h> int main() { int a=2,b=3,c; c=a>b?a:b; printf("%d is greater ",c); return 0; } • Output • 3 is greater 28-07-2022 15:08 62
  • 63. Errors in C • Error is an illegal operation performed by the user which results in abnormal working of the program. • Programming errors often remain undetected until the program is compiled or executed. • Some of the errors inhibit the program from getting compiled or executed. • Thus errors should be removed before compiling and executing. 28-07-2022 15:08 63
  • 64. Syntax errors • Errors that occur when you violate the rules of writing C syntax are known as syntax errors. • Most frequent syntax errors are: • Missing Parenthesis (}) • Printing the value of variable without declaring it • Missing semicolon 28-07-2022 15:08 64
  • 65. Run-time Errors • Errors which occur during program execution(run-time) after successful compilation are called run-time errors • One of the most common run-time error is division by zero also known as Division error 28-07-2022 15:08 65
  • 66. Linker Errors • These error occurs when after compilation we link the different object files with main’s object • These are errors generated when the executable of the program cannot be generated. • This may be due to wrong function prototyping, incorrect header files. • One of the most common linker error is writing Main() instead of main(). 28-07-2022 15:08 66
  • 67. Logical Errors • On compilation and execution of a program, desired output is not obtained when certain input values are given • These types of errors which provide incorrect output but appears to be error free are called logical errors 28-07-2022 15:08 67
  • 68. Logical Errors : Example int main() { int i = 0; // logical error : a semicolon after loop for(i = 0; i < 3; i++); { printf("loop "); continue; } getchar(); return 0; } 28-07-2022 15:08 68
  • 69. Semantic errors • This error occurs when the statements written in the program are not meaningful to the compiler. • Example: // C program to illustrate // semantic error void main() { int a, b, c; a + b = c; //semantic error } 28-07-2022 15:08 69
  • 70. Object Code • Object code generally refers to the output, a compiled file, which is produced when the Source Code is compiled with a C compiler. • The object code file contains a sequence of machine-readable instructions that is processed by the CPU in a computer. 28-07-2022 15:08 70
  • 71. Executable code • Executable (also called the Binary) is the output of a linker after it processes the object code. • A machine code file can be immediately executable (i.e., runnable as a program), or it might require linking with other object code files (e.g. libraries) to produce a complete executable program. 28-07-2022 15:08 71
  • 72. Linker • In computer science, a linker is a computer program that takes one or more object files generated by a compiler and combines them into one, executable program. 28-07-2022 15:08 72
  • 73. Loader • In computer systems a loader is the part of an operating system that is responsible for loading programs and libraries. 28-07-2022 15:08 73