Thursday, July 23, 2015

Program Output

Previous | Home | Next


Gather

Read Chapter 1, section 'Program Output (p. 6-10)

Reflect 

In file sea.c (omit line numbers, empty lines are only put for clarity of output)
1:  #include <stdio.h>  
2:    
3:  int main(void) {  
4:    
5:    printf("from sea to shining C\n");  
6:    
7:    return 0;  
8:  }  
download code here.

Linux compiling:
 $ gcc -o sea sea.c  

Running program:
 ./sea  

Line 1:
Directive for compiler pre-processor which will include the content of the file stdio.h which is found in the usual place (known to the compiler). This file will allow me to use function printf and others

Line 3:
Every program starts with function main(). Function will return an int (integer) to the OS that started program (Linux here).

Line 5:
Function printf(), which is loaded with line 1 directive, will display on the screen the string (sequence of characters enclosed between quotation marks. The string will end with 'new line' character (\n).

Line 7:
Program before finishing its job, is going to return number 0 (return 0) to the OS (which is code for 'all went well').

Lines 3-8:
Body of function main() is between curly braces {}. It is the code that will be run sequentialy (top-to-bottom).

Create

Introducing some errors in the code to see how the compiler is going to react.

Error 1
1:  #include <stdio.h>  
2:    
3:  int main(void)   
4:    
5:    printf("from sea to shining C\n");  
6:    
7:    return 0;  
8:  }  

During compilation and linking I get:
 $ gcc -o sea sea.c   
 sea.c: In function ‘main’:  
 sea.c:5:5: error: expected declaration specifiers before ‘printf’  
    printf("from sea to shining C\n");  
    ^  
 sea.c:7:5: error: expected declaration specifiers before ‘return’  
    return 0;  
    ^  
 sea.c:8:1: error: expected declaration specifiers before ‘}’ token  
  }  
  ^  
 sea.c:8:1: error: expected ‘{’ at end of input  
   

What is the problem?

Error 2
1:  #include <stdio.h>  
2:    
3:  int main(void){   
4:    
5:    printf("from sea to shining C\n")  
6:    
7:    return 0;  
8:  }  

During compilation and linking I get:
 $ gcc -o sea sea.c   
 sea.c: In function ‘main’:  
 sea.c:7:5: error: expected ‘;’ before ‘return’  
    return 0;  
    ^  

What is the problem?

Error 3
1:  #include <stdio.h>  
2:    
3:  int main(void){   
4:    
5:    printf("from sea to shining C\n);  
6:    
7:    return 0;  
8:  }  

During compilation and linking I get:
 $ gcc -o sea sea.c   
 sea.c: In function ‘main’:  
 sea.c:5:12: warning: missing terminating " character [enabled by default]  
    printf("from sea to shining C\n);  
       ^  
 sea.c:5:5: error: missing terminating " character  
    printf("from sea to shining C\n);  
    ^  
 sea.c:7:5: error: expected expression before ‘return’  
    return 0;  
    ^  
 sea.c:8:1: error: expected ‘;’ before ‘}’ token  
  }  
  ^  

What is the problem?

Test

Write a program that displays the following:

first.c
first.c



Previous | Home | Next