You can write a Variadic Function
capable of receiving a string de formatação
as argument, enabling the manipulation of all primitive types in a standard way, see only:
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#define CMD_TAM_MAX (1024)
int systemf( const char * fmt, ... )
{
va_list args;
char cmd[ CMD_TAM_MAX + 1 ];
va_start( args, fmt );
vsnprintf( cmd, CMD_TAM_MAX + 1, fmt, args );
va_end(args);
return system(cmd);
}
int main( void )
{
int i = 123;
char ch = 'X';
char * txt = "Ola Mundo!";
float pi = 3.1415f;
double dbl = 1234567890L;
systemf( "echo int: %d", i );
systemf( "echo char: %c", ch );
systemf( "echo char*: %s", txt );
systemf( "echo float: %f", pi );
systemf( "echo double: %f", dbl );
return 0;
}
Exit:
int: 123
char: X
char*: Ola Mundo!
float: 3.141500
double: 1234567890.000000
See working on Ideone.com
To access the variáveis de ambiente
of your shell, you can use a main()
with a slightly different signature than usual:
int main( int argc, char **argv, char **envp );
Where char ** envp
is the list of all variáveis de ambiente
available in the format VARIAVEL=VALOR
, with a terminator NULL
indicating the end, see only:
#include <stdlib.h>
int main( int argc, char **argv, char **envp )
{
char **env = NULL;
for( env = envp; *env != NULL; env++ )
{
char * variavel = *env;
printf( "%s\n", variavel );
}
return 0;
}
Exit:
LANG=en_US.UTF-8
WORKSPACE=/tmp/a4a2fff3-ba98-4fda-9d1d-e31d83ac61ee
PWD=/home/0NxZg3
HOME=/home/0NxZg3
TMPDIR=/tmp/WpSkY2
TMPDIR_GLOBAL=/tmp/a4a2fff3-ba98-4fda-9d1d-e31d83ac61ee
SHLVL=0
PATH=/usr/local/bin:/usr/bin:/bin
See working on Ideone.com
To access a variavel de ambiente
specific within your program, you can use the function getenv()
of the standard library stdlib.h
, look at you:
char * valor = getenv("PATH");
printf( "PATH=%s\n", valor );
Missed the vice versa, the C picking things from the shell
– Jefferson Quesado
@Jeffersonquesado the question does not speak of this, it speaks of printing a letter.
– Maniero
Last paragraph: "What can I do to work with the content of a C variable for bash and vice versa??"
– Jefferson Quesado
It is that we can not know what he wants to do with it, deep down is an unclear (or broad) if we consider this. Put what you want to do and say no problem at all, you know that is not this tip ode question we wish here. And I’m seriously considering closing, and I probably should have done it sooner. I was in what he put and ignored the rest, I can not speculate what he wants. He may want to manipulate what the call of
system()
generated a result on the console or something.– Maniero