Explain strcmp() function in detail. Write a C program that demonstrate the use of printf() and scanf() function.Explain declaring and initialization of an array. Explain static and register variables. Write a C program to find the smallest number stored in an array.
Question
Explain strcmp() function in detail. Write a C program that demonstrate the use of printf() and scanf() function.Explain declaring and initialization of an array. Explain static and register variables. Write a C program to find the smallest number stored in an array.
Solution
-
The strcmp() function: The strcmp() function is a built-in function in C and is declared in the string.h header file. It is used to compare two strings and returns an integer value. If the first string is greater than the second string, it returns a positive value. If the first string is less than the second string, it returns a negative value. If both strings are equal, strcmp() returns 0.
-
C program demonstrating the use of printf() and scanf() function:
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("You entered: %d", num);
return 0;
}
In this program, printf() is used to print the message "Enter a number: " on the console. scanf() is used to read the input from the user.
-
Declaring and initializing an array: In C, an array is a collection of elements of the same type. You can declare an array by specifying its type, followed by its name and size in square brackets. For example,
int numbers[5];declares an array of integers named numbers with a size of 5. You can initialize an array at the time of declaration. For example,int numbers[] = {1, 2, 3, 4, 5};initializes an array with the given values. -
Static and register variables: Static variables in C have a property of preserving their value even after they are out of their scope. They are initialized only once and exist until the termination of the program. Register variables are stored in the CPU registers instead of RAM to have faster access.
-
C program to find the smallest number stored in an array:
#include <stdio.h>
int main() {
int numbers[] = {5, 2, 9, 1, 5, 6};
int smallest = numbers[0];
int array_size = sizeof(numbers)/sizeof(numbers[0]);
for(int i=1; i<array_size; i++) {
if(numbers[i] < smallest) {
smallest = numbers[i];
}
}
printf("Smallest number in the array is: %d", smallest);
return 0;
}
In this program, we first initialize the smallest variable to the first element of the array. Then we iterate over the array, and if we find a number smaller than the current smallest number, we update the smallest variable. Finally, we print the smallest number.
Similar Questions
What are the uses of stracures and how to design them using C programming
What will be the output of the following C code? (Initial values: x= 7, y = 8)#include <stdio.h> void main() { float x; int y; printf("enter two numbers \n"); scanf("%f %f", &x, &y); printf("%f, %d", x, y); }
What is the output of the following code?char str1[] = "C programming";char str2[20];strcpy(str2, str1);printf("%s\n", str2);
Explain different input and output functions in c with example (scanf,printf,gets,puts).
The fundamental data types in c are int, float and char. Today, we're discussing int and float data types.The printf() function prints the given statement to the console. The syntax is printf("format string",argument_list);. In the function, if we are using an integer, character, string or float as argument, then in the format string we have to write %d (integer), %c (character), %s (string), %f (float) respectively.The scanf() function reads the input data from the console. The syntax is scanf("format string",argument_list);. For ex: The scanf("%d",&number) statement reads integer number from the console and stores the given value in variable .To input two integers separated by a space on a single line, the command is scanf("%d %d", &n, &m), where and are the two integers.TaskYour task is to take two numbers of int data type, two numbers of float data type as input and output their sum:Declare variables: two of type int and two of type float.Read lines of input from stdin (according to the sequence given in the 'Input Format' section below) and initialize your variables.Use the and operator to perform the following operations:Print the sum and difference of two int variable on a new line.Print the sum and difference of two float variable rounded to one decimal place on a new line.Input FormatThe first line contains two integers.The second line contains two floating point numbers.Constraints integer variables float variables Output FormatPrint the sum and difference of both integers separated by a space on the first line, and the sum and difference of both float (scaled to decimal place) separated by a space on the second line.Sample Input10 44.0 2.0Sample Output14 66.0 2.0ExplanationWhen we sum the integers and , we get the integer . When we subtract the second number from the first number , we get as their difference.When we sum the floating-point numbers and , we get . When we subtract the second number from the first number , we get as their difference.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.