How to use scanf() without including stdio.h - c

Is there any possible methods to write a C program without including stdio.h as a header file. It was suggested that it can be implemented by declaring extern int scanf(char* format, ...);
#include <stdio.h> //I want this same code to work without including this line
int main ()
{
char str [80];
scanf ("%s",str);
return 0;
}

You can declare the scanf function with:
extern int scanf(const char *format, ...);
The extern keyword is optional but I like to include it as a reminder of the fact that the function is defined elsewhere.
Your example would then look like:
extern int scanf(const char *format, ...);
int main ()
{
char str [80];
scanf ("%s",str);
return 0;
}

In C-89, that code would compile without the #include, as function prototypes are optional.
Having said which, it comes under the list of 'really bad things to do' - scanf may be a macro, it might have one or more required parameters, ...
So you can do it, but it's like driving at night without any lights. You're liable to crash, even if you think you know the road.

Related

C Global Variable Code giving an compilation error

So I'm new to c and wrote some code but i'm not sure why i'm getting an error when i try to run it.
int GlobalVariable=0;
int main()
{
int LocalVariable=0; //can be used within main()
dis();
printf(GlobalVariable);
return 1;
}
int dis()
{
GlobalVariable=5; //Can be accessed in any functions and made changes to it
}
Here is the prototype of printf function:
int printf(const char * restrict format, ...);
And look what you are typing:
int GlobalVariable=0;
printf(GlobalVariable);
The problem is that you used a function without first telling the compiler about it.
In this case you must provide function prototype as the function definition itself is provided after main
int dis( void ); // function prototype
int main()
{
...
}
int dis() // function definition
{
...
}
Alternatively, you can put the function definition before main(). But usually it would be better to have function prototypes before main() and (usually) even better to put the prototypes in a separate header file - that way it'd be easier to look straight into the main program without being bother about other function details.

atof conflict while compiling code

I have just begun my journey with C programming. I have a problem with a lib conflict. Please find my sample code below.
#include <stdio.h>
#include <stdlib.h>
#define MAXLINE 100
//int getline declaration is here (removed for simplicity)
int main()
{
double sum, atof(char []);
char line[MAXLINE];
int getline(char line[], int max);
sum=0;
while (getline(line,MAXLINE)>0)
printf("\t%g\n", sum += atof(line));
40mi return 0;
}
When I run this code I get a following compilation error:
Error: conflicting types for 'atof'
I'm using CodeBlocks + mingw c compiler
Edit (in response to comment):
I want to download one argument and may return a double value , atof works without stdlib.h , but can not define getline
You don't need to declare atof if you include stdlib. It is declared in stdlib. The definition of atof in stdlib is double atof(const char *nptr). It is different of yours and this explains the conflict.
getline is defined in stdio.h and expects three arguments: ssize_t getline(char **lineptr, size_t *n, FILE *stream). You can't read from standard input with getline as you do. you have to use getline(line, MAXLINE, stdin). And don't declare it in main because it is already declared in stdio.h.
The declaration of atof() in <stdlib.h> is:
double atof (const char* str);
And you are declaring it as
double atof(char []);
The two declarations are different, hence, the error.

usage of strdup

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char *s;
s = strdup("foo");
printf("%s\n", s);
getchar();
return 0;
}
Looks pretty harmless, doesn't it ?
But my IDE, which is Dev-C++, gives my the following warning:
warning: assignment makes pointer from integer without a cast
The warning disappears if you would change the code like this:
char *s;
s = (char*)strdup("foo");
Can anyone help me explain this?
You're using Dev-C++, but strdup is not part of the C or C++ standard, it's a POSIX function. You need to define the proper (according to your IDE's documentation) preprocessor symbols in order for strdup to be declared by the header file ... this is necessary in order for the header file not to pollute the name space when included into conforming C or C++ source files.
For a simple portable alternative, consider
char* mystrdup(const char* s)
{
char* p = malloc(strlen(s)+1);
if (p) strcpy(p, s);
return p;
}
Or, if you know strdup is actually in the library, you can copy its declaration from string.h into your own source file or header ... or use the simpler declaration from the man page:
char *strdup(const char *s);
That's not right. strdup returns char * already. Something else is wrong. Probably because you did not include the right header file that declares the true return type for this function.
#include <string.h>
You're missing #include <string.h>. In the absence of function signatures, strdup is assumed by the compiler to return an int, hence the warning.
man strdup
you will get following things
#include<string.h>
char* strdup(const char * s);
so strdup() returns char* there shuld not be any problem
Actually in your case it takes implicit declaration of strdup() so by default return type is int hence you get this warning
Either include<string.h>
or
give forward declaration char* strdup(const char *);
Also don't forget to free(s) in last when all usage are done

How to read environment variable in Glibc code

I want to disable/enable printf() output to screen by reading an environment variable, similar to how LD_DEBUG works.
I want to control ./stdio-common/printf.c.
So if in environment variable says disable printf() it call
int
__printf (const char *format, ...)
{
return done;
}
else it executes the original code. How would I implement this?
Use getenv. See getenv(3) for details.
The recommended way is to use the solution provided by ANSI as getenv() function for maximum portability:
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
char * env = getenv(“PROMPT”));
if(env)
puts(env);
else
puts(“The environmental variable not available”);
return 0;
}
There are other non-standard ways of doing the same which is not recommended.
The third argument to main() could be char **envp is used widely to get the information about the environment and is non-standard.
int main(int argc, char **argv, char **envp)
{
while(*envp)
printf("%s\n",*envp++);
}
Using the third argument in main() is not strictly conforming to standard.
There is another widely used non-standard way of accessing the environmental variables and that is through the environ external variable.
int i=0;
extern char ** environ;
while(environ[i])
printf("\n%s",environ[i++]);
NOTE: The examples are in-complete and lacks error handling.

Why I can't pass two chars as function arguments in C?

I have a function:
int get_symbol(tab *tp, FILE *fp, char delim)
and I call it like this:
get_symbol(tp, fp, ';')
I always have it declared in the header as:
int get_symbol(tab *, FILE *, char);
No this all works fine, I can execute the code in the function and the delim is set.
But if I try to add one more char to the function's signature like:
int get_symbol(tab *tp, FILE *fp, char delim1, char delim2)
The function stops executing. Why would that be?
You should have :
int get_symbol(tab *tp, FILE *fp, char delim1, char delim2)
{
blah blah;
return 1;
}
...
...
get_symbol(tp, fp, ';','?')
do You?
OK, there's not enough information here, so I'm going to make a wild stab at an answer.
You're using a C++ compiler, and don't have warning levels set very high. You've changed the prototype for the function, but you've not changed the arguments when you call it. The C++ compiler is treating these as different functions due to overloading, and so is not calling the right one.
This may be way off what's happening. If it is, give us something more to go on….
As a guess at what "stops executing" could mean, did you update the signature in the header file as well?

Resources