First Program in C With Output
C Language Keywords
Variables In C
Initialize int value in declaration
Using a variable to store value
Meaningful variable name
Define 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); }
Use printf to output variable
Printing Variable Contents
Finding the limits
#include #include #include 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_MA X); 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", U LLONG_MAX); 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_D IG); printf("\nVariables of type double provide %u decimal digits precision.", DBL _DIG); printf("\nVariables of type long double provide %u decimal digits precision.", LDBL_DIG); return 0; }
OUTPUT
Finding the size of a type
Scope of variables
value of i is 1 the value of i is 10 The Value of i is 1 The Value of i is 10
Inner variable shadows outer variable
Output
Declare global variables
Define and use Global variables
Local variable shadows global variable.
Static variable
Output
Static versus automatic variables
Output address and value
Using the & operator
Calculate an average using variable argument lists
Adding Comments
Indentation and Code Format
Without Indentation
With Indentation
Clarity
Header Files
IncludeHeader File
Newly Added Headers
External Reference
// Program in file A1.c #include void hell() { int i =60; printf("value o f i %d\n",i); }
// Program in file A2.c #include extern int i; void main() { int i =50; clrscr(); hell(); printf("value of i %d\n",i); }
output The value of variable i is 60
Data Types
Unsigned Variables
Reading Integer’s
Reading integer’s can be done through scanf void main(){ int i; scanf(“%d”,&i); printf(“i=%d”,i); }
#include int main() { printf( "%4d\n", 1 ); printf( "%4d\n", 12 ); printf( "%4d\n", 123 ); printf( "%4d\n", 1234 ); printf( "%4d\n\n", ); printf( "%4d\n", -1 ); printf( "%4d\n", -12 ); printf( "%4d\n", -123 ); printf( "%4d\n", ); printf( "%4d\n", ); return 0; }
#include int main() { printf( "%4d\n", 1 ); printf( "%4d\n", 12 ); printf( "%4d\n", 123 ); printf( "%4d\n", 1234 ); printf( "%4d\n\n", ); printf( "%4d\n", -1 ); printf( "%4d\n", -12 ); printf( "%4d\n", -123 ); printf( "%4d\n", ); printf( "%4d\n", ); return 0; }
Simple Int Calculation
Divide Integer
Sum the integers from 1 to a user- specified number
If both operands i1 and i2 are integers, the expression i1/i2 provides integer division
The atoi() function: read numeric values from the keyboard
Hexadecimal Numbers
Octal Numbers
Save Tab key into a char, you use an escape sequence
Output
#include int main() { clrscr(); // char tab='\x9C'; printf("**********"); printf("%%%\\Hello"); // printf("a%cb",tab); return 0; }
SOME % WILL BE DIMINISHED BECOZ OF “\\” output
#include int main() { clrscr(); // char tab='\x9C'; printf("**********"); printf("\?Hello\?"); // printf("a%cb",tab); return 0; }
Display the ASCII characters and their corresponding codes, from Code 32 on up to Code 127
Overflow in char and unsigned char data type Overflow means you are carrying out an operation such that the value either exceeds the maximum value or is less than the minimum value of the data type. (Definition from C & Data Structures by P.S. Deshpande and O.G. Kakde Charles River Media 2004)
Compare characters to characters in if statement #include void main() { char cResponse = '\0'; clrscr(); printf("\n\tAC Control Unit\n"); printf("\na\tTurn the AC on\n"); printf("b\tTurn the AC off\n"); printf("\nEnter your selection: "); scanf("%c", &cResponse);
Compare char variable in if statement
if (cResponse == 'a') printf("\nAC is now on\n"); if (cResponse == 'b') printf("\nAC is now off\n"); getch(); }
Using functions islower, isupper, tolower, toupper
Using functions isdigit, isalpha, isalnum, and isxdigit
Using functions isspace, iscntrl, ispunct, isprint, isgraph
Floats
C provides two main floating-point representations: float (single precision) and double (double precision ). A floating-point number has a fractional part and a biased exponent. Float occupies 4 bytes and double occupies 8 bytes.
Use the format %f for printing floating numbers
Float Basics
Define float constant value using Macro
Assignment of an integer expression to a floating-point variable. C will automatically perform the conversion from integer to floating point. A similar conversion is performed when a floating-point number is assigned to an integer.
output
To create precision with floating-point numbers
use the %E in printf() to display scientific-notation numbers
Printing floating-point numbers with floating-point conversion specifiers
Read float number and do a calculation