Function getting redeclared [closed] - c

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 2 years ago.
Improve this question
#include<stdio.h>
#define MAX 10000
#define CHECK 1000
#define OPN 1
#define CLS 0
char program[MAX];
void si_qoute (void);
void do_qoute (void);
void getprogram (void);
int main(){
printf ("Type your code\n");
getprogram ();
si_quote ();
do_quote ();
return 0;
}
void si_quote (void){
int opn[CHECK], cls[CHECK], i, lcnt = 0, opn_cnt = 0;
for (i = 0; i < CHECK; i++)
opn[i] = cls[i] = 0;
for (i = 0; program[i] != '\0'; i++){
if (program [i] == '\n')
lcnt++;
if (program[i] == '\''){
opn[opn_cnt] = lcnt;
if (program[i + 1] == '\\' && program[i + 3] =='\''){
cls[opn_cnt++] == lcnt;
i += 3;
}
else if(program[i + 2] == '\''){
cls[opn_cnt++] == lcnt;
i += 2;
}
else
opn_cnt++;
}
}
opn[opn_cnt] = -1;
for(i = 0; opn[i] != -1; i++)
if (opn[i] && cls[i] == 0)
printf ("Single Quote opened at line %d not closed \n", opn[i]);
}
void do_quote (void){
int opn[CHECK], cls[CHECK], i, lcnt = 0, opn_cnt = 0;
for (i = 0; i < CHECK; i++)
opn[i] = cls[i] = 0;
for (i = 0; program[i] != '\0'; i++){
if (program[i] == '\n')
lcnt++;
if (program[i] == '\"'){
opn[opn_cnt] = lcnt;
while (program[++i] != '\"'){
if (program[i] == '\\' && program [i + 1] == '\"')
i++;
else if (program[i] == '\n'){
opn_cnt++;
lcnt++;
break;
}
}
if (program[i] == '\"')
cls[opn_cnt++] = lcnt;
}
}
opn[opn_cnt] = cls[opn_cnt] = -1;
for (i = 0; opn[i] != -1; i++)
if (opn[i] && cls[i] == 0)
printf ("Double inverted quoted opened at %d not closed \n", opn[i]);
}
void getprogram (void){
int i, c;
for (i = 0; i < MAX - 1 && (c = getchar()) != EOF; i++)
program[i] = c;
program[i++] = '\n';
program[i] = '\0';
}
I am getting and error which states implicit declaration of two functions si_quote() and do_quote(). What I am intending to do is to call these functions from main. While calling a function as per my knowledge we need to try write the function name along with the argument list within parenthesis. However as these functions doesn't accept any arguments, the parameter list is empty. I think the functions are probably getting redeclared with int as their return type. I can't understand what is really the issue with it. If it is getting redeclared as per my guess why is getprogram() still working fine. I would really appreciate if someone could help me out.

You have si_quote in one place and si_qoute in the other. These are not equal.
This simple typo makes the compiler think you declare (but not use) two of the functions and use (but do not declare) two other, completely unrelated functions.

Related

