Checking if it is equal, Normal int and #define macro. but it doesn't work - objective-c

I have an iVar named,
int DATA_IN_TRANSIT;
and I have defined several macros, e.g.
#define PLACES 0;
When I do something like the following,
if(DATA_IN_TRANSIT == PLACES)
{
NSLog(#"Make LLVM Dance!");
}
I get a compiler error (expression expected) in the line if(DATA_IN_TRANSIT == PLACES)
I don't know why it's giving me an error? Am I doing something naive?

#define PLACES 0
but without ';'
otherwise you'll get
if(DATA_IN_TRANSIT == 0;)
{
NSLog(#"Make LLVM Dance!");
}

Related

Storing int values in an uint8_t array in code composer studio vs 5.4

I have a string in a uint8_t str[] array and I am trying to store the positions of characters within the str in another variable called uint8_t pos[]. The code is written in Code Composer Studio vs 5.4
I tried using sprintf(), type casting as well as changing the type of uint8_t pos[] to int pos[] as well as unsigned int pos[]. None of these work.
The code breaks at the sprintf statement and comes to a halt by reaching an undefined memory location. When I run in assembly after reaching sprintf statement, it gives an error saying that a source code for sprint.c cannot be found in location.
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include "tm4c123gh6pm.h"
#include <stdio.h>
void initHw()
{
.
.
}
int main(void)
{
// Initialize hardware
initHw();
char strRx[80];
int count =0;
int count_enter=0;
uint8_t posStr[80];
uint8_t typeStr[80];
int pos=0;
int len;
unsigned int j=0, argCount=0;
while(1)
{
if(count == 0)
{
putsUart0("Enter characters for the string\r\n");
}
if(count <= 80)
{
char c = getcUart0();
if(c=='\b')
if(count>0)
count--;
else
break;
if(c>=' ')
{
strRx[count]=c;
count++;
}
if(count==80 || c==13)//'\r')
{
count_enter++;
if(count_enter==1) //count==80 before carriage return
{
len = count;
strRx[count]='\0';
while(count!=80)
strRx[count++]='\0';
count_enter=0;
putsUart0("\r\nEntered string is:\r\n");
putsUart0(strRx);
putsUart0("\r\n");
}
j=0;
//char a[10];
for(pos=0; pos!=len; pos++)// strRx[pos]!='\0'; pos++)
{
char a[80];
if((strRx[pos]>='A' && strRx[pos]<='Z') || (strRx[pos]>='a' && strRx[pos]<='z'))
{
typeStr[j]='a';
//posStr[j]=pos;
a[j]=pos;
sprintf(a,"%u",pos); //source not found
//a[j]=pos;
//posStr[j]=sprintf("%c",a);
//posStr[j]=(uint8_t)a;//a;
while(strRx[pos]!='\0'&&((strRx[pos]>='A' && strRx[pos]<='Z') || (strRx[pos]>='a' && strRx[pos]<='z')))
{
pos++;
}
pos--;
j++;
}
else if(strRx[pos]>='0' && strRx[pos]<='9')
{
typeStr[j]='n';
a[j]=pos;
sprintf(a,"%u",pos);
//posStr[j]=pos;//a;
while(strRx[pos]!='\0'&&((strRx[pos]>='0' && strRx[pos]<='9')))
{
pos++;
}
pos--;
j++;
}
else
{
while(strRx[pos]!='\0'&&((strRx[pos]<'A' && strRx[pos]>'Z') && (strRx[pos]<'a' && strRx[pos]>'z') && (strRx[pos+1]<'0' && strRx[pos+1]>'9')))
pos++;
}
}
argCount=j;
while(j!=80)
{
typeStr[j++]='\0';
posStr[j++]='\0';
}
count = 0;
}//if(count==80 || c==13)//'\r')
}//if count<=80
}//while(1)
}//main
The "unable to locate sprintf.c" error probably just means that the debugger cannot locate the source file, which means it cannot show the source code in the debugger window. It's possible that you don't have the source code for sprintf.c and all you have is the precompiled library. Or maybe you do have the source code and the debugger is simply looking in the wrong path. If you have the source code then there may be a way to tell the debugger which path to find it.
But that is just a debugger display issue -- It is not what is causing your program to crash. The problem may be in your code but you'd have to share your code for us to identify that. Or the problem may be a couple other issues that can occur with the printf related routines.
1) printf routines can use a relatively large amount of stack space. So check for a stack overflow and increase the stack size if necessary.
2) Some embedded libraries provide multiple implementations of the printf routines such as "small", "no-float", and "full". The limited implementations use less code space but don't support all of the format specifiers. So make sure the implementation of sprintf that you've linked with supports all the format specifiers that you're actually using. Look through the project settings under linker or libraries for an option to select which version of printf is used.

ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]?

