我对逻辑运算符很困惑.我有这两行代码.这里num和j都是int.我有一种情况,两个条件都满足,但我不知道为什么它不打印的价值j.任何人都可以指出错误吗?提前致谢.
if(k==1 && num%j==0)
printf("%d",j);
Run Code Online (Sandbox Code Playgroud) 我知道return和exit()(链接)之间的区别,但我不知道在何时何地选择一个而不是另一个.例如,根据这个答案,我理解这return是一个更好的选择,但从另一个我理解相反.
一个例子:在这个代码中(来自这个问题)是否优先使用exit()或return?
int read_file (char *filename, int **vet)
{
FILE *fin;
if ( !(fin = fopen(filename, "r")) )
{
perror(filename);
return -1;
}
* vet = malloc (10 * sizeof(int));
if ( *vet == NULL )
{
perror("Memory allocation error.\n");
return -2;
}
/* ... */
return fclose(fin);
}
int main ()
{
char filename[100];
int *vet;
if ( read_file(filename, &vet) ) …Run Code Online (Sandbox Code Playgroud) 我想从函数(在示例funzione中)返回一个字符串到main.这该怎么做?谢谢!
#include <stdio.h>
#include <string.h>
#define SIZE (10)
/* TODO*/ funzione (void)
{
char stringFUNC[SIZE];
strcpy (stringFUNC, "Example");
return /* TODO*/;
}
int main()
{
char stringMAIN[SIZE];
/* TODO*/
return 0;
}
Run Code Online (Sandbox Code Playgroud)
[已编辑]对于那些需要它的人来说,以前代码的完整版本(但没有stringMAIN)是:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE (10)
char *funzione (void)
{
char *stringa = malloc(SIZE);
strcpy (stringa, "Example");
return stringa;
}
int main()
{
char *ptr = funzione();
printf ("%s\n", ptr);
free (ptr);
return 0;
}
Run Code Online (Sandbox Code Playgroud) #include <stdio.h>
int main(){
char last[20];
char first[20];
printf("Please enter your last name:");
scanf("%s",last);
printf("\nPlease enter your first name:");
scanf("%s",first);
printf("Here your email address\n",last,first,@student.com); //last first@student.com
}
Run Code Online (Sandbox Code Playgroud)
我希望用户写下他们的名字,我会自动输出他们的电子邮件.