C Program Structure Prepared By G J Kamani

C Program Structure Prepared By: G. J. Kamani

C Program Structure Documentation Section set of comment lines giving program information. Link Section instruction to the compiler to link functions from the system library Definition Section Global Declaration Section main( ) Function Section { defines all symbolic constants. There are some variables that are used in more than one function. Such variables are called global variables and are declared in the global declaration section Every C program must have one main( ) function section } Subprogram Section Function 1 Function 2 (User Define Functions) contains all the user defined functions that are called in the main function

Simple Program /* Simple Program to Display Welcome To Documentation Section CAET-Godhra on the screen. */ #include <stdio. h> Link Section void main() { main( ) Function Section printf(“Welcome To CAET-Godhra”); }

C Character Set C language character set denotes any valid character in C programming language. This character can be alphabet, digit, special symbols and white spaces. Following table shows the valid alphabets, numbers, special symbols and white spaces allowed in C language. Alphabets : A, a, B, b, C, c etc…. Numbers : 1, 2, 3 etc… Special Symbols : , (Comma), ; (Semi colon), # (Hash), =(equal) etc… White Space : Blank Space, Horizontal Tab, New Line etc…

C Constants in C refer to fixed values that do not change during the execution of a program. C language constants are also known as Literals. C supports several types of constants as under. NUMERIC CONSTANT: Integer Constant=> 11, 22, -400, +323, 0, 99 etc… Real Constant 12. 32, -23. 465, 0. 50 etc… => CHARACTER CONSTANT: Single Character Constant => ‘a’, ‘A’, ‘ 5’, ‘$’ etc… String Constant => “Hello”, “ 5+3” etc…

C Keywords In C language, keywords are special words with fixed meaning. These meanings cannot be changed. 32 keywords available in C. those are given below: 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

C Data Types PRIMARY DATA TYPES Integral Data Types Character Signed Unsigned int unsigned int char short int unsigned short int signed char long unsigned char Floating point Type void float double long double

Data type value range Type char signed char unsigned char Length 8 bits Range -128 to 127 0 to 255 int signed int unsigned int signed short int unsigned short int long signed long unsigned long 16 bits 8 bits 32 bits -32768 to 32767 0 to 65535 -128 to 127 0 to 255 -2147483648 to 2147483647 0 to 4294967295 float double long double 32 bits 64 bits 80 bits 3. 4 * 10– 38 to 3. 4 * 10+38 1. 7 * 10– 308 to 1. 7 * 10+308 3. 4 * 10– 4932 to 3. 4 * 10+4932

C Variables A variable is a data name that may be used to store a data value. A variable may take different values at different times during execution. A variable name can be chosen by the programmer in a meaningful way. A variable name must following rules. 1. The first character in the variable name must be Letter. 2. Variable name length is maximum 31 characters. 3. Keywords are not allowed as variable name. 4. No commas or blanks are allowed within a variable name. 5. No special symbol other than underscore( as in gross_sal ) can be used in a variable name. Valid Variable Name: name, marks, tot_marks, sub 1, sub_10 Invalid Variable Name: 1 sub, 123, (name), tot*marks

printf() Function The printf function is just a useful function from the standard library(stdio. h) of functions that are accessible by C programs. printf() Format Specifiers printf() escape sequences There are many format specifiers defined in C. %i or %d int n (newline) %c char %f float %lf double %s string %x Hexadecimal b (backspace) %o Octal r (carriage return) %u Unsigned value Note: %lf stands for long float. t (tab) v (vertical tab) f (new page)

printf() function Program 1 #include <stdio. h> void main() { int a; float b; a=100; b=10. 5628; printf("%d n", a); printf("%5 d n", a); printf("%-5 d n", a); printf("%f n", b); printf("%. 2 f n", b); printf("%10. 3 f n", b); printf("%-10. 3 f n", b); printf(“%c n”, ’A’); printf(“%c n”, 65); } Output 1 0 0 1 0. 5 6 2 8 1 0. 5 6 1 0. A A 5 6 3

printf() function Program 2 #include <stdio. h> void main() Output { printf("Hexadecimal: %xn", 255); F F printf("Octal: %on", 255); 3 7 7 printf("Unsigned value: %un", 150); 1 5 0 printf("Unsigned value: %un", -10); 6 5 5 2 6 printf("%sn", "HELLO"); H E L L 0 H E printf(“%10 sn", " HELLO"); printf(“%-10 sn", " HELLO"); H E L printf(“%. 3 sn", " HELLO"); H E L printf(“%10. 3 sn", " HELLO"); } L L L O H E L O

Thank You
- Slides: 13