我希望代码运行,直到用户输入整数值.
该代码适用于char和char数组.
我做了以下事情:
#include<stdio.h>
int main()
{
int n;
printf("Please enter an integer: ");
while(scanf("%d",&n) != 1)
{
printf("Please enter an integer: ");
while(getchar() != '\n');
}
printf("You entered: %d\n",n);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
问题是如果用户输入一个浮点值scanf就会接受它.
Please enter an integer: abcd
Please enter an integer: a
Please enter an integer: 5.9
You entered: 5
Run Code Online (Sandbox Code Playgroud)
怎么可以避免?
请解释这个片段:
#include <stdio.h>
int puts(const char *str) {
fputs("Hello world!\n", stdout);
}
int main() {
printf("Goodbye\n");
}
Run Code Online (Sandbox Code Playgroud)
输出:Hello world!返回13
我的表达式如下所示: -
while (count)
{
...
...
index = ((count == 20)? 0 : index++);
...
...
}
Run Code Online (Sandbox Code Playgroud)
现在三元运算符是C中的序列点,但我相信序列点在测试部分结束.
这种理解是否正确,因此这种说法会导致未定义的行为?
#include<string.h>
#include<stdio.h>
void main()
{
char *str1="hello";
char *str2="world";
strcat(str2,str1);
printf("%s",str2);
}
Run Code Online (Sandbox Code Playgroud)
如果我运行这个程序,我得到运行时程序终止.
请帮我.
如果我用这个:
char str1[]="hello";
char str2[]="world";
Run Code Online (Sandbox Code Playgroud)
然后它正在工作!
但为什么
char *str1="hello";
char *str2="world";
Run Code Online (Sandbox Code Playgroud)
这段代码不起作用????
使用scandir()它时,用于alphasort以相反的顺序对目录内容列表进行排序.现在如何scandir()在c中使用升序打印目录.
.并且..必须在最顶层.
这是代码:
#include<stdio.h>
#include <dirent.h>
#include<string.h>
#include<sys/dir.h>
#include<malloc.h>
int main(void)
{
struct dirent **namelist;
int n;
n = scandir(".", &namelist,NULL,alphasort);
if (n < 0)
perror("scandir");
else {
while (n--) {
printf("%s\n", namelist[n]->d_name);
free(namelist[n]);
}
free(namelist);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud) 创建打印出Body Mass Index的代码
printf("What is your height in inches?\n");
scanf("%d", height);
printf("What is your weight in pounds?\n");
scanf("%d", weight);
Run Code Online (Sandbox Code Playgroud)
我有高度和重量初始化为int height,int weight但程序不让我运行它,因为它说格式是int*两scanf行上的类型.让这个程序运行我做错了什么?
我正在创建一个函数,它将返回一个我必须为其分配内存的数组,但是我找不到一种方法来确保在程序结束时删除内存.
这是功能:
int* RGB::getColor() const{
int* arr = new int[3];
arr[0] = red;
arr[1] = green;
arr[2] = blue;
return arr;
}
Run Code Online (Sandbox Code Playgroud)
这是一个使用它的例子:
int main(){
int green;
RGB test(50, 100, 150);
green = test.getColor()[1];
}
Run Code Online (Sandbox Code Playgroud)
由于它不是一个对象,我无法删除RGB的类析构函数中的任何内容.如何确保在"getColor()"函数结束时删除内存?谢谢.
我试图使用WinAPI递归搜索计算机中的文件:FindFirstFile和FindNextFile
我不明白为什么,但我的代码不起作用.在第一次运行中,一切都很好 - 我可以看到C盘中的所有内容.例如C:\Program Files (x86),当我进入子文件夹时,我发现文件夹中的所有文件都是正确的.,而如果我使用的函数没有递归搜索,使用给定的路径C:\Program Files (x86),我会得到一个列表里面的所有文件夹.
这是我的代码:
#include <Windows.h>
#include <iostream>
#include <string>
void FindFile(std::string directory)
{
std::string tmp = directory + "\\*";
WIN32_FIND_DATA file;
HANDLE search_handle=FindFirstFile(tmp.c_str(), &file);
if (search_handle != INVALID_HANDLE_VALUE)
{
do
{
std::wcout << file.cFileName << std::endl;
directory += "\\" + std::string(file.cFileName);
FindFile(directory);
}while(FindNextFile(search_handle,&file));
if (GetLastError() != 18) // Error code 18: No more files left
CloseHandle(search_handle);
}
}
int main()
{
FindFile("C:");
}
Run Code Online (Sandbox Code Playgroud)
该变量tmp …
在下面的程序中,Here ptr已被声明为指向整数指针的指针并分配了数组的基址,该数组p[]已被声明为整数指针数组.假设ptr包含地址9016(假设p的起始地址是9016)在ptr递增之前和之后ptr++,它将包含值9020(假设int占用4个字节).
所以ptr-p应该将输出设为4即(9020-9016 = 4).但它输出为1.为什么?
#include<stdio.h>
int main()
{
static int a[]={0,1,2,3,4};
static int *p[]={a,a+1,a+2,a+3,a+4};
int **ptr=p;
ptr++;
printf("%d",ptr-p);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 您好,我正在尝试解码以下字符串 10410532119111114108100 我刚刚以 ascii 编码,现在我也想将字符串转换为纯文本,我有以下代码:
int texto = 10410532119111114108100;
string resultado = "";
resultado = resultado + System.Convert.ToChar(texto);
Console.WriteLine(resultado);
Run Code Online (Sandbox Code Playgroud)
但不起作用,有人可以帮助我吗?
我目前正在学习C语言,而且我在double乘法主题方面遇到了麻烦.
我需要打印的原始值,然后2*value的double.
double num = 34.39;
printf("Original value = %d, 2x original value = %d", num, num*2);
Run Code Online (Sandbox Code Playgroud)
如何使2x值真正是原始值的2倍?
输出应该是5 10 14 18.但即使是严格的默认情况也会执行输出5 10 15 20.
#include<stdio.h>
void main(void)
{
int i=0;
for(i=0;i<20;i++){
switch(i){
case 0: i+=5; break;
case 1: i+=2; break;
case 5: i+=5; break;
default: i+=4;
}
printf("%d \n",i);
}
}
Output -
5
10
15
20
Run Code Online (Sandbox Code Playgroud)
输出不应该是5 10 14 18?
我最近遇到过这样的代码
struct tcpheader {
unsigned char tcph_reserved:4, tcph_offset:4;
....
Run Code Online (Sandbox Code Playgroud)
这显然是什么:样的,但为什么我从未正式见过这个?我无法找到:运营商的正式定义.我搜索了变量声明的分区,拆分和划分无济于事.
任何人都有关于:运营商的一些信息?