Explain the structure of C program. Explain type conversion in C. Write a C program to find prime numbers between a range(using function). Explain any 5 string handling functions available in C. Write a C program to print the address of a variable along with its value.
Question
Explain the structure of C program. Explain type conversion in C. Write a C program to find prime numbers between a range(using function). Explain any 5 string handling functions available in C. Write a C program to print the address of a variable along with its value.
Solution
-
Structure of a C Program: A typical C program consists of the following parts:
- Preprocessor Commands: These are directives for the C preprocessor. They start with a '#' symbol, e.g., #include, #define.
- Function: A collection of statements that perform a specific task. The 'main()' function is the entry point of a C program.
- Variables: These are names given to memory locations to store values of a specific type.
- Statements & Expressions: These can create, manipulate, and control variables and functions.
- Comments: These are used to provide information about the program to the reader. They are ignored by the compiler.
-
Type Conversion in C: Type conversion in C is a way to convert one data type into another. It can be done in two ways:
- Implicit Type Conversion: This is done by the compiler automatically.
- Explicit Type Conversion: This is done by the programmer manually, also known as type casting.
-
C Program to Find Prime Numbers Between a Range:
#include <stdio.h>
int check_prime(int num) {
int i;
for(i = 2; i <= num/2; i++) {
if(num % i == 0)
return 0;
}
return 1;
}
int main() {
int start, end, i;
printf("Enter the range: ");
scanf("%d %d", &start, &end);
printf("Prime numbers between %d and %d are: ", start, end);
for(i = start; i <= end; i++) {
if(check_prime(i))
printf("%d ", i);
}
return 0;
}
-
Five String Handling Functions in C:
- strcpy(s1, s2): Copies string s2 into string s1.
- strcat(s1, s2): Concatenates string s2 onto the end of string s1.
- strlen(s1): Returns the length of string s1.
- strcmp(s1, s2): Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2.
- strrev(s1): Reverses the string s1.
-
C Program to Print the Address of a Variable Along with Its Value:
#include <stdio.h>
int main() {
int var = 5;
printf("Value: %d\n", var);
printf("Address: %p", &var);
return 0;
}
This program will print the value of the variable 'var' and its memory address.
Similar Questions
Structure of a C Program:
Write down the C code for the following questions.a) Write a program in C to check if a given number is even or odd using the function.b) Write a program in C to swap two numbers using a function.c) Write a program in C to find the square of a number using the function.d) Write a program in C to find the factorial of a number using the function.e) Write a program in C to calculate the area of circle using the function.f) Write a C program to compute the sum of the first “N” natural numbers using thefunction.g) Write a program in C to print the Fibonacci series using the function.
Explain Input Output Statements in C
write easies code for me : Write a program in C to find if a number is prime or not.
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.
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.