matheus__serpa

Linguagem de programação - C

Sep 25th, 2012
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 13.04 KB | None | 0 0
  1. //Headers and libraries
  2.     #include <stdio.h>
  3.     #include <string.h>
  4.     #include <stdlib.h>
  5.     #include <math.h>
  6.     #include <ctype.h>
  7.  
  8. //Comments // or /* */
  9.  
  10. //Types of variables     Bytes  Range(256^bytes)
  11.     pointer                4    
  12.     FILE                 148    
  13.     void                   1    
  14.     char                   1    //-127 a 127
  15.     unsigned char          1    //0 a 255
  16.     int                    4    //-2.147.483.648 a 2.147.483.647
  17.     unsigned int           4    //0 a 4.294.967.295
  18.     short int              4    //-32.768 a 32.767
  19.     unsigned short int     2    //0 a 65.535
  20.     long int               4    //same int
  21.     unsigned long int      4    //same int
  22.     long long int          8    //same int
  23.     unsigned long long int 8    //same int
  24.     bool                   1    //0 a 1
  25.     float                  4    //Six digits of precision
  26.     double                 8    //Ten digits of precision
  27.     long double           12    //Ten digits of precision
  28.  
  29. //Arithmetic operators
  30.     Basic assignment =
  31.     Addition         +
  32.     Subtraction      -
  33.     Multiplication   *
  34.     Division         /
  35.     Modulo           %
  36.     var++;             //Suffix, the value of the variable is used and after incremented.
  37.     --var;             //Prefix, the value is incremented and used after the command.
  38.  
  39. //Compound assignment operators
  40.     var = var + var2;  ||  var += var2;
  41.     var = var - var2;  ||  var -= var2;
  42.     var = var * var2;  ||  var *= var2;
  43.     var = var / var2;  ||  var /= var2;
  44.  
  45. //Comparison operators/relational operators
  46.     Greater than             >
  47.     Greater than or equal to >=
  48.     Less than                <
  49.     Less than or equal to    <=
  50.     Equal to                 ==
  51.     Not equal to             !=
  52.  
  53. //Logical operators
  54.     Logical AND              &&
  55.     Logical OR               ||
  56.     Logical negation (NOT)    !
  57.  
  58. //Operator precedence
  59. //Left-to-right
  60.     Suffix increment, Suffix decrement, Function call
  61. //Right-to-left
  62.     Prefix increment, Prefix decrement,Logical NOT, Type cast, Indirection(*), Address-of(&), Size-of
  63. //Left-to-right
  64.     Multiplication, Division, Modulo, Addition, Subtraction, Less than, Less than or equal to, Greater than, Greater than or equal to, Equal to, Not, equal to, Logical AND, Logical OR
  65. //Right-to-left
  66.     Direct assignment(=), Assignment by sum(+=), by difference, by product, by quotient, by remainder
  67. //Left-to-right
  68.     Comma(,)
  69.  
  70. //Format specifiers
  71.     "%c" Character.
  72.     "%s" String of character.
  73.     "%d" Decimal.
  74.     "%i" Integer.
  75.     "%u" Unsigned Decimal.
  76.     "%e" Scientific notation, lowercase.
  77.     "%E" Scientific notation, uppercase.
  78.     "%f" Decimal floating point, lowercase.
  79.     "%F" Decimal floating point, uppercase.
  80.     "%g" Use the shortest representation: %e or %f.
  81.     "%G" Use the shortest representation: %E or %F.
  82.     "%o" Octal.
  83.     "%x" Hexadecimal integer lowercase.
  84.     "%X" Hexadecimal integer uppercase.
  85.     "%a" Hexadecimal floating point, lowercase.
  86.     "%A" Hexadecimal floating point uppercase.
  87.     "%%" % followed by another % character will write a single %.
  88.     "%p" Pointer address.
  89.  
  90. // Formatting codes
  91.     New line    "\n"
  92.     Tab         "\t"
  93.     Backspace   "\b"
  94.     Quotes      "\""
  95.     Slash       "\\"
  96.     Jump form   "\f"
  97.     Null        "\0"
  98.  
  99. int printf ( const char * format, ...);//The total number of characters written is returned.
  100. /*A format specifier follows this prototype:
  101. %[flags][width][.precision][length]specifier
  102.  
  103. Flags      Description
  104.   -        Left-justify.
  105.   +        Plus or minus sign (+ or -) even for positive numbers.
  106.  
  107. Width      Description
  108. number     Minimum number of characters to be printed.
  109. *          The width is not specified in the format string, but as an additional integer value.
  110.  
  111. precision  Description
  112. .number    Precision specifies the minimum number of digits to be written or number of digits to be printed after the decimal point.
  113. .*         The width is not specified in the format string, but as an additional integer value.
  114. */
  115.  
  116. int scanf ( const char * format, ...);//The function returns the number of items of the argument list successfully filled.
  117. /*A format specifier follows this prototype:
  118. %[*][width][length]specifier
  119.  
  120. sub-specifier  Description
  121. *              An optional starting asterisk indicates that the data is to be read from the stream but ignored.
  122. width          Specifies the maximum number of characters to be read in the current reading operation.
  123. */
  124.  
  125. int fflush ( FILE * stream );//A zero value indicates success.
  126. //If stream is a null pointer, all such streams are flushed.
  127. //stdin  -> Standard input stream
  128. //stdout -> Standard output stream
  129. //stderr -> Standard error stream
  130. int getchar ( void );//On success, the character read is returned (promoted to an int value).
  131. //Returns the next character from the standard input (stdin).
  132.  
  133.     #include <stdio.h>
  134.     int main(){
  135.         int var;
  136.         printf("Enter any value:");
  137.     fflush(stdin);
  138.         scanf("%d",&var);
  139.         printf("\nvar = %d",var);
  140.         printf("\nThe double %d is %d",var,2*var);
  141.     fflush(stdin);
  142.     getchar();
  143.         return 0;
  144.     }
  145.  
  146. //if - else - else if
  147.  
  148.     if (condition) {
  149.     /* Block to be executed if the condition is true */
  150.     }
  151.     else {
  152.     /* Block to be executed if the condition is false */
  153.     }
  154.  
  155. //switch - case
  156.  
  157.     switch (var) {
  158.         case value_1:
  159.             instructions;
  160.             break;
  161.         case value_n:
  162.             instructions;
  163.         break;
  164.         default:
  165.             instructions;;
  166.  }
  167.  
  168. // ?:
  169.  
  170.  condition ? true : false
  171.  
  172. //while
  173.  
  174.     while (condition) {
  175.         instructions;    
  176.     }
  177.  
  178. //while infinite
  179.  
  180.     while (1) {
  181.         instructions;    
  182.     }
  183.  
  184. //do - while
  185.  
  186.     do{
  187.         instructions;
  188.     }while (condition);
  189.  
  190. //for
  191.  
  192.     for (initializations, condition, increments) {
  193.         instructions;
  194.     }
  195.  
  196. //break and continue
  197.     break;//Always causes program execution to continue at the first statement following the loop or block.
  198.     continue; //Equates to reach the end of the block, the increments are performed and the condition is reevaluated.
  199.  
  200. //Exemplo vetor
  201.     #include <stdio.h>
  202.  
  203.     int main(){
  204.         int vetor[5],i;
  205.         float media = 0;
  206.  
  207.         for(i = 0;i < 5;i++){
  208.             printf("Vetor[%d]: ",i);
  209.             scanf("%d",&vetor[i]);
  210.         }
  211.  
  212.         for(i = 0;i < 5;i++){
  213.             media =  media + vetor[i];
  214.         }
  215.         printf("\nMédia = %.1f",media/5);
  216.         return 0;
  217.     }
  218.  
  219. //Exemplo matriz
  220.     #include <stdio.h>
  221.  
  222.     int main(){
  223.         int matriz[3][3] = {00,01,02,10,11,12,20,21,22};
  224.         int i,j;
  225.  
  226.         for(i = 0;i < 3;i++){
  227.             printf("\n");
  228.             for(j = 0;j < 3;j++){
  229.                 printf("%.2d ",matriz[i][j]);
  230.             }
  231.         }
  232.         printf("\n");
  233.  
  234.         for(i = 0;i < 3;i++){
  235.             printf("\n");
  236.             for(j = 0;j < 3;j++){
  237.                 if(i == j){
  238.                     printf("__ ");
  239.                 }
  240.                 else{
  241.                     printf("%.2d ",matriz[i][j]);
  242.                 }
  243.             }
  244.         }
  245.         printf("\n");
  246.  
  247.         for(i = 0;i < 3;i++){
  248.             printf("\n");
  249.             for(j = 0;j < 3;j++){
  250.                 if(i + j == 2){
  251.                     printf("__ ");
  252.                 }
  253.                 else{
  254.                     printf("%.2d ",matriz[i][j]);
  255.                 }
  256.             }
  257.         }
  258.         printf("\n");
  259.  
  260.         for(i = 0;i < 3;i++){
  261.             printf("\n");
  262.             for(j = 0;j < 3;j++){
  263.                 if(i > j){
  264.                     printf("__ ");
  265.                 }
  266.                 else{
  267.                     printf("%.2d ",matriz[i][j]);
  268.                 }
  269.             }
  270.         }
  271.         printf("\n");
  272.  
  273.         for(i = 0;i < 3;i++){
  274.             printf("\n");
  275.             for(j = 0;j < 3;j++){
  276.                 if(i < j){
  277.                     printf("__ ");
  278.                 }
  279.                 else{
  280.                     printf("%.2d ",matriz[i][j]);
  281.                 }
  282.             }
  283.         }
  284.         printf("\n");
  285.  
  286.         return 0;
  287.     }
  288.  
  289. //Exemplo string
  290.     #include <stdio.h>
  291.     #include <string.h>
  292.  
  293.     int main(){
  294.         int i,aux=0;
  295.         char letra,string[201];
  296.         char letraA = 'a';
  297.         char string1[101] = "string";
  298.         char string2[101] = {'s','t','r','i','n','g','\0'};
  299.  
  300.         printf("String1: ");
  301.         puts(string1);
  302.         printf("String2: ");
  303.         puts(string2);
  304.  
  305.         printf("\nDigite um caractere: ");
  306.         letra = getchar();
  307.         fflush(stdin);
  308.         printf("Digite uma string: ");
  309.         gets(string);
  310.  
  311.         printf("\nCaractere: ");
  312.         putchar(letra);
  313.         printf("\nString: ");
  314.         puts(string);
  315.  
  316.         printf("Tamanho da string: %d\n",strlen(string));
  317.  
  318.         if(strcmp(string1,string2) == 0) printf("\nString1 == String2"); else printf("\nString1 != String2");
  319.  
  320.         strcpy(string1,"Um");
  321.         strcpy(string2,"Dois");
  322.  
  323.         printf("\n\nString1: ");
  324.         puts(string1);
  325.         printf("String2: ");
  326.         puts(string2);
  327.  
  328.         strcat(string,string1);
  329.         strcat(string,string2);
  330.  
  331.         printf("\nString: ");
  332.         puts(string);
  333.  
  334.         for(i = 0;i < strlen(string); i++){
  335.             if (string[i] == letraA){
  336.                 aux++;
  337.             }
  338.         }
  339.         printf("\nQuantidade de letras a na String: %d",aux);
  340.  
  341.         return 0;
  342.     }
  343.  
  344. //Exemplo switch - case
  345.     #include <stdio.h>
  346.  
  347.     int main(){
  348.         int num;
  349.         printf("Digite 1 para solteiro ou 2 para casado:\n");
  350.         scanf("%d",&num);
  351.         printf("\n");
  352.         switch(num){
  353.             case 1: printf("Solteiro"); break;
  354.             case 2: printf("Casado"); break;
  355.             default: printf("Valor invalido");
  356.         }
  357.         return 0;
  358.     }
  359.  
  360. //Exemplo while
  361.     #include <stdio.h>
  362.  
  363.     int main(){
  364.         char tecla;
  365.  
  366.         while(tecla != 'a'){
  367.             tecla = getchar();
  368.         }
  369.         return 0;
  370.     }
  371.  
  372. //Exemplo do - while
  373.     #include <stdio.h>
  374.  
  375.     int main(){
  376.         int i=0;
  377.  
  378.         do{
  379.             i++;
  380.             printf("%d\t",i);
  381.         }while(i < 10);
  382.         return 0;
  383.     }
  384.  
  385. // Variáveis locais e globais
  386.     #include <stdio.h>
  387.     int varGlobal;// Qualquer função pode acessá-la.
  388.  
  389.     main(){
  390.         int varLocal;// Apenas a função main() tem acesso a esta variável.
  391.     }
  392.  
  393. //Exemplo 1 Modularidade(Passagem por Valor)
  394.     #include <stdio.h>
  395.  
  396.     float media(float a, float b, float c);
  397.  
  398.     int main(){
  399.         float n1,n2,n3;
  400.  
  401.         printf("Digite três valores quaisquer: \n");
  402.         scanf("%f%f%f",&n1,&n2,&n3);
  403.  
  404.         printf("\nMédia = %.1f",media(n1,n2,n3));
  405.         return 0;
  406.     }
  407.  
  408.     float media(float a, float b, float c){
  409.         return (a + b + c) / 3;
  410.         }
  411.  
  412. //Exemplo 2 Modularidade
  413.     #include <stdio.h>
  414.     float n1,n2,n3;
  415.     void media();
  416.  
  417.     int main(){
  418.         printf("Digite três valores quaisquer: \n");
  419.         scanf("%f%f%f",&n1,&n2,&n3);
  420.         media();
  421.         return 0;
  422.     }
  423.  
  424.     void media(){
  425.         printf("\nMédia = %.1f",(n1 + n2 + n3) / 3);
  426.     }
  427.  
  428. //Exemplo Ponteiros
  429. #include <stdio.h>
  430.  
  431.     int main(){
  432.         int a, *pa;
  433.         a = 50;
  434.         pa = &a; // pa recebe o endereço de a;
  435.  
  436.         printf("\nEndereço do ponteiro pa: %X", pa);
  437.         printf("\nEndereço da variável contida no ponteiro pa: %X", &pa);
  438.         printf("\nValor da variável contida no ponteiro pa: %d", *pa);
  439.  
  440.         return 0;
  441.     }
  442.  
  443. //Exemplo 1 Parâmetro(Passagem de um valor por referência)
  444.     #include <stdio.h>
  445.  
  446.     float media(float *a, float b, float c);
  447.  
  448.     int main(){
  449.         float n1,n2,n3;
  450.  
  451.         printf("Digite três valores quaisquer: \n");
  452.         scanf("%f%f%f",&n1,&n2,&n3);
  453.  
  454.         printf("\nMédia = %.1f",media(&n1,n2,n3));
  455.         return 0;
  456.     }
  457.  
  458.     float media(float *a, float b, float c){
  459.         return (*a + b + c) / 3;
  460.         }
  461.  
  462. //Exemplo 2 Parâmetro(Passagem e retorno por referência)
  463.     #include <stdio.h>
  464.  
  465.     void media(float *a, float *b, float *c, float *retorno);
  466.  
  467.     int main(){
  468.         float n1,n2,n3,aux;
  469.  
  470.         printf("Digite três valores quaisquer: \n");
  471.         scanf("%f%f%f",&n1,&n2,&n3);
  472.  
  473.         media(&n1,&n2,&n3,&aux);
  474.  
  475.         printf("\nMédia = %.1f",aux);
  476.         return 0;
  477.     }
  478.  
  479.     void media(float *a, float *b, float *c, float *retorno){
  480.         *retorno =  (*a + *b + *c) / 3;
  481.         }
  482.  
  483. //Exemplo 3 Parâmetro(Vetor por referência)
  484.     #include <stdio.h>
  485.  
  486.     void media(float *vetor, float *retorno);
  487.  
  488.     int main(){
  489.         float v[3],aux;
  490.         int i;
  491.  
  492.         for(i = 0; i < 3; i++){
  493.             printf("v[%d]: ",i);
  494.             scanf("%f",&v[i]);
  495.         }
  496.  
  497.         media(v,&aux);
  498.  
  499.         printf("\nMédia = %.1f",aux);
  500.         return 0;
  501.     }
  502.  
  503.     void media(float *vetor, float *retorno){
  504.         int i;
  505.         float aux = 0;
  506.  
  507.         for(i = 0;i < 3; i++)
  508.             aux += vetor[i];
  509.  
  510.         *retorno =  aux/3;
  511.     }
Advertisement
Add Comment
Please, Sign In to add comment