C and Pointers chapter two programming exercises

Source: Internet
Author: User

1. Write a program that consists of 3 functions, each of which is stored in a separate source file. The function increment accepts an integer parameter whose return value is the value of the parameter plus the 1.increment function should be located in the file increment.c. The second function, called Negate, also accepts a shaping parameter whose return value is the negative of the parameter. The last function is main, which is stored in the file main.c, which calls the other two functions separately with the parameter 10,0,-10 and prints the error.

int increment (int n) {return n+1;} int negate (int n) {return-n;}    int main () {printf ("%d\n", Increment (10));    printf ("%d\n", Increment (0));    printf ("%d\n", Increment (-10));    printf ("%d\n", negate (10));    printf ("%d\n", negate (0));    printf ("%d\n", negate (-10));    System ("pause"); return 0;}

2. Write a program that reads the C source code from the standard input and verifies that all curly braces are correctly paired. Note: You do not have to worry about curly braces inside the comment, inside the string constant, or in the form of a character constant.

Int main () {    int i = 0;    char str[1000]  = { 0 };    int count = 0;     printf ("Please enter: \ n");     SCANF ("%s",  str);    while  (Str[i]  !=  ' \ n ')     {       if  (str[i] ==   ' {')        {           count++;       }       if  ( str[i] ==  '} ') && (count==0)        {           count--;           break;       }       if  ((str[i)  ==  '} ')  &&  (count != 0))        {           count--;       }        i++;    }    if  (count == 0)      {       printf ("All curly braces appear in pairs \ \");    }     else    {       printf ("Curly braces do not appear in pairs \ \");     }    system ("pause");     return 0;}

This article is from the "Sean" blog, so be sure to keep this source http://seann.blog.51cto.com/11126490/1740206

C and Pointers chapter two programming exercises

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.