我有一个名为CustomerTypeApp的主类.它有一个类变量private static float itemPrice = 0.0f.在main方法中是以下代码:
String promptType = (" To determine a discount for " + nf.format((float) itemPrice) + ", type in the... ...Select => ");
char customer = Validation.getChar(promptType);
Run Code Online (Sandbox Code Playgroud)
我还有一个名为Validation的类,其方法是getChar.代码如下:
public static char getChar(String prompt){
String input;
char choice;
System.out.printf(" " + prompt);
input = sc.nextLine();
choice = input.charAt(0);
return choice;
}
Run Code Online (Sandbox Code Playgroud)
在CustomerTypeApp类中,nf.format显然是指NumberFormat.但是,教授说我们不能使用NumberFormat而是必须使用printf.getChar方法确实使用了printf语句.但是,我无法传递printf(变量?)的值.这就是我的意思:我编辑了CustomerTypeApp的代码,如下所示:
String promptType = (" To determine a discount for $%.2s, type in the... ...Select => ");
Run Code Online (Sandbox Code Playgroud)
问题是我必须通过itemPrice传递$%.2s.我尝试将它添加到promptType的末尾,如下所示:
(" To determine...$%.2s...Select => ", itemPrice); …Run Code Online (Sandbox Code Playgroud) 该程序:
#include <stdio.h>
#include <limits.h>
int main( void )
{
#ifdef __CHAR_UNSIGNED__
printf( "%d\n", __CHAR_UNSIGNED__ );
#endif
printf( "%d\n", CHAR_MAX );
printf( "%d\n", CHAR_MIN );
printf( "%hhd\n", CHAR_MAX );
}
Run Code Online (Sandbox Code Playgroud)
输出(在我的x86_64桌面上):
127
-128
127
Run Code Online (Sandbox Code Playgroud)
这是预期的.现在,我在Raspberry Pi(ARM)上运行了相同的操作:
1
255
0
-1
Run Code Online (Sandbox Code Playgroud)
所以... 显然我误解了一些步骤CHAR_MAX正在采取输出的方式,因为我在最后一行预期的输出 - 传递CHAR_MAX给%hhd一个char没有签名的机器- 本来就是255.
(如果你应该问,结果是相同的(char)CHAR_MAX,和(unsigned char)CHAR_MAX.)
我错过了什么?
在回归测试我自己printf()在Raspberry Pi上的实现时发生了这种情况- 顺便说一句,它会打印出来255.所以...只有一个实施正确,我有下沉的感觉,这不是我的......
为什么,当我做map{printf "%4s\n",File::Spec->rel2abs($_)}glob '*';或print map{sprintf "%4s\n",File::Spec->rel2abs($_)}glob '*';4个空格字符输出到字符串的左边?...
/var/www/html/Physics/Electronics/file1.txt
/var/www/html/Physics/Electronics/file2.txt
/var/www/html/Physics/Electronics/file3.txt
/var/www/html/Physics/Electronics/file4.txt
Run Code Online (Sandbox Code Playgroud)
似乎我必须做map{print "\x{20}\x{20}\x{20}\x{20}",File::Spec->rel2abs($_),"\n"}glob '*';的是输出字符串左边的4个空格字符...
/var/www/html/Physics/Electronics/file1.txt
/var/www/html/Physics/Electronics/file2.txt
/var/www/html/Physics/Electronics/file3.txt
/var/www/html/Physics/Electronics/file4.txt
Run Code Online (Sandbox Code Playgroud) 我完全难过了.无论我做什么,我的编译器都认为我需要在上一次printf中使用while循环?它只是一直在寻找没有意义的事情.当我将它们添加到其中时只是告诉我它不应该存在,当我删除它时我被告知我需要它.
int main() {
float userInput; //The grades that the user inputs.
int passingGrade = 0; //Number of passing grades
int failingGrade = 0; //Failing grades
int invalidGrade = 0; //Invalid grades
//This do for loop is simply counting the number of grades of each catagory posted.
do {
printf("Enter a grade (Enter -1 to quit):");
scanf("%f", &userInput);
printf("You entered: %.1f\n", userInput);
if ((userInput > 100) || (userInput < 0)) { //Over 100 is impossible.
invalidGrade = invalidGrade + 1; …Run Code Online (Sandbox Code Playgroud) 我有这段代码:
int a;
char *x;
x = (char *) &a;
a = 512;
x[0] = 1;
x[1] = 2;
printf("%d\n");
return 0;
Run Code Online (Sandbox Code Playgroud)
这打印513.请解释,尤其是第3行.
我试图了解以下内容之间的区别:
printf("%f",4.567f);
printf("%f",4.567);
Run Code Online (Sandbox Code Playgroud)
如何使用f后缀更改/影响输出?
我想做这样的事情printf("%d,%d",array[c], array[c+1]),只有一个变量在每次迭代中都会增加.我写了下面的代码,我期待"1,2"到stdout,我错了:
#include <stdio.h>
int main()
{
int c = 1;
printf("%d,%d",c++,c++); /* 2,1 */
//printf("%d,%d",c,c++); /* 2,1 */
//printf("%d,%d",c++,c); /* 1,2 */
}
Run Code Online (Sandbox Code Playgroud)
如果我尝试做printf("%d,%d",c++,c)和 printf("%d,%d",c,c++),我可以看到,有没有定义的"C++"语句的执行顺序.
有人可以解释我写的代码是如何编译的吗?为什么以及如何根据"c ++"语句的位置进行更改.
TL; DR:
printf()printLotInfo()通过值时打印垃圾.
码
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <math.h>
#include <string.h>
typedef struct Time
{
int hour; //Hour of day
int minute; //Minute of hour
} Time;
typedef struct Car
{
char * plateNumber; //String to hold plate
char hasPermit; //True/False
Time * enteringTime; //Time Struct
int lotParkedIn; //Where is the car located
} Car;
typedef struct ParkingLot
{
int lotNumber; //Lot identifier
double hourlyRate; //$$/h
double maxCharge; //Maximum daily charge
int capacity; //How many …Run Code Online (Sandbox Code Playgroud) I recently encountered with an interview question. I did not understand the behaviour of printf function in this case
#include <stdio.h>
int main() {
int k = printf("String");
printf("%d",k);
}
Run Code Online (Sandbox Code Playgroud)
Expected result : Compilation Error
Output : String6
Why is the output String6?
当我的程序启动时,它只是创建一个fifo并将其打开,之后我只想在屏幕上输出一些信息,但是什么也没打印出来。这是我的代码片段:
void listen(const server_config_t* server_conf)
{
// Create FIFO
if (mkfifo(SERVER_FIFO_PATH, 0660) == -1) {
fprintf(stdout, "server FIFO not created as it already exists. continuing...\n");
}
// Open FIFO (for reading)
int fd;
if ((fd = open(SERVER_FIFO_PATH, O_RDONLY)) == -1) {
// fprintf(stderr, "error: could not open server FIFO\n");
perror("FIFO");
exit(1);
}
// Open dummy FIFO (for writing, prevent busy waiting)
// TODO: find way to wait without another file descriptor?
int fd_dummy;
if ((fd_dummy = open(SERVER_FIFO_PATH, O_WRONLY)) == -1) …Run Code Online (Sandbox Code Playgroud)