0% found this document useful (0 votes)
4 views24 pages

C Programming Basics: Syntax & I/O

The document outlines various practical assignments in C programming, focusing on basic syntax, input/output operations, variable declaration, control flow structures, looping constructs, functions, recursion, and string handling. Each practical includes objectives, descriptions of key concepts, and sample code demonstrating the implementation of these concepts. The assignments are aimed at enhancing understanding of fundamental programming principles and practices in C.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views24 pages

C Programming Basics: Syntax & I/O

The document outlines various practical assignments in C programming, focusing on basic syntax, input/output operations, variable declaration, control flow structures, looping constructs, functions, recursion, and string handling. Each practical includes objectives, descriptions of key concepts, and sample code demonstrating the implementation of these concepts. The assignments are aimed at enhancing understanding of fundamental programming principles and practices in C.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

NAME :- ROHIT SONKAR SUBJECT :- C PROGRAMMING

Practical No.: 01

Aim: Basic Syntax And I/0 Operations

(Basic syntax, input/output functions, and operators)

Description:
1. Basic Syntax and Variables :-

In C PROGRAM Variable Declaration and Initialization: Write a program that declares variables
of different data types (e.g., integer, float, character, boolean/string depending on the language)
and initializes them with values. Print the values of these variables to the console.
Comments: Include both single-line and multi-line comments in your code to explain the
purpose of different sections.

Basic Arithmetic Operations: Perform basic arithmetic operations (addition, subtraction,


multiplication, division, modulo) using numerical variables and print the results.

2. Input Output Functions :-

In C PROGRAM the basic syntax, input/output functions, and operators. This assignment is
designed to help you get hands-on experience with how C programs are structured, how to use
standard input/output functions (printf and scanf), and how to apply different types of
operators in C

3. Operators :-

a) Arithmetic Operators :-

Arithmetic operators in C are used to perform basic mathematical operations such as addition
(+), subtraction (−), multiplication (*), division (/), and modulus (%). These operators work
with numerical values and return the result of the operation. For example, if a = 10 and b =
3, then a + b gives 13, a - b gives 7, a * b gives 30, a / b gives 3 (integer division),
and a % b gives 1 (remainder). These operators are commonly used in calculator programs
and mathematical calculations.

b) Relational Operators :-

Relational operators are used to compare two values and return a Boolean result — either true
(1) or false (0). The common relational operators in C include: == (equal to), != (not equal
to), > (greater than), < (less than), >= (greater than or equal to), and <= (less than or
equal to). For example, if a = 5 and b = 8, then a < b returns 1 (true), and a == b returns
0 (false). These operators are frequently used in decision-making statements like if, while,
and loops.

F.Y. [Link]. (CS & DF) Roll Number: CD25054


NAME :- ROHIT SONKAR SUBJECT :- C PROGRAMMING

c) Logical Operators :-

Logical operators in C are used to combine multiple conditions and return a true or false
result. They are essential in building complex decision-making logic. The main logical
operators are: && (logical AND), || (logical OR), and ! (logical NOT). For example, if a =
5 and b = 10, the expression (a > 0 && b > 0) returns true because both conditions are
true. The expression (a > 10 || b == 10) also returns true because at least one condition is
true. The NOT operator ! is used to reverse a condition, e.g., !(a == b) returns true if a is
not equal to b.

CODE OF THE PROGRAM

#include <stdio.h>

int main()

int a, b;

printf("Enter two numbers: ");

scanf("%d %d", &a, &b);

printf("Addition of two numbers is : %d\n", a + b);

printf("Subtraction of two numbers is : %d\n", a - b);

printf("Multiplication of two numbers is : %d\n", a *

b); printf("Division of two numbers is : %d\n", a / b);

return 0;

F.Y. [Link]. (CS & DF) Roll Number: CD25054


NAME :- ROHIT SONKAR SUBJECT :- C PROGRAMMING

F.Y. [Link]. (CS & DF) Roll Number: CD25054


NAME :- ROHIT SONKAR SUBJECT :- C PROGRAMMING

Practical No.: 02

Aim: Variables and Datatypes

(Programs to demonstrate variable declaration, initialization, and type conversion)

