0% found this document useful (0 votes)
64 views7 pages

Unit - I: For (Initialization Condition Increment/decrement) (//code)

The document provides code snippets and explanations for various C programming concepts across 5 units. These include loops, functions, arrays, pointers, structures, unions, strings and more. Each concept is accompanied by an example code snippet and an explanation of the output.

Uploaded by

Sujithra sai
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)
64 views7 pages

Unit - I: For (Initialization Condition Increment/decrement) (//code)

The document provides code snippets and explanations for various C programming concepts across 5 units. These include loops, functions, arrays, pointers, structures, unions, strings and more. Each concept is accompanied by an example code snippet and an explanation of the output.

Uploaded by

Sujithra sai
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/ 7

UNIT - I

QA101: Compare the for, while, and do-while loops.

For Loop: Executes a sequence of statements multiple times and abbreviates the
code that manages the loop variable. Syntax: for(initialization; condition;
increment/decrement) { //code }

While Loop: Repeats a statement or group of statements while a given condition is


true. Syntax: while(condition) { //code }
Do-While Loop: Similar to a while loop, except that it is guaranteed to execute at
least one time. Syntax: do { //code } while(condition);

QA102: Write a program to initialize the value as 7680 & display the value on the
monitor.

c Copy code

#include <stdio.h> int main() { int value = 7680; printf("Value: %d\n",


value); return 0; }

QA103: Illustrate the C Programming Structure.

A typical C program structure includes:

1. Preprocessor Directives: #include<stdio.h>


2. Global Declarations: Variables and constants.
3. Main Function: int main() { //code }
4. User-defined Functions: Custom functions defined by the user.

QA104: Produce the output for the following C program

c Copy code

#include<stdio.h> int main() { int i = 0; for(;;) { printf("%d", i); return 0;


} }

Output Explanation:
The program will print 0 and then terminate because the return 0; statement is inside the
infinite loop, but it executes immediately after the first iteration.
QA105: Write a C program to find area and circumference of circle.

c Copy code

#include <stdio.h> #define PI 3.14159 int main() { float radius, area,


circumference; printf("Enter the radius of the circle: "); scanf("%f",
&radius); area = PI * radius * radius; circumference = 2 * PI * radius;
printf("Area: %.2f\n", area); printf("Circumference: %.2f\n", circumference);
return 0; }

UNIT - II
QA201: Discuss the types of arguments passing in C.

Pass by Value: A copy of the actual parameter is passed to the function.


Pass by Reference: The actual parameter's reference (address) is passed to the
function, allowing direct modification of the original variable.

QA202: Compare the pointer and pointer to pointer variable with suitable example.

Pointer: Holds the address of a variable. Example: int *p;


Pointer to Pointer: Holds the address of a pointer. Example: int **pp;

Example:

c Copy code

#include <stdio.h> int main() { int var = 10; int *p = &var; int **pp = &p;
printf("Value of var: %d\n", var); printf("Address of var: %p\n",
(void*)&var); printf("Value of p (Address of var): %p\n", (void*)p);
printf("Value at *p (Value of var): %d\n", *p); printf("Value of pp (Address
of p): %p\n", (void*)pp); printf("Value at *pp (Value of p): %p\n",
(void*)*pp); printf("Value at **pp (Value of var): %d\n", **pp); return 0; }

QA203: Write a C program to print the address of variable, pointer and pointer value.
(Assume “i” is variable and “p” is pointer )

c Copy code

#include <stdio.h> int main() { int i = 42; int *p = &i; printf("Address of


variable i: %p\n", (void*)&i); printf("Address of pointer p: %p\n",
(void*)&p); printf("Pointer p holds address: %p\n", (void*)p); printf("Value
pointed to by p: %d\n", *p); return 0; }

QA204: Write a C program to find greatest among three numbers using functions
(Without argument and with return type)

c Copy code

#include <stdio.h> int greatest() { int a, b, c; printf("Enter three numbers:


"); scanf("%d %d %d", &a, &b, &c); if(a > b && a > c) return a; else if(b > c)
return b; else return c; } int main() { int result = greatest(); printf("The
greatest number is: %d\n", result); return 0; }

QA205: Write a C program to multiply two numbers using pointers.

c Copy code

#include <stdio.h> int main() { int a, b, result; int *p1, *p2; printf("Enter
two integers: "); scanf("%d %d", &a, &b); p1 = &a; p2 = &b; result = (*p1) *
(*p2); printf("Multiplication result: %d\n", result); return 0; }

UNIT - III
QA301: Illustrate the Array and how to initialize an array?

Array: A collection of elements of the same type stored in contiguous memory


locations.
Initialization: int array[5] = {1, 2, 3, 4, 5};

QA302: Determine 3-Dimensional Array and indexing with neat diagram.

3-Dimensional Array: An array of arrays where each element is another array.

Example:

c Copy code

int arr[2][3][4] = { { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} }, { {13,
14, 15, 16}, {17, 18, 19, 20}, {21, 22, 23, 24} } };

