Code working only in one compiler [closed] - c

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I was doing homework and when i thought i finally did it fail to work in Visual Studio.
I have code like this and its working perfectly fine in Dev C++, but its not in Visual Studio. Can you explain me why and how to fix it.
Edit: Its compiling in both. In VS its display always 0, and it should 0 for text and a integers and for integers this number.
#include <stdio.h>
#include <string.h>
#include <cstdlib>
#define czy_cyfra( c ) (( c ) > '0' && ( c ) < '9' )
#define jaka_cyfra( c ) (( c ) - '0' )
int czy_liczba_calkowita(char * a)
{
if (*a == '\0')
return -1;
do
{
if (!czy_cyfra(*a))
return -1;
} while (*++a != '\0');
return 0;
}
int jaka_liczba(char * a)
{
int liczba = 0;
do
{
liczba = liczba * 10 + jaka_cyfra(*a++);
} while (*a != '\0');
return liczba;
}
int main()
{
char a[255];
printf("Wprowadz lancuch\n");
scanf_s("%s", a);
printf("%u", czy_liczba_calkowita(a) == 0 ? jaka_liczba(a) : 0);
return 0;
system("pause");
}

You have missed out an argument for scanf_s and this results in undefined behaviour - which may or may not work. It should be
scanf_s("%s", a, (unsigned)sizeof a);
The reason for the unsigned cast? The MSVC man page for scanf_s says
The size parameter is of type unsigned, not size_t.
Not all compilers check that you have supplied arguments for all the fields of XscanXX and XprintXX families. For example, MSVC 9.0 does not check these supposedly "safer" functions for the correct number of arguments, but MSVC 14.0 does, giving a compilation error.

Related

Thinking of a code to show if a number is prime or non prime? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
Now im having problems with the new code in terms of compiling. I have two great answers but chux's answer is addressed to rectify my code . So by his/her directions my new code is:
#include <math.h>
#include <conio.h>
int main()
{
int n,i,r;
printf("Enter A Number to know its prime or non prime");
scanf("%d",&n);
for(i=2;i<=n-1;i++)
{
if(n%i==0)
{r==1;
break;
}
}
if(r==1)
printf("%d is a non-prime number",n);
else
printf("%d is a prime number",n);
return 0;
}
But on the output it show as 87 is a prime number. I don't know why. But can someone spot my mistake?
At few problems
Assignment vs. compare
if (r=1) assigns 1 to r, so if (r=1) is always true. Certainly a compare was needed, #Ry
// if (r=1)
if (r == 1)
No early break
OP's code: The value of r depends on the last iteration. Certainly once a factor is found, loop should exit.
for(i=2;i<=n-1;i++) {
if(n%i==0)
// r=1;
{ r = 1; break; }
else
r=0;
}
Incorrect functionality for n == 0,1
All values n < 2 incorrectly report as prime.
Inefficient
Code performs up to n loops. Only need to perform sqrt(n) loops. Tip: Do not use floating point math here for an integer problem.
// for(i=2;i<=n-1;i++)
for(i = 2; i <= n/i; i++)
Alternate
Only peek if you must code.
First off, " ... conio.h is a C header file used mostly by MS-DOS compilers to provide console input/output. It is not part of the C standard library or ISO C .." I was able to get the code to compile without that library file, so you may wish to consider removing it. As for as the code goes, well here is what I came up with:
#include <math.h>
#include <stdio.h>
int isPrime(int value) {
int i = 2;
for(; i < value; i++) {
if((value % i) == 0) {
return 0;
}
}
return value > 1;
}
int main(void){
int n=0,i=0, r=0;
char * s;
printf("\nPlase enter a number to learn if it is prime:");
scanf("%d",&n);
r = isPrime(n);
printf("\n%d is ", n);
s = (r==0)? " not a prime number" : "a prime number";
puts(s);
return 0;
}
After the user inputs a number, the code checks whether it is prime by calling the function isPrime(), a function that returns an int. isPrime is a simple function that attempts to factor a number.
See here for similar live code that I devised.

