#include<stdio.h>
#define UPPER 999999
#define LOWER 11111
int main(void)
{
// Local Declarations
double price = 89.99;
char grade = 'B';
int age = 97;
// Statements
printf("Homework 2:\n\nUsing printf\n");
printf(" age = %c, age\n");
printf("grade = %d, grade\n");
printf("price = %f, price\n\n");
printf("Using both printf and scanf\n");
printf("Enter a new value for age: ");
scanf("%d", &age);
printf("Enter a new value for grade: ");
scanf("%c", &grade);
printf("Enter a new value for price: ");
scanf("%lf", &price);
printf("Print the new values\n");
printf(" age = %d \n", age);
printf("grade = %c\n", grade);
printf("price = %lf\n\n", price);
print("\n\nPrinting two defined constants: "UPPER" and "LOWER"\n");
print("UPPER = %08d\n", UPPER);
print("LOWER = %08d\n", LOWER);
return 0;
} // end of main
Above is my programme and im supposed to fix it. I've been at it for almost 3 hours now still can figure out the problem. I've got a error and a few warnings.
warning: too few arguments for format
Several warnings for the statements in the mid body
error: expected ')' before numeric constant
this error is for printing two constants.
print("\n\nPrinting two defined constants: "UPPER_S" and "LOWER_S"\n");
would only work if UPPER_S and LOWER_S were #defined as:
#define UPPER_S "999999"
#define LOWER_S "11111"
Alternatively you could use the two following macros to "stringify" the numerical #defines:
#define _STRINGIFY(s) #s
#define STRINGIFY(s) _STRINGIFY(s)
and then do:
#define UPPER 999999
#define LOWER 11111
fputs("\n\nPrinting two defined constants: "STRINGIFY(UPPER)" and "STRINGIFY(LOWER)"\n", stdout);
I came here looking for the answer to this question and it seems most people just got hung up on syntax errors rather than caring what the actual question is. You can use printf with #defined values as though they are normal variables. However you must pay close attention to the type.
#define HEXNUM 0xA8
#define NUMBER 129
#define STRING "somestring"
#include <stdio.h>
int main(void) {
printf("hex number: %x\n", HEXNUM);
printf("number: %d\n", NUMBER);
printf("string: %s", STRING);
return 0;
}
Things like
printf("grade = %d, grade\n");
expect an argument for '%d'. It should be in the form of
printf("grade = %d\n", grade);
You need to put your printf args outside of quotes.
Current:
// Statements
printf("Homework 2:\n\nUsing printf\n");
printf(" age = %c, age\n");
printf("grade = %d, grade\n");
printf("price = %f, price\n\n");
Should be:
// Statements
printf("Homework 2:\n\nUsing printf\n");
printf(" age = %c\n", age);
printf("grade = %d\n", grade);
printf("price = %f\n\n", price);
One method is to assign the constant defined variable value to a local variable value. This way it will be useful in printf("%d", X); method.
However, you could use const int EXAMPLE=12, and then EXAMPLE could be used in the standard printf method, e.g. printf("%d\n", EXAMPLE);
Related
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int num1, num2, result, result2, result3;
printf("Primeiro numero: \n", num1);
scanf("%d", &num1);
printf("Segundo numero: \n", num2);
scanf("%d", &num2);
result = num1 + num2;
if (result > 20) {
result2 = result + 8;
printf("Resultado: %d \n", result2);
scanf("%d", &result2);
}
else {
(result < 20);
result3 = result - 5;
printf("Resultado: %d \n", result3);
scanf("%d", &result3);
}
}
Please remove the scanf from your if/else statements.
your else statement has (result < 20); which does nothing. So you have remove it too.
#include <stdio.h>
#include <stdlib.h>
int main ()
{
int num1, num2, result;
printf("Primeiro numero: ");
scanf("%d", &num1);
printf("Segundo numero: \n");
scanf("%d" , &num2);
result = num1 + num2;
if (result > 20) {
printf("Resultado: %d \n", result + 8);
}
else {
printf("Resultado: %d \n", result - 5);
}
return 0;
}
You have a few errors in your code:
printf("Primeiro numero: \n", num1);
num1 is not used by printf there; you need a %d in the format string (the first argument) to indicate you want to print a number (the second argument in this case). If it were used, you could get any output because num1 is not initialized yet; you are reading it in the next line of code with a scanf. So just print a message telling the user to enter a number, then read that number, then print it.
(result < 20);
That check should go together with the else. So you first check result > 20, and then, if that is not true, you go on and check result < 20. Notice you are not checking result == 20.
scanf("%d", &result2);
scanf("%d", &result3);
Those 2 lines are not needed. You just want to print some results, not read them from the user.
Also:
Prefer to declare variables as near as possible to the place you are first going to use them.
Try and initialize variables when you declare them.
You don't really need to declare result2 and result3.
[Demo]
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
int num1 = 0;
int num2 = 0;
printf("Primeiro numero: ");
scanf("%d", &num1);
printf("%d\n", num1);
printf("Segundo numero: ");
scanf("%d", &num2);
printf("%d\n", num2);
int result = num1 + num2;
if (result > 20) {
printf("Resultado: %d\n", result + 8);
}
else if (result < 20) {
printf("Resultado: %d\n", result - 5);
}
}
This question already has an answer here:
C Access violation writing location scanf_s
(1 answer)
Closed 1 year ago.
While the program is running , in the middle of it (after typing the Vaccine name ) it stops and gives me {Exception Thrown} can anyone help me fix this issue? I have also included a photo of the error it gives me
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int choose, dosage, quantity;
char code[10];
char name[32];
char produce[64];
float population;
{
printf("COVID-19 Vaccination System \n");
printf("1:- Inventory Creation \n");
printf("2:- Update Vaccine Quantities \n");
printf("3:- Search Vaccine \n");
printf("4:- Produce a list of all Vaccines and their distributed Quantities \n");
printf("5:- EXIT \n\n");
printf("==>");
scanf_s("\n%d", &choose);
if (choose == 1)
{
char ch;
FILE* cv;
errno_t err;
err = fopen_s(&cv, "vaccine.txt", "w");
for (int i = 0; i < 5; i++)
{
printf("Please Enter Vaccine Full information %d \n", i + 1);
printf(" Name of Vaccine : ");
scanf_s("\n%s", &name);
printf("Please Enter Vaccine Code %c \n", i + 1);
printf(" Vaccine Code : ");
scanf_s("\n%s", &code);
printf("Please Enter Vaccine Producing Country %c \n", i + 1);
printf("Producing Country : ");
scanf_s("\n%s", &produce);
printf("Please Enter Dosage Required %d \n", i + 1);
printf("Dosage Required (MAX=2 - MIN=1) : ");
scanf_s("\n%d", &dosage);
printf("Please Enter Population Covered %d \n", i + 1);
printf("Population Covered (%) : ");
scanf_s("\n%f", &population);
printf("Please Enter Vaccine Quantity %d \n", i + 1);
scanf_s("\n%d", &quantity);
fprintf("%s %s %s %d %f %d", name, code, produce, dosage, population, quantity);
}
}
}
return 0;
}
The line
fprintf("%s %s %s %d %f %d", name, code, produce, dosage, population, quantity);
is wrong. You have to pass a file pointer as the first argument of fprintf(). It should be:
fprintf(cv, "%s %s %s %d %f %d", name, code, produce, dosage, population, quantity);
Also
You should check if fopen_s succeeded before proceeding to next operation and stop operation if it failed.
You should remove & before the array names (code, name, produce) used in scanf_s. Array in expressions (excluding some exceptions) are automatically converted to pointers to the first element, so no explicit & is not needed here. Also it will make the type wrong scanf() expects char* for %s, but things like &code are an pointers to arrays (like char(*)[10]).
The gradeOne and secondGrade does not save the number that i give. So i cant have the average , because, always end in a division of (0 + 0)/2, can anyone help me?
#include <stdio.h>
#include <stdlib.h>
int main()
{
int students;
char name[50];
double gradeOne, secondGrade, classAverage = 0, average = 0;
for(students = 1;students <= 15; students++){
printf("Tell me Your name: \n");
fflush(stdin);
scanf("%[^\n]s", &name);
//saves name
printf("Tell me your first grade: \n");
scanf("%f", &gradeOne);
printf("Tell me your second grade: \n");
scanf("%f", &secondGrade);
//saves grade
printf("\n========================================\n");
printf("Student: %s\n", name);
printf("First Grade: %3.2f \nSecond Grade: %3.2f \n", gradeOne, secondGrade);
printf("%f", average);
printf("\n========================================\n");
average = (gradeOne + secondGrade)/2;
//creates average
printf("Average of the %d student: %3.2f",students, average);
classAverage += average;
//creates class average
}
classAverage = classAverage / (students - 1);
printf("The class average was: %3.2f", classAverage);
return 0;
}
%f in scanf() is for reading float. You should use %lf to read double.
Note that you should use %f in printf() for printing double. Newer specification allows %lf for printf(), but %f should be better for compatibility.
I'm learning C using C by Example. There is a question where we need to take an input integer and square it and print it to the screen. I'm having a strange error, when no matter what Int I enter it give me this output:
Please enter a number.
5
Number = 2686764 Square of Number = 2686760
Here is the program I wrote.
#include <stdio.h>
#include <conio.h> //for getch()
main(){
int number, square;
printf("Please enter a number. \n");
scanf("%d", &number);
square = number*number;
printf("\n Number = %d ", &number);
printf("\t Square of Number = %d", &square);
getch();
}
You are printing the memory address of the number and square variables, not their values.
Try this instead:
printf("\n Number = %d ", number);
printf("\t Square of Number = %d", square);
Your printf statements are incorrect. Do not use & in this case:
printf("\n Number = %d ", number);
printf("\t Square of Number = %d", square);
To add to the above answers ... look at this example:
int number = 5;
printf(" %d\n", number); // Prints value of 'number'.
printf(" %p\n", &number); // Prints address of 'number'.
printf(" %d\n", *(&number)); // Prints value at address of 'number' ...
void main ()
{
int sayi;
int ikisayi;
printf("first number");
scanf("%d",&sayi);
printf("second number");
scanf ("%d", &ikisayi);
snc = ikisayi + sayi;
printf(\n Total= %f \n , snc );
}
What is wrong with that can you help me?
You need to declare int snc;, and the format specifier in printf should be %d. In addition, the return type of main should be int, you should include stdio.h and you should have double quotes around your format string in printf.
#include <stdio.h>
int main() {
int a, b, c;
printf("first number: ");
scanf("%d", &a);
printf("second number: ");
scanf("%d", &b);
c = a + b;
printf("total: %d\n", c);
return 0;
}
Also, printf("\n Total= %d \n" , snc );