Diagram:

css Copy code

arr[0][0][0] = 1, arr[0][0][1] = 2, ..., arr[0][0][3] = 4 arr[0][1][0] = 5,


..., arr[0][2][3] = 12 arr[1][0][0] = 13, ..., arr[1][2][3] = 24

QA303: Predict the output for the following C program

c Copy code

#include <stdio.h> int main() { int array[5]= {10,30,50,70,80}; int i =


0,n=5,ele=30,j=0; while( j < n) { if( array[j] == ele ) { break; } j = j + 1;
} printf("Found element %d at position %d\n", ele, j+1); return 0; }

Output Explanation:
The program searches for the element 30 in the array and prints its position, which is 2 (1-
based index).

QA304: Create a C program to read n elements as input and print the sum of all
elements of the array (integer).

c Copy code

#include <stdio.h> int main() { int n, i, sum = 0; printf("Enter the number of


elements: "); scanf("%d", &n); int array[n]; printf("Enter the elements: ");
for(i = 0; i < n; i++) { scanf("%d", &array[i]); sum += array[i]; }
printf("Sum of all elements: %d\n", sum); return 0; }

QA305: List the differences between searching and sorting techniques.

Searching: Finding a specific element in a dataset. Examples: Linear Search, Binary


Search.
Sorting: Arranging elements in a particular order (ascending/descending). Examples:
Bubble Sort, Quick Sort.
UNIT - IV
QA401: Define structure with an example.

Structure: A user-defined data type that groups related variables of different data
types.

Example:

c Copy code

#include <stdio.h> struct Student { char name[50]; int age; float marks; };
int main() { struct Student s1 = {"John", 20, 85.5}; printf("Name: %s\n",
s1.name); printf("Age: %d\n", s1.age); printf("Marks: %.2f\n", s1.marks);
return 0; }

QA402: Produce the output for the following program

c Copy code

#include<stdio.h> int main() { struct saveetha { char *ptr; int born; };


struct saveetha l1 = {"nagu", 1981}; printf("%s %d", l1.ptr, l1.born); return
0; }

Output Explanation:
The program initializes a structure with a string and an integer and prints: nagu 1981.

QA403: Define self-referenced structure with an example.

Self-referenced Structure: A structure that includes a pointer to an instance of the


same structure.

Example:

c Copy code

#include <stdio.h> struct Node { int data; struct Node *next; }; int main() {
struct Node node1; node1.data = 10; node1.next = NULL; printf("Data: %d\n",
node1.data); return 0; }

QA404: Predict the output for the following program


c Copy code

#include<stdio.h> int main() { union test { int i; int j; }; union test var =
{10}; printf("%d,%d\n", var.i, var.j); return 0; }

Output Explanation:
The program initializes the union with a value of 10 and prints: 10,10.

QA405: How can you represent a linked list node in C program?

A linked list node can be represented as:

c Copy code

struct Node { int data; struct Node *next; };

UNIT - V
QA501: Write a program to find sum of two fraction values

c Copy code

#include <stdio.h> struct Fraction { int numerator; int denominator; }; int


main() { struct Fraction f1, f2, result; printf("Enter numerator and
denominator of first fraction: "); scanf("%d %d", &f1.numerator,
&f1.denominator); printf("Enter numerator and denominator of second fraction:
"); scanf("%d %d", &f2.numerator, &f2.denominator); result.numerator =
f1.numerator * f2.denominator + f2.numerator * f1.denominator;
result.denominator = f1.denominator * f2.denominator; printf("Sum of
fractions: %d/%d\n", result.numerator, result.denominator); return 0; }

QA502: Write a C program to find the length of the string “saveetha”.

c Copy code

#include <stdio.h> #include <string.h> int main() { char str[] = "saveetha";


printf("Length of the string: %lu\n", strlen(str)); return 0; }

QA503: Define Conditional Operator with an example?


Conditional Operator: Also known as the ternary operator, it takes three operands.
Syntax: condition ? expression1 : expression2;

Example:

c Copy code

#include <stdio.h> int main() { int a = 10, b = 20; int max = (a > b) ? a : b;
printf("Max value is: %d\n", max); return 0; }

QA504: Write a C program to concatenate two strings without using strcat() using while
loop.

c Copy code

#include <stdio.h> int main() { char str1[100], str2[50]; int i = 0, j = 0;


printf("Enter first string: "); gets(str1); printf("Enter second string: ");
gets(str2); while(str1[i] != '\0') { i++; } while(str2[j] != '\0') { str1[i] =
str2[j]; i++; j++; } str1[i] = '\0'; printf("Concatenated string: %s\n",
str1); return 0; }

QA505: Write a C program to find the ASCII value of &.

c Copy code

#include <stdio.h> int main() { char ch = '&'; printf("The ASCII value of %c


is %d\n", ch, ch); return 0; }

This should cover the provided questions for the units. Let me know if you need further
assistance!

You might also like