C Programming Note For Class Test
C Programming Note For Class Test
C Programming Note For Class Test
#include <stdio.h>
int main() {
int numbers[5];
int sum = 0;
int max, min;
// Displaying results
printf("Sum: %d\n", sum);
printf("Maximum: %d\n", max);
printf("Minimum: %d\n", min);
return 0;
}
Display the series 1, 4, 9, 16...up to 10th position by using c programming.
#include <stdio.h>
int main() {
int n = 10; // Number of terms
int i;
return 0;
}
Write a program which, accepts number until press zero and display only the odd numbers
among them
#include <stdio.h>
int main() {
int number;
int numbers[100]; // Array to store the numbers
int count = 0; // Count of numbers entered
return 0;
}
Write a program by using c programming which, accepts number until press zero and display
only the even numbers among them
#include <stdio.h>
int main() {
int number;
int evenNumbers[100]; // Array to store even numbers
int count = 0; // Counter for even numbers
while (1) {
scanf("%d", &number);
if (number == 0) {
break;
}
if (number % 2 == 0) {
evenNumbers[count] = number;
count++;
}
}
printf("Even numbers:\n");
for (int i = 0; i < count; i++) {
printf("%d\n", evenNumbers[i]);
}
return 0;
}
Write a program that will take input as your name and display the following statement "Hello
Mr......." by using c programming
#include <stdio.h>
int main() {
char name[100];
return 0;
}
"int main (int)", from the quoted statement , what do you mean by "int" in both places?
The first int specifies the return type of the main function. In C, the main function is
expected to return an integer value. This integer value is typically used to indicate the
success or failure of the program to the operating system.
A return value of 0 usually indicates successful execution, while any other value
indicates an error or abnormal termination.
Example:
int main() {
// Program code here
return 0; // Indicate successful execution
}
The second int appears as part of the function's parameter list and represents an integer
parameter named argc.
argc stands for "argument count" and indicates the number of command‐line arguments
passed to the program, including the program's name.
argv is an array of character pointers (strings) representing the actual command‐line
arguments.
Example:
Write down the history of the programming language C. What are the application areas of
language C?
C programming language was created by Dennis Ritchie at the Bell Laboratories in 1972. It was
designed as a general‐purpose, high‐level programming language to write the UNIX operating
system. C evolved from an earlier programming language called B, which was also developed at
Bell Labs. The development of C was influenced by the limitations of earlier languages and the
growing need for a more versatile and efficient language. C gained popularity due to its efficiency,
flexibility, and portability. It became the preferred language for system programming, embedded
systems, and other applications requiring direct access to hardware and low‐level functionality.
1. System Programming:
o Operating Systems: C is extensively used in developing operating systems,
including UNIX, Linux, and Windows.
o Embedded Systems: C is widely used in programming embedded systems due to
its close‐to‐hardware operations and efficiency.
2. Compiler Design:
o Many compilers for other programming languages are written in C, leveraging its
performance and control over system resources.
3. General‐Purpose Applications:
o Software Development: C is used for developing general‐purpose software,
including databases, word processors, and spreadsheets.
o Games: C is used in game development, particularly for performance‐critical
parts of games.
4. Networking:
o Network Devices: C is used to program network devices and write network
protocols.
o Communication Protocols: Many Internet protocols and socket programming
libraries are implemented in C.
5. Hardware Interfaces:
o Device Drivers: C is commonly used to write device drivers due to its ability to
manipulate hardware directly.
o Hardware Programming: C is used in the development of firmware and
hardware control software.
6. Embedded Systems:
o C is the language of choice for developing embedded systems software, such as
microcontroller programming, real‐time systems, and small‐footprint
applications.
7. Academic and Research:
o C is widely taught in computer science courses as a fundamental programming
language.
o Researchers use C for performance‐critical applications and simulations.
8. Graphics and Game Development:
o Graphics Libraries: C is used in the development of graphics libraries like OpenGL
and DirectX.
o Game Engines: Parts of game engines are often written in C for optimized
performance.
9. High‐Performance Computing:
o C is used in applications requiring high performance, such as scientific
simulations, numerical methods, and real‐time systems.
10. Database Systems:
o Many database management systems (DBMS) like MySQL and PostgreSQL are
written in C.
C's efficiency, control over system resources, and widespread adoption make it a versatile
language with applications spanning a wide range of domains.
Difference between SWITCH CASE over IF‐ELSE? Explain with necessary example?
The if‐else statement is used to choose between two options, but the switch case
statement is used to choose between numerous options.
If the condition inside the if block is false, the statement inside the else block is
executed. If the condition inside the switch statement is false, the default statements
are run.
If‐else values are determined by constraints, whereas switch case values are determined
by user preferences.
It's difficult to make changes to if‐else statements because it's time‐consuming to figure
out where the change needs to be made. Switch statements, on the other hand, are
simple to change since they are easy to trace.
For numerous statements, you can use several if statements. For numerous statements
in Switch, you only have one expression.
Switch-Case Statement
The switch-case statement is useful when you have a single expression (typically an integer or
character) that you want to compare against multiple constant values. It provides a clear and
concise way to handle multiple branches of execution based on the value of a single variable.
If-Else Statement
The if-else statement is a more general-purpose conditional statement that allows you to
evaluate any boolean condition. It is versatile and can handle complex conditions involving
logical operators (&&, ||, !) and relational operators (<, >, ==, !=).
In C programming, the continue statement is used within loops (like for, while, or do‐while) to
immediately skip the remaining iterations of the current loop iteration and proceed to the next
iteration. It effectively "continues" with the next iteration of the loop without executing the
remaining code in the current iteration.
The continue statement is commonly used with if conditions within loops but can also
be used with other conditional constructs.
It is important to ensure that the condition that triggers continue eventually allows the
loop to proceed to the next iteration; otherwise, it could lead to infinite loops.
The continue statement is specific to loops (for, while, do-while) and cannot be used
outside of them.
It helps to streamline code by skipping unnecessary computations or actions within a loop
iteration based on a condition.
After continue is executed, control immediately jumps to the next iteration of the loop,
skipping any remaining code in the current iteration.
int main() {
// Print even numbers from 1 to 10 using a loop
for(int i = 1; i <= 10; i++) {
// Check if i is odd; if true, skip to the next iteration
if(i % 2 != 0) {
continue; // Skip the rest of the loop body for odd numbers
}
// Print the even number
printf("%d\n", i);
}
return 0;
}
1. Loop Setup:
o The for loop is initialized with i = 1 and continues as long as i is less than or
equal to 10 (i <= 10).
o i++ increments i by 1 after each iteration.
2. Condition Check (if Statement):
o Within the loop, there's an if statement that checks if i is odd (i % 2 != 0).
o If i is odd, the continue statement is executed.
3. Effect of continue:
o When i is odd, the continue statement skips the remaining code inside the loop
(printf("%d\n", i);) and immediately jumps to the next iteration of the loop.
4. Output:
o As a result, only even numbers (2, 4, 6, 8, 10) are printed because odd
numbers (1, 3, 5, 7, 9) trigger the continue statement.
In summary, the continue statement in C programming allows you to control the flow of
execution within loops by skipping the remaining code in the current iteration and moving to
the next iteration of the loop.
“Do while loop will execute at least once” justify the statement with necessary example
The statement "Do while loop will execute at least once" is true because of its structure: the loop
body is executed first, and then the condition is checked. This guarantees that the loop body will
execute at least once, regardless of the initial condition.
int main() {
int i = 1;
return 0;
}
Key Points:
The do-while loop guarantees that the loop body executes at least once, even if the
condition initially evaluates to false.
It is useful when you need to execute a block of code at least once and then decide
whether to repeat based on a condition.
The condition is checked after the first execution of the loop body. If the condition is
true, the loop continues; otherwise, it exits.
In summary, the do-while loop structure ensures that the loop body executes at least once before
checking the loop condition. This makes it distinct from other loop constructs like while and
for, which check the condition before entering the loop body.