[go: up one dir, main page]

0% found this document useful (0 votes)
3 views6 pages

Basic Question

The document provides a series of programming examples in C, demonstrating the use of printf and scanf functions for output and input operations, respectively. It covers topics such as declaring variables, using keywords, defining identifiers, and formatting output. Each section includes a question, code snippet, and expected output to illustrate the concepts clearly.

Uploaded by

Akash Satdeve
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

Basic Question

The document provides a series of programming examples in C, demonstrating the use of printf and scanf functions for output and input operations, respectively. It covers topics such as declaring variables, using keywords, defining identifiers, and formatting output. Each section includes a question, code snippet, and expected output to illustrate the concepts clearly.

Uploaded by

Akash Satdeve
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

. What is printf and how is it used?

 Question: Write a program to display "Hello, World!" using printf.


 Code:

#include <stdio.h>

int main() {
printf("Hello, World!\n");
return 0;
}

 Output:

Hello, World!

2. How do you use scanf to take integer input?

 Question: Write a program to take an integer input from the user and display it.
 Code:

#include <stdio.h>

int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
printf("You entered: %d\n", num);
return 0;
}

 Output:

Enter an integer: 5
You entered: 5

3. What is a keyword in C?

 Question: Why can't you use a keyword like int as a variable name? Provide an
example.
 Code:

// Invalid example
int int = 5; // Error: 'int' is a keyword

 Explanation: Keywords are reserved words in C and cannot be used as variable


names.

4. How do you define an identifier?

 Question: Write a program using a valid identifier for a variable.


 Code:
#include <stdio.h>

int main() {
int age = 25; // 'age' is a valid identifier
printf("Age: %d\n", age);
return 0;
}

 Output:

Age: 25

5. How do you declare and initialize a variable in C?

 Question: Write a program to declare an integer variable and initialize it with the
value 10.
 Code:

#include <stdio.h>

int main() {
int number = 10; // Declaration and initialization
printf("Number: %d\n", number);
return 0;
}

 Output:

Number: 10

6. How do you take input for a floating-point number using scanf?

 Question: Write a program to read a float and display it.


 Code:

#include <stdio.h>

int main() {
float num;
printf("Enter a float number: ");
scanf("%f", &num);
printf("You entered: %.2f\n", num);
return 0;
}

 Output:

Enter a float number: 3.14


You entered: 3.14

7. How do you print characters using printf?

 Question: Write a program to print a character.


 Code:
#include <stdio.h>

int main() {
char ch = 'A';
printf("Character: %c\n", ch);
return 0;
}

 Output:

Character: A

8. How do you use multiple printf and scanf statements?

 Question: Write a program to take two integers as input and display their sum.
 Code:

#include <stdio.h>

int main() {
int a, b;
printf("Enter two integers: ");
scanf("%d %d", &a, &b);
printf("Sum: %d\n", a + b);
return 0;
}

 Output:

Enter two integers: 4 5


Sum: 9

9. What are data types in C?

 Question: Write a program to display the size of different data types in C.


 Code:

#include <stdio.h>

int main() {
printf("Size of int: %lu bytes\n", sizeof(int));
printf("Size of float: %lu bytes\n", sizeof(float));
printf("Size of double: %lu bytes\n", sizeof(double));
printf("Size of char: %lu bytes\n", sizeof(char));
return 0;
}

 Output:

Size of int: 4 bytes


Size of float: 4 bytes
Size of double: 8 bytes
Size of char: 1 byte

10. How do you use printf to format output?


 Question: Write a program to format a float to display only two decimal places.
 Code:

#include <stdio.h>

int main() {
float num = 3.14159;
printf("Formatted float: %.2f\n", num);
return 0;
}

 Output:

Formatted float: 3.14

11. How to input multiple values using a single scanf?

 Question: Write a program to take two float values as input and display their product.
 Code:

#include <stdio.h>

int main() {
float x, y;
printf("Enter two float numbers: ");
scanf("%f %f", &x, &y);
printf("Product: %.2f\n", x * y);
return 0;
}

 Output:

Enter two float numbers: 1.2 2.3


Product: 2.76

12. How do you use scanf to read a character?

 Question: Write a program to read a character and display it.


 Code:

#include <stdio.h>

int main() {
char ch;
printf("Enter a character: ");
scanf(" %c", &ch); // Note the space before %c
printf("You entered: %c\n", ch);
return 0;
}

 Output:

Enter a character: B
You entered: B
13. How to define a variable of type double?

 Question: Write a program to declare a double variable and display its value.
 Code:

#include <stdio.h>

int main() {
double pi = 3.141592653589793;
printf("Value of pi: %.15lf\n", pi);
return 0;
}

 Output:

Value of pi: 3.141592653589793

14. What happens if you use %d to print a float?

 Question: Demonstrate the error by trying to print a float using %d.


 Code:

#include <stdio.h>

int main() {
float num = 3.14;
printf("Float as integer: %d\n", (int)num); // Typecasting for
demonstration
return 0;
}

 Output:

Float as integer: 3

15. How to use multiple data types in a single program?

 Question: Write a program that uses int, float, and char variables.
 Code:

#include <stdio.h>

int main() {
int age = 25;
float height = 5.9;
char initial = 'J';
printf("Age: %d, Height: %.1f, Initial: %c\n", age, height,
initial);
return 0;
}

 Output:

Age: 25, Height: 5.9, Initial: J

You might also like