Module1-A
Module1-A
/*
Overview of the code
.
*/
Preprocessor section
The preprocessor section contains all the header files used in a
program. It informs the system to link the header files to the system
libraries.
#include<stdio.h>
#include<conio.h>
# define pi 3.14
Global declaration
The global section comprises of all the global
declarations in the program.
Main function
main() is the first function to be executed by the
computer. It is necessary for a code to include the
main(). It is like any other function available in the C
library. Parenthesis () are used for passing parameters (if
any) to a function.
User defined functions
The user defined functions specified the functions
specified as per the requirements of the user
/* This is the structure of a typical C program */
#include <stdio.h>
#define PI 3.14159
int gv = 10;
void display ()
{ printf("Hello from display\n");
}
int main()
{
printf("Hello, World!\n");
display();
printf(“\n%d”,gv);
gv=200;
printf(“\n%d”,gv);
printf(“%lf”, PI*5*5);
}
Data types
• Data types in C define the type of data that a
variable can store.
• They determine the type of value a variable
can hold and the operations that can be
performed on it
Category of datatypes
1. Primary Data Types
2. Derived Data Types
3. Enumeration Data Types
4. User-defined Data Types
Primary data types
int: Used for integers.Example: int age = 25;
Size: Typically 4 bytes (platform-dependent)
float: Used for single-precision floating-point numbers.Example:
float salary = 2500.50;
Size: Typically 4 bytes
double: Used for double-precision floating-point
numbers.Example: double pi = 3.141592653589;
Size: Typically 8 bytes
char: Used for storing single characters.Example: char grade = 'A';
Size: 1 byte
void: Represents no value (used in functions).Example: void
functionName() {}
Derived Data Types
• These are based on primary data types.
• Array: A collection of elements of the same
type.
– Example: int numbers[5] = {1, 2, 3, 4, 5};
• Pointer: Stores the memory address of
another variable.
– Example: int *ptr = &age;
• Function: Functions return data types.
Enumeration Data Types
• Allows defining a variable that can take
predefined values.
• Syntax:
enum varname {val1,val2,…,valn}
• enum week {Sunday, Monday, Tuesday,
Wednesday, Thursday, Friday, Saturday};
#include <stdio.h>
int main() {
enum week today;
today = Wednesday;
if (today == Wednesday) {
printf("Mid-week day!\n");
}
return 0;
}
Points to remember
• Enumeration Declaration:
– enum week {Sunday, Monday, Tuesday,
Wednesday, Thursday, Friday, Saturday};
– Each value is automatically assigned an integer
starting from 0 (e.g., Sunday = 0, Monday = 1, ...).
• Assign Specific Values: You can manually
assign values to the constants:
enum week {Sunday = 1, Monday, Tuesday = 10,
Wednesday};
User-defined Data Types
struct stud
{
int rno;
char name[20];
double cgpa;
};
int main() {
getch();
return 0;
}
printf("\nint =%d bytes",sizeof(i));
int i; printf("\nshort int =%d bytes",sizeof(j));
short int j; printf("\nlong int =%d bytes",sizeof(k));
long int k; printf("\nsigned int =%d bytes",sizeof(l));
signed int l; printf("\n unsigned int =%d bytes",sizeof(m));
unsigned int m;
printf("\n float =%d bytes",sizeof(f));
printf("\n double =%d bytes",sizeof(d));
float f;
double d; printf("\n char =%d bytes",sizeof(c));
printf("\n string =%d bytes",sizeof(s));
char c; printf("\n name =%d bytes",sizeof(name));
char s[10];
char name[10][20]; for (i=0;i<10;i++)
scanf("%s",name[i]);
printf("\n Names are as follows:");
for (i=0;i<10;i++)
printf("\n%s",name[i]);
Typedef
typedef: Used to create an alias printf("\n unsigned int %d and its
for a data type. size %d bytes",u,sizeof(u));
typedef unsigned int uint; s.rno=100;
uint age = 45; strcpy(s.n,"sunitha");
printf("\n Student Details");
printf("\n rno=%d\t
struct student name=%s",s.rno,s.n);
{
int rno;
char n[10];
};
typedef struct student stud;
stud s;
Constants
• constants are fixed values that do not change
during the execution of a program
• Constants are used to represent fixed values in code
• Constants supported by C are
1. Integer Constants
2. Floating-Point Constants
3. Character Constants
4. String Constants
5. Symbolic Constants
Symbolic constants
• Constants defined using #define or const
keyword.
• #define directive is a preprocessor command
to define constants.
• The const keyword declares variables that
cannot be modified after initialization
Example
#define PI 3.14159
const int MAX = 100;
int main()
{
const int MAX1 = 10;
printf("Maximum value: %d\n%d", MAX,MAX1);
printf("Value of PI: %.2f\n", PI);
return 0;
}
Difference
Operators in C
operators are symbols used to perform operations on variables
and values
The category of operators are
1. Arithmetic Operators
2. Relational (Comparison) Operators
3. Logical Operators
4. Bitwise Operators
5. Assignment Operators
6. Increment/Decrement Operators
7. Conditional (Ternary) Operator
8. Special Operators
9. Miscellaneous Operators
Arithmetic Operators
#include <stdio.h>
int main() {
int a = 9, b = 4, c;
c = a + b;
printf("a + b = %d\n", c);
c = a - b;
printf("a - b = %d\n", c);
c = a * b;
printf("a * b = %d\n", c);
c = a / b;
printf("a / b = %d\n", c);
c = a % b;
printf("Remainder when a divided by b = %d\n", c);
return 0;
}
Example
void main()
{
double a,b; int c,d; float l,m;
clrscr();
c=9; d=2; a=9;b=2; l=9,m=2;
printf("\n %lf",a/b); printf("\n
%d",a/b);
printf("\n %lf",c/d); printf("\n
%d",c/d);
printf("\n %f",l/m); printf("\n
%d",l/m);
getch();
}
Relational Operators
int main() {
int a = 5, b = 5;
if (a == b)
printf("a is equal to b\n");
a=5; b=10;
if (a <= b)
printf("a is less than or equal to b\n");
if (a != b) {
printf("a is not equal to b\n");
}
Logical Operators
Logical operators in C are used to combine
multiple conditions/constraints. Logical
Operators returns either 0 or 1
int main() {
int a = 5, b = 10;
if (!(a > b)) {
printf("Condition is false, so this message is
printed.\n");
} else {
printf("Condition is true, so this message is
printed.\n");
}
return 0;
}
Questions
• int x = 1, y = 0, z = 5,a;
• a = x && y || z++;
• Printf(‘%d%d’’,a,z);
Bitwise Operators
Bitwise operators allow direct manipulation of
individual bits within data
AND & operator
• Performs a bitwise AND operation. Each bit is compared,
and the result is 1 only if both corresponding bits are 1.
• #include <stdio.h>
int main() {
int a = 5;
int b = 3;
int result = a & b;
printf("Result of 5 & 3 = %d\n", result);
return 0;
}
OR (|) Operator
Performs a bitwise OR operation. Each bit is compared,
and the result is 1 if either of the corresponding bits is 1
#include <stdio.h>
int main() {
int a = 5;
int b = 3;
int result = a | b;
printf("Result of 5 | 3 = %d\n", result);
return 0;
}
XOR (^) operator
Performs a bitwise XOR operation. Each bit is compared,
and the result is 1 only if the corresponding bits differ
#include <stdio.h>
int main() {
int a = 5;
int b = 3;
int result = a ^ b;
printf("Result of 5 ^ 3 = %d\n", result);
return 0;
}
NOT (~) operator
Inverts all the bits in a number. Each 0 becomes 1 and each 1 becomes 0
#include <stdio.h>
int main() {
int a = 5;
int result = ~a;
return 0;
}
~n = -(n+1)
Left Shift (<<) operator
Shifts the bits of a number to the left by a specified
number of positions, filling the new bits with 0
#include <stdio.h>
int main() {
int a = 5;
int result = a << 1;
int main() {
int a = 5;
int result = a >> 1;
printf("Result of 5 >> 1 = %d\n", result);
return 0;
}
Example
int a=10, b=8;
int x=0,y=5,z=5;
printf("\n a&b = %d", a&b);
printf("\n a|b = %d", a|b);
printf("\n a^b = %d", a^b);
printf("\n ~a = %d", ~a);
a=10;
printf("\n a<<2 = %d", a<<2);
a=10;
printf("\n a>>2 = %d", a>>2);
a=x&&y||z++;
printf("\n a=%d\tz=%d",a,z);
Assignment Operators
Increment/Decrement Operators
Example
a=5;
printf("\n%d",++a);
printf("\n%d",--a);
printf("\n%d",a--);
printf("\n%d",a++);
Conditional (Ternary) Operator
? Conditional operator
conditional operator is similar to the if-else statement
conditional operator takes less space
helps to write the if-else statements in the shortest
way possible
It is also known as the ternary operator
Syntax
variable = Expression1 ? Expression2 : Expression3;
(condition) ? (variable = Expression2) : (variable =
Expression3);
int a,b,c;
double p,q,r,s;
a=8; b=9;
c = (a>b) ? a:b;
printf("\n largest of two is %d\n",c);
c = (a>b) ? printf ("largest is a=%d",a) : printf("\n largest is b=
%d",b);
r=4; q=1; s=1;
p = ( r*r - 4*q*s)==0 ? printf("\n equal Roots"): ((r*r -4*q*s)>0 ? printf("\n real roots") :
printf("\n Imaginary roots"));