Micro c Programming
Micro c Programming
These are the fundamental data types used to store simple values like integers, characters, and
floating-point numbers.
The enum type is used to assign symbolic names to integral constants, making the program
more readable.
Example: c
Copy code
Here, RED, GREEN, and BLUE are constants with values 0, 1, and 2, respectively.
Modifier Effect
Copy code
#include <stdio.h>
#include <string.h>
int main() {
int length;
// Demonstrate strlen
length = strlen(str1);
// Demonstrate strcat
strcat(str1, str2);
// Demonstrate strcpy
strcpy(str3, str2);
return 0;
Explanation:
Example: strlen(str1) returns the number of characters in str1 (excluding the null terminator).
A function declaration specifies the function's name, return type (if applicable), and its
parameters (if any). It acts as a blueprint or prototype and informs the compiler/interpreter about
the function's existence before its actual definition.
Characteristics:1Does not contain the function's body.2Often used in languages like C or C++ for
forward declarations or when separating function implementation into different files.
2. Function Definition
A function definition provides the actual implementation or body of the function. It includes the
logic/code that will execute when the function is called.
Syntax:
return_type function_name(parameter_list) {
// Function body. }
Characteristics:1Contains the function's name, parameters, and code logic.2Must match the
declaration if the function is declared elsewhere.
3. Function Calling
Function calling is the process of invoking a function to execute the code defined in its body.
During the call, arguments can be passed to the function's parameters.
Syntax:function_name(argument_list);
Example: int result = add(5, 10); // Calls the 'add' function with arguments 5 and 10
Characteristics:1Transfers control to the function's code.2Returns control to the caller after the
function execution is completed (along with any return value, if applicable).
int main() { . int x = 5, y = 10.. // Function calling int result = add(x, y) .cout << "The sum is: " <<
result << endl; .return 0; }. .// Function definition
#include <stdio.h>
int main() {
int num, rev = 0, rem;
printf("Enter a number: ");
scanf("%d", &num);
while (num) {
rem = num % 10;
rev = rev * 10 + rem;
num /= 10;
}
#include <stdio.h>
int main() {
int num1, num2, q, r;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
q = num1 / num2;
r = num1 % num2;
#include <stdio.h>
#include <string.h>
int main() {
char source[] = "CPC is very easy programming language";
char dest[17];
#include <stdio.h>
int main() {
int myArray[5] = {1, 2, 3, 4, 5};
return 0;
}
int main() {
int myVar = 10;
printf("Value of myVar: %d", myVar);
return 0;
}
Q- Define union and structure in C programming with suitable examples.
#include <stdio.h>
union MyUnion {
int x;
float y;
char z;
};
struct MyStruct {
int a;
float b;
char c;
};
int main() {
union MyUnion myUnion;
struct MyStruct myStruct;
return 0;
}
#include <stdio.h>
int main() {
int myArray[3] = {1, 2, 3};
return 0;
}
int main() {
int matrix3x3[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int matrix2x2[2][2] = {{10, 11}, {12, 13}};
return 0;
}
Q- Write a program to create a simple calculator to perform addition, subtraction,
division, and multiplication operations.
#include <stdio.h>
int main() {
float num1, num2, result;
char op;
#include <stdio.h>
int main() {
float side = 5.0; // Replace with user input if needed
printf("Area of square: %f", calculateSquareArea(side));
return 0;
}
Q-Write a program to find the maximum number from 10 numbers entered by the user.
#include <stdio.h>
int main() {
int numbers[10];
int maxNumber;
#include <stdio.h>
int main() {
int number = 5; // Replace with user input if needed
printf("Factorial: %d", calculateFactorial(number));
return 0;
}
Q- Write a program to perform and print the addition and subtraction of two
matrices.
#include <stdio.h>
int main() {
int matrixA[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int matrixB[3][3] = {{10, 11, 12}, {13, 14, 15}, {16, 17, 18}};
return 0;
}
Q- Write the program to perform the following operations on a string, with
and without using library functions.
#include <stdio.h>
#include <string.h>
int main() {
char originalString[] = "Hello, World!";
char copiedString[17]; // One extra for the null terminator
char reversedString[17];
int i;
for (i = 0; i < length; ++i) {
reversedString[i] = originalString[length - i - 1];
}
reversedString[i] = '\0';
return 0;
}
Q-Create a structure of a student with fields such as student name, roll number, and
marks of two subjects as members. Calculate the average of the two subjects, read the
details of N students from the user, and then display the data in a formatted way.
#include <stdio.h>
struct Student {
char name[50];
int rollNumber;
float marks[2];
};
int main() {
int numStudents = 3; // Replace with user input if needed
struct Student students[numStudents];
for (int i = 0; i < numStudents; i++) {
printf("Enter Roll Number, Name, Subject 1 Marks, and Subject 2 Marks for
Student %d: ", i+1);
scanf("%d %s %f %f", &students[i].rollNumber, students[i].name,
&students[i].marks[0], &students[i].marks[1]);
}
printf("\nStudent Details:\n");
for (int i = 0; i < numStudents; i++) {
printf("Student %d\nRoll Number: %d\nName: %s\nSubject 1 Marks: %.2f\nSubject
2 Marks: %.2f\n\n",
i+1, students[i].rollNumber, students[i].name, students[i].marks[0],
students[i].marks[1]);
}
return 0;
}
Q-Define structure with a suitable example.
#include <stdio.h>
struct Point {
int x;
int y;
};
int main() {
struct Point p; // Code to use the structure...
return 0;
}
Q- Write a program in C to create a structure named "books" consisting of title, author,
subject, and book ID as its members. Read the details of five books from the user and
then display the data entered by the user on the screen.
#include <stdio.h>
struct Book {
char title[100];
char author[50];
char subject[50];
int bookID;
};
int main() {
int numBooks = 5; // Replace with user input if needed
struct Book books[numBooks]; // Display data entered by the user...
return 0;
}
union U {
int x;
float y;
char z;
};
struct S {
int a;
float b;
char c;
};
int main() {
union U u;
struct S s;
return 0;
}
Q-What is the difference between structure and union?:
1.Memory Allocation:
--Structure: Each part (like name, age, salary) has its own space in memory.
--Union: All parts share the same space in memory, but only the largest part is used.
2.Usage of Memory:
--Structure: Used when you want to keep different pieces of information together.
--Union: Used when you want to save memory and only need one piece of
information at a time.
3.Accessing Members:
--Structure: You can look at or change each part independently.
--Union: You can only look at or change one part at a time.
4.Initialization:
--Structure: You can set each part to a value separately.
--Union: You set the first part to a value, and others might be affected.
Q-Define flowchart :
Definition : A flowchart is a visual representation of the steps or actions in a process
or algorithm.
--Oval: Represents the start or end of a process.
--Rectangle: Depicts an operation or task within the program.
--Diamond: Indicates a decision point where the program takes different paths.
--Parallelogram : Represents input or output actions.
--Arrows: Show the flow and direction of the program, connecting different symbols.
Q-Define variable. Define constant. Give the difference between variable and constant
--Variable: A variable is a labeled container in the computer's memory that can hold
different values during program execution.
--Constant : A constant is a fixed value that remains unchanged throughout the
program.
--Difference The key distinction lies in variability – variables can change their values,
allowing for dynamic data storage, while constants provide stability to specific
program elements.
Q-Define function
--Definition: In C, a function is a modular block of code designed to perform a specific
task.
--Modularity: Functions promote code modularity, allowing for the reuse of code in
different parts of a program.
--Code Organization :They contribute to code organization by breaking down
complex tasks into manageable and reusable units.
Q-Explain function signature :
--Definition: A function signature includes the function's name, return type, and
parameter types.
--Function Name : It identifies the function and distinguishes it from others.
--Return Type : Specifies the type of value the function returns.
--Parameter Types : Outline the data types of the values the function expects as input,
providing information about the function's interface.
Q-What is an algorithm? :
--Definition : An algorithm is a systematic, step-by-step approach to solving a problem
or accomplishing a task.
--Problem-Solving Blueprint : It serves as a blueprint for writing code, ensuring a logical
and efficient solution to a given computational problem.
Q-Define variable:
--Definition : A variable is a labeled container in the computer's memory that can hold
different values during program execution.
--Dynamic Data Storage: Variables allow for dynamic data storage, enabling flexibility in
the manipulation of values.
--Key Role: They play a crucial role in programming by facilitating the storage and
manipulation of data within a program.
Syntax:
data_type array_name[array_size];
--Switch:
Syntax:
switch (expression) {
case constant_value_1:
break;
case constant_value_2:
break;
default:
--Function:
Syntax:
return_type function_name(parameters) {
return value; // optional, depending on the return type
}
Q-Define errors and explain various types of errors in C programming (5 marks):
Errors in C programming refer to mistakes that can occur during the development
or execution of a program.
1.Syntax Errors- These errors involve grammatical mistakes that prevent the
compiler from translating the code into machine language.
2.Semantic Errors- While not breaking syntax, semantic errors lead to incorrect
program behavior due to logical issues.
3.Runtime Errors- Occurring during program execution, runtime errors are often
caused by actions like dividing by zero or accessing data beyond defined limits.
Q-Write a short note on tokens in language (5 marks):
--In C, tokens are the basic building blocks of your code. They are similar to words
in a sentence, each having a specific meaning and purpose. The compiler reads
your C code and breaks it down into these essential elements to understand the
structure and functionality of your program.
--Types of tokens:
--Keywords: These are reserved words with predefined meanings in C, like int, if,
for, etc. You cannot use them as variable names.
--Identifiers: These are user-defined names for variables, functions, structures, etc.
They must follow naming rules (letters, numbers, underscore).
--Constants: Fixed values like numbers, characters, or strings. They cannot be
changed during program execution.
--Operators: Symbols that perform operations like addition (+), subtraction (-),
multiplication (*), etc.
--Special symbols: Punctuation marks like semi-colons (;) and commas (,) that
separate statements and elements.
--Strings: Sequences of characters enclosed in double quotes ("").Explain three
types of operators, along with their precedence and associativity (6 marks):
In C programming, errors are primarily categorized based on when and how they occur. Let’s dive
into the details of different types of errors:
Definition: Occur when the programmer violates the grammatical rules of the C programming
language.When: Detected during compilation.Examples:
Missing semicolon:
C. Copy code
esolution: These errors are identified by the compiler, which provides error messages to help the
programmer locate and fix
2. Semantic Errors Definition: Occur when the statements are syntactically correct but do not
make logical sense or do not produce the expected result.When: May not be directly identified by
the compiler.
Copy code
c. Copy code
int result = a / 0; Resolution: Requires debugging and understanding the logic of the program.
3. Logical Errors
Definition: Errors in the logic or algorithm of the program that lead to incorrect results despite the
program running without crashing.
Copy code
4. Runtime Errors Definition: Errors that occur while the program is running, often due to invalid
operations or resource limitations.When: Detected only during program execution.
c Copy code
int arr[5];
c Copy code
int *ptr = NULL;