A data structure is a group of data elements grouped together under one name.
These data elements, known as
members, can have different types and different lengths.
[Link]
[Link]
[Link] Program( 1 ) [Link] Variables( 2 )
1.2.C Language Keywords( 1 ) [Link] Address( 2 )
[Link] function( 1 ) [Link] Pointer( 2 )
[Link] Line Arguments( 1 ) [Link] argument lists( 1 )
[Link] Declaration( 7 ) [Link]( 5 )
[Link] Output( 1 ) [Link]( 2 )
[Link] Size and Limitation( 2 ) [Link] Files( 6 )
[Link] Scope( 2 ) [Link] references( 1 )
[Link] variables( 2 ) [Link]( 1 )
[Link] 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
[Link]() function in C
In all C programs, the starting point is the main() function.
Every C program has one.
#include <stdio.h>
int main()
{
printf("Goodbye!\n");
return(0);
}
[Link] the command line arguments
#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;
}
[Link] 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
[Link]
1. You can store values in variables.
2. Each variable is identified by a variable name.
3. Each variable has a variable type.
4. Variable names start with a letter or underscore (_), followed by any number of letters, digits, or
underscores.
5. Uppercase is different from lowercase, so the names sam, Sam, and SAM specify three different
variables.
6. The variables are defined at the begining of the block.
The following is an example of some variable names:
average /* average of all grades */
pi /* pi to 6 decimal places */
number_of_students /* number students in this class */
The following are not variable names:
3rd_ /* Begins with a number */
all$ /* Contains a "$" */
the end /* Contains a space */
int /* Reserved word */
Meaningful variable name:
entry_total /* total number of items in current entry */
all_total /* total of all entries */
[Link] to declare a variable
A variable declaration serves three purposes:
1. It defines the name of the variable.
2. It defines the type of the variable (integer, real, character, etc.).
3. It gives the programmer a description of the variable.
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.
[Link] a variable to store value
#include <stdio.h>
int main(void)
{
int salary;
salary = 10000;
printf("My salary is %d.", salary);
return 0;
}
My salary is 10000.
[Link] 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
[Link] variable name
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)*/
[Link] three variables and use assignment operator to assign value
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);
}
[Link] 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
[Link] Variable Contents
Use the printf function with formatting options.
#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
Conversion Specifier Description
%d Displays integer value
%f Displays floating-point numbers
%c Displays character
[Link] Size and Limitation
[Link] the limits
[Link] the size of a type
[Link] the limits
#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
The size of the smallest non-zero value of type float is 1.175e-38
The size of the largest value of type float is 3.403e+38
The size of the smallest non-zero value of type double is 2.225e-308
The size of the largest value of type double is 1.798e+308
The size of the smallest non-zero value of type long double is 3.362e-4932
The size of the largest value of type long double is 1.190e+4932
Variables of type float provide 6 decimal digits precision.
Variables of type double provide 15 decimal digits precision.
Variables of type long double provide 18 decimal digits precision.