Expected expression error in C - c

I'm trying to create two functions. The first function accepts integer inputs from the user until they are between 0 and 100. The seconds function display the validation to the stdOut.
However, it keeps giving me an error saying "Expected expression" when I call the function.
#include <stdio.h>
// function prototype for the function input
int input(int);
// function prototype for the function validate
int validate(int);
//main function
int main(void)
{
//calling the function input
input(int x)
//calling the function validate
validate(int y)
return 0;
}
// Function definition for input
int input(int a)
{
int r;
printf("Enter the int value of r\n");
scanf("%d",&r);
}
// Function definition for validate
int validate(int b)
{
int r;
if(r>= 0 && r<= 100)
printf("Valid number");
else
printf("Invalid");
}

There is at least one bug on almost every line of this program.
This is a standard problem for which there is a whole lot of incorrect advice out there (most importantly, only the strtol/strtoul/strtod family of functions should be used to convert strings to numbers; never use the atoi family and never use scanf) so I am going to give a complete worked example of how to write this program correctly, including proper use of comments.
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
long read_number_in_range(const char *prompt, long lo, long hi)
{
// a signed 64-bit number fits in 21 characters, +1 for '\n', +1 for NUL
char buf[23], *endp;
long rv;
for (;;) {
puts(prompt);
if (!fgets(buf, sizeof buf, stdin)) {
perror("stdin");
exit(1);
}
errno = 0;
rv = strtol(buf, &endp, 10);
if (endp != buf && (*endp == '\0' || *endp == '\n')
&& !errno && rv >= lo && rv <= hi) {
return rv;
}
// if we get here, fgets might not have read the whole line;
// drain any remainder
if (!strchr(buf, '\n')) {
int c;
do c = getchar();
while (c != EOF && c != '\n');
}
puts("?Redo from start");
}
}
int main(void)
{
long val = read_number_in_range("Enter the int value of r", 0, 100);
// do something with val here
return 0;
}
Read on for line-by-line nitpicking of the original program.
#include <stdio.h>
Correct.
// function prototype for the function input
Comment redundant with code.
int input(int);
Function signature incorrect (see comments on body of function).
// function prototype for the function validate
Comment redundant with code.
int validate(int);
Function signature incorrect (see comments on body of function).
//main function
Comment redundant with code.
int main(void)
{
Correct.
//calling the function input
Comment redundant with code.
input(int x)
Variables cannot be declared inside function call expressions.
Return value of function is ignored.
Missing semicolon at end of line.
//calling the function validate
Comment redundant with code.
validate(int y)
Value returned from input should be passed to validate, presumably, instead of a new uninitialized variable.
Variables cannot be declared inside function call expressions.
Return value of function is ignored.
Missing semicolon at end of line.
return 0;
}
Correct.
// Function definition for input
Comment redundant with code.
int input(int a)
{
Parameter a is unnecessary.
int r;
Correct.
printf("Enter the int value of r\n");
Minor: use puts when there is nothing to format.
scanf("%d",&r);
Never use scanf.
}
Missing return r;.
// Function definition for validate
Comment redundant with code.
int validate(int b)
{
Function has no return value, so should be void validate(int b).
int r;
Unnecessary variable.
if(r>= 0 && r<= 100)
r should be b on this line.
printf("Valid number");
else
printf("Invalid");
Minor: again, puts.
}
Correct.

You have some stray ints in your calls, they need to go.
The calls should probably be:
x = input();
validate(x);
You can't pass an integer to a function and expect it to change in the caller's context, that is not how C's pass-by-value semantics work. You should just return the number from input() instead, i.e. its prototype should be int input(void);.

You need to add semicolons at the end of each line of code in your main() function, and also remove the type specifier in the function calls. Also, don't forget to declare the variables x and y somewhere:
int main(void)
{
int x=0;
int y=0;
//calling the function input
input(x);
//calling the function validate
validate(y);
return 0;
}

I bet the compiler asks you for an expression in some place where you write a function declaration with a missing semicolon.
//calling the function input
input(int x)
//calling the function validate
validate(int y)
Neither of these is a function call.

