Why is it when I run my code I get this result:
Name: Zeref
Age: 20
Float: 20.11
-----
Name: Zeref
Age: 1072324272
Dec: 0.000000
Code:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
char name[524288];
int age[524288];
float dec[524288];
printf("Name: ");
scanf("%s", name);
printf("Age: ");
scanf("%d", age);
printf("Float: ");
scanf("%f", dec);
printf("-----\n");
printf("Name: %s\n", name);
printf("Age: %d\n", age);
printf("Dec: %f\n", dec);
return 0;
}
it isn't taking in what I say correctly, why?
I want it to print out exactly what I write but using age as an int, dec as a float and name as a string. Only name works
This is just printing the address of the array age as a decimal integer (actually it introduces undefined behavior);
printf("Age: %d\n", age);
Try this
printf("Age: %d\n", age[0]);
Related
I want to take input from user using structure. So I'm using the code like below. But it's not printing the values which I'm entering. Can anyone help me?
#include <stdio.h>
int main()
{
struct book
{
char name;
float price;
int pages;
};
struct book b1, b2;
printf("Enter the Name, Price and Pages of Book 1: ");
scanf("%c %f %d", &b1.name, &b1.price, &b1.pages);
printf("Enter the Name, Price and Pages of Book 2: ");
scanf("%c %f %d", &b2.name, &b2.price, &b2.pages);
printf("Here is the data you've entered: \n");
printf("Name: %c Price: %f Pages: %d\n", b1.name, b1.price, b1.pages);
printf("Name: %c Price: %f Pages: %d\n", b2.name, b2.price, b2.pages);
return 0;
}
But I'm not getting the output as desired. My
Output Image
Your question is similar to this one: scanf() leaves the newline character in the buffer
And can be corrected like this:
#include <stdio.h>
int main()
{
struct book
{
char name;
float price;
int pages;
};
char buffer[255];
struct book b1, b2;
printf("Enter the Name, Price and Pages of Book 1: ");
scanf(" %c %f %d", &b1.name, &b1.price, &b1.pages);
printf("Enter the Name, Price and Pages of Book 2: ");
scanf(" %c %f %d", &b2.name, &b2.price, &b2.pages);
printf("Here is the data you've entered: \n");
printf("Name: %c Price: %f Pages: %d\n", b1.name, b1.price, b1.pages);
printf("Name: %c Price: %f Pages: %d\n", b2.name, b2.price, b2.pages);
return 0;
}
Note the leading space before the %c input.
For some weird reason when I input the values in the marks section, the printed values are all 0, please help, I tried running a smaller version of the code and it worked I don't know why this doesn't.
here is the code:
Stack overflow keeps giving me an error since apparently most of my question is code so pls ignore this
#include <stdio.h>
#include <stdlib.h>
int main()
{
int marksComm;
char gradeComm[1];
int marksEco;
char gradeEco[1];
int marksCompsys;
char gradeCompsys[1];
int marksProg;
char gradeProg[1];
int marksDis;
char gradeDis[1];
int marksLab;
char gradeLab[1];
int marksPhy;
char gradePhy[1];
printf("CCS001\nMarks> ");
scanf("%d", &marksComm);
printf("Grade> ");
scanf("%s", &gradeComm);
printf("\nCCS009\nMarks> ");
scanf("%d", &marksEco);
printf("Grade> ");
scanf("%s", &gradeEco);
printf("\nCSC111\nMarks> ");
scanf("%d", &marksCompsys);
printf("Grade> ");
scanf("%s", &gradeCompsys);
printf("\nCSC112\nMarks> ");
scanf("%d", &marksProg);
printf("Grade> ");
scanf("%s", &gradeProg);
printf("\nCSC113\nMarks> ");
scanf("%d", &marksDis);
printf("Grade> ");
scanf("%s", &gradeDis);
printf("\nCSC126\nMarks> ");
scanf("%d", &marksPhy);
printf("Grade> ");
scanf("%s", &gradePhy);
printf("\nCSC115\nMarks> ");
scanf("%d", &marksLab);
printf("Grade> ");
scanf("%s", &gradeLab);
printf("\nCourse Code Marks Grade\n");
printf("CCS001 %d %s \n", marksComm, gradeComm);
printf("CCS009 %d %s \n", marksEco, gradeEco);
printf("CSC111 %d %s \n", marksCompsys, gradeCompsys);
printf("CSC112 %d %s \n", marksProg, gradeProg);
printf("CSC113 %d %s \n", marksDis, gradeDis);
printf("CSC115 %d %s \n", marksLab, gradeLab);
printf("CSC126 %d %s \n", marksPhy, gradePhy);
}
Your arrays are two small. C strings are NUL-terminated, so you need a char for the letter, and a char for the NUL. That's two char. But you reserve space for one.
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.
This code is designed to print out the values inputted by the user but the code is printing some invalid results
#include<stdio.h>
int main(){
int age;
char array[][50] = {"name",
"colour", "country"};
printf("What is your
name\n");
scanf("%s", array[1]);
printf("How old are
you\n");
scanf("%d", &age);
printf("What is your
favorite colour\n");
scanf("%s", array[2]);
printf("Which country are
youu from\n");
scanf("%s", array[3]);
printf("Information
collected about you is\n");
printf("Name: %s\n Age:
%d\n Colour: %s\n Colour:
%s\n Country: %s\n",
array[1], age, array[2],
array[3]);
}
You have five printings, in the final line.
printf ("Name: %s\n Age: %d\n Colour: %s\n Colour: %s\n Country: %s\n", array[1], age, array[2], array[3]);
That is, you have 2 color entries, and you only want 1.
The compiler does not know where to look for the Country %s part and then generates a segmentation fault. Segmentation faults happen when you dereference an invalid pointer.
#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);