#include <stdio.h> #include <string.h> #include <stdarg.h> #include <unistd.h> #include <stdlib.h> char *ft_strchr(const char *s, int c) { while (*s != (char)c && *s) s++; if (*s == (char)c) return ((char *)s); return (0); } void *ft_calloc(size_t count, size_t size) { size_t i; void *str; i = 0; str = malloc(count * size); if (!str) return (0); while (i < (count * size)) ((char *)str)[i++] = '\0'; return (str); } int ft_hex(long unsigned x, int fd, int cap) { int z; int f; f = 0; if (x > 15) f = ft_hex(x / 16, fd, cap); z = x % 16; z = z + 48 + (z >= 10) * (7 + (cap > 100) * 32); write(1, &z, ++f > 0); return (f); } size_t ft_strlen(const char *s) { size_t i; i = 0; while (s[i]) i++; return (i); } int zong_xina(long n) { int i; i = 0; if (n == 0) return (1); if (n < 0) { n *= -1; i++; } while (n > 0) { n /= 10; i++; } return (i); } char *ft_itoa(int n) { char *string; int i[2]; long l; i[0] = 0; i[1] = zong_xina(n); string = ft_calloc((i[1] + 1), sizeof(char)); if (!string) return (0); l = n; if (l < 0) { string[i[0]] = '-'; l *= -1; i[0]++; } while (--i[1] >= i[0]) { string[i[1]] = (l % 10) + 48; l /= 10; } return (string); } void ft_putchar_fd(char c, int fd) { write (fd, &c, 1); } void put_bling(char a, int fd) { write (fd, &a, 1); } void ft_bzero(void *s, size_t n) { size_t i; i = 0; while (i < n) { ((char *)s)[i] = '\0'; i++; } } void ft_putstr_fd(char *s, int fd) { int i; i = -1; if (!s) return ; while (s[++i]) write (fd, &s[i], 1); } int cr(int i, int m) { if (m == 4) return (ft_strchr("xXcu", i) == 0); if (m == 1) return (ft_strchr("cp", i) == 0); if (m == 115) return (0); if (m == 99 || m == 0) return (write(1, &i, 1)); if (m == -1) { ft_putchar_fd('%', 1); if (i != '%') write(1, &i, 1); return (2); } return (0); } int po(void *s, int fd, int x) { int n; char *px; n = -1; if (x == 's') { px = (char *)s; if (px == 0 || *px == 0) px = "(null)"; printf(">%d<",fd); while (++n < fd) cr(px[n], 0); return (n); } else if (x == 'c') return (write(1, &fd, 1)); else if (x == 'p') { if (fd != -1) ft_putstr_fd("0x", 1); return ((fd != -1) * 2 + ft_hex((long unsigned)s, 1, 'x')); } return (0); } void ft_putnbr_fd(int n, int fd) { if (n == -2147483648) { write (fd, "-2147483648", 11); return ; } else if (n < 0) { write (fd, "-", 1); n *= -1; ft_putnbr_fd(n, fd); } else if (n > 9) { ft_putnbr_fd(n / 10, fd); ft_putnbr_fd(n % 10, fd); } else if (n <= 9) put_bling(n + 48, fd); } void *ft_memcpy(void *dst, const void *src, size_t n) { size_t i; i = 0; if (!src && !dst) return (dst); while (i < n) { ((char *)dst)[i] = ((char *)src)[i]; i++; } return (dst); } int ft_atoi(const char *str) { unsigned long res; int sign; sign = 1; res = 0; while (*str == 32 || (*str >= 9 && *str <= 13)) str++; if (*str == '-' || *str == '+') { if (*str == '-') sign *= -1; str++; } while (*str >= '0' && *str <= '9') { res = res * 10 + *str - '0'; str++; } if (res > 2147483647 && sign == 1) return (-1); if (res > 2147483648 && sign == -1) return (0); return (res * sign); } int ft_nbr(unsigned int i, int s, int pr) { char c; int f; f = 0; if (i == 0 && pr == 0) return (0); if (s == 'x' || s == 'X') return (ft_hex(i, 1, s)); if (s == 'c') return (cr(i, 0)); if (s != 'u' && (int)i < 0) { if (pr == 1) ft_putchar_fd('-', 1); i = -1 * i; s = 0; } if (i >= 10) f = ft_nbr(i / 10, 2, pr); c = (i % 10) + 48; write(1, &c, ((++f != 0) && (pr != 0))); return (f + (++s == 1)); } int sp(int x, char i, int mod) { int y; y = -1; if (mod >= 1) { cr('0', 0); cr(120 - ((mod == 88) * 32), 0); return (2); } if ((mod == 0 || ++y < 0) && i != 0) while (++y < x) cr(i, 0); return (y * (y > 0)); } int calc(char s, long unsigned i, void *o) { int l; l = (s == 's') * ft_strlen((char *)o); if (s == 'X' || s == 'x' || s == 'u' || s == 'z') { if (i >= (10 + (s != 'u') * 6)) l = calc(s, i / (10 + (s != 'u') * 6), 0); return (l + 1); } else if (s == 'd' || s == 'i') return (ft_nbr((int)i, s, 0)); else if (s == 'p') return (2 + calc('z', (long unsigned)o, 0)); else if (s == '%') return (1); else if (s == 's' && ((char *)o == 0 || l == 0)) return ((6 > i) * i + (6 <= i) * 6); else if (s == 's' && l >= 0) return (((long unsigned)l > i) * i + ((long unsigned)l <= i) * l); else if (s == 'c') return (1); else return (2); } int prb(int p[], unsigned int u, void *s, int x) { int z; int b; int l; l = ((((int)u < 0) * 45) + (p[0] * 43) + (p[2] * 32)) * cr(x, 4); u += (x == 's') * p[6]; b = calc(x, u, s) - 1 * (l == '-') - 2 * (x == 'p'); z = p[5] - b * (x == 'c' || x == 's'); if (x == 's' || x == 'c') return (sp(z * !p[1], 32, 0) + po(s, b, x) + sp(z * p[1], 32, 0)); u = (1 - (2 * ((x == 'd' || x == 'i') && (int)u < 0))) * u; p[4] = p[4] * !p[1] * (u != 0); p[5] = p[5] - b - (l > 0) - 2 * (p[3] || x == 'p') - p[6] * (p[6] > 0); p[5] = p[5] * (p[5] > 0) + (l > 0) * cr(x, 1) * !u * (p[6] == -1 || p[6] == 0); p[6] = (p[6] - b); z = sp((l > 0) * p[4] * (x == 'p'), l, -5 * !(l > 0)); z += sp((l > 0) * p[4], l, ((p[3] * x) + (x == 'p')) * p[4]); z += sp(!p[1] * p[5], ' ' + (p[4] * 16), 0); z += sp((l > 0) * !p[4] * (x == 'p'), l, -5 * (l <= 0)); z += sp((l > 0) * !p[4], l, ((p[3] * x) + (x == 'p')) * !p[4]); z += sp(p[6], '0', 0); x = x - ((x == 'd' || x == 'i') * (x - 'u')); if (x != 'p') return (z + ft_nbr(u, x, b + p[6]) + sp(p[1] * p[5], ' ', 0)); return (z + po(s, -1, x) + sp(p[1] * p[5], ' ', 0)); } int bp(const char *s, int y, int p[], va_list args) { unsigned int q; void *x; int a; q = 0; x = 0; while (ft_strchr("1234567890+- #0.*", s[++y])) ; if (s[y] == '%' || ft_strchr("dxXiucsp", s[y]) == 0) return (cr(s[y], -1)); else if (ft_strchr("dxXiuc", s[y]) != 0) q = va_arg(args, unsigned int); else if (s[y] == 's' || s[y] == 'p') x = va_arg(args, void *); a = (s[y] != 'p' && s[y] != 'd' && s[y] != 'i'); p[2] = p[2] * !((int) q < 0 || p[0] == 1 || a != 0); p[0] = p[0] * !((int) q < 0 || a != 0); p[3] = p[3] * !(q == 0) * (s[y] == 88 || s[y] == 'x'); if (s[y] == 's') p[6] = (p[6] * (p[6] > 0) + (p[6] < -1) * ft_strlen((char *)x)); p[4] = p[4] * (p[6] == -2); a = -1; while (++a < 5) p[a] = (p[a] == 1) * p[a]; return (prb(p, q, x, s[y])); } int ft_bonu(const char *s, int y, va_list args) { int pr[7]; int p; // printf("(%c)",s[y]); ft_memcpy (pr, (int []){'+', '-', ' ', '#', '0', 0, -2}, 7 * sizeof(int)); p = -1; while (ft_strchr("+- #0", s[++y]) != 0) while ((++p % 6) != 5) { // printf(">>%d<<",pr[p]); if (s[y] == pr[p % 5]) pr[p % 5] = 1;} p = y - 1; if (s[y] >= 49 && s[y] <= 57) pr[5] = ft_atoi(s + y); else if (s[y] == '*' && ++y > 0) pr[5] = va_arg(args, int); if (s[y] >= 49 && s[y] <= 57) while (++y > 0 && (ft_strchr("1234567890", s[y]) != 0)) ; if (s[y] == '.' && ++pr[6] < 0 && ft_strchr("1234567890", s[y + 1]) != 0) pr[6] = ft_atoi(s + y + 1); else if (s[y] == '.' && s[y + 1] == '*') pr[6] = va_arg(args, int); return (bp(s, p, pr, args)); } int ft_printf(const char *str, ...) { va_list args; int l; int x; char *e; va_start(args, str); x = -1; l = 0; e = "+-# .1234567890*"; while (++x > -3 && str[x] != 0) if (l >= 0 && str[x] == 37 && ft_strchr(e, str[x + 1]) != 0) l = -1 * (l + ft_bonu(str, x, args)); else if (str[x] == '%' && ft_strchr("csidupxX%", str[x + 1]) && l >= 0) if (str[++x] == 'p' || str[x] == 's') l += po(va_arg(args, void *), -2, str[x]); else if (str[x] == '%') l += cr('%', 0); else l += ft_nbr(va_arg(args, unsigned int), str[x], 1); else if (l >= 0) l += write(1, str + x, 1); else if (ft_strchr(e, str[x]) == 0) l *= -1; va_end(args); return (l); } int main() { int z=ft_printf(">%.0d<",0); printf("\n\n%d",z); return 0; }
Write, Run & Share C Language code online using OneCompiler's C online compiler for free. It's one of the robust, feature-rich online compilers for C language, running the latest C version which is C18. Getting started with the OneCompiler's C editor is really simple and pretty fast. The editor shows sample boilerplate code when you choose language as 'C' and start coding!
OneCompiler's C online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample C program which takes name as input and print your name with hello.
#include <stdio.h>
int main()
{
char name[50];
printf("Enter name:");
scanf("%s", name);
printf("Hello %s \n" , name );
return 0;
}
C language is one of the most popular general-purpose programming language developed by Dennis Ritchie at Bell laboratories for UNIX operating system. The initial release of C Language was in the year 1972. Most of the desktop operating systems are written in C Language.
When ever you want to perform a set of operations based on a condition if-else
is used.
if(conditional-expression) {
// code
} else {
// code
}
You can also use if-else for nested Ifs and if-else-if ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to if-else-if ladder.
switch(conditional-expression) {
case value1:
// code
break; // optional
case value2:
// code
break; // optional
...
default:
// code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
// code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while(condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Array is a collection of similar data which is stored in continuous memory addresses. Array values can be fetched using index. Index starts from 0 to size-1.
data-type array-name[size];
data-type array-name[size][size];
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity.
Two types of functions are present in C
Library functions are the in-built functions which are declared in header files like printf(),scanf(),puts(),gets() etc.,
User defined functions are the ones which are written by the programmer based on the requirement.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
//code
}