0% found this document useful (0 votes)
2 views2 pages

Testfile 6

The document contains a C program that demonstrates various programming concepts including variable declaration, assignment, control structures (if statements, for loops), and input/output operations. It showcases the use of constants, character types, and includes examples of different forms of for loops. The program also illustrates how to handle input and output using functions like printf, getint, and getchar.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Testfile 6

The document contains a C program that demonstrates various programming concepts including variable declaration, assignment, control structures (if statements, for loops), and input/output operations. It showcases the use of constants, character types, and includes examples of different forms of for loops. The program also illustrates how to handle input and output using functions like printf, getint, and getchar.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

int main() {

// Introduce const and char types


const int const_var = 10;
char char_var = 'A';
int x = 5;
int y = 0;

// Print student ID
printf("70066017\n");

// Assignment statement: LVal '=' Exp ';'


x = x + const_var;
printf("%d\n", x);

// Expression statements: [Exp] ';'


x + 1; // With Exp
; // Without Exp

// Block statement: Block


{
int z = 20;
printf("%d\n", z);
}

// If statement with else: 'if' '(' Cond ')' Stmt 'else' Stmt
if (x > 10) {
printf("x > 10\n");
} else {
printf("x <= 10\n");
}

// For loops: Various forms


// 1. No omissions
int i;
for (i = 0; i < 3; i = i + 1) {
printf("%d\n", i);
}

// 2. Omitted initialization
int j ;
for (j = 3; j < 7; j = j + 1) {
printf("%d\n", j);
}

int k;
// 3. Omitted condition, using break
for (k = 7;; k = k + 1) {
if (k >= 10) break; // break statement
printf("%d\n", k);
}

// 4. Omitted update
int m;
for (m = 0; m < 3;) {
printf("%d\n", m);
m = m + 1;
}

// 5. All omitted, using break and continue


int n = 0;
for (;;) {
if (n >= 3) break;
if (n == 1) {
n = n + 1;
continue; // continue statement
}
printf("%d\n", n);
n = n + 1;
}

// getint and getchar


int input_int ;
input_int = getint(); // LVal '=' 'getint' '(' ')' ';'
char input_char;
input_char = getchar(); // LVal '=' 'getchar' '(' ')' ';'
printf("%d\n", input_int); // Print input integer
printf("%c\n", input_char); // Print input character

// printf without Exp


printf("Test\n"); // 'printf' '(' StringConst ')' ';'

// printf with Exp


printf("Value: %d\n", x); // 'printf' '(' StringConst ',' Exp ')' ';'

// Using char variable


printf("Char: %c\n", char_var);
// Using const variable
printf("Const: %d\n", const_var);

// return statements: With and without Exp


if (y == 0) {
return 0; // With Exp
}
return 0;
}

You might also like