c Programming - Module II
c Programming - Module II
Outlines
• Introduction to Computer .
• Memory System
• Data Representation – Number System, Character Representation code , binary , Octal, Hexadecimal code.
• Number system operations ( Binary Arithmetic, Floating point Arithmetic, Signed and Unsigned number)
Outlines ..
• Introduction & History of C Programming Language
• Concept of Variables and its Types
• Operators & types in C programming
• Expressions
• Managing input and outputs
• Formatting I/O
INTRODUCTION – C
Greater Noida Campus
C is a general-purpose programming language that has been widely used for over 50 years.
C is very powerful; it has been used to develop operating systems, databases, applications, etc.
C is strongly associated with UNIX, as it was developed to write the UNIX operating system.
Why C?
• It is one of the most popular programming languages in the world.
• If you know C, you will have no problem learning other popular programming languages such as
Java, Python, C++, C#,
• C is very fast, compared to other programming languages, like Java and Python
• C is very versatile;
• it can be used in both applications and technologies
C - Compiler
Greater Noida Campus
C Install IDE
IDE (Integrated Development Environment) used for compiled the code.
Download Process
http://www.codeblocks.org/. mingw-setup.exe
Let’s Work
Open Codeblocks and go to File > New > Empty File .
Write the following C code and save the file as Experiment1.c (File > Save File as)
Editor Environment
Greater Noida Campus
!"
Line 1: #include <stdio.h> is a header file library that lets us work with input and output functions,
Line 2: A blank line. C ignores white space. But we use it to make the code more readable.
This is called a function. Any code inside its curly brackets {} will be executed.
Variables are containers for storing data values, like numbers and characters.
In C, there are different types of variables (defined with different keywords),
// Print variables
printf("%d\n", my);
Print value without variable printf("%f\n", myF);
#include <stdio.h> printf("%c\n", myL);
int main()
{ Da
ta M
printf("My favorite number is: %d\n", 15); Diff ay
ere be
printf("My favorite letter is: %c", 'D'); nt
return 0;
}
Change value & Add Variables
Greater Noida Campus
#include <stdio.h>
void main() #include <stdio.h>
{ int main()
int m = 15; {
m = 100; int x = 5;
printf("%d", myNum); int y = 6;
} int sum = x + y;
printf("%d", sum);
Check the code and find error & correct them return 0;
}
Multiple Variables
Greater Noida Campus
Example
Syntax #include <stdio.h>
int x = 5, y = 6, z = 50; void main()
printf("%d", x + y + z); {
int x = 5, y = 6, z = 50;
printf("%d", x + y + z);
}
Assignment 1
Write Program in C Language to display student’s details like Name, Course , Roll
no, Phone number, Grade , Year with proper flow chart.
Operators
Greater Noida Campus
#include <stdio.h>
int main()
{
#include <stdio.h> int x = 5;
int main() int y = 2;
{ printf("%d", x % y);
int x = 5; return 0;
#include <stdio.h> printf("%d", --x); }
int main() return 0;
{ }
int x = 5;
printf("%d", ++x);
return 0;
}
Assignment Operators
Greater Noida Campus
#include <stdio.h>
void main()
{
int x = 10;
x += 5;
printf("%d", x);
}
Assignment Operators
Greater Noida Campus
= (Assignment)- Used to assign a value from right side operand to left side operand.
+= (Addition Assignment)- To store the sum of both the operands to the left side operand.
-= (Subtraction Assignment) – To store the difference of both the operands to the left side operand.
*= (Multiplication Assignment) – To store the product of both the operands to the left side
operand.
/= (Division Assignment) – To store the division of both the operands to the left side operand.
Assignment Operators
Greater Noida Campus
Logical operators are used to determine the logic between variables or values, by
&&
||
!
Logical Operators Example
Greater Noida Campus
#include <stdio.h>
#include <stdio.h> int main()
int main() {
{ int x = 5;
int x = 5; int y = 3;
int y = 3; printf("%d", x > 3 || x < 4);
printf("%d", x > 3 && x < 10); return 0;
return 0; }
}
#include <stdio.h>
Void main()
Out Put {
??? int x = 5;
int y = 3;
printf("%d", !(x > 3 && x < 10));
}
Comparison Operators
Greater Noida Campus
The return value of a comparison is either 1 or 0, which means true (1) or false (0)
Comparison Operators
Greater Noida Campus
Comparison Operators
Greater Noida Campus
#include <stdio.h>
void main() #include <stdio.h>
{ int main()
int x = 5; {
int y = 5; int x = 5;
printf("%d", x == y); int y = 3;
} printf("%d", x <= y);
return 0;
#include <stdio.h> }
void main()
{
int x = 5;
int y = 3;
printf("%d", x != y);
}
Miscellaneous Operators
Greater Noida Campus
sizeof – It returns the memory occupied by the particular data type of the operand.
& (Pointer) – It refers to the address (memory location) in which the operand is stored.
& (Bitwise AND) – Converts the value of both the operands into binary form and
performs AND operation bit by bit.
| (Bitwise OR) – Converts the value of both the operands into binary form and performs
OR operation bit by bit.
^ (Bitwise exclusive OR) – Converts the value of both the operands into binary form and
performs EXCLUSIVE OR operation bit by bit.
~ (One’s complement operator): Converts the operand into its complementary form.
<< – Left shift
>> – Right shift
Operator Precedence and Associativity
Greater Noida Campus
• It is very common for compiler [first evaluates the operator with higher precedence]
• It helps to maintain the ambiguity of the expression and helps to avoiding unnecessary use of
parenthesis.
Operator Precedence and Associativity
Greater Noida Campus
Operator Precedence and Associativity
Greater Noida Campus
Operator Precedence and Associativity
Greater Noida Campus
Operator Precedence and Associativity
Greater Noida Campus
Operator Precedence and Associativity
Greater Noida Campus
Formatting input/output
Greater Noida Campus
Unformatted console input/output functions are used to read a input from the user at console
It also allows us to display the value in the output to the user at the console.
Assignment 2:
Explain the given functions of
Formatted and unformatted with
proper example
Unformatted input/output
Greater Noida Campus
Web Links
https://www.javatpoint.com/c-operators
https://data-flair.training/blogs/operators-in-c-and-cpp/
https://www.tutorialspoint.com/cprogramming/c_operators.htm
https://byjus.com/gate/operators-in-c/
Book Reference
Quiz