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);
}
Related
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
I just started with C and am tasked with using a header to house a prototype for a function. The problem is that nothing happens when I'm expecting a prompt for input. I didn't get an error and would like to know where to look at first to solve my problem. This is what I have so far.
LAB2.c
#include <stdio.h>
#include "LAB2HEADER.h"
int main(){
double *p;
double array [10];
p = array;
const int size = 10;
void input(p,size);
return 0;
}
LAB2HEADER.h
#ifndef LAB2HEADER_H_
#define LAB2HEADER_H_
void input (double *array,const int size);
#endif
LAB2HEADER.c
#include <stdio.h>
#include "LAB2HEADER.h"
void input (double *array,const int size){
for (int i = 0; i < size ; i++)
{
printf("Input a value");
scanf("%lf", &array[i]);
}
}
A lot of the notes I look at seem to only either use Int as a parameter or have a function with no needed parameters, could my mistake be in my array pointer is it a problem with the way I made my function?
void input(p,size);
This line makes no sense. If this is supposed to be a function call, you need to remove void.
Also, since your print statement does not end with a newline, nor do you flush stdout before reading in the value, your prompt might still be in the output buffer and not be output until you hit the newline AFTER entering the value.
I would like to be able to call a function with a 2D array named using const char*. In my program, I open up a CSV input, read the number of lines, create a 2D array of the appropriate size (copy over the data from the file-this step is not in the code for the sake of the question) and then want to print it (not in the main function though, to cut down on number of lines).
For this example we can ignore the CSV component so I've added a sample array just to try it out.
I know I'm getting lost in the pointers somewhere in the hand-off of the array between main() and show() since I'm getting the error "subscripted value is neither array nor pointer nor vector." I just don't know how to fix it. Any pointers on how to remedy this?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define col 4
void show(const char* A){
int i, j;
for(i=0;i<4;i++){
for(j=0;j<4;j++){
if(A[i][j])
printf("%s\t", A[i][j]);
else
printf("%s\t", "NULL");
}
printf("\n");
}
}
int main () {
FILE* t1count = fopen("input/t1_input.csv", "r");
t1row = countlines(t1count); // Where countlines is another function I have written but not relevant in this case
const char *strings[t1row][col]; //For this example we can use: = {{"4001","CA52","C14M731345","5"},{"4010","CA52","C14M731559","5"},{"4101","CA52","C14M731559","5"},{"4029","CA72","B15M731038","9"}};
show(strings);
}
Thanks!
There is type mismatch between your function parameter void show(const char* A) and the argument you are passing while calling show(strings);
change your function prototype to
void show(const char* A[][col])
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.
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.