C program is skipping code statements while compiling and executing [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 2 years ago.
Improve this question
The following code is for searching a string or character in another string :-
`
#include<stdio.h>
#include<string.h>
int strindex( char primary[] , char searchfor[]){
int k;
for(int i = 0 ; primary[i] != '\0' ; i++){
for(int j = i , k = 0 ; (searchfor[k] != '\0') && (primary[j] == searchfor[k]) ; j++ , k++){
;
}
if( k > 0 && searchfor[k] == '\0'){
return i;
}
}
return -1;
}
int line( char *str ){
int i = 0;
char c;
for (;(c = getchar()) != '\n' && (c != EOF) ; i++){
str[i] = c;
}
if( c = '\n') str[i] = '\n';
str[++i] = '\0';
return i;
}
int main(){
char str1[100] , str2[100];
int i ;
puts("Enter the main string:-");
line(str1);
puts("Enter string to search for:-");
line(str2);
i = strindex(str1 , str2);
if( i >= 0){
printf("String found at index : %d \n",i);
}else{
puts("stirng not found.");
}
puts("");
return 0;
}
`
The compile result :-
PS E:\Workspace\C> gcc p1.c
PS E:\Workspace\C> ./a.exe
Enter the main string:-
this is a demo text
Enter string to search for:-
demo
PS E:\Workspace\C>
I am using visual studio code ,
As we can see that the "if-else" and "print" statements after the function call are not being executed . I have tried a lot , but could not make it . Please someone help me and tell me where I am going wrong ??
int k;
for (int j = i, k = 0; ...) { blahBlahBlah(); }
doSomethingWith(k);
That k in the for loop is not the k declared before it, it's a new variable that shadows (hides) the original. That's because using int inside the for loop is a new declaration for all variables following it.
Then, when it comes time to doSomethingWith(k), that's using the original k, as the other one has gone out of scope.
And, since that original k will have some arbitrary value, hilarity will ensue :-)
The quickest solution is probably to remove the declaration from the for statement:
int j, k;
for (j = i, k = 0; ...) { blahBlahBlah(); }
doSomethingWith(k);
In your case, that would be along the lines of:
int strindex(char *primary, char *searchfor) {
int j, k;
for (int i = 0; primary[i] != '\0'; i++) {
for (j = i, k = 0; (searchfor[k] != '\0') && (primary[j] == searchfor[k]); j++, k++);
if (k > 0 && searchfor[k] == '\0') return i;
}
return -1;
}
As an aside, this is also a problem (in line()):
if (c = '\n') str[i] = '\n'; // First = should be ==, anyway.
str[++i] = '\0';
It preserves the newline character so that you would be looking for "demo\n" in "this is a demo text\n", which won't be found. Better to just use:
str[i] = '\0';
And, on top of that, c should be an int, not a char. That's because it has to cater for all possible char values and EOF.

