Members, Can Have Different Types and Different Lengths
Members, Can Have Different Types and Different Lengths
http://www.java2s.com/Tutorial/C/0040__Data-Type/Catalog0040__Data-Type.htm
1.Language
1.5.Variable Declaration( 7 ) 1.15.Comments( 5 )
1.6.Variable Output( 1 ) 1.16.Convention( 2 )
1.10.Local variable( 1 )
First program
#include <stdio.h>
main(){
printf("Hi \n");
}
C language keywords
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while
1.3.1.main() function in C
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("Program name: %s\n", argv[0]);
int i;
for(i = 1 ; i<argc ; i++)
printf("\nArgument %d: %s", i, argv[i]);
return 0;
}
1.5.Variable Declaration
1.5.1. Variables
1.5.2. How to declare a variable
1.5.3. Using a variable to store value
1.5.4. Initialize int value in declaration
1.5.5. Meaningful variable name
1.5.6. Define three variables and use assignment operator to assign value
1.5.7. Use printf to output variable
1.5.1.Variables
average /* average of all grades */
pi /* pi to 6 decimal places */
number_of_students /* number students in this class */
3rd_ /* Begins with a number */
all$ /* Contains a "$" */
the end /* Contains a space */
int /* Reserved word */
entry_total /* total number of items in current entry */
all_total /* total of all entries */
int answer; /* the result of our expression */
1. The keyword int tells C that this variable contains an integer value.
2. The variable name is answer.
3. The semicolon (;) marks the end of the statement.
4. The comment is used to define this variable for the programmer.
#include <stdio.h>
int main(void)
{
int salary;
salary = 10000;
printf("My salary is %d.", salary);
return 0;
}
My salary is 10000.
1.5.4.Initialize int value in declaration
#include <stdio.h>
int main(void)
{
int number0 = 10, number1 = 40, number2 = 50, number3 = 80, number4 = 10;
int number5 = 20, number6 = 30, number7 = 60, number8 = 70, number9 = 110;
int sum = number0 + number1+ number2 + number3 + number4+
number5 + number6 + number7 + number8 + number9;
float average = (float)sum/10.0f;
printf("\nAverage of the ten numbers entered is: %f\n", average);
return 0;
}
Average of the ten numbers entered is: 48.000000
int p,q,r;
Now consider another declaration:
int account_number;
int balance_owed;
int account_number; /* Index for account table */
int balance_owed; /* Total owed us (in pennies)*/
int main()
{
int term; /* term used in two expressions */
int term_2; /* twice term */
int term_3; /* three times term */
term = 3 * 5;
term_2 = 2 * term;
term_3 = 3 * term;
return (0);
}
1.5.7.Use printf to output variable
#include <stdio.h>
int main()
{
int term; /* term used in two expressions */
term = 3 * 5;
printf("Twice %d is %d\n", term, 2*term);
printf("Three times %d is %d\n", term, 3*term);
return (0);
}
Twice 15 is 30
Three times 15 is 45
#include <stdio.h>
main(){
int x;
float y;
char c;
x = -4443;
y = 554.21;
c = 'M';
printf("\nThe value of integer variable x is %d", x);
printf("\nThe value of float variable y is %f", y);
printf("\nThe value of character variable c is %c\n", c);
}
The value of integer variable x is -4443
The value of float variable y is 554.210022
The value of character variable c is M
#include <stdio.h>
#include <limits.h>
#include <float.h>
int main(void)
{
printf("Variables of type char store values from %d to %d", CHAR_MIN, CHAR_MAX);
printf("\nVariables of type unsigned char store values from 0 to %u", UCHAR_MAX);
printf("\nVariables of type short store values from %d to %d", SHRT_MIN, SHRT_MAX);
printf("\nVariables of type unsigned short store values from 0 to %u",USHRT_MAX);
printf("\nVariables of type int store values from %d to %d", INT_MIN, INT_MAX);
printf("\nVariables of type unsigned int store values from 0 to %u", UINT_MAX);
printf("\nVariables of type long store values from %ld to %ld", LONG_MIN, LONG_MAX);
printf("\nVariables of type unsigned long store values from 0 to %lu", ULONG_MAX);
printf("\nVariables of type long long store values from %lld to %lld", LLONG_MIN, LLO
NG_MAX);
printf("\nVariables of type unsigned long long store values from 0 to %llu", ULLONG_M
AX);
printf("\n\nThe size of the smallest non-zero value of type float is %.3e", FLT_MIN);
printf("\nThe size of the largest value of type float is %.3e", FLT_MAX);
printf("\nThe size of the smallest non-zero value of type double is %.3e", DBL_MIN);
printf("\nThe size of the largest value of type double is %.3e", DBL_MAX);
printf("\nThe size of the smallest non-zero value of type long double is %.3Le", LDBL
_MIN);
printf("\nThe size of the largest value of type long double is %.3Le\n", LDBL_MAX);
printf("\nVariables of type float provide %u decimal digits precision.", FLT_DIG);
printf("\nVariables of type double provide %u decimal digits precision.", DBL_DIG);
printf("\nVariables of type long double provide %u decimal digits precision.", LDBL_D
IG);
return 0;
}
Variables of type char store values from -128 to 127
Variables of type unsigned char store values from 0 to 255
Variables of type short store values from -32768 to 32767
Variables of type unsigned short store values from 0 to 65535
Variables of type int store values from -2147483648 to 2147483647
Variables of type unsigned int store values from 0 to 4294967295
Variables of type long store values from -2147483648 to 2147483647
Variables of type unsigned long store values from 0 to 4294967295
Variables of type long long store values from -9223372036854775808 to
9223372036854775807
Variables of type unsigned long long store values from 0 to 18446744073709551615