Why is the isalpha declaration giving me an error code - c

I'm trying to use the isalpha function from the <ctype.h> library. I have used other functions from other libraries in different files but I cant get isalpha to work. The error is:
test.c:9:21: error: expected ')'
int isalpha(int c);
^
The code I'm working on:
// Function Declarations
int isalpha(int c);
int main(void) {
char letter = get_char("Letter:");
bool yesno = isalpha(letter);
if (yesno == true) {
printf("True\n");
} else {
printf("False\n");
}
}
The Library is included in the header I just didn't know how to include the header here.
Thanks for the help

You should just include <ctype.h> and not redefine isalpha with an explicit prototype in your code. The reason you get an error is isalpha() is probably defined as a macro in <ctype.h> and this macro gets expanded as the compiler parses your prototype and the result of the expansion is a syntax error.
Note also that isalpha() should not be called with an argument of type char because it has undefined behavior for negative values on platforms where char is signed by default. Cast char arguments as (unsigned char) to avoid this undefined behavior.
Furthermore, the return value of isalpha is an int and the value 0 means false and any other value means true. Comparing isalpha(letter) == true is incorrect. Your code relies on the boolean conversion between int and bool, which is available since C99, but it is error prone to rely on this implicit conversion as this subtlety is probably beyond your skill level.
Here is a modified version:
#include <ctype.h>
#include <stdio.h>
#include <cs50.h>
int main(void) {
char letter = get_char("Letter:");
if (isalpha((unsigned char)letter) {
printf("True\n");
} else {
printf("False\n");
}
return 0;
}

Related

Can we define boolean without a value?

Can we define boolean without a value?
To use Boolean variables in C you need to #include <stdbool.h> in your headers.
You can very much define a boolean variable without a value, as evidenced by the following code (but note the second header include, see below for explanation):
#include <stdio.h>
#include <stdbool.h>
int main (void) {
bool u, t = true, f = false;
if (t) puts("t is true"); else puts("t is false");
if (f) puts("f is true"); else puts("f is false");
if (u) puts("u is true"); else puts("u is false");
return 0;
}
This outputs (for me):
t is true
f is false
u is false
However, you should be aware of the normal rules regarding initialisation of variables where you don't make it explicit. In the code given, u will hold some arbitrary value and, indeed, using a higher warning level of gcc makes that clear:
prog.c: In function ‘main’:
prog.c:8:8: warning: ‘u’ is used uninitialized in this function [-Wuninitialized]
8 | if (u) puts("u is true"); else puts("u is false");
| ^
Note that, if you want to use bool, true, and false, these are not keywords, they're instead defined in the stdbool header file so you need to include that.
Otherwise all you get is the _Bool type(1) and neither of the nice constants, so you'll have to define some yourself or use 0 and 1 (although any non-zero value converts to 1).
Since this is scarcely a step up for doing the same thing with int, you probably want to do it properly (i.e., use the header).
Note further that this is only available as of C99. The presence of an #include <conio.h> in your code may indicate that you're using a hideously outdated implementation of C (it was a common header for DOS-era compilers such as Turbo C).
If that is the case, it's likely you don't even have a stdbool header that you can use, so you'll have to stick with int, 0, and 1.
Alternatively, you may be able to create your own my_stdbool.h such as the following, assuming there's no naming conflict as previously mentioned (most code I've seen that defines the constants tends to use the upper-case FALSE and TRUE in keeping with commonly-used guidelines):
#ifndef XYZZY_STDBOOL_H_INCLUDED
#define XYZZY_STD_BOOL_H_INCLUDED
typedef bool int;
#define false 0
#define true 1
#endif
(1) Uglier, yes, but it ensures there's no clash between code that may have been written before this became part of the standard since names beginning with _X (where X is any upper-case letter or another _) are reserved for the implementation. Hence you shouldn't be using them.
In C, you can declare a boolean variable using the _Bool type. Following as an example. However, it is not a recommended method.
#include <stdio.h>
int main()
{
printf("Hello, World!\n");
_Bool k=0, l = 1, m = 324234;
printf("%d\n", k);
printf("%d\n", l);
printf("%d\n", m);
return 0;
}
You can further use a macro to define _Bool as bool.
#include <stdio.h>
typedef _Bool bool;
int main()
{
printf("Hello, World!\n");
bool k=0, l = 1, m = 324234;
printf("%d\n", k);
printf("%d\n", l);
printf("%d\n", m);
return 0;
}

