(Data Types, Character Strings, Formatted IO, Operators, Expressions and Statements)
Mao
● Keywords: int , short , long , unsigned , char , float , double , Bool , Complex ,
● Operator: sizeof ● Function: scanf() ● The basic data types that C uses ● The distinctions between integer types and floating-point types ● Writing constants and declaring variables of those types ● How to use the printf() and scanf() functions to read and write values of
different types
● Function: strlen() ● Keywords: const ● Character strings ● How character strings are created and stored ● How you can use scanf() and printf() to read and display character strings ● How to use the strlen() function to measure string lengths ● The C preprocessor’s #define directive and ANSI C’s const modifier for creating
symbolic constants
● Operators: = - * / ● % ++ -- (type) ● C’s multitudinous operators, including those used for common arithmetic
operations
● Operator precedence and the meanings of the terms statement and
expression
Take a look at the fundamental type keywords recognized by C. K&R C recognized seven keywords relating to types. The C90 standard added two to the list. The C99 standard adds yet another three.
An integer is a number with no fractional part. In C, an integer is never written with a decimal point.
Storing the integer 7 using a binary code.
A floating-point number more or less corresponds to what mathematicians call a real number . Real numbers include the numbers between the integers. Some floating-point numbers are 2.75, 3.16E7, 7.00, and 2e–8.
https://dev.to/fkkarakurt/data-types-variables-and-constants-in-c-10a3
You can use the printf() function to print int types. The %d is called a format specifier because it indicates the form that printf() uses to display a value. Each %d in the format string must be matched by a corresponding int value in the list of items to be printed.
You may have to insert a getchar(); statement in the code for some IDEs to keep the program execution window from closing immediately.
To display an integer in octal notation instead of decimal, use %o instead of %d . To display an integer in hexadecimal, use %x . If you want to display the C prefixes, you can use specifiers %#o , %#x , and %#X to generate the 0 , 0x , and 0X prefixes respectively.
Other integer types are declared in the same manner as the int type.
long int estine; long johns; short int erns; short ribs; unsigned int s_count; unsigned long headcount; unsigned short yesvotes;
The char type is used for storing characters such as letters and punctuation marks, but technically it is an integer type. Why? Because the char type actually stores integers, not characters.
To handle characters, the computer uses a numerical code in which certain integers represent certain characters. The most commonly used code in the U.S. is the ASCII code.
https://commons.wikimedia.org/wiki/File:ASCII-Table-wide.pdf
Declaring Type char Variables:
char response; char itable, latan;
This code would create three char variables: response , itable , and latan . You can assign the character A to grade with the following initialization:
char grade = 'A';
A single character contained between single quotes is a C character constant . When the compiler sees 'A' , it converts the 'A' to the proper code value. The single quotes are essential. Here’s another example:
char broiled; /* declare a char variable */ broiled = 'T'; /* OK */ broiled = T; /* NO! Thinks T is a variable */ broiled = "T"; /* NO! Thinks "T" is a string */
Because characters are really stored as numeric values, you can also use the numerical code to assign values:
char grade = 65; /* ok for ASCII, but poor style */
In this example, 65 is type int , but, because the value is smaller than the maximum char size, it can be assigned to grade without any problems. Because 65 is the ASCII code for the letter A .
To represent certain awkward characters in C is to use special symbol sequences. These are called escape sequences .
The printf() function uses %c to indicate that a character should be printed. Recall that a character variable is stored as a 1-byte integer value.
Therefore, if you print the value of a char variable with the usual %d specifier, you get an integer. The %c format specifier tells printf() to display the character that has that integer as its code value.
The scanf() function then fetches the character you typed, and the ampersand ( & ) causes the character to be assigned to the variable ch . The printf() function then prints the value of ch twice, first as a character (prompted by the %c code) and then as a decimal integer (prompted by the %d code).
Floating-point variables are declared and initialized in the same manner as their integer cousins. Here are some examples:
float noah, jonah; double trouble = 3.1415; float planck = 6.63e-34; long double gnp; int num;
The printf() function uses the %f format specifier to print type float and double numbers using decimal notation, and it uses %e to print them in exponential notation.
If your system supports the hexadecimal format for floating-point numbers, you can use a or A instead of e or E. The long double type requires the %Lf , %Le , and %La specifiers to print that type. Note that both float and double use the %f , %e , or %a specifier for output.
Many computations in science and engineering use complex and imaginary numbers.
In brief, there are three complex types, called float _Complex , double _Complex , and long double _Complex. A float _Complex variable, for example, would contain two float values, one representing the real part of a complex number and one representing the imaginary part.
Similarly, there are three imaginary types, called float _Imaginary, double _Imaginary, and long double _Imaginary.
What type sizes does your system use? Use sizeof().
Here are the main new features of this program:
● It uses an array to hold a character string . Here, someone’s name is read into the array, which, in this case, is a series of 40 consecutive bytes in memory, each able to hold a single character value. ● It uses the %s conversion specification to handle the input and output of the string. Note that name , unlike weight , does not use the & prefix when used with scanf() . (As you’ll see later, both &weight and name are addresses.) ● It uses the C preprocessor to define the symbolic constant DENSITY to represent the value 62.4 .
C has no special variable type for strings. Instead, strings are stored in an array of type char. Characters in a string are stored in adjacent memory cells, one character per cell, and an array consists of adjacent memory locations.
Note that the character \0 in the last array position. This is the null character, and C uses it to mark the end of a string. The null character is not the digit zero.
The brackets after name identify it as an array. The 40 within the brackets indicates the number of elements in the array. The char identifies the type of each element.
Declaring a variable versus declaring an array.
String literals can be assigned without size. Here, the name of the string str acts as a pointer because it is an array.
char str[] = "snow";
String literals can be assigned with a predefined size. But we should always account for one extra space which will be assigned to the null character. If we want to store a string of size n then we should always declare a string with a size equal to or greater than n+1.
char str[5] = "snow"; char str[5] = {'s', 'n', 'o', 'w','\0'};// NULL character '\0' is required in this declaration
You do not have to put the null character into the name array yourself. That task is done for you by scanf() when it reads the input. Nor do you include a null character in the character string constant
PRAISE .
The string constant "x" is not the same as the character constant 'x' . One difference is that 'x' is a basic type ( char ), but "x" is a derived type, an array of char . A second difference is that "x" really consists of two characters, 'x' and '\0' , the null character.
The character 'x' and the string "x".
The array name has 40 memory cells, and that is what the sizeof operator reports. Only the first 11 cells are needed to hold Serendipity, however, and that is what strlen() reports.
The twelfth cell in the array name contains the null character, and its presence tells strlen() when to stop counting.
11 characters.
First comes #define . Next comes the symbolic name for the constant and then the value for the constant. (Note that this construction does not use the = sign.) So the general
Note that no semicolon is used.
The #define statement can be used for character and string constants, too. Just use single quotes for the former and double quotes for the latter. The following examples are valid:
#define BEEP '\a'
#define TEE 'T'
#define ESC '\033'
#define OOPS "Now you have done it!"
C90 added a second way to create symbolic constants—using the const keyword to convert a declaration for a variable into a declaration for a constant:
const int MONTHS = 12; // MONTHS a symbolic constant for 12
This makes MONTHS into a read-only value.
That is, you can display MONTHS and use it in calculations, but you cannot alter the value of MONTHS .
Using printf()
Control-string is the phrase enclosed in double quotes.
Conversion Specification Modifiers for printf()
Using scanf()
Like printf() , scanf() uses a control string followed by a list of arguments. The control string indicates the destination data types for the input stream of characters.
Assignment Operator: =
In C, the equal sign does not mean “equals.” Rather, it is a value-assigning operator. The statement
bmw = 2002;
assigns the value 2002 to the variable named bmw . Consider the following common type of computer statement:
i = i + 1;
It means “Find the value of the variable named i , add 1 to that value, and then assign this new value to the variable i"
+ and -
The addition operator + causes the two values on either side of it to be added together. For example, the statement
printf("%d", 4 + 20);
causes the number 24 to be printed, not the expression 4 + 20.
The subtraction operator – causes the number after the – sign to be subtracted from the number before the sign. The statement
takehome = 224.00 – 24.00;
assigns the value 200.0 to takehome .
https://medium.com/@nisargpatel_58137/operators-in-c-7af8614675cc
++ and – –
++ and – –
+=, –=, *=, /= and %=
● Stephen Prata. C Primer Plus (6th Edition). Addison-Wesley
Professional, 2013.
Algorithms and Data Structures - Mao © 2022 There is no failure just changeable code.