print multiple parameters in c - c

I have this function takes more than one char parameters.how to print each of them and add a '\n' at the end of each char?
void printAndSave(char* msg,...)
{
//printing single one.
//printf("Log:%s\n",msg);
//saveToLog(msg);
//how to print all of them?
}

I think you will need to change the signature of printAndSave() to specify the number of char* arguments being passed. For example:
void printAndSave(const unsigned int arg_count, ...)
{
unsigned int i;
char* val;
va_list vl;
va_start(vl,arg_count);
for (i=0;i<arg_count;i++)
{
val=va_arg(vl,char*);
printf ("%s\n",val);
}
va_end(vl);
}

Look for example at the end of this page. You should easily adapt it for your problem ;) Please note you have to know the type of them.

Related

I want to show integer while using write function

1) I need to create a function that displays the number entered as a parameter. The function has to be able to display all possible values within an int type variable.
2) and prototype should be like void ft_putnbr(int nb); this way
Here is my code,
#include <unistd.h>
void ft_putnbr(int nb)
{
write(1, &nb, 1);
}
int main()
{
ft_putnbr(42);
}
And the result was not integer numbers, it's just like.. asciicode
It showed "*"
What's wrong with my code? Can you correct my code? or suggest more appropriate way?
plus) int main function will be given automatically, so I just have to make prototyped function
Thanks for your help
The reason your code is not working as expected, is because you passing an integer when you supposed to passing a pointer to the write function and the last argument of write should be the length of what you writing to the file descriptor (standard output in your case).
Here is an explanation of C pointers that may help you understand how to use them
https://boredzo.org/pointers/
Here are some resources on the write function.
https://en.wikibooks.org/wiki/C_Programming/POSIX_Reference/unistd.h/write
http://codewiki.wikidot.com/c:system-calls:write
You need to treat as a string before using write, so you could convert it to a string before had or print it digit by digit
Here is how you could approach it
void ft_putnbr(int n) {
char * const str_num = itoa(n);
const size_t len = strlen(str_num)
write(1, str_num, len);
}
You need to build a string representation of the number, otherwise you're printing the binary.
void ft_putnbr(int nb)
{
char nbuf[16];
const int len = snprintf(nbuf, sizeof nbuf, "%d", nb);
write(1, nbuf, len);
}

Functions keep returning 0

I have a three functions one inside another, they use the same inputs as it shown in my code, every time I printf the inputs after I pass the three functions I get a "0" as a value.
I tried to delete the "free()", as I thought it's because of it that my inputs get a 0 as values, but if I do, the code freeze.
char passcode[20];
void configuration(int **d, char *c) {
*d = malloc(sizeof(int));
**d = 1;
readn(*d, c);
return;
}
void readn(int *d, char *c) {
*d = 2;
configuration_SYS(d, c);
return;
}
void configuration_SYS(int *d, char *c) {
strcpy(c, "1234");
*d = 3;
return;
}
void main() {
int *Timeout;
configuration(&Timeout, passcode);
printf("%d\n", *Timeout);
printf("%s", passcode);
}
I expect to get a values different than 0 when I printf the two variables.
Timeout should equal 6 and passcode = "1234".
You do not need to use malloc() every time you use a pointer. malloc() allocates space for your use. NOTE: this space is not necessarily initialized (meaning that the data could be any value)
There is no need to return; at the end of a void function. The closing brace will return for you.
You call configuration_SYS(char* abc, int* d, char* c) with two arguments, yet the function takes three parameters.
As it is, I cannot compile this program. I'm not sure why it prints 0s for you. I would work on creating a Minimal Complete Verifiable Example

Printing integers passed as arguments in function in C