When I compiled the following program like:
g++ -O2 -s -static 2.cpp it gave me the warning ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result].
But when I remove -02 from copiling statement no warning is shown.
My 2.cpp program:
#include<stdio.h>
int main()
{
int a,b;
scanf("%d%d",&a,&b);
printf("%d\n",a+b);
return 0;
}
What is the meaning of this warning and what is the meaning of -O2 ??
It means that you do not check the return value of scanf.
It might very well return 1 (only a is set) or 0 (neither a nor b is set).
The reason that it is not shown when compiled without optimization is that the analytics needed to see this is not done unless optimization is enabled. -O2 enables the optimizations - http://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html.
Simply checking the return value will remove the warning and make the program behave in a predicable way if it does not receive two numbers:
if( scanf( "%d%d", &a, &b ) != 2 )
{
// do something, like..
fprintf( stderr, "Expected at least two numbers as input\n");
exit(1);
}
I took care of the warning by making an if statement that matches the number of arguments:
#include <iostream>
#include <cstdio>
using namespace std;
int main() {
int i;
long l;
long long ll;
char ch;
float f;
double d;
//6 arguments expected
if(scanf("%d %ld %lld %c %f %lf", &i, &l, &ll, &ch, &f, &d) == 6)
{
printf("%d\n", i);
printf("%ld\n", l);
printf("%lld\n", ll);
printf("%c\n", ch);
printf("%f\n", f);
printf("%lf\n", d);
}
return 0;
}

In Objective-C, how to test against macro definition value?

I'd like to use
#define IS_APP_FULL_VERSION NO
and the code
isAppFullVersion = IS_APP_FULL_VERSION;
to set instance variable. But is there a way to also do
#if IS_APP_FULL_VERSION == "NO"
// add some methods here
#endif
but it would give a compile error, and so is #if (IS_APP_FULL_VERSION == "NO"). Is there a way to check it against YES and NO? (check against the substitution value)
Update: it seems like > and == 0 is allowed, such as
#if IS_APP_FULL_VERSION == 0
so we can actually use 0 and 1 for false and true, and use == 1 to test, but it will be better if YES and NO can be used.
Update 2:
One possible solution turns out to be:
#define IS_APP_FULL_VERSION 1
#if IS_APP_FULL_VERSION
// add some methods here
#endif
isAppFullVersion = IS_APP_FULL_VERSION;
will all work, and we can just change 1 to 0 to toggle the code.
Your basic problem is that the macro processor #if statement only has one data type -- integer. All #if expressions are evaluated in integer, with undefined symbols being replaced with zero. String expressions (including comparisons) cannot be evaluated.
Actually, normally, I would just use a flag macro rather than a value one, since there's only two cases here:
#define APP_IS_FULL_VERSION
Either define or don't define that macro. Then, if you later wanted to include/exclude code based on that, you can use:
#ifdef APP_IS_FULL_VERSION
// Do something
#endif
(or #ifndef for the opposite case, obviously).
For the code segment, it's a simple matter of using that to select the correct code:
#ifdef APP_IS_FULL_VERSION
isAppFullVersion = YES;
#else
isAppFullVersion = NO;
#endif
I think, you're some confused comparison MACRO.
#define format is following it.
#define MACRO_NAME VALUE (VALUE is blank spaces are also fine. MACRO_NAME need is.)
if you want comparison compile-time following like it. this is comparison mean only defined vs no defined. not YES vs NO
#define IS_APP_FULL_VERSION 1
#if defined IS_APP_FULL_VERSION
NSLog(#"full-verison");
#endif
#define IS_APP_FULL_VERSION blahblah
#if defined IS_APP_FULL_VERSION
NSLog(#"full-verison");
#endif
If you want to build separate versions of. refer a following like it.
#define IS_APP_FREE_VERSION
#if defined IS_APP_FREE_VERSION
NSLog(#"free-version");
#endif
#define IS_APP_FULL_VERSION
#if defined IS_APP_FULL_VERISON
NSLog(#"full-version");
#endif
if you want comparison runtime following like it. this is comparsion normally.
#define IS_APP_FULL_VERSION 1
if(IS_APP_FULL_VERSION)
{
NSLog(#"full-verison");
}
#define IS_APP_FULL_VERSION 0
if(!IS_APP_FULL_VERSION)
{
NSLog(#"no full-version");
}

How to compare value of variable against #define

I have made the following #defines
#define GOLD 1;
#define SILVER 2;
later in my program I set up some variables
int source, target;
They are then set to the values:
source = GOLD;
target = SILVER;
I now want to compare in an if statement:
if(source == GOLD) {...}
But the compiler complains Expected ')' before ';' token. If I change the line to
if(source == 1) {...}
There is no problem with the compiler or the behavior. How do I reference the #define in the if statement to make myself clear to the compiler?
Because you have an errant ; after the #define GOLD 1 and #define SILVER 2. Remove them, your problem will go away.

Declaring and using a C function in Objective C

This must be very simple, but I can't figure out how to do this: I have a C-function to monitor current memory usage:
natural_t report_memory(void) {
struct task_basic_info info;
mach_msg_type_number_t size = sizeof(info);
kern_return_t kerr = task_info(mach_task_self(),
TASK_BASIC_INFO,
(task_info_t)&info,
&size);
if( kerr == KERN_SUCCESS ) {
return info.resident_size;
} else {
NSLog(#"Error with task_info(): %s", mach_error_string(kerr));
return 0;
}
}
Now, I would like to use it. How do I declare it in the .h?
I tried the (for me) obvious within the objective c methods:
natural_t report_memory(void);
Calling this somewhere in the code:
NSLog(#"Memory used: %u", rvC.report_memory());
The Compiler complains error: called object is not a function. Thus, I assume, the declaration is somehow wrong. I tried several options, but the best I could get was a runtime error...
How to fix this?
rvC.report_memory()
should be replaced with
report_memory()
since it is a C function.
If you want to use this function in other modules, you should also put in your header (.h) file this line
extern natural_t report_memory(void);