Erros que aprendemos quando as variáveis booleanas são false = 0 e true = 1


Arquivo: Exemplo.c Arquivo: Exemplo.c Arquivo: Exemplo.c Arquivo: Exemplo.c
#include <stdio.h>
#include <string.h>

typedef enum {false=0, true=1} boolean;

int main(int argc, char **argv){

   boolean x = true;

   if(x == true){
      printf("true");
   } else {
      printf("false");
   }
   printf("\n");

   return 0;
}
#include <stdio.h>
#include <string.h>

typedef enum {true=0, false=1} boolean;

int main(int argc, char **argv){

   boolean x = true;

   if(x == true){
      printf("true");
   } else {
      printf("false");
   }
   printf("\n");

   return 0;
}
#include <stdio.h>
#include <string.h>

typedef enum {false=2000, true=3000} boolean;

int main(int argc, char **argv){

   boolean x = true;

   if(x == true){
      printf("true");
   } else {
      printf("false");
   }
   printf("\n");

   return 0;
}
#include <stdio.h>
#include <string.h>

typedef enum {false=3000, true=2000} boolean;

int main(int argc, char **argv){

   boolean x = true;

   if(x == true){
      printf("true");
   } else {
      printf("false");
   }
   printf("\n");

   return 0;
}
~$ gcc Exemplo.c -o Exemplo
~$ ./Exemplo
true
~$
~$ gcc Exemplo.c -o Exemplo
~$ ./Exemplo
true
~$
~$ gcc Exemplo.c -o Exemplo
~$ ./Exemplo
true
~$
~$ gcc Exemplo.c -o Exemplo
~$ ./Exemplo
true
~$


Arquivo: Exemplo.c Arquivo: Exemplo.c Arquivo: Exemplo.c Arquivo: Exemplo.c
#include <stdio.h>
#include <string.h>

typedef enum {false=0, true=1} boolean;

int main(int argc, char **argv){

   boolean x = true;

   if(x){
      printf("true");
   } else {
      printf("false");
   }
   printf("\n");

   return 0;
}
#include <stdio.h>
#include <string.h>

typedef enum {true=0, false=1} boolean;

int main(int argc, char **argv){

   boolean x = true;

   if(x){
      printf("true");
   } else {
      printf("false");
   }
   printf("\n");

   return 0;
}
#include <stdio.h>
#include <string.h>

typedef enum {false=2000, true=3000} boolean;

int main(int argc, char **argv){

   boolean x = true;

   if(x){
      printf("true");
   } else {
      printf("false");
   }
   printf("\n");

   return 0;
}
#include <stdio.h>
#include <string.h>

typedef enum {false=3000, true=2000} boolean;

int main(int argc, char **argv){

   boolean x = true;

   if(x){
      printf("true");
   } else {
      printf("false");
   }
   printf("\n");

   return 0;
}
~$ gcc Exemplo.c -o Exemplo
~$ ./Exemplo
true
~$
~$ gcc Exemplo.c -o Exemplo
~$ ./Exemplo
false
~$
~$ gcc Exemplo.c -o Exemplo
~$ ./Exemplo
false
~$
~$ gcc Exemplo.c -o Exemplo
~$ ./Exemplo
false
~$

strcmp

Operação Resultado
strcmp("FRED","FRED")==0 true (verdadeiro)
strcmp("FRED","SAM")!=0 true (verdadeiro)
strcmp("ONE","TWO")==0 false (falso)
strcmp("THREE","THREE")!=0 false (falso)

 

Arquivo: Exemplo.c
#include <stdio.h>
#include <string.h>

int main(int argc, char **argv){

   char string1[100],string2[100];

   printf("\nString 1 = \"A\" String 2: \"A\"\n");

   strcpy(string1,"A");
   strcpy(string2,"A");

   if(strcmp(string1,string2) == 0){
      printf("String 1 é IGUAL a String 2\n");
   } else if(strcmp(string1,string2) != 0){
      printf("String 1 é DIFERENTE de String 2\n");
   }

   printf("\nString 1 = \"A\" String 2 = \"B\"\n");

   strcpy(string1,"A");
   strcpy(string2,"B");

   if(strcmp(string1,string2) == 0){
      printf("String 1 é IGUAL a String 2\n");
   } else if(strcmp(string1,string2) != 0){
      printf("String 1 é DIFERENTE de String 2\n");
   }

   printf("\nString 1 = \"A\" String 2 = \"B\"\n");

   strcpy(string1,"A");
   strcpy(string2, "B");

   if(strcmp(string1,string2) > 0){
      printf("String 1 = \"%s\" é MAIOR QUE String 2 = \"%s\"\n", string1, string2);
   } else if(strcmp(string1,string2) < 0){
      printf("String 1 = \"%s\" é MENOR QUE String 2 = \"%s\"\n", string1, string2);
   }

   printf("\nString 1 = \"B\" String 2 = \"A\"\n");

   strcpy(string1,"B");
   strcpy(string2,"A");

   if(strcmp(string1,string2) > 0){
      printf("String 1 = \"%s\" é MAIOR QUE String 2 = \"%s\"\n", string1, string2);
   } else if(strcmp(string1,string2) < 0){
      printf("String 1 = \"%s\" é MENOR QUE String 2 = \"%s\"\n", string1, string2);
   }

   printf("\n");

   return 0;
}
~$ gcc Exemplo.c -o Exemplo
~$ ./Exemplo