implicit declaration of function 'enterChar' [-Wimplicit-function-declaration] [duplicate]

This question already has answers here:
Message "warning: implicit declaration of function"
(10 answers)
Closed 2 years ago.
I have two questions here regarding my C program:
1) In main(), the lines C = enterChar();, N = enterNum();, leftJustifiedPic(C, N);, rightJustifiedPic(C, N); are all giving me implicit declaration of function. What does that even mean? I'm used to Java and is it a little bit different in C with regards to the code?
2) In method enterChar(), Im getting conflicting types for 'enterChar' error and again do not understand what it means and why it happens. I'm working on Eclipse (Cygwin-GCC) if it has anything to do with the problem.
Could smb please detail me on this types of errors and warnings? I appreciate it!
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Welcome to the menu!");
printf("The menu is:\n1. Enter/Change Character\n2. Enter/Change Number\n3. Print Triangle Type 1(Left Justified)\n4. Print Triangle Type 2(Right Justified)\n5. Quit");
printf("\n");
printf("Now enter a number from the menu from 1 through 5: \n");
int num = 0;
scanf("%d", &num);
char C;
int N = 0;
switch(num){
case 1:
C = enterChar();
break;
case 2:
N = enterNum();
break;
case 3:
leftJustifiedPic(C, N);
break;
case 4:
rightJustifiedPic(C, N);
break;
default:
printf("Smth is wrong!");
}
return 0;
}
char enterChar(){
printf("Enter your input as a character. Only 'C' and 'c' are allowed!\n");
char input = 0 ;
scanf("%c", &input);
while(input != 'c' || input != 'C'){
if(input != 'C' || input != 'c'){
printf("You have to enter 'C' or 'c'. Try again!");
}
}
return input;
}
1) You haven't declared the functions before you use them, and the dialect of C that you are using has "implicit function declarations". This means the function are implicitly declared to return int and take any number of parameters of any type.
2) Because you have an implicit function declaration int enterChar(), that clashes with the definition char enterChar().
The solution is to provide function declarations before main().
char enterChar(); // and other function declarations
int main(void) {
....
}
// function definitions
char enterChar() { .... }
Depending on your use-case, it may be worth investigating using a more recent version of C, which doesn't have these implicit function declarations (e.g. C99 or C11)
When the prototype of a function is not declared, the compiler assumes that the return type is an int.
That is what it calls an implicit declaration.
and then, you go to declare enterChar that returns a char. Since the compiler had used an implicit declaration when it was called it in main, it complains about the conflicting types.
You can resolve that problem by providing an explicit declaration of the function before using it in main.
char enterChar();
It's a good practice to provide explicit declarations of all functions before they are used.
Functions must at least be declared before they are called; if possible, they should be defined before they are called.
When the functions are defined in the same source file from which they are called, move the definition of the function to precede the caller, like so:
char enterChar( void ) // void indicates function takes no arguments
{
// body of enterChar
}
int enterNum( void )
{
// body of enterNum
}
int main( void )
{
...
c = enterChar();
...
N = enterNum();
...
}
Yes, this makes the code read "backwards", but it eliminates some headaches.
When functions are defined in a different source file from which they are called, make sure you have a declaration of that function (using prototype syntax!) in place before the call. The declaration may appear at file scope (outside of any function), or within the function from which the call is made:
// enterChar and enterNum are *defined* in a different source file
int enterNum( void ); // declaration at file scope, valid for remainder
// of file
int main( void )
{
char enterChar( void ); // declaration within a block, only valid for
// duration of current block
...
c = enterChar();
...
}
In the case above, the declaration of enterNum is valid over the entire file, while the declaration for enterChar is only valid within the body of main; if any other function in the same source file wants to call enterChar, it must also have a declaration before the call. Either way, the declaration must precede the call.
The usual practice for handling declarations of functions defined in a different source file is to create a header file which contains the function declarations (not definitions!) and include that header in the file that defines the calling function(s):
/**
* main.c
*/
#include <stdio.h>
#include "utils.h" // contains declarations for enterChar and enterNum
int main( void )
{
...
c = enterChar();
...
N = enterNum();
}
Header file:
/**
* utils.h
*/
#ifndef UTILS_H // Include guard; prevents header file from being processed
#define UTILS_H // more than once per translation unit
char enterChar( void ); // void in the argument list indicates the function takes no arguments
int enterNum( void ); // an empty argument list in a declaration specifies that
// the function takes an *unspecified* number of arguments
// which is not the same thing, and not necessarily safe
#endif
Implementation file:
/**
* utils.c
*/
#include <stdio.h>
#include <stdlib.h>
#include "utils.h" // including our own header to make sure our declarations
// and definitions line up.
char enterChar( void )
{
// body of enterChar
}
int enterNum( void )
{
// body of enterNum
}
You'll notice that utils.c also includes utils.h. This is to make sure that our declarations and definitions are in sync.
You have "forward references" to undeclared functions. They need a function prototype, or implementing before being called. Without knowing what the function takes or returns, the compiler assumes int types, although I suspect some compilers will flag an error anyway.
You only posted one function, so I limit my example to that.
#include <stdio.h>
#include <string.h>
char enterChar(); // function prototype
int main(void)
{
char C;
//...
C = enterChar(); // function call
//...
return 0;
}
char enterChar() // function implementation
{
char input = 0;
//...
return input;
}

