C Programming: Design by
C Programming: Design by
What is a language?
A Language is the Spoken and signed forms of communication among Humankind. A formal language is a set of words, i.e. finite strings of letters, symbols, or tokens. The set from which these letters are taken is called the alphabet. Eg. English, Hindi etc.
What is a language?
Communicating with a Human Being involves speaking the language that human being understands.
Communicating with a computer involves speaking the language the computer and human being both understands,
What is C ?
C is a programming language developed at AT & Ts Bell Laboratories of USA in 1972. It was designed and written by a man named Dennis Ritchie. In the late seventies C began to replace the more familiar languages of that time like PL/I, ALGOL, etc. C is often called a middle-level computer language, because it has the features of both high-level languages, such as BASIC, PASCAL etc., and low-level languages such as assembly language.
Steps Of Learning C.
Alphabets Digits Special Symbols Constants Variables Key words Instructions Programs
A character denotes any alphabet, digit or special symbol used to represent information. Like:
Alphabets: A,B,C.X,Y,Z a,b,c,.....x, y, z Digits: 0,1,2,3,4,5,6,7,8,9. Special Symbols: ~!@#$%^&*()_+|-=\`[{}];:<>?/
Types of Constants
C constants can be divided into two major categories:
Primary Constants
Integer Constant. Real or Float Constant. Character Constant.
Secondary Constants
Array. Pointers Structure. Union Enum etc.
Integer Constant.
Rules of Integer Constants An integer constant must have at least one digit. It must not have a decimal point. It can be either positive or negative. If no sign precedes an integer constant it is assumed to be positive. No commas or blanks are allowed within an integer constant. The allowable range for integer constants is 32768 to 32767.
Integer Constant.
Example of valid Real or Float Constant +325.34 , 426.0 , -32.76, -48.5792 Example of in valid Real or Float Constant
1 1,000.0 Must Have (.) illegal Character (,)
2E+10.2
3 .20
The mantissa part and the exponential part should be separated by a letter e. The mantissa part may have a positive or negative sign. Default sign of mantissa part is positive. The exponent must have at least one digit, which must be a positive or negative integer. Default sign is positive. Range of real constants expressed in exponential form is -3.4e38 to 3.4e38.
Mantissa Separator
Exponent
3.2 e -5
Character Constants
Rules for Constructing Character Constants A character constant is a single alphabet, a single digit or a single special symbol enclosed within single inverted commas. Both the inverted commas should point to the left. For example, A is a valid character constant whereas A is not. The maximum length of a character constant can be 1 character.
Character Constants
Example of Character Constants. 'A' 'I' '5' '=' Invalid character Constants.
a AB
Commas are not in same direction More than one alphabet is declared.
Variable Constant
Variable names are names given to locations in memory. These locations can contain integer, real or character constants. The types of variables that it can support depend on the types of constants that it can handle. This is because a particular type of variable can hold only the same type of constant. For example,
An integer variable can hold only an integer constant. A real variable can hold only a real constant. A character variable can hold only a character constant.
Variable Constant
Rules of Variable declaration. Variables must be declared before use immediately after {. Valid characters are letters, digits and _ . First character cannot be a digit. 31 characters recognized for local variables. (more can be used, but are ignored). Upper and lower case letters are distinct.
Variable Declaration
Variable type Variable Name int b; double x; int b; unsigned int a; char c; C is a typed language that means a variable has a
Type Name
C standard types
int, double, char, float, short, long, long double unsigned char, unsigned short, unsigned int, unsigned long
Variable Constant
Keywords.
In Standard C, the keywords have special significance to the compiler and therefore can not be used as identifiers.
auto break case char const continue default do double else enum extern float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while
The Format of C
Statements are terminated with semicolons(;) Indentation is ignored by the compiler C is case sensitive - all keywords and
Standard Library functions are lowercase Strings are placed in double quotes () New lines are handled via \n Programs are capable of flagging success or error, those forgetting to do so have one or other chosen randomly!
printf writes
integer values to screen when %d is used float values to screen when %f is used character values to screen when %c is used integer values from the keyboard when %d is used. float values from the keyboard when %f is used. character values from the keyboard when %c is used.
scanf reads
& VERY important with scanf & not necessary with printf because current value of parameter is used
*/
main ()
{
Global constants Declaration
First C program.
#include <stdio.h> int main() { printf(Hello World\n); } // send string to standard output // input-output library // function main
First C program.
#include <stdio.h> includes the standard I/O library which allows you to read from the keyboard (standard in) and write to the screen (standard out) int main() declares the main function. Every C-program must have a function named main somewhere in the code { ... } The symbols { and } mark the beginning and end of a block of code. printf(Hello World\n) Sends output to the screen. The portion in quotes ... is the format strings and describes how the data is formatted.
Second program.
#include <stdio.h> standard I/O file void main() main function Code block starts/ Main start { int a; declare a variable of type integer. float x; declare a variable of type float. declare a variable of type character. char c; printf(Enter integer:); display on screen Enter integer. scanf(%d,&a); store the integer value in a. printf(\nEnter float:); display on screen Enter Float. scanf(%f,&x); store the Float value in x. printf(\nEnter character:); display on screen Enter character. store the character value in c. scanf(%c,&c); printf(\nValue of a is %d, of x is %f, of c is %c\n,a,x,c); gives output } Code block end or Main end
End of lecture 1