Description:

In C PROGRAMMING, variables must be declared with a specific data type before they are used.
This helps the compiler allocate appropriate memory and manage data efficiently. After
declaration, variables can be initialized by assigning them values. For example, an integer can be
declared and initialized as int num = 10;. C also supports both implicit and explicit type
conversion. Implicit conversion occurs automatically when different data types are mixed in an
expression—for instance, when adding an int and a float, the int is automatically converted
to a float.

1. Variable Declaration

 Variables are declared with specific data types in C.


 In the program:
o int integer Number; — Declares an integer variable.
o float float Number; — Declares a floating-point variable.
o char letter; — Declares a character variable

2. Variable Initialization

 Assigning a value to a variable at the time of declaration.


 In the program:
o int integer Number = 10; assigns 10 to the integer variable.
o float float Number = 5.75; assigns 5.75 to the float variable.
o char letter = 'A'; assigns 'A' to the character variable.

3. Type Conversion (Type Casting)

 Done manually by the programmer to convert one type to another.


 In the expression

o floatVal (3.6) is cast to int (becomes 3).


o Then added to intVal, resulting in 13.
o The result is stored as an int.

F.Y. [Link]. (CS & DF) Roll Number: CD25054


NAME :- ROHIT SONKAR SUBJECT :- C PROGRAMMING

CODE OF THE PROGRAM

#include <stdio.h>
int main()
{
int age;
float height;
double weight;
char grade;

printf("Enter your age (integer): ");


scanf("%d", &age);

printf("Enter your height (float): ");


scanf("%f", &height);

printf("Enter your weight (double): ");


scanf("%lf", &weight);

printf("Enter your grade (single character): ");


scanf(" %c", &grade);

double sum = age + weight;


int weightInt = (int)weight;

printf("\nYou entered:\n");
printf("Age: %d\n", age);
printf("Height: %.2f\n", height);
printf("Weight: %.2lf\n", weight);
printf("Grade: %c\n", grade);

printf("\n--- Type Conversion ---\n");


printf("Sum of age and weight (implicit conversion to double): %.2lf\n", sum);
printf("Weight after explicit conversion to int: %d\n", weightInt);

return 0;

F.Y. [Link]. (CS & DF) Roll Number: CD25054


NAME :- ROHIT SONKAR SUBJECT :- C PROGRAMMING

Practical No.: 05

F.Y. [Link]. (CS & DF) Roll Number: CD25054


NAME :- ROHIT SONKAR SUBJECT :- C PROGRAMMING

Practical No.: 03

Aim: Controller flow Structures

Description:

1. What is Control Flow?

 Control flow determines the order in which instructions or statements are executed.
 It allows programs to make decisions and repeat actions based on conditions.

2. if Statement

 Executes a block of code only if a specified condition is true.


 Syntax: if (condition) {
 // code to execute if condition is true
 }

3. if-else Statement

 Executes one block of code if a condition is true, otherwise executes another block.
 Syntax: if (condition) {
 // code if true
 } else {
 // code if false
 }

4. Nested if-else

 It means placing an if-else statement inside another if or else block.


 Used when you need to check multiple conditions sequentially.
 Syntax: if (condition1) {
 // code if condition1 is true
 if (condition2) {
 // code if condition2 is true
 } else {
 // code if condition2 is false
 }
 } else {
 // code if condition1 is false
 }

F.Y. [Link]. (CS & DF) Roll Number: CD25054


NAME :- ROHIT SONKAR SUBJECT :- C PROGRAMMING

5. switch-case Statement

 Used for selecting one of many code blocks to execute based on the value of a variable.
 Syntax: switch (variable) {
 case value1:
 break;
 case value2:
 break;
 default:}

CODE OF THE PROGRAM

#include <stdio.h>
int main()
{
int number;
printf("Enter an integer: ");
scanf("%d", &number);

if (number > 0) {
printf("The number is positive.\n");
}
if (number % 2 == 0) {
printf("The number is even.\n");
} else {
printf("The number is odd.\n");
}
switch (number)
{ case 1:
printf("You entered One.\n");
break;
case 2:
printf("You entered Two.\n");
break;
case 3:
printf("You entered Three.\n");
break;
default:
printf("Number is not 1, 2, or 3.\n");
break;
}
return 0;
}

F.Y. [Link]. (CS & DF) Roll Number: CD25054


NAME :- ROHIT SONKAR SUBJECT :- C PROGRAMMING

F.Y. [Link]. (CS & DF) Roll Number: CD25054


NAME :- ROHIT SONKAR SUBJECT :- C PROGRAMMING

Practical No.: 04

Aim: Looping Constructs

(Program using for, while, do while loop to perform repetitive task)

Description:

In C PROGRAMMING, looping constructs are used to execute a block of code repeatedly,


either for a specific number of times or until a certain condition is met. There are three primary
types of loops in C: for, while, and do-while. The for loop is typically used when the
number of iterations is known in advance. It has a concise structure that includes initialization,
condition checking, and updating in a single line. The while loop is used when the number of
iterations is not known beforehand; it checks the condition before executing the code block,
and if the condition is false initially, the loop body may never run. The do-while loop is
similar to the while loop, but it ensures that the loop body is executed at least once, as the
condition is checked after the loop's body. These looping structures are essential for tasks such
as data processing, repetition, menu systems, and iterating through arrays or user inputs,
making them a fundamental part of structured programming in C.

1. What is Looping in C?

 Looping allows you to execute a block of code multiple times.


 Very useful when you need to repeat tasks, like printing values or processing arrays

2. Types of Loops in C

a) For Loop

 Used when the number of iterations is known in advance.


 Syntax: for (initialization; condition; increment/decrement) {
 // code block
 }

