The document outlines the basics of programming in C, including the structure of a C program, the importance of commenting and indentation, and the steps for coding in a lab environment using the Linux GCC compiler. It also includes a puzzle to illustrate logical reasoning and examples of simple C programs. Additionally, it provides guidelines for organizing and naming files for assignments.
The document outlines the basics of programming in C, including the structure of a C program, the importance of commenting and indentation, and the steps for coding in a lab environment using the Linux GCC compiler. It also includes a puzzle to illustrate logical reasoning and examples of simple C programs. Additionally, it provides guidelines for organizing and naming files for assignments.
• Assembly language • Higher level language • How do we do coding in our Lab? • Login with login names & passwords • Edit C program • Compile the program • Execute the program Puzzle Three students wanted to find out who was the wisest among them. So they turned to their grand master, asking to resolve their dispute. “Easy” the old sage said. “I will blind fold you and paint either red or blue dot on each of your forehead. When I take your blindfolds off, if you see at least one red dot, raise your hand. The one who guesses the color of the dot on his/her own forehead first, is the wisest.” The master blindfolded all and painted red dots on their forehead. When he took their blindfolds off, all three men raised their hands, and sat in silence for some time, thinking. Finally one of them said. “ I have a red dot on my forehead.” How did he guess? C program to print “ C is a nice programming language.” #include <stdio.h> main() { printf(“C is a nice programming language”); } • stdio.h is a standard library file written by the developers of C language which helps in printing the statement Standards followed in writing a program • Commenting (documenting) a program • You write who has written the program, what the program does etc.
/*Author of the program : Subhasree M.
This program prints a one line sentence */ #include <stdio.h> // includes standard input output header file main() { // printf statement prints the line printf(“C is a nice programming language”); }
• Indentations (spacing) : after curly braces, put a space which
specifies these statements are inside the curly braces. • Indentations makes the program more readable • The ; terminates each statement in a C program After writing the program (editing), what did we do? • We saved our program either by pressing ctrl s(both the keys together) or pressing the save button on the Graphical User Interface(GUI) available on our monitor • Why do we save our program? • Otherwise when you switch off, the program also vanishes from the computer, when it is stored in RAM(volatile memory) How does the program gets saved? • Memory of a computer is organized as consecutive locations • Each location has an address associated with it
• Operating system takes care of how to allocate
the memory using a memory management system & saves the program #include <stdio.h> main() { printf(“C is a nice programming language”); } • How many functions are there in the above program? • main() – important function in any C program where the execution of the program starts, wherever you ave written the function main • main is the name of the function • Inside the (), the arguments of the function is written • printf is the next function • Inside the (), the arguments of printf function is written C program to print two lines #include <stdio.h> main() { printf(“C is a nice programming language\n”); printf(“I like coding in C”); } • \n specifies new line. • Whatever is put inside the quotes other than the format specifiers are printed a such • After printing C is a nice programming language, the cursor goes to the next line and then prints I like coding in C C program to print one line with spaces in between #include <stdio.h> main() { printf(“Basic \t Fortran \t Java ”); } • \t is tab space • Write C program to print three statements with spaces in different lines with one blank line in between ? Problem: Sum of two numbers • Problem analysis – (Input, Output) • P = {((2,3),5), ((-1,2), 1), ((-10,-2),-13), ((0,2), 2),…} • Steps to solve the problem -Two numbers -Add them -Print the sum How do we give/specify two numbers ? • There are two ways: - 1st : Give the numbers in the program itself - 2nd: Give the numbers as input from the user • How does the compiler identifies between a number and a statement like “C is a nice programming language” • We have to specify the type of the number • How many types of numbers are there? • Numbers with out decimal points (integers), numbers with decimal points (floating point numbers) • Eg: 1, 0, -23 are integers • Eg: 222.30, 34.0 , -267.8 are floating point numbers How do we tell the computer that our number is an integer • Declare the number as an integer data type - int a; // int is a data type • what happens when we declare int a; • A memory location is created with integer as data type • Initialize the data • a= 20; // Memory location is filled with the value 20 C program to sum two numbers //program to obtain sum of two numbers #include <stdio.h> main() { int x,y,z; //declaration of three variables as integers x=12; y=4;// initilaization of x and y z= x+y; /*x & y are known as operands , + is an operator*/ printf(“Sum of two numbers is %d”,z); /* %d or %i is the format specifier for an integer data type. %d is associated with the variable after quotes (eg: z in this case) */ } Problems • How do we change the previous program: - If we want to use only two variables x & y - If we want to obtain the output as sum of two numbers 12 and 4 is 16 • Write a C program to swap two numbers: Input : a=20, b= 30 Output: a=30, b=20 - with using a temporary variable - 2 methods without using temporary variable The Linux GCC Compiler
We will be using the Linux GCC Compiler (For
exams too) Screen shot- Login page Screen shot - Asking password Screen shot- You are logged in Steps for doing coding • Make a directory with your own name : Right click on the mouse, select Create New Folder option and put your roll number as the name of the directory • You have to put all your C programs inside that directory • Open terminal : Press Ctrl, Alt, T (all three keys together) Screenshot- Opened a terminal On the terminal • Change to your directory : cd Desktop/Your directory name • Open an editor with file name print.c: gedit print.c (Here print.c is the file where you write your C program) • Compile your program : gcc print.c • Execute your program : ./a.out Steps for coding in lab • Make a directory with your own name : Right click on the mouse, select Create New Folder option and put your roll number as the name of the directory • Please make sure that you place all your C programs inside that directory, in different folders like Assignment1, Assignment2 etc., so that it is easy for you and your evaluators to refer. • Make sure that you write your file name as assignment1_Q1.c (a1_q1.c or a1_q1a.c,a1_q1b.c, a2_q1a.c etc) , so that it is easy for you and your evaluators to refer. Steps for coding in lab – contd. • Open terminal : Press Ctrl Alt T (all three keys together) • Change to your directory : cd Desktop/Your directory name • Open an editor with file name a1_q1.c: gedit a1_q1.c (Here a1_q1.c is the file where you write your C program) • Compile your program : gcc a1_q1.c • An executable file is created with the name a.out • Execute your program : ./a.out Happy coding