Lecture 1-Basic C Programming Concepts
Lecture 1-Basic C Programming Concepts
Concepts
1
Outline
• Keywords
• Identifiers
• Comments
• Variable and It’s Naming Rules.
• Constants
• Format Specifiers
• Input – Output
• Escape Sequence
2
Keywords
• Character Set: a set of alphabets, letters and some special characters that
are valid in C language.
• Upper Case: A B C ................................... X Y Z
• Lower Case: a b c ...................................... x y z
• Digits: 0 1 2 3 4 5 6 7 8 9
• Special Symbols:
3
Keywords (contd.)
• Keywords: predefined, reserved words used in C that have special
meanings to the compiler.
• They cannot be used as an identifier (variables or function name).
• 32 keywords in total.
• Example: int num1; /// ‘int’ is a keyword
4
Identifiers
• Identifier: User defined names for variables, functions, arrays, structures,
unions, labels, etc.
• An identifier (ID) should be unique.
• Multiple IDs shouldn’t have same names.
• Example: int num1, num2, num3;
5
Comments
• Comment: optional statements to explain code and increase readability.
• Types: Single Line Comment and Multiline Comment
• Multiline Comment:
• Used to comment multiple lines at once. Syntax: “ /*……*/ ” or “ /**…..*/ ”
• Example: /* int num1;
char float; */
6
Variable and It’s Naming Rules
• Variable: a container (storage area) to hold varied data (numbers,
characters, symbols, strings, etc).
• It’s an identifier that’s value can be changed at any time.
• It’s name should be unique.
• It’s a symbolic representation of a memory location.
• It’s ‘type’ is specified to indicate the size of a memory location.
7
Variable and It’s Naming Rules
• Example:
- int num1, num2, num3=100;
- num1 = 200, num2 = 300;
• Naming Rules:
• only letters (upper & lower case), digits, underscore and $ (dollar) are allowed.
• Comma, space and other symbols can’t be used in a variable name.
• It should not begin with a digit or other symbols except $ (dollar).
• First character of a variable should be always $, letters or underscore.
• Keywords can not be used as a variable (ID) name.
• Variable name shouldn’t exceed more than 31 characters.
• Names should be meaningful.
• Recommended style: a) snake case: first_name, b) camel case: firstName.
8
Constants
• Constant: an identifier whose value can not be changed once assigned.
• The keyword ‘const’ is used to create a constant identifier.
• When creating a constant, you should assign a value right there.
• Declare a variable as constant if it’s unlikely to change throughout the C program.
• Example: const float PI = 3.1416;
PI = 4.56; // error (once value is assigned, can’t be changed later)
9
Format Specifiers
• Format Specifier: a string that is used to determine the format of
input and output.
10
Format Specifiers (contd.)
• Format Specifier: a string that is used to determine the format of
input and output.
11
Format Specifiers (contd.)
• In case of short, long, signed, unsigned type data, use the following specifiers:
12
Input - Output
• Input: to take input from user, we use scanf() function.
• Syntax: scanf( “specifier1 specifier2”, &variable1, &variable2 );
• Example: -> input = 10 (variable n1 )
scanf(“%d”, &n1 );
13
Input – Output (contd.)
• Output: to print output to the output console, we use printf() function.
• Syntax: printf( “specifiers”, variable1, variable2 );
• Example: -> output = 10 (variable n1 ) -> output = “Hello World!”
printf(“%d”, n1 ); printf(“Hello World!”);
15
Escape Sequence
• Escape Sequence Characters: special characters that control the pattern of
strings (texts), numbers and other characters in C.
16
Escape Sequence (contd.)
• Example:
• New line: printf(“first line. \n Second line.);
-> output: First line.
Second Line.
17
C Syntax
• #include <stdio.h> is a header file library that lets us work with input and
output functions, such as printf(). Header files add functionality to C
programs.
• A blank line. C ignores white space. But we use it to make the code more
readable.
• Another thing that always appear in a C program is main(). This is called a
function. Any code inside its curly brackets {} will be executed.
• printf() is a function used to output/print text to the screen. In our example, it
will output "Hello World!".Every C statement ends with a semicolon ;
• return 0 ends the main() function.
• Do not forget to add the closing curly bracket } to actually end the main
function.
18