I can't get the argument index format specifier on fprintf() to work when compiling C code on Windows with gcc-7.2.0-mingw.
Take the following program as an example:
#include <stdio.h>
int main(void) {
int x;
x = 10;
fprintf(stdout, "%1$d == %1$d\n", x);
return 0;
}
Let's compile and run it:
C:\path\to\dir>gcc -Wall -std=c89 -o main.exe main.c
C:\path\to\dir>main.exe
$d == $d
C:\path\to\dir>
While I expected the output to be 10 == 10 (Try it online!).
What's happening and how can I make the argument index specifier work properly?
Note: the same happens if I try to print strings, floats or anything else.
I'm try to override some libc function using LD_PRELOAD technique, but cannot make it work.
this is the strlib.c
#include <stdio.h>
size_t strlen(const char *s){
printf("take strlen for %s\n",s);
return 0;
}
gcc -shared -fPIC -o strlib.so strlib.c (.c file, no name mangle here)
and the main app
#include <string.h>
int main(){
const char* s = "hello";
printf("length=%d\n",strlen(s));
}
gcc -o main main.c
then start to run it
LD_PRELOAD=./strlib.so ./main
it run but seem that it did not call my override function
$ LD_PRELOAD=./strlib.so ./main
length=5
Did I do anything wrong here?
#edit: as Emest mention, a changed the main.c to avoid compiler optimize, but it still did not work.
#include <string.h>
int main(int argc,char** argv){
const char* s = "hello";
printf("length=%d\n",strlen(argv[1]));
}
$ LD_PRELOAD=./strlib.so ./main hehe
length=4
Check the assembly code ( use the -s argument to gcc.) The compiler will optimize out the strlen() call on the compile-time constant string "hello", computing the length at compile time instead. Try calling your function on a string whose length isn't known until runtime, like one of the arguments to main(), and this should work as you expect.
I wrote little program to print "Hello world" in C. I'm not a C programmer, but I liked to try it. In my program there is an error. Please tell me what is it?
This is my program:
int main(){
printf("Hello World");
}
I wrote this with my Java Experiences. I can't find what is wrong.
You can't directly use printf() function as in Java. You should tell the compiler that you are going to use the input/output stream. You can tell it in this line:
#include <stdio.h>
and also you should enter this line at the end of the source code:
return 0;
this will tell the compiler :
"If the program succeed It will return 0 otherwise It will return any other number"
This means if your program is successful main() function will return 0. Then the compile know the program is Ok.
Then at last your complete code is:
#include <stdio.h>
int main() {
printf("Hello world");
return 0;
}
To compile this and see the word "Hello World", just save this file as a .c file and Open cmd in your program directory and type
gcc hello.c -o hello && hello
(Replace the 'hello.c' with your filename, and 'hello' with the name you want to put with your .exe file)
Remember My computer is Windows. And this compile code is for windows. If your OS is UNIX like OS. then use this code to compile:
gcc hello.c -o hello
./hello
A full hello world program in C:
#include <stdio.h>
int main(void) {
printf("Hello World\n");
return 0;
}
Then compile (assuming gcc) and execute it:
gcc -o test test.c
./test
First, you have to use a header file.
#include <stdio.h>
What that does is bring up a header file with a bunch of commands in them. That will make it recognize the "printf" piece of code.
Next, you have to close your program. By not having a closing statement, the program will not compile because it doesn't know if that is the end of the code. Use this at the end of your program...
return 0;
That closes the program, meaning that the compiler can stop looking for other code. You may also want to look at some programming manuals (there are dozens of free online ones) to learn about the syntax.
One last thing: Most pieces of C code require a semicolon at the end. This is not true for the "int main" statement, nor is it for the header file which I have defined above. The "return" function that closes the program, does however, need a semicolon.
Hoped this helped.
Should also include a pause at the end:
#include <stdio.h>
int main(void) {
printf("Hello World\n");
//Read a character from the console
getchar();
return 0;
}
Just like import in Java programs, in here you have to include libraries you're using in your program. You have used library function printf, but not included stdio.h.
I agree there are many ways to write one of the simplest way is
#include<stdio.h>
int main(void){
printf("Hello World\n");
return 0;
}
You can even use different ways as suggested above.
You should first look at the structure of "main". Try to understand the various parts as already explained so well in the above answers.
"#include" : The preprocessing directives to be included in the program. But why? Because you are trying to use the functions defined inside them.
int : The return type of "main" program. But why? Because the function calling "main" needs to know if the "main" program has functioned correctly.
main : The entry point of your code. Dont ask why here :-)
main( void ) : To tell the compiler that we are not passing any arguments to program "main"
return 0 : Beacuse you promised "main" that you will return something if "main" will function properly.
Finally the code:
#include <stdio.h>
int main( void )
{
printf( "Hello World\n" ) ; //Notice the '\n' here. Good coding practice.
return 0 ;
}
#include <stdio.h> //Pre-processor commands<br/>
void main() //Starting point of the program<br/>{ //Opening Braces
printf("Hello World\n"); //Print Hello World on the screen<br/>
return 0;
} //Ending Braces
Check it once it will work, I have written it with comments:
#include<stdio.h> //Pre-processor commands
void main() {
printf("Hello World\n"); //Print Hello World on the screen
}
A full hello world program in C:
#include <stdio.h>
int main(void) {
printf("Hello World\n");
return 0;
}
Then compile (assuming gcc) and execute it:
gcc -o test test.c
./test
You can't use printf() function as in Java. You have to tell the compiler what you are going to use.
You can tell this as follows:-
#include <stdio.h>
You must enter this line in last:-
return 0;
Then Your complete code is:-
#include <stdio.h>
int main(){
printf("Hello World");
return 0;
}
For compiling this and see the word "Hello World", just save this file as a .c file and Open cmd in your program directory and type:-
gcc hello.c -o hello && hello
(Replace the 'hello.c' with your filename, and 'hello' with the name you want to put with your .exe file)
Remember My computer is Windows. So I can compile only for Windows OS.
#include <stdio.h>
int main() {
// printf, used to print (display) Hello World
printf("Hello World ! ");
// return 0, as the main function is of type int so it must return an integer value
return 0;
}
I want to write a C program for implementing the include functionality of the preprocessor.
Example:
In header.h I have this code:
char *test (void);
And in program.c:
int x;
#include "header.h"
int
main (void)
{
puts (test ());
}
The input is program.c.
The output must be :
int x;
char *test (void);
int
main (void)
{
puts (test ());
}
How can I do this?
You'd need to read the input file line by line. Check to see if the line starts with #include (with optional leading whitespace). If not, print the line you've read. If so, open the specified file instead, and run this same algorithm on it (to handle secondary #includes).
you can run gcc -E "your source files", and then filter the line included "#",the left is your wanted, and the macros is replaced by its real forms.
for example:
gcc -E hello.c|sed '/# .,$/d' > a.c
a.c is your wanted file.
when use gcc
gcc -E -P program.c
or
cpp -P program.c
when use Visual c
cl /EP program.c
I have an example code like this:
int var;
var = MACRO_A;
I expect the MACRO_A has been defined like this:
#define MACRO_A 1234
However, I can not find the MACRO_A defined anywhere in the source code, but I can build the code successfully, so what is the value of var at the end ?
The compiler is gcc.
However, I can not find the MACRO_A defined anywhere in the source
code, but I can build the code successfully, so what is the value of
var at the end
It's there somewhere or it wouldn't compile. Perhaps it's defined directly on the command line ? (-DMACRO_A)
That macro could have been passed during compilation with -D option.
#include <stdio.h>
int main()
{
int i = MACRO_A;
printf("MACRO_A : %d \n", i);
return 0;
}
Output:
$ gcc macro.c -D MACRO_A=10
$ ./a.out
MACRO_A : 10
$