b) While Loop

 Used when the number of iterations is not fixed but depends on a condition.
 Syntax: while (condition) {
 // code block
 }

F.Y. [Link]. (CS & DF) Roll Number: CD25054


NAME :- ROHIT SONKAR SUBJECT :- C PROGRAMMING

c) Do While Loop

 Similar to while, but the loop is executed at least once, even if the condition is false.

 Syntax: do {

// code block

} while (condition);

CODE OF THE PROGRAM

#include <stdio.h>

int main() {
int n, i;

printf("Enter a number: ");


scanf("%d", &n);

printf("\nUsing FOR loop:\n");


for (i = 1; i <= n; i++)
printf("%d ", i);

printf("\n\nUsing WHILE loop:\n");


i = 1;
while (i <= n)
{ printf("%d ", i);
i++;
}

printf("\n\nUsing DO-WHILE loop:\n");


i = 1;
do {
printf("%d ", i); i+
+;
} while (i <= n);

return 0;
}

F.Y. [Link]. (CS & DF) Roll Number: CD25054


NAME :- ROHIT SONKAR SUBJECT :- C PROGRAMMING

F.Y. [Link]. (CS & DF) Roll Number: CD25054


NAME :- ROHIT SONKAR SUBJECT :- C PROGRAMMING

Practical No.: 05

Aim: Function and Recursion

Description:

In C programming, functions are essential building blocks that allow you to write
reusable and modular code. A function is a block of code designed to perform a
specific task, which can accept input values called parameters and return a result.
Functions help improve code organization, readability, and reduce repetition by
allowing the same code to be executed multiple times wherever needed. One
important concept related to functions is recursion, where a function calls itself to
solve a problem by breaking it down into smaller subproblems. Recursive functions
must have a base condition to stop the repeated calls; otherwise, they would lead to
infinite loops.

1. What is a Function?

 A function is a block of reusable code designed to perform a specific task.


 It improves code readability, reusability, and modularity.

2. Function Types in C

 Library Functions – Built-in functions like printf(), scanf(), etc.


 User-defined Functions – Functions created by the programmer.

3. Function Components

 Function Declaration (Prototype):


o Tells the compiler about the function's name, return type, and parameters.
o Example: int add(int a, int b);
 Function Definition:
o Actual implementation of the function.
o Example: int add(int a, int b) {
o return a + b;
o }

4. Parameters and Return Values

 Parameters allow passing data to a function.


 A function can return a single value using the return statement.
 If no value is returned, void is used.

F.Y. [Link]. (CS & DF) Roll Number: CD25054


