but I read somewhere else that I couldnt do test[i] == test2[i] in C.
That would be really painful to compare character-by-character like that. As you want to compare two character arrays (strings) here, you should use strcmp
instead:
if( strcmp(test, test2) == 0) { printf("equal"); }
Edit:
- There is no need to specify the size when you initialise the character arrays. This would be better:
char test[] = "idrinkcoke";
char test2[] = "idrinknote";
- It’d also be better if you use
strncmp
– which is safer in general (if a character array happens to be NOT NULL-terminated).if(strncmp(test, test2, sizeof(test)) == 0)
Related Posts:
- How big can a 64 bit unsigned integer be?
- How to use execvp()
- How to use execvp() to execute a command
- How does strtok() split the string into tokens in C?
- What is *(uint32_t*)?
- what is Segmentation fault (core dumped)? [duplicate]
- How to convert an int to string in C?
- How to print in C
- Why should we typedef a struct so often in C?
- What is a string of hexadecimal digits?
- What is newline character — ‘\n’
- Why does ENOENT mean “No such file or directory”?
- What does the question mark character (‘?’) mean?
- makefile:4: *** missing separator. Stop
- Stack smashing detected
- Stack smashing detected
- How do I determine the size of my array in C?
- Why am I getting “void value not ignored as it ought to be”?
- Pointer Arithmetic
- What is the difference between C and embedded C?
- Undefined reference to pthread_create
- Difference between malloc and calloc?
- What is Bit Masking?
- segmentation fault : 11
- What are .a and .so files?
- So what does “return 0” actually mean?
- warning: assignment makes integer from pointer without a cast
- Why use bzero over memset?
- Warning comparison between pointer and integer
- How do I properly compare strings in C?
- Connect: Socket operation on non-socket
- How to correctly use the extern keyword in C
- what is the meaning of == sign?
- How do I use extern to share variables between source files?
- Cannot assign requested address – possible causes?
- ARM Assembler – How do I use CMP, BLT and BGT?
- Invalid read of size 8 – Valgrind + C
- error: called object is not a function or function pointer
- munmap_chunk(): invalid pointer
- warning: incompatible implicit declaration of built-in function ‘xyz’
- strcpy vs strdup
- Warning comparison between pointer and integer in C language
- No Symbol Table using GDB on Compiled Programs
- What is char ** in C?
- Difference between scanf() and fgets()
- Return char[]/string from a function [duplicate]
- How to convert integer to char in C?
- Is there a printf converter to print in binary format?
- Program received signal SIGPIPE, Broken pipe
- What is a bus error? Is it different from a segmentation fault?
- Difference between char* and const char*?
- How to read from stdin with fgets()?
- Output single character in C
- Get the current time in C
- Does connect() block for TCP socket?
- Converting a C program to MIPS
- C: linker command failed with exit code 1
- What’s the equivalent of new/delete of C++ in C?
- sizeof float (3.0) vs (3.0f)
- Get text from user input using C
- How to trigger SIGUSR1 and SIGUSR2?
- How do I lowercase a string in C?
- warning: expression result unused
- error: indirection requires pointer operand (‘int’ invalid)
- What is the difference between exit(0) and exit(1) in C?
- What does “%.*s” mean in printf?
- Display value found at given address gdb
- Array definition – Expression must have a constant value
- How to print a char array in C through printf?
- How to clear all the elements of array in C?
- Constant pointer vs Pointer to constant
- Variable warning set but not used
- Expected declaration specifier error in function
- strcmp not working
- C char* to int conversion
- error: aggregate value used where an integer was expected
- char pointers: invalid conversion from ‘char*’ to ‘char’?
- “Multiple definition”, “first defined here” errors
- Does C have a string type?
- Execution of printf() and Segmentation Fault
- What are .axf files?
- How does the strtok function in C work? [duplicate]
- printf format specifiers for uint32_t and size_t
- error: function returns address of local variable
- What is an simple example of copy_from_user
- What is `S_ISREG()`, and what does it do?
- typedef struct pointer definition
- Math constant PI value in C
- Return a `struct` from a function in C
- C: scanf to array
- The code does not execute properly. Try to figure out why
- What are “prototypes” in a C program?
- ld.exe: cannot open output file … : Permission denied
- How to read from input until newline is found using scanf()?
- What’s the difference between “mod” and “remainder”?
- What is char ** in C? [duplicate]
- undefined reference to `std::ios_base::Init::Init()’
- previous declaration of ‘function’ was here in C [duplicate]
- segmentation fault using scanf
- error: `itoa` was not declared in this scope