I am very new to C programming and having trouble compiling what should be a very simple function. The function, called printSummary, simply takes 3 integers as arguments, then prints some text along with those integers. For example, if hits=1, misses=2, and evictions=3, then printSummary(hits,misses,evictions) should print the following:
hits:1 misses:2 evictions:3
Here is the code I'm using. Thanks in advance for any advice.
#include<stdio.h>
void printSummary(int hits, int misses, int evictions)
{
printf('hits: %d\n');
printf('misses: %d\n');
printf('evictions: %d\n');
}
int main()
{
int hit_count = 1;
int miss_count = 2;
int eviction_count = 3;
printSummary(hit_count, miss_count, eviction_count);
return 0;
}
Compiling this code gives me several warnings, but no errors. When I run the code, I get a segmentation fault. Like I said, I am fairly new to C so there is most likely a simply solution that I am just missing. Thanks in advance for any advice.
Make the below changes .
printf("hits: %d\n",hits);
printf("misses: %d\n",misses);
printf("evictions: %d\n",evictions);
printf has a
int printf(const char *format, ...)
prototype. So in the first argument you can pass format specifiers and in the next provide the actual variables/values to be printed out
errors are:
void printSummary(int hits, int misses, int evictions)
{
/* Name: printf
Prototype: int printf (const char *template, ...)
Description:
The printf function prints the optional arguments under the
control of the template string template to the stream stdout.
It returns the number of characters printed,or a negative value if
there was an output error.*/
printf("hits: %d\n", hits); // don't use ' it is used only for char variable for example: char a = 'c';
printf("misses: %d\n", misses);
printf("evictions: %d\n", evictions);
}
Your printf function is not being called correctly. You have to include the integers needed to print:
printf("hits: %d\n", hits);
printf("misses: %d\n", misses);
printf("evictions: %d\n", evictions);
Read more about the printf function here.

C - passing argument 1 of 'send_data' makes pointer from integer without a cast

Im writing uart code for a microcontroller. My code is working fine with passing string("..."), but giving problem when i try to passing the char('.'). Yes, it is i've declared "s" a pointer for string, but there is a chance of passing char value. Is there any possibilities to pass both string and char values in send_data?
#include <stdio.h>
void send_data( char *s)
{
while(*s!='\0')
{
send_dt1(*s);
s++;
}
}
void send_dt1( char in_c)
{
printf("%c",in_c);
}
int main(void)
{
send_data("sample_data"); //fine
send_data('Q'); //warning, no data displaying
return 0; /* terminate the program */
}
Thanks..
As send_data expects s to be a null-terminated string anyway, I would just recommend using a string:
send_data("Q");
Notes:
you should pass a const char* around as long as you don't modify the string at s
send_data('Q'); //warning, no data displaying
Don't pass character to it . It expects a char * .
You can do this -
send_data("Q");
Also you should declare a prototype for function void send_dt1( char in_c) as you make a call to it before it's definition in function void send_data( char *s) .
This will also generate warning.
You can't pass a char to a function that expects a char *.
If you really need to be able to pass both, you'll need another function to accept a char and put it in a string so that send_data can be called:
void send_char_data(char c)
{
char s[2];
s[0] = c;
s[1] = '\0';
send_data(s);
}
....
int main(void)
{
send_data("sample_data");
send_char_data('Q');
return 0;
}

C : write a function doesn't know how many parameter

When learning C I see that printf can receive many arguments as it is passed.
And I don't know how C can implement a function like this, where the user can type as many parameters as the user wants. I have thought about pointers, too but still have no bright idea. If anyone has any ideas about this type of function, please tell me.
You need to use va_args, va_list and the like.
Have a look at this tutorial.
http://www.cprogramming.com/tutorial/c/lesson17.html
That should be helpful.
You have to use the ... notation in your function declaration as the last argument.
Please see this tutorial to learn more: http://www.cprogramming.com/tutorial/c/lesson17.html
#include <stdarg.h>
#include <stdio.h>
int add_all(int num,...)
{
va_list args;
int sum = 0;
va_start(args,num);
int x = 0;
for(x = 0; x < num;x++)
sum += va_arg(args,int);
va_end(args);
return sum;
}
int main()
{
printf("Added 2 + 5 + 3: %d\n",add_all(3,2,5,3));
}
You use C varargs to write a variadic function. You'll need to include stdargs.h, which gives you macros for iterating over an argument list of unknown size: va_start, va_arg, and va_end, using a datatype: va_list.
Here's a mostly useless function that prints out it's variable length argument list:
void printArgs(const char *arg1, ...)
{
va_list args;
char *str;
if (arg1) We
va_start(args, arg1);
printf("%s ", arg1);
while ((str = va_arg(argp, char *)) != NULL)
printf("%s ", str);
va_end(args);
}
}
...
printArgs("print", "any", "number", "of", "arguments");
Here's a more interesting example that demonstrates that you can iterate over the argument list more than once.
Note that there are type safety issues using this feature; the wiki article addresses some of this.

Resources