String 1 = "A" String 2: "A"
String 1 é IGUAL a String 2

String 1 = "A" String 2 = "B"
String 1 é DIFERENTE de String 2

String 1 = "A" String 2 = "B"
String 1 = "A" é MENOR QUE String 2 = "B"

String 1 = "B" String 2 = "A"
String 1 = "B" é MAIOR QUE String 2 = "A"

~$

strchr

Arquivo: Exemplo.c Arquivo: Exemplo.c
#include <stdio.h>
#include <string.h>

int main(void){
   char nome[100];
   char ch;
   do {
      printf("Qual é o seu nome: ");

      fgets(nome, 100, stdin);
      if(nome[strlen(nome)-1]=='\n')
         nome[strlen(nome)-1]='\0';

      printf("Meu nome é %s\n",nome);

      do {
         printf("Continua [S]im ou [N]ão ?");
         ch = getchar();
         scanf("%*[^\n]"); scanf("%*c"); // Limpar o buffer
      } while (!strchr("SsNn",ch));
   } while (strchr("Ss",ch));
}
#include <stdio.h>
#include <string.h>

int main(void){
   char nome[100];
   char ch[10];
   do {
      printf("Qual é o seu nome: ");

      fgets(nome, 100, stdin);
      if(nome[strlen(nome)-1]=='\n')
         nome[strlen(nome)-1]='\0';

      printf("Meu nome é %s\n",nome);

      do {
         printf("Continua [S]im ou [N]ão ?");
         fgets(ch, 10, stdin);
         if(ch[strlen(ch)-1]=='\n')
            ch[strlen(ch)-1]='\0';
      } while (!strchr("SsNn",ch[0]));
   } while (strchr("Ss",ch[0]));
}
~$ ./Exemplo
Qual é o seu nome: João
Meu nome é João
Continua [S]im ou [N]ão ?n
~$
$ ./Exemplo
Qual é o seu nome: João
Meu nome é João
Continua [S]im ou [N]ão ?n
~$

 

Arquivo: Exemplo.c Arquivo: Exemplo.c
#include <stdio.h>
#include <string.h>

int main(void){
   char nome[100];
   char ch;
   do {
      printf("Qual é o seu nome: ");

      fgets(nome, 100, stdin);
      if(nome[strlen(nome)-1]=='\n')
         nome[strlen(nome)-1]='\0';

      printf("Meu nome é %s\n",nome);

      do {
         printf("Continua [S]im ou [N]ão ?");
         ch = getchar();
         scanf("%*[^\n]"); scanf("%*c"); // Limpar o buffer
      } while (!(ch == 'S' || ch == 's' || ch == 'N' || ch == 'n'));
   } while (ch == 'S' || ch == 's');
}
#include <stdio.h>
#include <string.h>

int main(void){
   char nome[100];
   char ch[10];
   do {
      printf("Qual é o seu nome: ");

      fgets(nome, 100, stdin);
      if(nome[strlen(nome)-1]=='\n')
         nome[strlen(nome)-1]='\0';

      printf("Meu nome é %s\n",nome);

      do {
         printf("Continua [S]im ou [N]ão ?");
         fgets(ch, 10, stdin);
         if(ch[strlen(ch)-1]=='\n')
            ch[strlen(ch)-1]='\0';
      } while (!(ch[0] == 'S' || ch[0] == 's' || ch[0] == 'N' || ch[0] == 'n'));
   } while (ch[0] == 'S' || ch[0] == 's');
}
~$ ./Exemplo
Qual é o seu nome: João
Meu nome é João
Continua [S]im ou [N]ão ?n
~$
~$ ./Exemplo
Qual é o seu nome: João
Meu nome é João
Continua [S]im ou [N]ão ?n
~$