Advertisement
acarrunto

ejemplos completos de prog en c

Jul 11th, 2022
1,216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.91 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #include <math.h>
  4. #include <string.h>
  5.  
  6. void datitos (char nombre[], int edad) // funcion
  7.     {
  8.         printf("tu nombre es: %s\n", nombre);
  9.         printf("tu edad es: %d", edad);
  10.     }
  11.  
  12. int datitos2 (int number) {
  13.  
  14.     int rpta = 2 * number;
  15.     return rpta;
  16. }
  17.  
  18. void datitos3 (char nombre[], int numero); // function prototype.
  19. // the function prototype cause the compiler to flag an error if arguments are misssing.
  20.  
  21.  
  22. struct node
  23. {
  24.     int a;
  25.     int b;
  26. };
  27.  
  28.  
  29. int main (){
  30. /*
  31.     printf("hola mundo \n"); // \x, these are called escape sequences.
  32.     printf("como estas \n");
  33.     printf("1\t2\t3\n4\t5\t6\n7\t8\t9\n"); // \n = salto de lina y \t = tab
  34.     printf("\"yo quiero pizza\" - javier"); // \" = poner comillas en un string
  35. */
  36.  
  37. /* declaration of variable
  38.     int x = 12;
  39.     x = 123;
  40.     float gol = 2.05;
  41.     char gra = 'c';
  42.     char nombre[] = "hola que hace"; // array of characters, it emulate a String like in other programming languages.
  43.     printf("you are %d years old \n",x); // C format Specifier = define data type
  44.     printf("%s \n",nombre);
  45.     // end of var. declaration
  46. */
  47.  
  48. /*
  49. // data type
  50.     char a = 'c'; // single character, %c
  51.    
  52.     char b[] = "javier"; //array of character, %s
  53.    
  54.     float c = 3.141592; //4 bytes, 6-7 digits, %f
  55.    
  56.     double d = 3.1415926535897993; // 8 bytes, 15-16 digits, %lf
  57.    
  58.     bool e = true; // 1 byte, "true" = 1 and "false" = 0, %d
  59.    
  60.     char f = 100; // its possible to store a num in "char", 1 byte (-128, 127), %d or %c
  61.     printf("%d\n",f); // char as decimal, print the number.
  62.     printf("%c\n",f); // chas is displayed as character according to ASCII table codes. ascii has a range from 0 to 127
  63.    
  64.     unsigned char g = 250; // 1 byte, 0 to 255, %d or %c, just to storage num in 1 byte.
  65.     printf("%d\n",g);
  66.    
  67.     short int h = 32767; // 2 bytes, from -32,768 to +32,767, %d, short h = 32767 (otra forma)
  68.    
  69.     unsigned short int i = 65535; // 2 bytes, from 0 to 65,535, %d, unsigned short i = 65535
  70.  
  71.     int j = 234; // 4 bytes, -2,147,483,648 to 2,147,483,647; %d
  72.  
  73.     unsigned int k = 222; // 4 bytes, 0 to 4,294,967,295; %u
  74.  
  75.     long long int l = 123423421341;//8 bytes, -9 quintillion to 9 quintillion, %lld
  76.  
  77.     unsigned long long int m = 23423423424234;//8 bytes, 0 to 18 quintillion, %llu
  78.     //end of data type.
  79. */
  80.    
  81. /*
  82.     //decimal presition
  83.     // %.x; x = enteros, decimal precision
  84.     // %x; x>=0, field width
  85.     // %-; left align
  86.     float numerito = 2.23;// without any decimal display change.
  87.     printf("%f\n",numerito);
  88.     printf("%.2f\n", numerito);
  89.     printf("%8.2f\n", numerito);
  90.    
  91.     const float PI = 3.141592;
  92. */
  93.  
  94. /*
  95.     // input with scanf
  96.     char name[25]; // when a char var doesn't have anything, must put # of bytes that will be in the array, 25 is the max. if we go above, we will produce a buffer overflow.
  97.     int age;
  98.     printf("what is your name: ");
  99.     scanf("%s", &name);
  100.     printf("write your age: ");
  101.     scanf("%d", &age);
  102.     printf("hello %s, how are you \n", name);
  103.     printf("your age is %d", age);
  104. */
  105.  
  106. /*
  107.     // math functions examples
  108.     double a1 = sqrt(9);
  109.     printf("\n%.2lf\n", a1);
  110.     double a2 = pow(2,4); // = 2^4
  111.     printf("%.2lf\n", a2);
  112.     int a3 = round(3.55); // round the number
  113.     printf("%d\n", a3);
  114.     int a4 = ceil(3.453); // always round the number up
  115.     printf("%d\n", a4);
  116.     int a5 = floor(3.89); // always round the number down
  117.     printf("%d\n", a5);
  118.     double a6 = fabs(-100); // give the absolute value. it work with INT var
  119.     printf("%.2lf\n", a6);
  120.     double a7 = log(3);
  121.     printf("%lf\n", a7);
  122.     double a8 = sin(45); // sin, cos , tan
  123. */
  124.  
  125. /*
  126.     // if-statement
  127.     int age;
  128.     printf("write a number: ");
  129.     scanf("%d", &age);
  130.  
  131.     if (age == 8){
  132.         printf("your are awesome");
  133.     } else if (age <= 3){
  134.         printf("you are not awesome");
  135.     } else {
  136.         printf("adios");
  137.     }
  138. */
  139.  
  140. /*
  141.     // switch case
  142.     char grade;
  143.     printf("enter a letter: ");
  144.     scanf("%c", &grade);
  145.     switch(grade){
  146.         case 'a':
  147.             printf("you are top student");
  148.             break;
  149.         case 'b':
  150.             printf("you are a good student");
  151.             break;  
  152.         case 'c':
  153.             printf("you are an average student");
  154.             break;
  155.         case 'd':
  156.             printf("you need to put more effort");
  157.             break;
  158.         default:
  159.             printf("we need to talk to your parents");
  160.     }
  161. */
  162.  
  163. /*
  164.     // funcion of "datitos"
  165.     char name[] = "javier";
  166.     int age = 21;
  167.     datitos(name, age);
  168. */
  169.  
  170. /*
  171.     // return statement in C
  172.     int dato_numerico;
  173.     printf("escribir un numero: ");
  174.     scanf("%d", &dato_numerico);
  175.     int x = datitos2(dato_numerico);
  176.     printf("el numero es: %d", x);
  177. */
  178.  
  179. /*
  180.     //ternary operator - (condition) ? "value if true" : "value if false"
  181.     // a shortcut to if/else when assigning/returning a value.
  182.     int roman_numb = 15;
  183.     int arab_numb = 9;
  184.     int hold_numb;
  185.     hold_numb = (roman_numb >= arab_numb) ? roman_numb : arab_numb;
  186.     printf("%d\n", hold_numb);
  187.    
  188.     //another example96
  189.     int edad_a = 23;
  190.     int edad_b = 23;
  191.     (edad_a == edad_b) ? printf("son iguales\n") : printf("son diferentes\n");
  192. */
  193.  
  194. /*
  195.     // prototype course
  196.     char primer_nombre[] = "julian";
  197.     int edad_alumno = 18;
  198.     datitos3(primer_nombre, edad_alumno);
  199. */
  200.  
  201. /*
  202.     // for loop
  203.     for(int i = 1; i <= 10; i++){   // e.g. i+=2 -> now counts by 2
  204.         printf("%d\n", i);
  205.     }
  206. */
  207.  
  208. /*
  209.     // do-while
  210.     int numbercito = 0;
  211.     int sumita = 0;
  212.  
  213.     do{
  214.         printf("enter a number greater than 0: ");
  215.         scanf("%d", &numbercito);
  216.         if (numbercito > 0){
  217.             sumita += numbercito;
  218.         }
  219.     }while (numbercito <= 10);
  220.     printf("%d", sumita);
  221. */
  222.  
  223. /*  
  224.     // nested loop
  225.     for (int i = 0; i <= 10; i++){
  226.        
  227.         for(int j = i; j >= 0; j--){
  228.            printf("%d ", j);
  229.         }
  230.         printf("\n");
  231.     }
  232. */
  233.  
  234. /*
  235.     // pointers
  236.     int civ = 22;
  237.     int *pCiv = &civ;
  238.     printf("valor de la variable es: %d\n", civ);
  239.     printf("valor de la direccion es: %p\n", &civ); // shows civ's address
  240.     printf("valor del puntero es: %p\n", pCiv); // show value of var. "pCiv", == civ's address
  241.     printf("\n");
  242.     printf("the value of civ is %d\n", civ);
  243.     printf("the values of the stored address is %d\n", *pCiv);// print the value at the address stored in the var. *pCiv, == civ's address
  244. */
  245.  
  246.     //struct node peru;
  247.     //printf("valor de dir. es: %p\n", &peru.a);
  248.     //printf("valor de dir. es: %p", &(peru.b));
  249.  
  250.     int array1[6] = {1,2,3,4,5,6};
  251.    
  252.     for (int i=0; i==6; i++){
  253.        
  254.     }
  255.  
  256.  
  257.  
  258. }
  259.  
  260. // prototype of a functions.
  261. void datitos3 (char nombre[], int numero){
  262.     printf("\nel nombre es %s y el numero es %d", nombre, numero);
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement