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

PIC assignment

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

PIC assignment

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

ASSIGNMENT

Unit-1

Programming in C

Submitted by: Submitted to:


Name: Sarthak vijay Mrs. Indu
Roll no: 20
Variables
In C, variables are used to store data values. Every variable in C has a
data type that determines the type and size of data the variable can
hold, as well as the operations that can be performed on it.

A variable is a named memory location used to store data. The value


stored in a variable can be changed during the execution of a program.

Types of Variables
Variables in C are categorized based on the scope, lifetime, and data
type.
a. Based on Data Type:
1. int (Integer): Used to store whole numbers.
For example, int x = 10;
2. float (Floating Point): Used to store decimal numbers with single
precision. For example, float pi = 3.14;
3. char (Character): Used to store single characters or small integers.
For example, char initial = 'A';
4. void: Indicates that the variable does not hold any value; often
used with pointers or functions.

b. Based on Scope and Lifetime:


1. Local Variables: Declared inside a function or block and only
accessible within that block. For example,
int main() {
int x = 5;
}
2. Global Variables: Declared outside of all functions and accessible
throughout the entire program. For example,
int x = 10; // Global variable
int main() {
printf("%d", x);
}
3. Static Variables: Retains its value between function calls.
void func() {
static int count = 0;
count++;
printf("%d\n", count);
}

Scope and Lifetime of Variables


 Scope refers to the region of the program where the variable can
be accessed.
o Local variables: Have block scope and are accessible only
within the block (e.g., function) in which they are declared.
o Global variables: Have file scope and can be accessed by any
function in the same file.
 Lifetime refers to how long the variable exists in memory.
o Local variables: Exist only during the execution of the block.
o Static variables: Persist throughout the entire program
execution.
o Global variables: Exist for the entire lifetime of the program.

Naming Rules for Variables


1. Must begin with a letter or underscore (_).
2. Can contain letters, numbers, and underscores.
3. No special characters (e.g., @, #, $) are allowed.
4. Cannot use C keywords (like int, float, return) as variable names.
Data Types
In C, data types are classifications that specify which type of data a
variable can hold. Understanding data types is crucial for managing
memory, performing operations, and ensuring that data is handled
correctly in your programs.

1. Basic Data Types


a. Integer Types (int)
 Description: Used to store whole numbers (both positive and
negative). For example: int a;
b. Floating-Point Types (float)
 Description: Used to store fractional numbers. For e.g. float f =
3.14f;
c. Character Type (char)
 Description: Used to store individual characters. For e.g. char ch =
‘A’;

2. Derived Data Types


a. Arrays
 Description: A collection of elements of the same data type,
stored in contiguous memory locations. For e.g. int numbers[10];
b. Pointers
 Description: Variables that store memory addresses of other
variables. For e.g. int *ptr;
c. Structures (struct)
 Description: A user-defined data type that groups different data
types under a single name.
 Example: struct Person {
char name[50];
int age;
}
Input/Output
In C, I/O (Input/Output) operations are used to take input from the user
or a file and display output to the user or store it in a file. These
operations are typically performed using standard input (stdin),
standard output (stdout), and standard error (stderr). The functions for
performing I/O operations are available in the <stdio.h> library.
Let's break down I/O in C into two categories: Input and Output.

1. Input Operations (Reading Data)

a. scanf()
 scanf() is used to take input from the user. It reads formatted data
from standard input (usually the keyboard).
 Example: int num;
printf("Enter a number: ");
scanf("%d", &num);

b. gets()
 gets() reads a line of text from standard input, including spaces.
 Example: char name[50];
gets(name);

c. fgets()
 fgets() is a safer alternative to gets() for reading strings. It allows
you to specify the maximum number of characters to read.
 Example: char name[50];
fgets(name, 50, stdin);

2. Output Operations (Displaying Data)


a. printf()
 printf() is used to display formatted output to the console.
 Example: printf("The value is: %d", num);

b. puts()
 puts() is used to display a string followed by a newline character.
 Example: char name[50] = "John";
puts(name);

3. File I/O Operation


In C, file I/O is performed using functions that allow you to read from
and write to files.

a. Opening a File
 fopen() is used to open a file.
 Example: *fp = fopen("data.txt", "r");
 Modes:
o "r": Open for reading.
o "w": Open for writing (creates a new file or truncates the
existing one).
o "a": Open for appending.
o "r+": Open for both reading and writing.

b. Reading and Writing Files


 fprintf(): Used to write formatted data to a file.
o Example: fprintf(fp, "Data: %d", data);
 fscanf(): Used to read formatted data from a file.
o Example: fscanf(fp, "%d", &data);
Interconversion Of
Variables
1. Implicit Type Conversion (Automatic Type Conversion):
Implicit type conversion, also known as automatic type conversion or
type promotion, occurs when the compiler automatically converts one
data type to another during an operation or assignment. This is done
without the programmer explicitly requesting it.
Rules of Implicit Conversion:
 If you mix data types in an expression (like adding an int and a
float), C will automatically convert them to a common type.
 The smaller data type gets promoted to a larger type. For
example:
o char → int
o int → float
o float → double
Example of Implicit Conversion:
#include <stdio.h>

int main() {
int a = 5;
float b = 3.5;

float result = a + b;
printf("Result: %f\n", result);

return 0;
}
2. Explicit Type Conversion (Type Casting):
Explicit type conversion, also known as typecasting, is when you
manually convert one data type to another by explicitly specifying the
type.
Example of Explicit Conversion:
#include <stdio.h>

int main() {
int a = 10, b = 3;

float result = (float) a / b;


printf("Result: %.2f\n", result);

return 0;
}
3. Type Conversion in Expressions
When different types of variables are used in expressions, C applies
implicit type conversion to ensure the operation is performed correctly.
General Rules:
1. Integer to Floating-point: If an int is combined with a float in an
arithmetic operation, the int is promoted to float.
2. Floating-point to Double: When performing arithmetic with float
and double, float is promoted to double.
Example :
#include <stdio.h>
int main() {
int x = 5;
float y = 2.5;
float result = x * y;
printf("Result: %.2f\n", result);

return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy