0% found this document useful (0 votes)
8 views

Module1-A

The document provides an overview of the structure and components of a typical C program, including sections like documentation, preprocessor, global declarations, main function, and user-defined functions. It also covers data types, operators, and constants in C, detailing primary, derived, enumeration, and user-defined data types, as well as various operators such as arithmetic, relational, logical, and bitwise operators. Additionally, it explains the use of typedef and constants, along with examples demonstrating their application.

Uploaded by

sunitha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Module1-A

The document provides an overview of the structure and components of a typical C program, including sections like documentation, preprocessor, global declarations, main function, and user-defined functions. It also covers data types, operators, and constants in C, detailing primary, derived, enumeration, and user-defined data types, as well as various operators such as arithmetic, relational, logical, and bitwise operators. Additionally, it explains the use of typedef and constants, along with examples demonstrating their application.

Uploaded by

sunitha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 61

Documentation section

It includes the statement specified at the beginning of a program, such as a


program's name, date, description, and title

/*
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>

enum week {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday,


Saturday};

int main() {
enum week today;
today = Wednesday;

printf("The numeric value of today (Wednesday) is: %d\n", today);

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

• Created by the programmer.


• struct: Used to group variables of different
types.
struct Student {
int id;
char name[50];
float marks;
};
• union: Similar to struct, but all members share
the same memory location.
union Data {
int i;
float f;
char str[20];
};
union
• union is a user-defined data type that allows
storing different data types in the same
memory location.
• only one member of a union can hold a value
at a time
• The size of a union is determined by its largest
member.
Example
union Data
{
int i;
float f;
char str[20];
};
Shared Memory:
In the union Data, i, f, and str share the same memory location.
Assigning a new value to one member overwrites the previous value.
Size of the Union:
The size of a union is determined by its largest member.
In this case, str[20] (20 bytes) is the largest, so the size of union Data is at least 20
bytes.
Behavior:
When you store a new value in one member, it affects the other members since they
share memory.
#include <stdio.h>
# include <conio.h>
# include <string.h>
union Data {
int i;
float f;
char str[20];
};

struct stud
{
int rno;
char name[20];
double cgpa;
};
int main() {

union Data data;


struct stud s;
clrscr();
printf("\n OPERATIONS on UNION");
printf("\nMemory allocated for union
%d",sizeof(data));
printf("\n memory for i%d\n memory for f %d\n
memory for str %d",sizeof(data.i),sizeof(data.f),sizeof(data.str));
data.i = 10;
printf("\nInteger: %d\n", data.i);
data.f = 3.14;
printf("\nFloat: %lf\n", data.f);
printf("\n%d\t%lf",data.i,data.f);
strcpy(data.str,"hello");
printf("\n String is %s",data.str);
printf("\n%d\t%lf\t%s", data.i,data.f,data.str);
printf("\n STRUCTURE DETAILS");
printf("\n size of structure %d",sizeof(s));
printf("\n size of rno %d\n size of name
%d\n size of cgpa
%d",sizeof(s.rno),sizeof(s.name),sizeof(s.cgpa));
s.rno=100;
strcpy(s.name,"sunitha");
s.cgpa=9.97;
printf("\n%d\t%s\t
%f",s.rno,s.name,s.cgpa);

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

• Logical AND ( && )


• Logical OR ( || )
• Logical NOT ( ! )
#include <stdio.h> I
int main() {
int a = 5, b = 10, c = 5;
if (a == c && b > a)
{ printf("Both conditions are
true.\n"); }
else { printf("One or both
conditions are false.\n");
}
return 0;
}
#include <stdio.h>
int main()
{
int a = 5, b = 10, c = 3;
if (a == c || b > a)
{ printf("At least one condition is true.\n"); }
else
{ printf("Both conditions are false.\n"); }
return 0;
}
#include <stdio.h>

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;

printf("Result of ~5 = %d\n", result);

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;

printf("Result of 5 << 1 = %d\n", result);


return 0;
}
Right Shift (>>) operator
Shifts the bits of a number to the right by a specified
number of positions
#include <stdio.h>

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"));

r=2; q=1; s=1

r=1; q=3; s=1;


Special Operators
Example
struct student s,*p, t[10];
printf("\n Enter the values for
structure data\n");
scanf("%d", &s.rno);
scanf("%s", s.n);
*p=s;
printf("\n student Details");
printf("\n RNo= %d\t%d",
s.rno,p->rno);
printf("\n NAME=%s\t
%s",s.n,p->n);
Miscellaneous Operators

comma ( , ) can be used in these contexts:


Comma as an operator
Comma as a separator
Example
a=5;
b=
(a+2,a+7,a+11);
printf("\n%d",b);

You might also like