Getting Started with C programming--enumeration and macro definitions

Source: Internet
Author: User

Enumerations are the basic data types that programmers define themselves, and variables of this type can only take the values in the enumeration, and the benefit is the ability to prevent the erroneous assignment of variables. Usage: Enum type name {variable 1, variable 2, variable 3, ...}

C language macro definition is defined in the program head, for preprocessing, there are no parameters and two, can be regarded as a process of invocation, and function calls are not the same, not involved in the stack out of the problem, call fast. Usage: #define REPLACE name originally


Macro Definition Example 1:

# include <stdio.h> #define DD "%d"//macro definition, in the following writing can be used instead of "%d" DD. int main (void) {Register int i;i = 54321;printf (DD "\ n", i);  Call the macro name with the same effect as the following statement printf ("%d\n", I); return 0;}

Macro Definition Example 2:

# include <stdio.h> #define D "%d"//macro definition is not a statement, you do not need to add a semicolon-defined P printf#define swap (A, b) {int c; c=a; a=b; b=c;} int main (void) {int i = 3;int j = 5;swap (i, j); P ("i =" D ", j =" D "\ n", I, j); Macro definitions with parameters are similar to functions, which are fundamentally different from functions and do not have a function call and run faster. return 0;}

Enumeration Example 1:

# include <stdio.h>//defines enumerated constant types, enumerating all possible values of a thing, so that the type variable can only take the enumerated value. Enum WeekDay//weekday for the new type name {MonDay, Tuesday =-1, Wednesday, Thursday, FriDay, SaturDay, SunDay}; enumerated constants, whose actual values are sequentially 0, 1, 2, 3 ..., if the value is assigned on a constant, then the subsequent value is added in turn by 1. int main (void) {//int day;//defines a one-week variable and is defined as an int type, which is obviously inappropriate because the week variable is from Monday to Sunday. Enum WeekDay day = friday;printf ("%d\n", day); enumerated constants whose actual values are sequentially 0, 1, 2, and 3...return 0;}

The value of the enumeration constant:

enumerated constants, whose actual values are sequentially 0, 1, 2, 3 ..., if the value is assigned on a constant, then the subsequent value is added in turn by 1.

# include <stdio.h>enum Weekday{monday, Tuesday, Wednesday, Thursday, FriDay, SaturDay, Sunday};void f (enum WeekDay int main (void) {enum WeekDay day;f (FriDay);//friday actual value is 4return 0;} void f (enum Weekdayi) {switch (i) {case 0:printf ("monday\n"), Break;case 1:printf ("tuesday\n"), Break;case 2:printf (" Wednesday\n "), Break;case 3:printf (" thursday\n "), Break;case 4:printf (" friday\n "); Break;case 5:printf (" SaturDay\n ") ; Break;case 6:printf ("sunday\n"); return;}

Complementary: Complement procedure:

/* Through the program, how much is the complement of a number. */# include <stdio.h>int main (void) {int i = 21;//positive integer complement hexadecimal output normal int j =-1;//negative complement hex output inverse plus 1, 0 reverse all 1, so the left is all 1, which is all f. int k = -3;int L = -100;int s = 0xfffffff5;char ch = 129; 129 the default is the int type, preceded by 0, and assigned to 1 byte variables, the first 3 bytes of data are lost. printf ("% #X \ n", i);p rintf ("% #X \ n", j);p rintf ("% #X \ n", K);p rintf ("% #X \ n", L);p rintf ("%d\n", s);p rintf ("%d\n", ch); This is why the output is-127. return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Getting Started with C programming--enumeration and macro definitions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.