C Programming Proj
C Programming Proj
#include <stdio.h>k///
void main()
{
printf(“wel come to C”);
getch();
}
Output: While trying to run this code, we encountered multiple problems ranging from “error
compiling” to “warning: extra tokens at end of #include directive”.
#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
printf(“Welcome to C”);
getch();
}
Output: Here, we’re greeted with the message “Welcome to C”. The code seems to work fine.
Discussion: In the first code, several syntax errors and incorrect usage of functions caused the
program to fail to compile. Issues such as extra characters (k///) in the #include directive,
the incorrect capitalization of void, and the use of non-standard quotation marks (“ and ”)
instead of straight double quotes (") were observed. Additionally, the use of getch() without
including <conio.h> results in a compilation error, as getch() is defined in <conio.h> and
is specific to certain compilers.
In the second code, these issues were corrected by including <conio.h> for the getch()
function and using clrscr() to clear the screen before displaying the message. The corrected
code successfully compiled and executed, displaying the message "Welcome to C". This
highlights the importance of using proper syntax, including necessary header files, and adhering
to compiler-specific requirements to ensure the smooth execution of programs.
Objective #2:
#include <stdio.h>
#include <conio.h>
void main()
{
int a;
clrscr();
printf("Enter the value of a: ");
scanf("%d", &a);
printf("a=%d", a);
getch();
}
Output: When executed, this code prompts the user to "Enter the value of a". The user inputs a
value (e.g., 5), and the program outputs "a=5".
Discussion: The program works as expected, accepting an integer input from the user and
displaying it. This exercise underlines the importance of understanding compiler-specific
features and adhering to standardized coding practices to ensure compatibility across different
environments.
Objective #3:
#include <stdio.h>
#include <conio.h>
void main()
{
int a;
float b;
char c;
clrscr();
printf("Enter an integer value: ");
scanf("%d", &a);
printf("Enter a float value: ");
scanf("%f", &b);
printf("Enter a character: ");
scanf(" %c", &c);
printf("\nYou entered:\n");
printf("Integer: %d\n", a);
printf("Float: %.2f\n", b);
printf("Character: %c\n", c);
getch();
}
Output: When the program is executed, it prompts the user to input an integer, a float, and a
character. After entering the values, it displays the entered values on the screen with proper
formatting. For instance, if the user enters 10, 3.14, and A, the output will confirm the inputs as:
Integer: 10, Float: 3.14, Character: A.
Discussion: The program demonstrates handling multiple data types using scanf and
printf. It highlights input/output for integers, floats, and characters. The use of %d, %f, and %c
placeholders ensures proper type handling, and formatting (like displaying floats with two
decimals) improves output clarity. This fundamental program is essential for understanding
input/output operations in C.
Objective #4:
#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, sum, sub, mul, div;
clrscr();
printf("\nEnter the value of a: ");
scanf("%d", &a);
printf("\nEnter the value of b: ");
scanf("%d", &b);
sum = a + b;
sub = a - b;
mul = a * b;
div = a / b;
printf("\nSum = %d", sum);
printf("\nSubtraction = %d", sub);
printf("\nMultiplication = %d", mul);
printf("\nDivision = %d", div);
getch();
}
Output: When executed, the program prompts the user to input two integers (a and b). It
calculates and displays their sum, difference, product, and division results. For example, if a=10
and b=2, the output will display:
● Sum = 12
● Subtraction = 8
● Multiplication = 20
● Division = 5.
Discussion: This program shows arithmetic operations on two integers and displays results. It
uses +, -, *, and / operators for addition, subtraction, multiplication, and division. The output
depends on the user inputs, showcasing the practical use of arithmetic operators.
Objective #5:
WAP to input int, float and character data type:
#include<stdio.h>
void main()
{
int a;
float b;
char c;
printf("Enter an integer: ");
scanf("%d", &a);
printf("Enter a float: ");
scanf("%f", &b);
printf("Enter a character: ");
scanf(" %c", &c);
printf("Integer: %d\n", a);
printf("Float: %.2f\n", b);
printf("Character: %c\n", c);
}
Objective #6:
#include<stdio.h>
#include<conio.h>
void main ()
{
int for;
printf("Enter the value of for:");
scanf("%d", &for);
printf("%d", for);
getch();
}
There was an error encountered while trying to run this code. error: expected identifier or ‘(’
before ‘for’.
Modified code:
#include<stdio.h>
#include<conio.h>
void main()
{
int num; // Changed variable name from 'for' to 'num'
printf("Enter the value of num: ");
scanf("%d", &num);
printf("%d", num);
getch();
}
Discussion: The program provided contains an error because for is a reserved keyword in C,
and it cannot be used as a variable name. Keywords like for, if, while, etc., have special
meanings in C and cannot be used for other purposes.
Objective #7:
#include<stdio.h>
#include<conio.h>
void main ()
{
printf("Hello!\n How are you");
getch();
}
#include<stdio.h>
#include<conio.h>
void main ()
{
printf("Hello!\t How are you");
getch();
}
Difference: \n (Newline) moves the cursor to the next line, causing the text to appear on a new
line. In contrast, \t (Tab) moves the cursor to the next tab stop, adding space between
characters. It aligns text at a specific tab width, which is typically 4 or 8 spaces, depending on
the environment.
Output:
First code with \n: Hello!
How are you
Objective #8:
#include<stdio.h>
#include<conio.h>
void main ()
{
int a; float b; char c;
a = 3, b = 3.3, c = 'p';
printf("\na=%d",a);
printf("\nb=%d",b);
printf("\nc=%d",c);
getch();
}
Discussion: The code contains several issues: Int should be written as int (lowercase i), as Int
is not a valid type in C. The format specifier for float and char is incorrect. We’re trying to print a
float variable (b) using %d, which is used for integers. You should use %f for printing float
values. For char, %d will print the ASCII value, but we should use %c to print the character itself.
Modified code:
#include<stdio.h>
#include<conio.h>
void main()
{
int a; float b; char c;
a = 3; b = 3.3; c = 'p';
printf("\na = %d", a);
printf("\nb = %.2f", b); // Use %.2f to print float values with 2 decimal places
printf("\nc = %c", c);
getch();
}
Explanation of Changes:
● Replaced Int with int to correct the data type.
● Used the correct format specifiers:
○ %d for the integer (a).
○ %.2f for the float (b), which prints the value with 2 decimal places.
○ %c for the character (c).