Control flow structures in C, such as if, else, and switch,
171 | allow you to make decisions in your program based on conditions.
172 |
173 | if-else statement:
174 | The if-else statement in C is a conditional statement that allows a program to execute
175 | different blocks of code based on a specified condition. The syntax for the if-else statement is as follows:
176 | Syntax:
177 | if (condition) {
178 | // code to be executed if the condition is true
179 | }
180 | else {
181 | // code to be executed if the condition is false
182 | }
183 |
184 |
185 | switch statement:
186 |
187 | The switch statement in C is a control statement that allows a variable to be tested for equality against
188 | a list of values. Here's the basic syntax of a switch statement:
189 | Syntax:
190 | switch (expression) {
191 | case 1:
192 | // code to be executed if expression is equal to constant1
193 | break;
194 |
195 | case 2:
196 | // code to be executed if expression is equal to constant2
197 | break;
198 |
199 | // more cases can be added as needed
200 |
201 | default:
202 | // code to be executed if expression doesn't match any of the constants
203 | }
204 |
205 |
206 |
207 |
208 |
209 |
210 |
If-else:
211 | #include <stdio.h >
212 |
213 | int main() {
214 | int num;
215 | printf("Enter an integer: ");
216 | scanf("%d", &num);
217 |
218 | // true if num is perfectly divisible by 2
219 | if(num % 2 == 0)
220 | printf("%d is even.", num);
221 | else
222 | printf("%d is odd.", num);
223 |
224 | return 0;
225 | }
226 |
Loops, including for, while, and do-while, enable you to repeat a block of code multiple times based on a specified condition.
274 | Loops are fundamental for controlling the flow of a program and are commonly used for tasks like iterating through arrays, processing data,
275 | and implementing repetitive tasks. Each type of loop has its own use cases, and the choice of loop depends on the specific
276 | requirements of the task at hand.
277 | for loop:
278 |
279 | The for loop is used when the number of iterations is known in advance.
280 | It consists of three parts: initialization, condition, and update.
281 | Initialization is executed once at the beginning, the condition is checked before each iteration, and the update is executed after each iteration.
282 |
283 | while loop:
284 |
285 | The while loop is used when the number of iterations is not known in advance.
286 | It continues to execute the loop body as long as the specified condition is true.
287 | The condition is checked before entering the loop, and if it is false initially, the loop body may not execute at all.
288 |
289 | do-while loop:
290 |
291 | The do-while loop is similar to the while loop but guarantees that the loop body is executed at least once.
292 | It first executes the loop body and then checks the condition.
293 | If the condition is true, it continues to execute the loop; otherwise, it exits
294 |
295 |
296 |
297 |
298 |
Loops:
299 | #include <stdio.h >
300 | int main()
301 | {
302 | int i = 0;
303 |
304 | for (i = 1; i <= 10; i++) //it is for loop
305 | {
306 | printf( "Hello World\n");
307 | }
308 | return 0;
309 | }
310 |
for Loops:
311 | #include <stdio.h >
312 |
313 | int main() {
314 | int i = 1;
315 | while (i <= 5) {
316 | printf("%d ", i);
317 | i++;
318 | }
319 | return 0;
320 | }
321 |
do-while:
322 | #include <stdio.h >
323 |
324 | int main() {
325 | int i = 1;
326 | do {
327 | printf("%d ", i);
328 | i++;
329 | } while (i <= 5);
330 | return 0;
331 | }
332 |
333 |
334 |
335 |
336 |
337 |
338 |
Functions
339 |
Functions in C allow you to break down your program into modular and reusable pieces of code.
340 | Functions in C contribute to code organization, reusability, and readability. They are essential for breaking
341 | down complex tasks into smaller, manageable units, making the code more modular and easier to understand.
342 |
343 |
344 |
345 |
Functions:
346 | // C program to show function
347 | // call and definition
348 |
#include <stdio.h >
349 |
350 | // Function that takes two parameters
351 | // a and b as inputs and returns
352 | // their sum
353 | int sum(int a, int b)
354 | {
355 | return a + b;
356 | }
357 | // Driver code
358 |
int main()
359 | {
360 | // Calling sum function and
361 | // storing its value in add variable
Arrays and pointers are crucial concepts for handling collections of data in C.
373 |
374 | 1. Arrays:
375 | - An array is a collection of elements of the same data type stored in contiguous memory locations.
376 | - Elements in an array are accessed using an index, starting from 0 for the first element.
377 | - Arrays provide a convenient way to store and manipulate a fixed-size sequence of values.
378 |
379 | 2. Pointers:
380 | - A pointer is a variable that stores the memory address of another variable.
381 | - Pointers are used to work with dynamic memory allocation, arrays, and to create more flexible functions.
382 | - The dereference operator (*) is used to access the value at the memory address stored in a pointer.
383 |
384 | 3. Array Pointers:
385 | - Arrays and pointers are closely related in C. The name of an array can be considered as a pointer to the first element of the array.
386 | - Pointer arithmetic allows you to navigate through array elements using pointer operations.
387 |
388 | 4. Pointer Arithmetic:
389 | - Pointer arithmetic involves adding or subtracting an integer from a pointer, which adjusts the pointer's address based on the size of the data type.
390 | - For example, if ptr is a pointer to an integer, ptr + 1 points to the next integer in memory.
391 |
392 | 5. Dynamic Memory Allocation:
393 | - Pointers are commonly used with functions like malloc, calloc, and realloc to dynamically allocate memory at runtime.
394 | - Dynamic memory allocation allows you to create data structures with a variable size.
395 |
396 | 6. Null Pointers:
397 | - A null pointer is a pointer that does not point to any memory location. It is represented by the value NULL in C.
398 | - Null pointers are used to indicate that a pointer does not currently point to a valid object.
399 |
400 | 7. Pointer to Functions:
401 | - Pointers can also point to functions, allowing you to create more flexible and generic code.
402 | - This is particularly useful for implementing callback functions and function pointers.
403 |
404 |
405 |
406 |
407 |
Arrays:
408 | // Program to take 5 values from the user and store them in an array
409 | // Print the elements stored in the array
410 |
411 | #include <stdio.h >
412 |
413 | int main() {
414 |
415 | int values[5];
416 |
417 | printf("Enter 5 integers: ");
418 |
419 | // taking input and storing it in an array
420 | for(int i = 0; i < 5; ++i) {
421 | scanf("%d", &values[i]);
422 | }
423 |
424 | printf("Displaying integers: ");
425 |
426 | // printing elements of an array
427 | for(int i = 0; i < 5; ++i) {
428 | printf("%d\n", values[i]);
429 | }
430 | return 0;
431 | }
432 |
433 |
Pointers:
434 | // C program to illustrate Pointers
435 | #include <stdio.h >
436 |
437 | void geeks()
438 | {
439 | int var = 10;
440 |
441 | // declare pointer variable
442 | int* ptr;
443 |
444 | // note that data type of ptr and var must be same
445 | ptr = &var;
446 |
447 | // assign the address of a variable to a pointer
448 | printf("Value at ptr = %p \n", ptr);
449 | printf("Value at var = %d \n", var);
450 | printf("Value at *ptr = %d \n", *ptr);
451 | }
452 |
453 | // Driver program
454 | int main()
455 | {
456 | geeks();
457 | return 0;
458 | }
459 |
460 |
461 |
462 |
463 |
464 |
465 |
Structures and Unions
466 |
Structures and unions enable you to create complex data types by grouping different variables together.
467 |
In this the structure and unions are same only the keywords are different
468 | A structure is a composite data type that groups together variables of different data types under a single name.
469 | Each variable within a structure is called a member or field.
470 | Structures allow you to create complex data structures to represent real-world entities with multiple attributes.
471 | Declaration of Structures:
472 |
473 | To define a structure, you use the struct keyword followed by the structure name and a list of members enclosed in curly braces.
474 |
475 | Unions:
476 |
477 | A union is a special data type that allows you to store different data types in the same memory location.
478 | Unlike structures, where all members have their own memory space, in a union, all members share the same memory.
479 | Unions are useful when you need to represent a value that can be of different types at different times.
480 | Declaration of Unions:
481 |
482 | Similar to structures, you use the union keyword to define a union, followed by the union name and a list of members enclosed in curly braces.
483 |
484 |
485 |
486 |
487 |
604 | // Program to take 5 values from the user and store them in an array
605 | // Print the elements stored in the array
606 |
607 | #include <stdio.h >
608 |
609 | int main() {
610 |
611 | int values[5];
612 |
613 | printf("Enter 5 integers: ");
614 |
615 | // taking input and storing it in an array
616 | for(int i = 0; i < 5; ++i) {
617 | scanf("%d", &values[i]);
618 | }
619 |
620 | printf("Displaying integers: ");
621 |
622 | // printing elements of an array
623 | for(int i = 0; i < 5; ++i) {
624 | printf("%d\n", values[i]);
625 | }
626 | return 0;
627 | }
628 |
629 |
Pointers:
630 | // C program to illustrate Pointers
631 | #include <stdio.h >
632 |
633 | void geeks()
634 | {
635 | int var = 10;
636 |
637 | // declare pointer variable
638 | int* ptr;
639 |
640 | // note that data type of ptr and var must be same
641 | ptr = &var;
642 |
643 | // assign the address of a variable to a pointer
644 | printf("Value at ptr = %p \n", ptr);
645 | printf("Value at var = %d \n", var);
646 | printf("Value at *ptr = %d \n", *ptr);
647 | }
648 |
649 | // Driver program
650 | int main()
651 | {
652 | geeks();
653 | return 0;
654 | }
655 |
656 |
1. What is the difference between malloc and calloc?
801 | Ans: malloc and calloc are both functions in the C programming language that are used for dynamic
802 | memory allocation, but they have some key differences.
803 |
804 |
805 |
2.Explain the concepts of pass by value and pass by reference in C.
806 | Ans: In C, when you pass arguments to a function, the method by which these arguments are
807 | passed can be categorized into two main concepts: pass by value and pass by reference.
808 |
809 |
3.How are arrays and pointers related in C?
810 | Ans:
811 | In C, arrays and pointers are closely related, and understanding their relationship is fundamental
812 | to working with C programming. Here are some key points to understand the relationship between arrays and pointers
813 |
814 |
815 |
816 |
4.What is the purpose of the const keyword in C?
817 | Ans:
818 | In the C programming language, the const keyword is used to declare constants and to indicate that a variable's
819 | value should not be modified after it has been initialized. It can be applied to variables, pointers, and function parameters.
820 |
821 |
822 |
5.How does the sizeof operator work in C?
823 | Ans:
824 | The volatile keyword is used to indicate that a variable may be changed at
825 | any time outside the current code, preventing the compiler from optimizing it.
826 | In C, the sizeof operator is used to determine the size, in bytes, of a variable or a data type.
827 |
828 |
6. What is the purpose of the volatile keyword in C?
829 | Ans:
830 | In C, the volatile keyword is used to indicate that a variable's value might change at any time without any action being taken by the code.
831 | It tells the compiler not to optimize or cache the value of the variable because it can be modified externally.
832 |
833 |
7.Explain the difference between ++i and i++ in C.
834 | Ans:
835 | In C, both ++i and i++ are increment operators. ++i is the pre-increment operator,
836 | which increments the value of
837 | i and then uses the updated value in the expression.
838 | i++ is the post-increment operator, which uses the original value of i in the expression and then increments it.
839 |
840 |
8.What are the differences between structures and unions in C?
841 | Ans:
842 | Structures (struct):
843 |
844 | Members have their individual memory spaces.
845 | Each member holds different information.
846 | Size is the sum of member sizes.
847 | All members can be accessed independently.
848 | Used for storing multiple types of data simultaneously.
849 | Unions (union):
850 |
851 | Members share the same memory space.
852 | Only one member is active at a time.
853 | Size is determined by the largest member.
854 | Modifying one member affects others.
855 | Ideal when only one type of data needs storage or access at any given time.
856 |
857 |
9.How do you use enum in C to define constants?
858 | Ans:
859 | In C, you can use enum (enumeration) to define a set of named integer constants, providing a way to create symbolic names for integral values.
860 |
861 |
862 |
10.What is a pointer to a function in C? Provide an example.
863 | Ans:
864 | In C, a pointer to a function is a variable that stores the memory address of a function. It allows you to call a function indirectly
865 | through the pointer, providing flexibility in function invocation.
866 | The syntax for declaring a function pointer involves specifying the return type and parameter types of the function it points to
867 |
868 |
11.Explain the purpose of the #include directive in C.
869 | Ans:
870 | The #include directive aids in creating modular and maintainable code by allowing developers to
871 | organize their code into separate files and reuse components across different parts of a program.
872 |
873 |
12.What is the role of the static keyword in C?
874 | Ans:
875 | the static keyword in C can be used to control the scope and lifetime of variables and functions,
876 | both within functions and at the global level.
877 |
878 |
13.How does memory management work in C? Explain the use of free() function.
879 | Ans:
880 | Memory management in C involves the allocation and deallocation of memory during program execution.
881 | C provides several functions for dynamic memory allocation,
882 | and the free() function is used for deallocating memory that was previously allocated by functions like malloc(), calloc(), or realloc().
883 |
884 |
14.What is a preprocessor in C? Explain some commonly used preprocessor directives.
885 | Ans:
886 | In C, the preprocessor is a tool that performs text manipulations on the source code before it is compiled.
887 | It handles directives, which are commands beginning with the # symbol.
888 |
889 |
15.How do you implement file I/O operations in C?
890 | Ans:
891 | Input/output (I/O) operations in C involve reading data from standard input (keyboard) or
892 | other sources,
893 | and writing data to standard output (console) or other destinations like files.
894 |
895 |