C Programming By Mrs. B. Dhivya M.C.A., M.Phil.,(Ph.D)., Technical Trainer / Assistant Professor, Department of Computer Science, Sri Ramakrishna College of Arts & Science, Coimbatore. C Basics
Agenda • What is C Language? • History of C • Features of C • Structure of C Programs • Tokens in C
What is C Language? • C is a general-purpose, procedural, high-level programming language. • C language was developed by Dennis M. Ritchie at the Bell Telephone Laboratories in 1972. • It is a powerful and flexible language which was first developed for the programming of the UNIX operating System. • C is one of the most widely used programming languages. • C programming language is known for its simplicity and efficiency.
History of C
Features of C
EXAMPLE PROGRAM #include <stdio.h> #include<conio.h> void main() { printf("Hiii, Studentsn"); getch(); }
Standard I/O Standard Input : printf() - This function stands for "print formatted" and is used for formatted output. It allows you to specify the format of the output by using format specifies like %d for integers, %f for floats, %s for strings, etc. int num = 10; printf("The number is: %dn", num); Standard Output: scanf() is a function in C programming used for taking input from the user. It reads formatted data from the standard input (usually the keyboard) and assigns the values to the variables passed as arguments. scanf("%d", &num); // Reads an integer input from the user and stores it in 'num
Tokens in C A token in C can be defined as the smallest individual element of the C programming language that is meaningful to the compiler. It is the basic component of a C program.
Keywords The keywords are pre-defined or reserved words in a programming language. Each keyword is meant to perform a specific function in a program. C language supports 32 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
Identifiers •Identifiers are used as the general terminology for the naming of variables, functions, and arrays. •These are user-defined names consisting of an arbitrarily long sequence of letters and digits with either a letter or the underscore(_) as a first character. •Identifier names must differ in spelling and case from any keywords.
Constants The constants refer to the variables with fixed values. They are like normal variables but with the difference that their values can not be modified in the program once they are defined. Constants may belong to any of the data types. Examples of Constants in C const int c_var = 20; const int* const ptr = &c_var;
Strings Strings are nothing but an array of characters ended with a null character (‘0’). This null character indicates the end of the string. Strings are always enclosed in double quotes. Whereas, a character is enclosed in single quotes in C. Examples of String char str[20] = {‘g’, ’e’, ‘e’, ‘k’, ‘s’, ‘f’, ‘o’, ‘r’, ‘g’, ’e’, ‘e’, ‘k’, ‘s’, ‘0’}; char str[20] = “geeksforgeeks”; Char s1[] = “geeksforgeeks”;
Special Symbols Special symbols are used in C having some special meaning and thus, cannot be used for some other purpose. Brackets[]: Opening and closing brackets are used as array element references. These indicate single and multidimensional subscripts. Parentheses(): These special symbols are used to indicate function calls and function parameters. Braces{}: These opening and ending curly braces mark the start and end of a block of code containing more than one executable statement. Comma (, ): It is used to separate more than one statement like for separating parameters in function calls. Colon(:): It is an operator that essentially invokes something called an initialization list. Semicolon(;): It is known as a statement terminator. It indicates the end of one logical entity. That’s why each individual statement must be ended with a semicolon. Asterisk (*): It is used to create a pointer variable and for the multiplication of variables. Assignment operator(=): It is used to assign values and for logical operation validation. Pre-processor (#): The preprocessor is a macro processor that is used automatically by the compiler to transform your program before actual compilation. Period (.): Used to access members of a structure or union. Tilde(~): Used as a destructor to free some space from memory.
Operators Operators are symbols that trigger an action when applied to C variables and other objects. The data items on which operators act are called operands. Unary Operators: Those operators that require only a single operand to act upon are known as unary operators. For Example increment and decrement operators Binary Operators: Those operators that require two operands to act upon are called binary operators. Binary operators can further are classified into: Arithmetic operators Relational Operators Logical Operators Assignment Operators Bitwise Operator Ternary Operator: The operator that requires three operands to act upon is called the ternary operator. Conditional Operator(?) is also called the ternary operator.
Activity - 1 Program to Print an Integer #include <stdio.h> int main() { int number; printf("Enter an integer: “, number); // reads and stores input scanf("%d", &number); // displays output printf("You entered: %d", number); return 0; }
Output Enter an integer:5 You entered:5
Activity - 2 Write a Program to Add Two Integers:. #include <stdio.h> int main() { int num1, num2, sum; printf("Enter two integers: “,num1, num2); scanf("%d %d", &num1, &num2); // calculate the sum sum = num1 + num2; printf("%d + %d = %d", num1, num2, sum); return 0; }
Output Enter two integers: 12 11 12 + 11 = 23
Predict the Output or error of the C Program 1. void main() { int const * p=5; printf("%d",++(*p)); } Answer: Compiler error: Cannot modify a constant value. Explanation: p is a pointer to a "constant integer". But we tried to change the value of the "constant integer".
2. main() { printf("nab"); printf("bsi"); printf("rha"); } Answer: hai Explanation: n (Newline): This escape sequence moves the cursor to the beginning of the next line. When printf encounters n, it moves the cursor to the next line, starting from the beginning. b (Backspace): This escape sequence moves the cursor back one position but doesn’t erase any character. When printf encounters b, it moves the cursor back without clearing any character. r (Carriage Return): This escape sequence moves the cursor to the beginning of the current line without advancing to the next line. Any characters printed after r overwrite the existing characters on that line.
3. #define clrscr() 100 main() { clrscr(); printf("%dn",clrscr()); } Answer: 100 Explanation: Preprocessor executes as a seperate pass before the execution of the compiler. So textual replacement of clrscr() to 100 occurs.
4. main() { printf("%p",main); } Answer: Some address will be printed. Explanation: Function names are just addresses (just like array names are addresses). main() is also a function. So the address of function main will be printed. %p in printf specifies that the argument is an address. They are printed as hexadecimal numbers.
5. main() { clrscr(); } clrscr(); Answer: No output/error Explanation: The first clrscr() occurs inside a function. So it becomes a function call. In the second clrscr(); is a function declaration (because it is not inside any function).
6. main() { int i=400,j=300; printf("%d..%d"); } Answer: 400..300 Explanation: printf takes the values of the first two assignments of the program. Any number of printf's may be given. All of them take only the first two values. If more number of assignments given in the program,then printf will take garbage value
7. #include <stdio.h> main() { int i=1,j=2; switch(i) { case 1: printf("GOOD"); break; case j: printf("BAD"); break; } } Answer: Compiler Error: Constant expression required in function main. Explanation: The case statement can have only constant expressions (this implies that we cannot use variable names directly so an error).
Assessment 1 1. Program to check if the given character is a vowel or consonant . 2. C Program to Find the Largest Number Among Three Numbers

C Programming - Basics of c -history of c

  • 1.
    C Programming By Mrs. B.Dhivya M.C.A., M.Phil.,(Ph.D)., Technical Trainer / Assistant Professor, Department of Computer Science, Sri Ramakrishna College of Arts & Science, Coimbatore. C Basics
  • 2.
    Agenda • What isC Language? • History of C • Features of C • Structure of C Programs • Tokens in C
  • 3.
    What is CLanguage? • C is a general-purpose, procedural, high-level programming language. • C language was developed by Dennis M. Ritchie at the Bell Telephone Laboratories in 1972. • It is a powerful and flexible language which was first developed for the programming of the UNIX operating System. • C is one of the most widely used programming languages. • C programming language is known for its simplicity and efficiency.
  • 4.
  • 5.
  • 7.
    EXAMPLE PROGRAM #include <stdio.h> #include<conio.h> voidmain() { printf("Hiii, Studentsn"); getch(); }
  • 8.
    Standard I/O Standard Input: printf() - This function stands for "print formatted" and is used for formatted output. It allows you to specify the format of the output by using format specifies like %d for integers, %f for floats, %s for strings, etc. int num = 10; printf("The number is: %dn", num); Standard Output: scanf() is a function in C programming used for taking input from the user. It reads formatted data from the standard input (usually the keyboard) and assigns the values to the variables passed as arguments. scanf("%d", &num); // Reads an integer input from the user and stores it in 'num
  • 9.
    Tokens in C Atoken in C can be defined as the smallest individual element of the C programming language that is meaningful to the compiler. It is the basic component of a C program.
  • 10.
    Keywords The keywords arepre-defined or reserved words in a programming language. Each keyword is meant to perform a specific function in a program. C language supports 32 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
  • 11.
    Identifiers •Identifiers are usedas the general terminology for the naming of variables, functions, and arrays. •These are user-defined names consisting of an arbitrarily long sequence of letters and digits with either a letter or the underscore(_) as a first character. •Identifier names must differ in spelling and case from any keywords.
  • 12.
    Constants The constants referto the variables with fixed values. They are like normal variables but with the difference that their values can not be modified in the program once they are defined. Constants may belong to any of the data types. Examples of Constants in C const int c_var = 20; const int* const ptr = &c_var;
  • 13.
    Strings Strings are nothingbut an array of characters ended with a null character (‘0’). This null character indicates the end of the string. Strings are always enclosed in double quotes. Whereas, a character is enclosed in single quotes in C. Examples of String char str[20] = {‘g’, ’e’, ‘e’, ‘k’, ‘s’, ‘f’, ‘o’, ‘r’, ‘g’, ’e’, ‘e’, ‘k’, ‘s’, ‘0’}; char str[20] = “geeksforgeeks”; Char s1[] = “geeksforgeeks”;
  • 14.
    Special Symbols Special symbolsare used in C having some special meaning and thus, cannot be used for some other purpose. Brackets[]: Opening and closing brackets are used as array element references. These indicate single and multidimensional subscripts. Parentheses(): These special symbols are used to indicate function calls and function parameters. Braces{}: These opening and ending curly braces mark the start and end of a block of code containing more than one executable statement. Comma (, ): It is used to separate more than one statement like for separating parameters in function calls. Colon(:): It is an operator that essentially invokes something called an initialization list. Semicolon(;): It is known as a statement terminator. It indicates the end of one logical entity. That’s why each individual statement must be ended with a semicolon. Asterisk (*): It is used to create a pointer variable and for the multiplication of variables. Assignment operator(=): It is used to assign values and for logical operation validation. Pre-processor (#): The preprocessor is a macro processor that is used automatically by the compiler to transform your program before actual compilation. Period (.): Used to access members of a structure or union. Tilde(~): Used as a destructor to free some space from memory.
  • 15.
    Operators Operators are symbolsthat trigger an action when applied to C variables and other objects. The data items on which operators act are called operands. Unary Operators: Those operators that require only a single operand to act upon are known as unary operators. For Example increment and decrement operators Binary Operators: Those operators that require two operands to act upon are called binary operators. Binary operators can further are classified into: Arithmetic operators Relational Operators Logical Operators Assignment Operators Bitwise Operator Ternary Operator: The operator that requires three operands to act upon is called the ternary operator. Conditional Operator(?) is also called the ternary operator.
  • 16.
    Activity - 1 Programto Print an Integer #include <stdio.h> int main() { int number; printf("Enter an integer: “, number); // reads and stores input scanf("%d", &number); // displays output printf("You entered: %d", number); return 0; }
  • 17.
  • 18.
    Activity - 2 Writea Program to Add Two Integers:. #include <stdio.h> int main() { int num1, num2, sum; printf("Enter two integers: “,num1, num2); scanf("%d %d", &num1, &num2); // calculate the sum sum = num1 + num2; printf("%d + %d = %d", num1, num2, sum); return 0; }
  • 19.
    Output Enter two integers:12 11 12 + 11 = 23
  • 20.
    Predict the Outputor error of the C Program 1. void main() { int const * p=5; printf("%d",++(*p)); } Answer: Compiler error: Cannot modify a constant value. Explanation: p is a pointer to a "constant integer". But we tried to change the value of the "constant integer".
  • 21.
    2. main() { printf("nab"); printf("bsi"); printf("rha"); } Answer: hai Explanation: n(Newline): This escape sequence moves the cursor to the beginning of the next line. When printf encounters n, it moves the cursor to the next line, starting from the beginning. b (Backspace): This escape sequence moves the cursor back one position but doesn’t erase any character. When printf encounters b, it moves the cursor back without clearing any character. r (Carriage Return): This escape sequence moves the cursor to the beginning of the current line without advancing to the next line. Any characters printed after r overwrite the existing characters on that line.
  • 22.
    3. #define clrscr()100 main() { clrscr(); printf("%dn",clrscr()); } Answer: 100 Explanation: Preprocessor executes as a seperate pass before the execution of the compiler. So textual replacement of clrscr() to 100 occurs.
  • 23.
    4. main() { printf("%p",main); } Answer: Someaddress will be printed. Explanation: Function names are just addresses (just like array names are addresses). main() is also a function. So the address of function main will be printed. %p in printf specifies that the argument is an address. They are printed as hexadecimal numbers.
  • 24.
    5. main() { clrscr(); } clrscr(); Answer: Nooutput/error Explanation: The first clrscr() occurs inside a function. So it becomes a function call. In the second clrscr(); is a function declaration (because it is not inside any function).
  • 25.
    6. main() { int i=400,j=300; printf("%d..%d"); } Answer:400..300 Explanation: printf takes the values of the first two assignments of the program. Any number of printf's may be given. All of them take only the first two values. If more number of assignments given in the program,then printf will take garbage value
  • 26.
    7. #include <stdio.h> main() { inti=1,j=2; switch(i) { case 1: printf("GOOD"); break; case j: printf("BAD"); break; } } Answer: Compiler Error: Constant expression required in function main. Explanation: The case statement can have only constant expressions (this implies that we cannot use variable names directly so an error).
  • 27.
    Assessment 1 1. Programto check if the given character is a vowel or consonant . 2. C Program to Find the Largest Number Among Three Numbers