C basic data types5/7/2023 ![]() ![]() Specific number of arguments, and the compiler can check to see whether You might be annoyed that the compiler doesn't catch such an obviousĮrror. Might the memory contents be different, but different compilers will manage Numbers you get could very well be different from those shown here. Whatever values happened to be lying around in memory for the next two! (The Program used ten to provide a value for the first %d and used Int constant 2, and the third %d represents the value Int variable ten, the second %d represents the Printf("%d minus %d is %d\n", ten ) // forgot 2 argumentsĬompiling and running the program produced this output on one system: Doing it right: 10 minus 2 is 8įor the first line of output, the first %d represents the Printf("%d minus %d is %d\n", ten, 2, ten - two ) Listing 3.2 The print1.c Program /* print1.c-displays some properties of printf() */ That initializes a variable and prints the value of the variable, the value of aĬonstant, and the value of a simple expression. Number of format specifiers matches the number of values the compiler That valueĬan be an int variable, an int constant, or any otherĮxpression having an int value. Each %d in the format string must be matched by aĬorresponding int value in the list of items to be printed. ![]() Specifier because it indicates the form that printf() uses toĭisplay a value. You saw in Chapter 2, the %d notation is used to indicate just where inĪ line the integer is to be printed. You can use the printf() function to print int types. Very large integers can be treated differently see the laterĭiscussion of the long int type in the section " longĬonstants and long long Constants." Printing int Values Therefore, 22 and ≤4 are integer constants, butĢ2.0 and 2.2E1 are not. Without a decimal point and without an exponent, C recognizes it as an integer. ![]() In the last example are integer constants. The various integers ( 21, 32, 14, and 94) In short, these declarations create and label the storage for the variablesĪnd assign starting values to each (see Figure 3.4).ģ.4 Defining and initializing a variable. Is best to avoid putting initialized and noninitialized variables in the same Lead you to think that dogs is also initialized to 94, so it In the last line, only cats is initialized. Int dogs, cats = 94 /* valid, but poor, form */ Justįollow the variable name with the assignment operator ( =) and the value In C, this can be done as part of the declaration. To initialize a variable means to assign it a starting, or Second, a variable can pick up a value from a functionfrom scanf(), for example. How do variables get values? You've seen two ways that they can pick up values in the program. These declarations create variables but don't supply values for them. The effect is the same: Associate names and arrange storage space for four int-sized variables. You could have used a separate declaration for each variable, or you could have declared all four variables in the same statement. The following are valid declarations: int erns To declare more than one variable, you can declare each variable separately, or you can follow the int with a list of names in which each name is separated from the next by a comma. First comes int, and then the chosen name of the variable, and then a semicolon. Declaring an int VariableĪs you saw in Chapter 2, "Introducing C," the keyword int is used to declare the basic integer variable. Integers by using the value of a particular bit to indicate the sign. ISO/ANSI C specifies that the minimum range for type int should be from Moving toward 64-bit processors that naturally will use even larger integers. Have 32-bit integers and fit an int to that size. Therefore, older IBM PC compatibles, which have a 16-bit word, use 16īits to store an int. Typically, an int uses one machine word for That means it must be an integerĪnd it can be positive, negative, or zero. Requirements of a particular task or machine, they are available. Type is the basic choice, but should you need other choices to meet the Values offered and in whether negative numbers can be used. In particular, the C integer types vary in the range of ![]() The answer is that C gives the programmer the option of matching a type The int TypeĬ offers many integer types, and you might wonder why one type isn'tĮnough. Types, so check your documentation to see which ones you have available. Some older C compilers do not support all these ForĮach type, we describe how to declare a variable, how to represent a constant,Īnd what a typical use would be. Now let's look at the specifics of the basic data types used by C. ![]()
0 Comments
Leave a Reply. |