#include <stdio.h>
int input(int*);
int validate(int);
int main(void){
int x;
input(&x);
if(validate(x))
printf("Valid number");
else
printf("Invalid");
return 0;
}
int input(int *r){
printf("Enter the int value of r\n");
scanf("%d", r);
return *r;
}
int validate(int r){
return r>= 0 && r<= 100;
}

Related

What is wrong with this code? It is showing the output correctly but ending with exit status 56. what does it mean?

#include<stdio.h>
#include<ctype.h>
#include <stdlib.h>
int main()
{
char n;
int a;
scanf("%s",&n);
if(isdigit(n))
{
a=atoi(&n);
if(a%2==0)
{
printf("EVEN");
}
else
{
printf("ODD");
}
}
else
printf("not a number");
}
it is showing an exit status 56 error along with the output. The question is to print given input is even or odd or it's not a number using nested if loop
You never return a value from main, so you invoke undefined behaviour and end up with an arbitrary return value. You should enable your compiler's warnings as it should warn about this.
In newer versions of C, a return 0; is implied for main. Your compiler does not appear to support this version of C.
As an aside, note that your program suffers from a buffer overflow. You allocate but a single character for the string to read in, which is too small for any useful input since the string-terminating NUL requires one character.
You need to use a large buffer. Even then, you'd need to add some protection against reading more than the buffer can handle.
Your int main() function is not explicitly returning any value, you have to return a value with the desired exit code at the end of your main function (or wherever you'd like to if you want to exit due to an error), you need to:
int main( void )
{
// your code here...
return EXIT_SUCCESS; // EXIT_SUCCESS => 0 (contained in stdlib.h)
}
you can also use these exit code macros to exit your program due to an error
int main( void )
{
int *ptr = calloc( 10, sizeof( *ptr ) );
if( ptr == NULL )
{
puts("Couldn't allocate memory");
exit( EXIT_FAILURE ); // or return EXIT_FAILURE; since you are in main function
}
return EXIT_SUCCESS;
}
and btw, your using a char variable to read a string, you should use %c in scanf since you are reading a single character
scanf("%c", &n);
You have several errors. Among other things, your code cannot give a 100% accurate result due to incorrect use of the isdigit() and atoi () functions. The isdigit() function works with a character, and the atoi() function works with a string. Depending on your conditions, there are several types of implementation.
According to the rules of the C language, the atoi( ) function returns a number-the result of converting a string if there is a number at the beginning of the string. If there is a non-digit character at the beginning of the string, the function returns zero. If the number in the string exceeds the maximum or minimum size of an int number, the function returns the maximum or minimum allowed number for the int type, respectively. The returned value depends on the hardware platform on which the program is running.
It differs from the atol () function by the type of the returned number and, as a result, the maximum long string to be converted. For atoi() , the returned number is of the int type, and, accordingly, the bit depth is 16 or 32 bits, depending on the hardware platform on which the program is running. For atol() , the returned number is of the long int type, which is always 32-bit, regardless of the processor architecture.
In accordance with this, I propose the following implementation of your task:
#include<ctype.h>
#include<stdlib.h>
#include<stdio.h>
int main(void)
{
char n[10];
int a;
scanf("%9s",n);
if((a=atoi(n))!=0)
{
if(a%2==0)
{
printf("EVEN");
}
else
{
printf("ODD");
}
}
else
printf("not a number");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int containsOnlyDigits(char s[100], int len)
{
int i, flag;
char ch;
flag = 1;
for (i = 0; i < len; i++)
{
if (s[i] >= '0' && s[i] <= '9')
continue;
flag = 0;
}
return flag;
}
int main()
{
char s[100];
int len, n;
scanf("%s", &s);
len = strlen(s);
if (containsOnlyDigits(s, len))
{
n = atoi(s);
if (n % 2 == 0)
printf("Even");
else
printf("Odd");
}
else
printf("Not a number");
return 0;

Function pointer not returning a value

We just learnt in school about function pointers in C and i wanted to try them out in a test program. The idea is obviously pretty simple. However, when i try to use result = (*funcPtr)(); i get an STATUS_ACCESS_VIOLATION exception but i can't tell what i did wrong. Any ideas on what i'm missing?
#include <stdio.h>
int fun1();
int fun2();
int (*funcPtr)(void);
int fun1() {
return 1;
}
int fun2() {
return 2;
}
int main(void) {
int input,result = 0;
scanf("%d",input);
if(input == 1) {
funcPtr = &fun1;
} else if(input == 2) {
funcPtr = &fun2;
}
result = (*funcPtr)();
printf("%d\n",result);
}
You forgot a & before input in the scanf. Therefore scanf writes not to the address of the input variable, but to its value (and the variable is uninitialized a this point).
Change that line to:
scanf("%d", &input);
to pass a pointer to input to scanf and not the value of input.
And as already pointed out by other users, do not forget to handle inputs other than 1 and 2. Otherwise you will call your uninitialized funcPtr variable, which most likly results in a Segmentation fault.

How to create multiple functions in C

I am new to C programming and I am trying to create functions. The first function executes but the second one doesn't.
#include <stdio.h>
char get_char();
int main(void)
{
char ch;
printf("Enter a character > ");
scanf("%c", &ch);
return ch;
}
int get_int()
{
int i;
printf("Enter an integer between 0 and 127 > ");
scanf("%d", &i);
return i;
}
For anyone else that arrives here, you may solve your problem, by making sure your main function is at the bottom of the file.
Why? Because if function a calls function b, a should be before b.
main is the entry point for your program. The C environment calls main when your program is executed. get_int() is not the name for an entry point, so the fact that you never call it directly or indirectly in main means it will never be executed.
You also didn't declare it before main meaning your compiler will warn about not finding it, but since get_int returns int it will link successfully regardless.
Fix:
int get_int();
int main ()
{
//...
}
int get_int()
{
//...
}
Your second function isn't called and it should be as follows:
int main()
{
int m = 10;
get_int(m);
return 0;
}
int get(int num)
{
int multiply = num * num;
return multiply;
}

Invalid conversion from int* to int using functions

I have this "simple" problem: I have in input 2 int numbers and i must output them in decreasing order.
#include <stdio.h>
#include <iostream>
int fnum()
{
int NUM;
scanf("%d",&NUM);
return NUM;
}
void frisultato(int x,int y)
{
if (x>y)
{
printf("%d",x);
printf("%d",y);
}
else
{
printf("%d",y);
printf("%d",x);
}
return;
}
int main()
{
int A,B;
A=fnum;
B=fnum;
frisultato(A,B);
}
I recieve an error at
A=fnum;
B=fnum;
my compiler says: invalid conversion from int(*)() to int.
This is the first time i use functions, what is the problem? Thank you!
Michelangelo.
A=fnum;
B=fnum;
You're not actually calling the function fnum here. You're attempting to assign a pointer to the function to the int variables A and B.
To call the function, do this:
A=fnum();
B=fnum();
Sorry, but since you seem to be new at programming, I couldn't help but refactor/comment on your code:
#include <stdio.h>
#include <iostream>
int fnum()
{
int num;
scanf("%d",&num);
return num;
}
void frisultato(int x, int y)
{
if (x>y)
{
printf("%d",x);
printf("%d",y);
}
else
{
printf("%d",y);
printf("%d",x);
}
/* No need to return in void */
}
int main()
{
/*
Variables in C are all lowercase.
UPPER_CASE is usually used for macros and preprocessor directives
such as
#define PI 3.14
*/
int a, b;
a = fnum(); //Function calls always need parenthesis, even if they are empty
b = fnum();
frisultato(a, b);
/*
Your main function should return an integer letting whoever
ran it know if it was successful or not.
0 means everything went well, anything else means something went wrong.
*/
return 0;
}
Also, don't sign your name on StackOverflow questions.

Undeclared identifier in C function

When I compile the following C function / program I get errors like "missing ';' before 'type' 'remainder' : undeclared identifier" - what is wrong with this function?
#include <stdio.h>
void conversionTo(int number,int base) {
if(number==0)
return;
int remainder=number%base;
conversionTo((number/base),base);
if(remainder<10)
printf("%c",'0'+remainder);
else
printf("%c",'a'-10+remainder);
}
int main() {
conversionTo(int number,int base);
return 0;
}
I'm not a C expert, but from experience very long ago I believe you cannot declare variables in the middle of a function.
Also, it's unclear what you are trying to do with the function / print statements.
Try this:
#include <stdio.h>
void conversionTo(int number,int base) {
int remainder=number%base;
if(number==0)
return;
conversionTo((number/base),base);
if(remainder<10)
printf("%c",'0'+ remainder); // Through the way ASCII works that gives the ASCII rep
// of the remainder.
else
printf("%c",'a'-10+remainder); // Hex digits (A-F).
}
int main() {
conversionTo(/*Any number here*/10, /*any base number here*/2);
return 0;
}
You need to defines variables, then they can be used.
So this:
int main() {
conversionTo(int number,int base);
return 0;
}
should become this:
int main(void)
{
int number;
int base:
number = 47;
base = 11;
conversionTo(number, base);
return 0;
}
Also non C99 compliant compilers do not like having variables declared in the middle of a context:
void conversionTo(int number,int base) {
if(number==0)
return;
int remainder=number%base; /* this would fail. */
conversionTo((number/base),base);
To get around this open another context:
void conversionTo(int number,int base) {
if(number==0)
return;
{
int remainder=number%base;
conversionTo((number/base),base);
if(remainder<10)
printf("%c",'0'+remainder);
else
printf("%c",'a'-10+remainder);
}
}
You have to invoke your function with a value or variable, not a declaration:
conversionTo(123, 10); // using constant value
or
int number = 123, base = 10; // variable declaration
conversionTo(number, base); // using variable
You are calling your function wrong - pass the arguments this way:
conversionTo(2,2); // assuming you want to convert 2 to binary
or
int number = 123, base = 10;
conversionTo(number, base); // note this is not the same number and base as in the definition of your conversionTo function
Full code:
#include <stdio.h>
void conversionTo(int number,int base)
{
if(number==0)
return;
int remainder=number%base;
conversionTo((number/base),base);
if(remainder<10)
printf("%c",'0'+remainder);
else
printf("%c",'a'-10+remainder);
}
int main()
{
conversionTo(2,2); // assuming you want to convert 2 to binary
return 0;
}
There are 3 things when using functions:
Function declaration / prototype - the prototype of your function is:
void conversionTo(int ,int );
Function definition:
void conversionTo(int number,int base /* Parameter list*/)
{
// your functionality
}
Function call where you pass your arguments to your function:
conversionTo(2,2);
The statement conversionTo(int number,int base); simply re-declares it. Try this even this will compile:
int main()
{
printf("Hello World");
int main();
}
conversionTo(int number, int base)
is the syntax for declaring which parameters the function can take. To actually call the function, you need to omit the type (assuming you have variables of the respective name)
int number = 5;
int base = 10;
conversionTo(number, base); // <-- no int here!
Or you can use numbers directly:
conversionTo(5, 10);
Your function definition number and base in your function declaration void conversionTo(int number, int base) are the names that the values passed to it will have inside the function. So, if you call conversionTo(2,5), 2 will be seen inside the function as number, while 5 will be seen as base.
If you want to use variables instead of contants to call the function, you could do that:
int main()
{
int base = 2;
int number = 5;
conversionTo(base, number);
return 0;
}
In this confusing example, the variables base and value have value 2 and 5, respectively. But as you pass them to the function, the values inside it will be number = 2 and base = 5. This shows that those variables are actually different, despite of having the same name.
You should compile with $CC -std=c99, to enable declaring variables in the middle of a block.
(see section 6.8 in the specification)
The declaration "int remainder" must come before any statements in a block.
A declaration may have an initializer as you have here.
You could do:
void conversionTo(int number,int base) {
if (number > 0) {
int remainder...
}
}
since the function will not work with negative numbers.
To fix the other bugs in the routine:
void conversionTo(int number,int base)
{
if(number>=0&&base>0&&base<=36)
{
int remainder=number%base;
number/=base;
if(number>0)conversionTo(number,base);
printf("%c",(remainder<10)?'0'+remainder:'a'+remainder-10);
}
}
This will print a 0 if number is zero and only recurse if needed.

Resources