Last active
October 22, 2022 16:33
-
-
Save CarloCattano/585059dcb8626fdee246afd6243535dd to your computer and use it in GitHub Desktop.
Example of main argc and argv - 42 Berlin practice
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* ************************************************************************** */ | |
/* */ | |
/* ::: :::::::: */ | |
/* main.c :+: :+: :+: */ | |
/* +:+ +:+ +:+ */ | |
/* By: ccattano <ccattano@student.42Berlin.de> +#+ +:+ +#+ */ | |
/* +#+#+#+#+#+ +#+ */ | |
/* Created: 2022/10/22 09:36:07 by ccattano #+# #+# */ | |
/* Updated: 2022/10/22 09:36:09 by ccattano ### ########.fr */ | |
/* */ | |
/* ************************************************************************** */ | |
#include <unistd.h> | |
#include <stdio.h> | |
/* | |
Exercise for the talk on 1st floor on Saturday 22 oct 14:00 pm | |
Few examples commented out to go one by one. | |
ex1 - shows argument count and value | |
ex2 - iterates over every letter on them | |
ex3 - print the first letter of each argument | |
Uncomment each exercise to try them and comment the other | |
*/ | |
void ft_putchar(char c) | |
{ | |
write(1, &c, 1); | |
} | |
int main(int argc, char *argv[]) | |
{ | |
int i; | |
char numberOfArguments; | |
i = 0; | |
/* CONVERT AC a.k.a agc to a char number to display */ | |
numberOfArguments = (argc + 48); | |
/* NUMBER OF ARGUMENTS DISPLAY HELPER */ | |
write(1,"----------------------------\n", 29); | |
write(1, "Number of arguments:\t\targc ---> ", 32); | |
write(1, &numberOfArguments, sizeof(numberOfArguments)); | |
ft_putchar('\n'); | |
write(1,"----------------------------\n", 29); | |
/* ONLY if we give one argument or more */ | |
if (argc == 3 || argc == 2) | |
{ | |
/* ------------------------------------------------- | |
ex1, ARGUMENT COUNT AND VALUE | |
examples: | |
./program one two | |
./program "Hallo mama, Im ok I just look tired" "Piscine effects on epidermis by julian et al" | |
-------------------------------------------------*/ | |
/* printf("Program Name (First argument arg[0]):\t"); | |
printf("%s \n",argv[0]); | |
printf("Argument 1:\t\t\t\t"); | |
printf("%s \n",argv[1]); | |
printf("Argument 2:\t\t\t\t"); | |
printf("%s \n",argv[2]); */ | |
/* ----------- END OF EX1 ----------- */ | |
/* ------------------------------------------------- | |
EX2 - CHAR BY CHAR, step by step | |
examples: | |
./program pizza pasta | |
./program 12 12 | |
./program "hello world!" "Save the planet" | |
-------------------------------------------------*/ | |
/* write(1,"Argument n2 \targv[1]:\t---> ",27); | |
while(argv[1][i] != '\0') | |
{ | |
write(1, &argv[1][i],1); | |
i++; | |
} | |
ft_putchar('\n'); | |
i = 0; | |
write(1,"Argument n3 \targv[2]:\t---> ",27); | |
while(argv[2][i] != '\0') | |
{ | |
write(1, &argv[2][i],1); | |
i++; | |
} | |
ft_putchar('\n'); */ | |
/* ----------- END OF EX2 -------------- */ | |
/* ------------------------------------------------- | |
EX3, First letter of each | |
examples: | |
./program one two | |
./program "Hallo mama, Im ok I just look tired" "Piscine effects on epidermis by julian et al" | |
-----------------------------------------------*/ | |
/* printf("first letter of each\n------------\n"); | |
printf("First letter in arg[1]:\t\t\t %c \n",argv[1][0]); | |
printf("First letter in arg[2]:\t\t\t %c \n",argv[2][0]); */ | |
/* ----------- END OF EX3 --------------------------------*/ | |
} | |
/* If more or less args given, treat your user how they deserve */ | |
else if(argc < 2) | |
printf("---------\nTo Argue about arguments , you do need arguments ;-)\n"); | |
else if(argc > 3) | |
{ | |
printf("Let's keep it simple, shall we ?\ninput a maximum of 2 arguments\n"); | |
printf("Remember that the program name its always index 0 and count 1"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment