我是C的初学者.如果我的问题很蹩脚,请不要介意.在我编写的这个程序中,当我第一次使用'for'循环时,我预计只有3个值存储在一个数组中,但它存储了4个值,并且在下一个'for'循环中,如预期的那样显示3个值.我的问题是为什么在第一个'for'循环中它需要4个值而不是3个?
#include<stdio.h>
void main()
{
int marks[3];
int i;
for(i=0;i<3;i++)
{
printf("Enter a no\n");
scanf("%d\n",(marks+i));
}
for(i=0;i<3;i++)
{
printf("%d\n",*(marks+i));
}
}
Run Code Online (Sandbox Code Playgroud) 假设我有一个用户输入的字符串asdfgh\hj,我希望找出\字符串中的字符索引.我怎么能用C做呢?
我尝试了strchr()函数,strchr("asdfgh\hj",'\')但编译器抛出错误.
然后我使用==运算符,但同样的问题 - 再次编译器抛出错误.
我希望输入一个int和另一个longex:1和1000000000,现在我希望创建一个大小为1000000000的数组.然后在数组的每个索引处,存储int val,ex : arr[100000000] = 4.
当我试图这样做时,Netbeans在这一行显示错误:
arr = new long[y+1]` and `arr[j] = 0`
Run Code Online (Sandbox Code Playgroud)
"从long到int可能有损转换".这是我的代码: -
public static void main(String[] args) throws IOException
{
BufferedReader s = new BufferedReader(new InputStreamReader(System.in));
String[] xe = s.readLine().split(" ");
int x = Integer.parseInt(xe[0]);
long y = Long.parseLong(xe[1]);
long []arr;
arr = new long[y+1];
for(long j=0;j<=y;j++)
arr[j] = 4;
}
Run Code Online (Sandbox Code Playgroud) 帮助我摆脱这个问题.我在ubuntu12.04上使用GCC.当我编写这个程序从键盘n获取5个字符串然后在屏幕上打印这些字符串.程序已编译但在执行期间它从键盘获取字符串但仅打印最后一个字符串.我写的程序如下:
void main()
{
char names[10];
int i,j;
for(i=0;i<5;i++)
{
printf(" Enter a name which you want to register\n");
scanf("%s",names);
}
for(i=0;i<5;i++)
printf(" the names you enter are %s\n", names);
}
Run Code Online (Sandbox Code Playgroud)