What do i do wrong (c language with file.txt) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
There is a following exercise:
I have a file.txt containing a lot of unknown numbers, separated by one or more spaces or by a new line.(file.txt in one folder with my program).
I must find total amount of numbers and maximum value. Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
FILE *in;
in=fopen("file.txt","r");
int arr[100],i=0,j,max;
while(fscanf(in,"%d",&arr[i] != EOF)) {
i++;
}
arr[0]=max;
for(j=1;j<i;j++) {
if(max < arr[j]) {
max = arr[j];
}
}
printf("Tot.number: %d\nMax: %d",i,max);
}
But, codeblock cannot execute the program.
fscanf(in,"%d",&arr[i] != EOF)
will be
fscanf(in,"%d",&arr[i]) != EOF
But there are other problems also
Not checking the return value of fopen.
if(in != NULL ){
/* File opened correctly */
}
No guard while accessing the array. if i>=100 there will be array index out of bound error leading to undefined behavior.
You forgot to declare initialize max.
You meant to initialize max to arr[0]. So correct will be max = arr[0]. There is no use assigning value of arr[0] in case you want to find the maximum.
Also you have to keep in mind that if fscanf doesn't return EOF then you can't be relieved that it worked unless you check that 2 value is being parsed correctly. The output of fscanf should be equal to 1 since one value is being read.
int r = fscanf("%d",&arr[i]);
if( r == 1 ){
/* SUCCESS */
}
else if( errno ) {
/* Error occured */
}
else if( r == EOF ){
/* Either EOF reached or some I/O error. */
}
Also among other things (not an error but a good practice)
Not closing the file.
fclose(in);

Card Hangover C [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am trying to do this problem: http://poj.org/problem?id=1003
#include <stdio.h>
int c;
int a = 0;
int i;
int main()
{
scanf("%.2f", &c);
if (0.01 <= c <= 5.20){
for (i = 1; a < c; ++i){
a += (1/(i + 1));
}
printf("%d card(s)", i + 1);
}
return 0;
}
My code isn't working? For some reason it always returns 2 card(s) no matter what I enter. Can someone find the problem?
Thanks!
Problem 1: This is not how you test if a variable is between two values:
if (0.01 <= c <= 5.20){
The correct way is
if (0.01 <= c && c <= 5.20){
Your code is interpreted as if you'd written:
if ((0.01 <= c) <= 5.20){
(0.01 <= c) will be either 0 or 1, and both of these are less than 5.20, so it's always true.
Problem 2: The variables a and c need to be float, not int, because int variables can't have fractions in them, and %f format in scanf requires that the corresponding argument be a pointer to float.

Tokenise a String in C programming [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm new to C programming and I know there have been other explanations on how to split a string into words but none of them seem similar to my program. I'm having difficulty finding the errors in my program:
#include <stdio.h>
#include <stdlib.h>
int tokenise(char str[], int start, char result[]) {
if (str[start] == "/o") {
return -1;
} else {
result = str[start];
}
}
int main() {
const int MAX_STRING = 256;
char buffer[MAX_STRING];
fgets(buffer, MAX_STRING, stdin);
char result[256];
int start;
start = tokenise(buffer, 0, result);
while ( start != -1 ) {
printf("%s\n", result);
start = tokenise(buffer, start, result);
}
}
In your function tokenise -
if(str[start] == "/o"){
What is "/o" you compare with? It should be '\0'.
if(str[start] == '\0'){
And in else your function does not return anything , therefore , in that case UB.
You function doesn't have any loop or use recursion to iterate over array ,therefore , your logic doesn't seem to achieve anything close .
You have many problems with your code:
else {
result = str[start];
}
No return value. That is undefined behaviour.
str[start] == '\o'
Thats incorrect as you want to compare to the EOS null termination character
Do this instead:
str[start] == '\0'
Lastly, if you want your tokenise function to write into result, you need to pass a pointer to result, not the value of result.
ps: semantic errors aside, your function does nothing resembling what you want. Look into loops and their implementation.

called object 'strn' is not a function [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
While compiling the below code, i am getting error
"called object strn is not a function"
tired of this error !! need a solution!!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define num 400
int main()
{
char strn[num];
int count;
int a=0,e=0,i=0,o=0,u=0;
printf("enter your string!\n");
gets(strn);
for(count=0;count<strlen(strn);count++)
{
if ( strn(count)=='a' )
{
a++;
}
if (strn(count)=='e')
{
e++;
}
You are trying to use strn as if it's a function: strn(count).
You are presumably trying to access the value at the count index, so you should use strn[count].
The error is quite indicative. You have declared strn as an array of characters.
char strn[num];
And using it as strn(count) which is wrong. The compiler considers it as a function. You should use square brackets [ ] instead of parentheses ( ).
In your code, strn(count) represents a function call to strn() with one argument count. What you need is to use the Array subscripting operator [], not ()
You need to change
strn(count)
to
strn[count]
Also, please consider using fgets() over gets().
The subscript operator uses symbols [] for enclosing index. So for example instead of
strn(count)=='a'
you have to write
strn[count]=='a'
Also function gets is not supported any more by the C Standard because it is an unsafe function. Use instead fgets.
The program can look like
#include <stdio.h>
#include <ctype.h>
#define num 400
int main( void )
{
char strn[num];
char *p;
int a = 0, e = 0, i = 0, o = 0, u = 0;
printf( "Enter your string: " );
fgets( strn, num, stdin );
for ( p = strn; *p != '\0'; ++p )
{
char c = tolower( *p );
switch ( c )
{
case 'a':
a++;
break;
case 'e':
e++;
break;
// and so on...

Resources