What are the mistakes i did in the code. It is not giving any output? [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 4 years ago.
Improve this question
What are the mistakes i did in the code ? It is not giving any output. The question is given in the image. I am using the ASCII variable set to print the set of alphabet and digits .
#include <ctype.h>
#include <stdio.h>
#include <string.h>
int main()
{
int i, j, temp, t, s, e;
char test[20], start = 'a', end = 'z';
scanf("%s", test);
for (i = 0; i < strlen(test) && (test[i] == '-'); i++);
for (; i < strlen(test););
{
if (isalpha(test[i]) != 0)
{
start = test[i];
for (; (i < strlen(test)) && (isdigit(test[i]) == 0); i++);
}
end = test[i - 1];
temp = end - start + 1;
if (isdigit(test[i]) != 0)
{
s = test[i];
for (; (i < strlen(test)) && (isalpha(test[i]) == 0); i++);
}
e = test[i - 1];
t = e - s + 1;
for (j = 0; j < temp; ++j, start++)
{
printf("%c", start);
}
printf("\n");
for (j = 0; j < t; ++j, s++)
{
printf("%c", s);
}
printf("\n");
}
return 0;
}
You have put a ; at the end of for loops and that's the reason the body of the for loop is not being executed as expected.
Effect of semicolon after 'for' loop
for (i = 0; i < strlen(test) && (test[i] == '-'); i++);
for (; i < strlen(test););
for (; (i < strlen(test)) && (isdigit(test[i]) == 0); i++);
for (; (i < strlen(test)) && (isalpha(test[i]) == 0); i++);
Remove the ; from the end of each for loop.
You should also remove the inner for loop as it has already mentioned in the outer for loop. Please check the body of each for loop properly.

Exercise 1-13: Why does this code not work in Code Blocks? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 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
Why does this code not work in code blocks...I can enter in text but I'm not able to print the desired histogram? The purpose of the code is to print a histogram. It's exercise 1-13 in the C Programming Language
#include <stdio.h>
#define MAXHIST 15
#define MAXWORD 11
#define IN 1
#define OUT 0
main()
{
int c, i, nc, state;
int len;
int maxvalue;
int ovflow;
int wl[MAXWORD];
state = OUT;
nc = 0;
ovflow = 0;
for(i=0; i < MAXWORD; ++i)
wl[i] = 0;
while(( c = getchar()) !=EOF) {
if (c==' '|| c=='\n'|| c == '\t' ){
state = OUT;
if (nc>0)
if (nc < MAXWORD)
++wl[nc];
else
++ovflow;
nc=0;
}else if (state == OUT){
state = IN;
nc = 1;
}else
++nc;
}
maxvalue = 0;
for (i = 1; 1<MAXWORD; ++i)
if (wl[i] > maxvalue)
maxvalue = wl[i];
for (i=1; i<MAXWORD; ++i){
printf("%5d - %5d : ", i, wl[i]);
if (wl[i]> 0){
if ((len=wl[i] * MAXHIST / maxvalue) <=0)
len = 1;
}else
len = 0;
while (len > 0 ) {
putchar('*');
--len;
}
putchar('\n');
}
if (ovflow > 0)
printf("There are %d words >= %d\n", ovflow, MAXWORD);
}
This line is a big problem:
for (i = 1; 1<MAXWORD; ++i)
You have a typo where you compare 1 to MAXWORD. The line should probably be this:
for (i = 1; i<MAXWORD; ++i)
Also not sure why you are incrementing i before you enter the loop instead of after.

Loop counter prints same value each time with different input [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
So this loop is supposed to count the number of numerical chars in a line, but it prints the same value each time (6356732). What am I doing wrong?
if((i >= '0') && (i <= '9'))
{
printf("%c\n", i);
count = count++;
}
count is just declared 'int count = 0;'.
edit; I made a change suggested below but the output hasn't changed?
while(fscanf(f, "%c\n", &i) !=EOF)
{
if((i >= '0') && (i <= '9'))
{
count = 0;
sum = 0;
printf("%c\n", i);
count++;
sum++;
}
}
edit 2; OK so I've got the program working as intended with the help of all you lovely folk! Thankyou very much!
while(fscanf(f, "%c\n", &i) !=EOF)
{
if((i >= '0') && (i <= '9'))
{
printf("%c\n", i);
count++;
}
}
count is defined at zero at the start of the program.
count = count++;
is undefined behaviour.
For your purpose, you just need:
count++;
Here's a simple example to read a line from stdin and count chars (which you can easily modify to suit your file reading and counting):
#include <stdio.h>
int main(void)
{
char line[256];
int count = 0;
int sum = 0;
size_t i = 0;
if (fgets(line, sizeof line, stdin)) {
while(line[i]) {
if((line[i] >= '0') && (line[i] <= '9')) {
printf("%c\n", line[i]);
count++;
sum = sum + line[i] - '0';
}
i++;
}
}
printf("%d %d\n", sum, count);
return 0;
}
You should just use count++; or count = count + 1;, because count = count++; is not defined. You can read more about the subject in this post.

Why there is an error? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
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.
Improve this question
I wrote the following code block I have all the time error in the function find_brackets and calculation. can someone explain to me how to fix it. And the two functions will function together
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void find_brackets(char str[], int len);
void calculation(char str1);
int main(void) {
int len;
char str1[99];
char str[99]; // (4/2)
printf("Enter a math exercises: \n");
gets(str);
len = strlen(str);
find_brackets(str);
calculation(str1);
}
void find_brackets(str[], len) {
char str1[len];
int i, j;
for(i = 0; i < len; i++) {
if(str[i] == '(') {
i++;
while(str[i] != ')') {
str1[j] = str[i];
i++;
j++;
}
}
}
}
void calculation(str1[], len) {
char str[len];
char strp[len];
char str2[len];
char str3[len];
char *rev;
int i, k, j = 0, aPos, zPos;
int sum1, sum2;
float sum;
strcpy (str, str1);
strcpy (strp, str1);
aPos = zPos = -1;
for(i = 0; i < len; i++) {
if(str[i] == '+') {
aPos = i;
}
else if(str[i] == '/') {
zPos = i;
break;
}
}
if(aPos != -1 && zPos != -1) {
for(k = 0, i = zPos-1; i > aPos; --i, ++k) {
str2[k] = str[i];
}
}
rev = strrev(str2);
printf("%s\n", rev);
for(i = 0; i < len; i++) {
if(strp[i] == '/') {
while(strp[i+1] != '+') {
str3[j++] = strp[++i];
}
}
}
printf("%s\n", str2);
sum1 = atoi(str2);
sum2 = atoi(str3);
sum = sum1 / sum2;
printf("%.0f\n", sum);
}
Thanks for the help I appreciate it
Function declaration is void find_brackets(char str[], int len); and the caller from main() is find_brackets(str); which is wrong. Where is the 2nd arg.
Also function calculation() has differnce in declaration and how it is invoked. Maintain a match in function formal arguments followed by callee actual arguments passed.
void find_brackets(str[],len)
void calculation(str1[],len)
please specify data type of len and str[] at function definations.
Also
find_brackets(str);
calculation(str1);
which pass one argument but you declared that with two which is also wrong.

Resources