When I compile this code I get an error "in front of int val, there isn't" ;
how can I get rid of this error?
#include <stdio.h>
#include <stdlib.h>
int main()
{
char card_name[3];
puts("카드 이름을 입력하세요: ");
int val = 0;
if(card_name[0]=='K') {
val = 10;
}
else if (card_name[0] == 'Q') {
val = 10;
}
else if (card_name[0] == 'J') {
val = 10;
}
else if (card_name[0] == 'A') {
val = 11;
}
else
{
val = atoi(card_name);
}
printf("카드값은 다음과 같습니다 : %i/n", val);
return 0;
}
Declare all variables in the top of main just after { ,i.e, declare val before the first puts. It is because your compiler uses C89 which forbids mixed declarations and code. From C99 onwards , they can be declared (almost) anywhere.
As mentioned in other answers, C89 does not support declaring variables other than at the start of the block. If you are using clang or gcc, you might want to add '-std=gnu99' to your CFLAGS. If using another compiler or an IDE, look for the language and change it to C99 or higher.
It seems that the compiler requires that all definitions of varaibles would be in the beginninh of block.
Try to write
char card_name[3];
int val = 0;
puts("카드 이름을 입력하세요: ");
Also take into account that array card_name is not initialized.
Related
I am trying to use recursion in a function and for that I have to use local variables. The compiler gives error c141 in the line where I am defining my local variable.
int minimax(int board[9], int player) {
int winner;
winner = win(board);
if (winner != 0) return winner*player;
int moveminimax;
moveminimax = -1;
int scoreminimax;
scoreminimax = -2;
int i3;
for (i3= 0; i3 < 9; ++i3) {//For all moves,
if (board[i3] == 0) {//If legal,
board[i3] = player;//Try the move
int thisScore;
thisScore = -minimax(board, player*-1);
if (thisScore > scoreminimax) {
scoreminimax = thisScore;
moveminimax = i3;
}board[i3] = 0;//Reset board after try
}
}
if (moveminimax == -1) return 0;
return scoreminimax;
}
6-3-17 4 01pm.c(116): error C141: syntax error near 'int'
//c(116) is the where int winner is defined
When i define my variables globally in the beginning of program the error goes away.
My guess is that the Keil C compiler is not following the C99 standard, where variables could be defined anywhere, but instead follow the older C89 standard where local variables only could be defined at the beginning of block.
That means code like
int winner;
winner = win(board);
if (winner != 0) return winner*player;
int moveminimax;
moveminimax = -1;
int scoreminimax;
scoreminimax = -2;
int i3;
is invalid since it contains mixed declarations and statements.
Two of the statements can be removed completely by initializing the variables when you declare them, which leaves the function call and if statement that needs to be moved.
Try this instead:
int winner;
int moveminimax = -1;
int scoreminimax = -2;
int i3;
winner = win(board);
if (winner != 0) return winner*player;
I'm trying to convert character of a string to uppercase letters
int main (void)
{
int i = 0;
int n = 0;
static char *str[] = { "wow",
"RACEcar",
"No devil lived on.",
"rotor" };
for(i = 0; i < strlen(*str); i++)
{
if(str[i] != NULL)
{
n = function(str[i]);
}
}
return 0;
}
int function(char* x)
{
int i = 0;
int j = strlen(x);
char c;
for(i = 0; i < j; i++)
{
c = toupper(x[i]);
x[i] = c;
}
return 0;
}
I got an error saying exc bad access, code 2 at line where x[i] = c;
I'm not sure why I get this error, do I need to create another string and assign c to the new string?
toupper return the uppercase version of the character but didnt actually change the element itself, so I'm not sure what wrong with assigning the value return by toupper back to the element.
Your code attempts to modify a string literal , which causes undefined behaviour.
The string "No devil lived on." is non-modifiable. To help the compiler catch the error you should declare the array as:
static char const *str[] = { "wow", // etc.
For historical reasons, the compiler has to let it pass without breaking compilation if you forget to include the const. But it is still an error nonetheless, and some compilers will warn anyway.
For gcc you can use the flag -Wwrite-strings to disable support for the historical case; this will cause an error message to be generated for your code as-is.
My task is to convert a float or integer value to string in C. I can't use sprintf since I am working in embedded platform. So I thought to use something like this.
#define CURRENT(STRING,S_NO,VALUE) (str = "S"#S_NO#VALUE"A")
and invoking it like this
int a=10,b=20;
CURRENT(str,a,b);
So str should be S1020A. But I am getting SabA.
What am I doing wrong here?
Macros are evaluated before compilation so CURRENT(str,a,b); is expanded using the variable names a and b rather than their values which may only be available at runtime.
To convert an int to a char array at runtime without use of sprintf etc. use itoa if available or you could write a function like the following (untested!)
#define MAX_INT_STRING_BYTES (11)
void IntToString(int val, char* str)
{
char reversed[MAX_INT_STRING_BYTES]
int index, i = 0;
bool negative = false;
if (val == 0) {
*str++ = '0';
*str = '\0';
return;
}
if (val < 0) {
negative = true;
}
while(val != 0) {
reversed[index++] = (char)('0' + abs(val % 10));
val /= 10;
}
if (negative) {
*str++ = '-';
}
for (i = index; i > 0; i--) {
*str++ = reversed[i - 1]);
}
*str = '\0';
}
It only knows what the value of a and b is at runtime, and preprocessor directives are resolved at compile time. Thus what you're trying to do won't work.
You can however do something like:
#define CURRENT(STRING,S_NO,VALUE) sprintf(STRING, "S%i%iA", S_NO, VALUE)
Or just make it a function.
Or just call the sprintf directly instead of CURRENT.
I want to do something like this:
if([FUNCTION] > 3){
//do stuff
}
where FUNCTION is a function that performs some action and returns the result as an int. For example, function can be defined as:
a + 1;
return a;
where a was a previously defined variable. Is there any way to do this in C? Many thanks!
First of all
a + 1;
is a statement with no effect, so I'm assuming you meant a = a + 1. Then yes, you can of course call a function from within an if statement.
int foo(int* a) {
*a = *a + 1;
return *a;
}
// later ...
int a = 7;
if (foo(&a) > 3) {
// do stuff
}
If a is not known in the scope of the if statement, you probably meant something like this:
int a = 7;
int foo() {
a = a + 1;
return a;
}
// later ...
if (foo() > 3) {
// do stuff
}
Since grammatically, C allows if ( expression ) and a function call on the left of a relational expression with > is an expression as well, the answer is yes, you can do this in the straightforward way,
if (some_function() > 3) {
/* Do stuff. */
}
What kind of C book is it that doesn't make this clear? I highly recommend Kernighan, Ritchie, The C Programming Language, 2nd Edition (update for ANSI/ISO C89).
#include<stdio.h>
int a=5;
int foo(int a)
{
a++;
return a;
}
int main()
{
if(foo(a) > 3) {
printf("%d\n",a);
// Do stuff
}
return 0;
}
It's simple.
EDIT :It's just a demo source code to show that if condition returns true.
I'm writing my first program in C for a class; I've managed to even out most of the syntax errors, but I'm getting a strange error when gcc tries to link the object files together. It prints exactly like below:
gcc -o proj04.support.o proj04.driver.o
Undefined first referenced
symbol in file
convert proj04.driver.o
I've looked around for a few answers, but none really make sense to me. I'll post the files I'm using to make the program below, and if you've got the answer I would really appreciate the help. It seems to be a pretty basic error, so it's probably something silly I didn't do.
Makefile (posting this first because I suspect the issue is here)
# Comments
# Comments
proj04: proj04.support.o proj04.driver.o
gcc -o proj04.support.o proj04.driver.o
proj04.support.o: proj04.support.c
gcc -Wall -c proj04.support.c
proj04.driver.o: proj04.driver.c
gcc -Wall -c proj04.driver.c
Header file (provided by the professor, unchangeable, one line long):
int convert( int, unsigned, char[], int )
Implementation file
#include <stdio.h>
#include "/user/cse320/Projects/project04.support.h"
#include <string.h>
void formatdisplay( char[], int );
int convert( int I, unsigned base, char result[], int display )
{
int quotient, dividend, remainder;
const int divisor = base;
int count = 0;
char ending[] = " base ";
dividend = I;
remainder = 0;
quotient = 1;
while (quotient != 0)
{
if (count <= strlen(result))
{
quotient = (dividend / divisor);
remainder = (dividend % divisor);
//convert to ascii char
result[count] = remainder;
count++;
}
}
formatdisplay ( result, display );
strrev(result);
if ( I >= 0 ) { result[0] = '+'; }
if ( I < 0 ) { result[0] = '-'; }
printf( "%s" , strcat (result, ending));
}
void formatdisplay ( char str[], int disp )
{
if ( disp < 0 )
{
unsigned i = 0;
for ( i; i < strlen(str)-1; i++)
{
if ( str[i] = '\0') { str[i] = '0'; }
}
}
if ( disp >= 0 )
{
unsigned i = 0;
for ( i; i < strlen(str)-1; i++)
{
if ( str[i] = '\0') { str[i] = ' '; }
}
}
}
Driver file (not really implemented yet)
#include <stdio.h>
#include "/user/cse320/Projects/project04.support.h"
int main () {
char Result1[32];
int T = convert(10, 2, Result1, 1);
}
Yes, the problem is probably in the Makefile:
proj04: proj04.support.o proj04.driver.o
gcc -o proj04.support.o proj04.driver.o
The -o option to gcc takes an argument, the output filename. So this is asking gcc to link the file proj04.driver.o, producing an output file of proj04.support.o.
gcc -o proj04 proj04.support.o proj04.driver.o should work better.
I had the almost the same problem.
Undefined first referenced symbol in file isThreeOfAKind /var/tmp//ccIQWbaj.o
My problem was that I had missed on a letter in one of my functions, so the declaration and the function misaligned. Ex:
void isThreeOfAKind (void);
void isThreeOfAkind {}
Missed the uppercase K, and wrote lowercase k instead.
After I changed the k to a uppercase K it compiled fine.
I dont know if it helps, but it could be something as easy as that.
I am no expert but as Andreas Joensson (who, judging by function name is writing the exact same program I am) says above this problem seems to occur when there's some mismatch between declaration and use of a function.
Your function convert is declared as returning an int but I find no return value. That might be the problem.