C Programming Notes 18 September Unit1+Unit 2
C Programming Notes 18 September Unit1+Unit 2
FUNDAMENTALS OF C PROGRAMMING
C Language Introduction
is as follows:
Application of C
• The printf() and scanf() functions are used for input and output in C
language. Both functions are inbuilt library functions, defined in
stdio.h (header file).
printf() function
The printf() function is used for output. It prints the given statement to
the console.
The syntax of printf() function is given below:
printf("format string",argument_list);
scanf() function
• The scanf() function is used for input. It reads the input data from the
console.
• scanf("format string",argument_list);
#include<stdio.h>
int main(){
int number;
printf("enter a number:");
scanf("%d",&number);
printf("cube of number is:%d ",number*number*number);
return 0;
}
Constants in C
Example:
#include<stdio.h>
int main(){
const float PI=3.14;
printf("The value of PI is: %f",PI);
return 0;
}
C #define
• C=a+b
• Here, ‘+’ is the operator known as the addition operator, and ‘a’
and ‘b’ are operands. The addition operator tells the compiler
to add both of the operands ‘a’ and ‘b’.
Unary Operators: ++, --
• Operators that operate or work with a single operand are unary
operators. For example: Increment(++) and Decrement(- -) Operators
#include<stdio.h>
int main()
{
int a=9;
a++;
printf("%d",a);
}
Output: 10
Binary Operators: +, -, /, *, %
• Operators that operate or work with two operands are binary operators. For
example: Addition(+), Subtraction(-), multiplication(*), Division(/) operators
#include<stdio.h>
int main()
{
int a=9,b=8,sum;
sum=a+b;
printf("%d",sum);
}
Output: 17
Relational Operators in C: ==, >= , <=
• These are used for the comparison of the values of two operands.
For example, checking if one operand is equal to the other operand
or not, whether an operand is greater than the other operand or not,
etc. Some of the relational operators are ==, >= , <=
#include<stdio.h>
int main()
{
int a=9,b=8;
printf("%d",a>b);
}
output: 1
Logical Operator in C: && , II ,!
• Logical Operators are used to combining two or more
conditions/constraints or to complement the evaluation of the
original condition in consideration. The result of the operation of a
logical operator is a Boolean value either true or false. 0 or 1
#include<stdio.h>
int main()
{
int a=9,b=8;
printf("%d",a!=b);
}
Output: 1
• #include<stdio.h>
• int main()
•{
• int a=9,b=8,c=7;
• printf("%d",(a>b)&&(b<c));
•}
• Output: 0
Bitwise Operators in C
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0
#include <stdio.h>
int main()
{ // a = 5(00000101), b = 9(00001001)
unsigned int a = 5, b = 9;
// The result is 00000001
printf("a&b = %d\n", a & b);
// The result is 00001101
printf("a|b = %d\n", a | b);
return 0;
}
Output: a^b = 12
a|b = 13
Ternary Operator
bool -> char -> short int -> int ->unsigned int -> long -> unsigned ->
long long -> float -> double -> long double
#include <stdio.h>
int main()
{
int x = 10; // integer x
char y = 'a'; // ASCII value of 'a' is 97
x = x + y;
float z = x + 1.0;
printf("x = %d, z = %f", x, z);
return 0;
}
2. Explicit Type Conversion
• This process is also called type casting and it is user-defined. Here the
user can typecast the result to make it of a particular data type. The
syntax in C Programming:
• (type) expression
• Type indicated the data type to which the final result is converted.
#include<stdio.h>
int main()
{
double x = 1.2;
// Explicit conversion from double to int
int sum = (int)x + 1;
printf("sum = %d", sum);
return 0;
}
#include <stdio.h>
int main() {
float a = 1.5;
int b = (int)a;
printf("a = %f\n", a);
printf("b = %d\n", b);
return 0;
}
UNIT 2
BRANCHING AND CONTROL STATEMENT
if (condition) {
//code to be executed if condition specified evaluates is
true
}
If the condition is evaluated to be true, the code within curly braces will be executed but if
the condition is false, the code in braces will be skipped.
EXAMPLE
include <stdio.h>
int main() {
int x = 10;
if (x > 5) {
printf("x is greater than 5");
}
return 0;
}
else Statement
if (condition) {
// statements
} else{
// code executed if condition is false
}
#include <stdio.h>
int main()
{
int x = 10;
if (x < 5)
{
printf("x is less than 5\n");
}
else
{
printf("x is greater than or equal to 5\n");
}
return 0;
}
else if Statement
The else if statement in C is used when we want to check
multiple conditions. It follows an "if" statement and is executed if
the previous "if" statement’s condition is false.
Syntax of else-if Statement in C
if (condition1) {
// code to be executed if condition1 is true
}
else if (condition2) {
// code to be executed if condition2 is true and condition1 is false
}
• The "else if" statement can be repeated multiple times to check
for multiple conditions. If all conditions are false, the code
within the final "else" block will be executed.
#include <stdio.h>
int main() {
int x = 10;
if (x > 15) {
printf("x is greater than 15");
}
else if (x > 5) {
printf("x is greater than 5 but less than or equal to 15");
}
else {
printf("x is less than or equal to 5");
} Output
x is greater than 5 but less than or equal to 15
return 0;
}
switch Statement
• The switch statement in C is used when we have multiple
conditions to check. It is often used as an alternative to multiple
"if" and "else if" statements.
Syntax of switch Statement in C
switch (expression) {
case value1:
// code to be executed if expression equals value1
break;
case value2:
// code to be executed if expression equals value2
break;
default:
// code to be executed if none of the cases match
break;
}
Unconditional Branching Statements in C
break;
Output
case 3:
#include <stdio.h> printf("x is 3");
int main() { break;
int x = 2; default:
switch (x) { printf("x is not 1, 2, or 3");
case 1: break;
printf("x is 1"); }
break; return 0;
case 2: } Output
printf("x is 2");
break; x is 2
goto Statement
goto label;
...
label:
// code to be executed
#include <stdio.h>
int main() {
int x = 0;
start:
x++;
if (x < 10) {
goto start;
}
printf("x is %d", x);
Output
return 0; x is 10
}
continue Statement
continue;
#include <stdio.h>
int main() {
int i;
for (i = 0; i < 10; i++) {
if (i % 2 == 0) {
continue; // skip even numbers
}
printf("%d ", i);
}
return 0;
} Output
13579
• Ques 1. Can the "switch" statement be nested in C?
Ans. No, we cannot nest the switch statement in C. This means that
a switch statement in C cannot contain another switch statement
inside it.
• Ques 2. How many "case" statements can a "switch" statement have
in C?
Ans. A "switch" statement in C can have any number of "case"
statements.
• Ques 3. What happens if none of the "case" statements match in a
"switch" statement in C?
Ans. If none of the "case" statements match in a "switch" statement
in C, the code within the "default" block is executed.
• Ques 4. Can the "break" statement be used outside of loops and
switch statements in C?
Ans. No, the "break" statement can only be used within loops and
switch statements in C.
• Ques 5. Can the "goto" statement be used to jump to a label defined
in another function in C?
Ans. No, the "goto" statement can only jump to a label defined within
the same function in C.
• Ques 4. Can the “continue" statement be used outside of loops and
switch statements in C?
Ans. No , The continue statement can only be used on loops and not
on switches.
Leap year program
ending with 00). The century year is a leap year only if it is perfectly
divisible by 400.
#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
if (year % 400 == 0) {
printf("%d is a leap year.", year);
else if (year % 4 == 0) {
}
printf("%d is a leap year.", year);
else if (year % 100 == 0) {
}
printf("%d is not a leap year.",
year); else {
} printf("%d is not a leap year.",
year);
}
return 0;
}
#include<stdio.h>
Int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
if(((year%4==0) && ((year%400==0) || (year%100!==0))
{
printf("%d is a leap year", &year);
} else {
printf("%d is not a leap year", &year);
} retirn 0;
}
#include <stdio.h>
int main() Largest of three numbers using if else
{
int A, B, C;
printf("Enter the numbers A, B and C: ");
scanf("%d %d %d", &A, &B, &C);
if (A >= B && A >= C)
printf("%d is the largest number.", A);
else if (B >= A && B >= C)
printf("%d is the largest number.", B);
else
(C >= A && C >= B)
printf("%d is the largest number.", C);
return 0;
}
Largest of three numbers using ternary operator
#include <stdio.h>
int main()
{
int n1 = 5, n2 = 10, n3 = 15, max;
max = (n1 > n2) ? (n1 > n3 ? n1 : n3) : (n2 > n3 ? n2 : n3);
printf("Largest number among %d, %d and %d is %d.",n1, n2, n3, max);
return 0;
}
Looping or iterating Statements:
Loops are produced by iteration statements in a program.
Iteration is the process of repeatedly running the same piece of
code until a predetermined condition is met. The same
instructions are carried out by iteration statements up until a
termination condition is satisfied.
statements loop in C is as follows:
1. while loop
2. for loop
3. do-while loop
While Loop :
#include<stdio.h>
int main()
{
int a=1;
while(a<=4)
{
printf("%d", a);
a++;
}
return 0;
}
#include <stdio.h>
int main()
{
int i = 1;
while(i <= 5)
{
printf("%d ", i);
i = i + 1;
}
return 0;
}
#include <stdio.h>
int main() {
Reverse of a
int n, reverse = 0, remainder; Number
printf("Enter an integer: ");
scanf("%d", &n);
while (n != 0) {
remainder = n % 10;
reverse = reverse * 10 + remainder;
n =n/ 10;
}
printf("Reversed number = %d", reverse);
return 0;
}
do...while
A do...while loop is similar to a while loop, except the fact that it is
guaranteed to execute at least one time.
Syntax
The syntax of a do...while loop in C programming language is −
do {
statement(s);
} while(condition);
#include <stdio.h>
int main () {
/* local variable definition */
int a = 10;
/* do loop execution */
do {
printf("value of a: %d\n", a);
a = a + 1;
}while( a < 10 );
return 0;
}
For Loop
The for loop is a control flow statement that allows you to execute a
block of code repeatedly based on a specified condition.
int main() {
int i;