NAME :- ROHIT SONKAR SUBJECT :- C PROGRAMMING

CODE OF THE PROGRAM

#include <stdio.h>

int add(int a, int b)


{
return a + b;
}

int factorial(int n) {
if (n == 0)
return 1;
else
return n * factorial(n - 1);
}

int main()
{ int x, y;

printf("Enter two numbers to add: ");


scanf("%d %d", &x, &y);
printf("Sum = %d\n", add(x, y));

printf("Enter a number to find factorial: ");


scanf("%d", &x);
printf("Factorial of %d is %d\n", x, factorial(x));

return 0;
}

F.Y. [Link]. (CS & DF) Roll Number: CD25054


NAME :- ROHIT SONKAR SUBJECT :- C PROGRAMMING

Practical No.: 06

Aim: String Handeling

(Implement string operations like concatenation, comparison, and length calculation


using String Function )

Description:

This C PROGRAM demonstrates basic string handling operations using the built-in functions
provided in the C standard library (<string.h>). It performs three major string operations:
concatenation, comparison, and length calculation. The program starts by taking two strings
as input from the user. Then, it calculates and displays the length of each string using the
strlen() function. It uses strcmp() to compare the two strings and display whether they are
equal, or which one comes first in lexicographical order. Finally, it uses strcat() to join
(concatenate) the second string to the first and displays the resulting string. These string
functions make it easy to manipulate character arrays in C without writing complex code
manually.

1. Purpose of the Program :

 To demonstrate basic string operations using built-in C functions


 Operations include:

a. String length calculation


b. String comparison
c. String concatenation

2. Header File Used :

 The Program uses the Header :


 # include<string.h>
 This provides functions to handle string operations.

3. String Input :

 The program takes two strings as input from the user using :
 gets() or fgets() (recommended).
 Displaying Arrays Elements

F.Y. [Link]. (CS & DF) Roll Number: CD25054


NAME :- ROHIT SONKAR SUBJECT :- C PROGRAMMING

4. String Length Calculation :

unction to find the number of characters in each string.


n(str);

5. String Comparison :

 Uses strcmp() to compare two strings:


 int result = strcmp(str1, str2);

4. String Concatenation :

 Uses strcat() to append one string to the end of another :


 strcat(str1, str2);

F.Y. [Link]. (CS & DF) Roll Number: CD25054


NAME :- ROHIT SONKAR SUBJECT :- C PROGRAMMING

CODE OF THE PROGRAM

#include <stdio.h>
#include <string.h>

int main()
{
char str1[100], str2[100];

printf("Enter first string: ");


fgets(str1, sizeof(str1), stdin);
str1[strcspn(str1, "\n")] = 0;

printf("Enter second string:


"); fgets(str2, sizeof(str2),
stdin); str2[strcspn(str2, "\
n")] = 0;

printf("\nLength of first string: %lu", strlen(str1));


printf("\nLength of second string: %lu",
strlen(str2));

int cmp = strcmp(str1,


str2); if (cmp == 0)
printf("\nThe strings are equal.");
else if (cmp < 0)
printf("\nFirst string is less than second string.");
else
printf("\nFirst string is greater than second string.");

strcat(str1, str2);
printf("\nConcatenated string: %s\n", str1);

return 0;
}

F.Y. [Link]. (CS & DF) Roll Number: CD25054


NAME :- ROHIT SONKAR SUBJECT :- C PROGRAMMING

Practical No.:

07 Aim: Nested Loop and Pattern Printing

(Program using nested loops to generate patterns and solve complex tasks)

Description:

This C PROGRAM demonstrates the use of nested loops to print patterns and solve
structured, repetitive tasks. Nested loops occur when one loop runs inside another,
which is especially useful for handling multi-dimensional data or printing grid-like
patterns. In pattern printing, the outer loop typically controls the rows, and the inner
loop controls the columns or the content of each row. Common patterns include stars
(*), numbers, alphabets, and pyramid or triangle shapes. These types of programs help
develop logic- building skills, improve understanding of loop control structures, and
teach students how to manage iteration in multiple dimensions.

1. Purpose of the Program :

 To demonstrate how nested loops work in C Program


 To generate patterns using characters, numbers, or symbols