toupper tolower

How to use topper and tolower in the C language?
I've tried to run the program that I've made, it runs properly
the problem is since I should submit it to a website to check it whether it's right or wrong, every time I submit it, it says compile error.
I made the code on macbook, using Xcode and it says on my toupper and tolower code -- implicit declaration of function 'toupper' is invalid in C99
#include <stdio.h>
#include <string.h>
int main()
{
int input;
scanf("%d",&input);
int jumlahkata;
char kalimat[100];
for(int i=0;i<input;i++)
{
scanf("%s",kalimat);
jumlahkata=strlen(kalimat);
for(int j=0;j<jumlahkata;j++)
{
if(j%2==0 || j==0)
{
kalimat[j]=toupper(kalimat[j]);
}
else
{
kalimat[j]=tolower(kalimat[j]);
}
}
printf("%s\n",kalimat);
}
return 0;
}
toupper and tolower are defined in ctype.h. Simply include this file with the line #include <ctype.h>.
You need to include header <ctype.h> .
Also int jumlahkata; should be of type size_t as you store result of strlen in it.
Or don't use it (as also pointed out by #iharob Sir ) , it unnecessary. As it is string , just check for null character as a condition in loop.
You are mixing C with C++:
int input;
scanf("%d",&input); // in C, following the first executable statement you may not declare variables until the next block
int jumlahkata; // declaring the variable here is C++
char kalimat[100]; // declaring the variable here is C++
for(int i=0;i<input;i++) // declaring the variable here is C++

conflicting types for 'outchar'

I am a newbie to C programming. I'm learning by reading the chapters and doing the examples from the book "Teach Yourself C" by Herbert Schildt. I'm trying to run this program in Dev C:
#include <stdio.h>
#include <stdlib.h>
main()
{
outchar('A');
outchar('B');
outchar('C');
}
outchar(char ch)
{
printf("%c", ch);
}
but I get this error when I compile it:
20 1 C:\Dev-Cpp\main.c [Error] conflicting types for 'outchar'
21 1 C:\Dev-Cpp\main.c [Note] an argument type that has a default
promotion can't match an empty parameter name list declaration
15 2 C:\Dev-Cpp\main.c [Note] previous implicit declaration of 'outchar' was here
Please help me with this!
It's because you haven't declared outchar before you use it. That means that the compiler will assume it's a function returning an int and taking an undefined number of undefined arguments.
You need to add a prototype pf the function before you use it:
void outchar(char); /* Prototype (declaration) of a function to be called */
int main(void)
{
...
}
void outchar(char ch)
{
...
}
Note the declaration of the main function differs from your code as well. It's actually a part of the official C specification, it must return an int and must take either a void argument or an int and a char** argument.
In C, the order that you define things often matters. Either move the definition of outchar to the top, or provide a prototype at the top, like this:
#include <stdio.h>
#include <stdlib.h>
void outchar(char ch);
int main()
{
outchar('A');
outchar('B');
outchar('C');
return 0;
}
void outchar(char ch)
{
printf("%c", ch);
}
Also, you should be specifying the return type of every function. I added that for you.

What is wrong with usage of atof function?

int main()
{
char str[10]="3.5";
printf("%lf",atof(str));
return 0;
}
This is a simple code I am testing at ideone.com. I am getting the output as
-0.371627
You have not included stdlib.h. Add proper includes:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char str[10]="3.5";
printf("%lf",atof(str));
return 0;
}
Without including stdlib.h, atof() is declare implicitly and the compiler assumes it returns an int.
It could be undefined behavior.

Resources