2. What is a Nested Loop?

 A loop inside another loop


 Used when one operation (like printing a row) contains multiple repetitive
tasks (like printing columns)

3. Structure

 The outer loop controls the number of rows


 The inner loop controls what is printed on each row

4. Common Patterns Printed :

 Star patterns (*)


 Number patterns (1, 2, 3,...)
 Alphabet patterns (A, B, C,...)
 Pyramids, triangles, and inverted shapes

F.Y. [Link]. (CS & DF) Roll Number: CD25054


NAME :- ROHIT SONKAR SUBJECT :- C PROGRAMMING

CODE OF THE PROGRAM

#include <stdio.h>
int main()
{
int i, j;
printf("Pattern 1: Star Triangle\n");

for (i = 1; i <= 5; i++)


{ for (j = 1; j <= i; j++)
{ printf("* ");
}
printf("\n");
}
printf("\nPattern 2: Number Triangle\
n"); for (i = 1; i <= 5; i++) {
for (j = 1; j <= i; j++)
{ printf("%d ", j);
}
printf("\n");
}

printf("\nPattern 3: Inverted Star Triangle\n");


for (i = 5; i >= 1; i--) {
for (j = 1; j <= i; j++)
{ printf("* ");
}
printf("\n");

F.Y. [Link]. (CS & DF) Roll Number: CD25054


NAME :- ROHIT SONKAR SUBJECT :- C PROGRAMMING

F.Y. [Link]. (CS & DF) Roll Number: CD25054


NAME :- ROHIT SONKAR SUBJECT :- C PROGRAMMING

Practical No.:

08 Aim: Array Sorting and Searching

(Implement Algorithms to sort and search elements in array)

Description:

This C PROGRAM This C program demonstrates how to implement basic algorithms for
sorting and searching elements in a one-dimensional array. Sorting is the process of arranging
the elements of an array in a specific order — usually ascending or descending. This helps in
organizing data and improves the efficiency of other operations, such as searching. Common
sorting techniques include Bubble Sort, Selection Sort, and Insertion Sort. Once the array is
sorted, the program can perform searching operations to locate a specific value. Searching can
be done in two ways: Linear Search (scans each element one by one) and Binary Search (fast,
but requires sorted data). These operations form the foundation of many real-world applications
where data management and retrieval are necessary. The program helps users understand array
manipulation and the importance of algorithm efficiency..

1. Purpose of the Program :

 To sort and search elements in an array using algorithmic logic

2. What is Sorting?

 Arranging elements in a specific order (Ascending or Descending)


 Make Data easier to read, manage, and to search

3. Common Sorting Algorithms :

 Bubble Sort: Swaps adjacent elements if they are in the wrong order.

 Selection Sort: Selects the smallest/largest and places it in order.

 Insertion Sort: Builds the final sorted array one item at a time.

4. Types and Searching Techniques :

 Linear Search: Checks every element until the target is found.

 Binary Search: Divides the array in half repeatedly (only works on sorted arrays)

F.Y. [Link]. (CS & DF) Roll Number: CD25054


NAME :- ROHIT SONKAR SUBJECT :- C PROGRAMMING

CODE OF THE PROGRAM


#include <stdio.h>
int main()
{
int n, i, j, temp, key, found = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];

printf("Enter %d elements: ", n);


for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);

for(i = 0; i < n-1; i++)


{ for(j = i+1; j < n; j++)
{ if(arr[i] > arr[j]) {

temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
printf("Sorted Array: ");
for(i = 0; i < n; i++)
{ printf("%d", arr[i]);

}
printf("\nEnter element to search: ");
scanf("%d", &key);
for(i = 0; i < n; i++)
{ if(arr[i] == key)
{ found = 1;
printf("Element %d found at position %d\n", key, i+1);
break;
}
}
if(!found) {
printf("Element %d not found in the array\n", key);

}
return 0;

F.Y. [Link]. (CS & DF) Roll Number: CD25054


NAME :- ROHIT SONKAR SUBJECT :- C PROGRAMMING

F.Y. [Link]. (CS & DF) Roll Number: CD25054

You might also like